• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang runtime.NewCtx函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/PuerkitoBio/agora/runtime.NewCtx函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCtx函数的具体用法?Golang NewCtx怎么用?Golang NewCtx使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewCtx函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: NewAgoraClosure

// NewAgoraClosure loads a full agora context with the module passed in, then
// returns a closure that calls the 'Run' method on the closure and returns the
// result
func NewAgoraClosure(modPath string) func() string {
	ctx := runtime.NewCtx(new(HTTPResolver), new(compiler.Compiler))
	f, _ := os.Open(modPath)
	defer f.Close()

	ctx.Compiler.Compile(modPath, f)

	ctx.RegisterNativeModule(new(stdlib.FmtMod))
	ctx.RegisterNativeModule(new(stdlib.FilepathMod))
	ctx.RegisterNativeModule(new(stdlib.ConvMod))
	ctx.RegisterNativeModule(new(stdlib.StringsMod))
	ctx.RegisterNativeModule(new(stdlib.MathMod))
	ctx.RegisterNativeModule(new(stdlib.OsMod))
	ctx.RegisterNativeModule(new(stdlib.TimeMod))

	mod, err := ctx.Load(modPath)
	if err != nil {
		fmt.Println("Couldn't load module", err.Error())
		os.Exit(1)
	}

	return func() string {

		val, err := mod.Run()
		if err != nil {
			fmt.Println("Error executing module", err.Error())
			os.Exit(1)
		}

		return val.String()
	}
}
开发者ID:ryansb,项目名称:livesrv,代码行数:35,代码来源:generator.go


示例2: TestMin

func TestMin(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)

	cases := []struct {
		src []runtime.Val
		exp runtime.Val
	}{
		0: {
			src: []runtime.Val{runtime.Number(3), runtime.Number(0), runtime.Number(-12.74), runtime.Number(1)},
			exp: runtime.Number(-12.74),
		},
		1: {
			src: []runtime.Val{runtime.String("24"), runtime.Bool(true), runtime.Number(12.74)},
			exp: runtime.Number(1),
		},
		2: {
			src: []runtime.Val{runtime.Number(0), runtime.String("0")},
			exp: runtime.Number(0),
		},
	}

	for i, c := range cases {
		ret := mm.math_Min(c.src...)
		if ret != c.exp {
			t.Errorf("[%d] - expected %f, got %f", i, c.exp.Float(), ret.Float())
		}
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:30,代码来源:math_test.go


示例3: TestStringsSplit

func TestStringsSplit(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_Split(runtime.String("aa:bb::dd"), runtime.String(":"))
	ob := ret.(runtime.Object)
	exp := []string{"aa", "bb", "", "dd"}
	if l := ob.Len().Int(); l != int64(len(exp)) {
		t.Errorf("expected split length of %d, got %d", len(exp), l)
	}
	for i, v := range exp {
		got := ob.Get(runtime.Number(i))
		if got.String() != v {
			t.Errorf("expected split index %d to be %s, got %s", i, v, got)
		}
	}
	ret = sm.strings_Split(runtime.String("aa:bb::dd:ee:"), runtime.String(":"), runtime.Number(2))
	ob = ret.(runtime.Object)
	exp = []string{"aa", "bb::dd:ee:"}
	if l := ob.Len().Int(); l != int64(len(exp)) {
		t.Errorf("expected split length of %d, got %d", len(exp), l)
	}
	for i, v := range exp {
		got := ob.Get(runtime.Number(i))
		if got.String() != v {
			t.Errorf("expected split index %d to be %s, got %s", i, v, got)
		}
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:29,代码来源:strings_test.go


示例4: TestOsFields

func TestOsFields(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	om := new(OsMod)
	om.SetCtx(ctx)
	ob, err := om.Run()
	if err != nil {
		panic(err)
	}
	{
		ob := ob.(runtime.Object)
		ret := ob.Get(runtime.String("PathSeparator"))
		exp := string(os.PathSeparator)
		if ret.String() != exp {
			t.Errorf("expected path separator %s, got %s", exp, ret.String())
		}
		ret = ob.Get(runtime.String("PathListSeparator"))
		exp = string(os.PathListSeparator)
		if ret.String() != exp {
			t.Errorf("expected path list separator %s, got %s", exp, ret.String())
		}
		ret = ob.Get(runtime.String("DevNull"))
		exp = os.DevNull
		if ret.String() != exp {
			t.Errorf("expected dev/null %s, got %s", exp, ret)
		}
		ret = ob.Get(runtime.String("TempDir"))
		exp = os.TempDir()
		if ret.String() != exp {
			t.Errorf("expected temp dir %s, got %s", exp, ret)
		}
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:32,代码来源:os_test.go


示例5: TestOsOpen

func TestOsOpen(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	om := new(OsMod)
	om.SetCtx(ctx)
	fn := "./testdata/readfile.txt"
	f := om.os_Open(runtime.String(fn))
	fl := f.(*file)
	ret := fl.Get(runtime.String("Name"))
	if ret.String() != fn {
		t.Errorf("expected Name to be '%s', got '%s'", fn, ret)
	}
	exp := "ok"
	ret = fl.readLine()
	if ret.String() != exp {
		t.Errorf("expected read line 1 to be '%s', got '%s'", exp, ret)
	}
	exp = ""
	ret = fl.readLine()
	if ret.String() != exp {
		t.Errorf("expected read line 2 to be '%s', got '%s'", exp, ret)
	}
	ret = fl.readLine()
	if ret != runtime.Nil {
		t.Errorf("expected read line 3 to be nil, got '%v'", ret)
	}
	ret = fl.closeFile()
	if ret != runtime.Nil {
		t.Errorf("expected close file to be nil, got '%v'", ret)
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:30,代码来源:os_test.go


示例6: TestTimeConv

func TestTimeConv(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	tm := new(TimeMod)
	tm.SetCtx(ctx)
	nw := time.Now().UTC()
	n := tm.time_Date(runtime.Number(nw.Year()),
		runtime.Number(nw.Month()),
		runtime.Number(nw.Day()),
		runtime.Number(nw.Hour()),
		runtime.Number(nw.Minute()),
		runtime.Number(nw.Second()),
		runtime.Number(nw.Nanosecond()))
	ob := n.(runtime.Object)
	cnv := ob.Get(runtime.String("__string"))
	f := cnv.(runtime.Func)
	ret := f.Call(nil)
	exp := nw.Format(time.RFC3339)
	if ret.String() != exp {
		t.Errorf("expected string to return '%s', got '%s'", exp, ret)
	}
	cnv = ob.Get(runtime.String("__int"))
	f = cnv.(runtime.Func)
	ret = f.Call(nil)
	{
		exp := nw.Unix()
		if ret.Int() != int64(exp) {
			t.Errorf("expected int to return %d, got %d", exp, ret.Int())
		}
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:30,代码来源:time_test.go


示例7: TestTimeNow

func TestTimeNow(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	tm := new(TimeMod)
	tm.SetCtx(ctx)
	exp := time.Now()
	ret := tm.time_Now()
	ob := ret.(runtime.Object)
	if yr := ob.Get(runtime.String("Year")); yr.Int() != int64(exp.Year()) {
		t.Errorf("expected year %d, got %d", exp.Year(), yr.Int())
	}
	if mt := ob.Get(runtime.String("Month")); mt.Int() != int64(exp.Month()) {
		t.Errorf("expected month %d, got %d", exp.Month(), mt.Int())
	}
	if dy := ob.Get(runtime.String("Day")); dy.Int() != int64(exp.Day()) {
		t.Errorf("expected day %d, got %d", exp.Day(), dy.Int())
	}
	if hr := ob.Get(runtime.String("Hour")); hr.Int() != int64(exp.Hour()) {
		t.Errorf("expected hour %d, got %d", exp.Hour(), hr.Int())
	}
	if mn := ob.Get(runtime.String("Minute")); mn.Int() != int64(exp.Minute()) {
		t.Errorf("expected minute %d, got %d", exp.Minute(), mn.Int())
	}
	if sc := ob.Get(runtime.String("Second")); sc.Int() != int64(exp.Second()) {
		t.Errorf("expected second %d, got %d", exp.Second(), sc.Int())
	}
	if ns := ob.Get(runtime.String("Nanosecond")); ns.Int() < int64(exp.Nanosecond()) {
		t.Errorf("expected nanosecond %d, got %d", exp.Nanosecond(), ns.Int())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:29,代码来源:time_test.go


示例8: TestStringsTrim

func TestStringsTrim(t *testing.T) {
	cases := []struct {
		args []runtime.Val
		exp  string
	}{
		0: {
			args: []runtime.Val{
				runtime.String(" "),
			},
			exp: "",
		},
		1: {
			args: []runtime.Val{
				runtime.String("\n  \t   hi \r"),
			},
			exp: "hi",
		},
		2: {
			args: []runtime.Val{
				runtime.String("xoxolovexox"),
				runtime.String("xo"),
			},
			exp: "love",
		},
	}
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	for i, c := range cases {
		ret := sm.strings_Trim(c.args...)
		if ret.String() != c.exp {
			t.Errorf("[%d] - expected %s, got %s", i, c.exp, ret)
		}
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:35,代码来源:strings_test.go


示例9: TestFilepathBaseDirExt

func TestFilepathBaseDirExt(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	fm := new(FilepathMod)
	fm.SetCtx(ctx)
	p, e := filepath.Abs("./testdata/readfile.txt")
	if e != nil {
		panic(e)
	}
	// Base
	exp := filepath.Base(p)
	ret := fm.filepath_Base(runtime.String(p))
	if ret.String() != exp {
		t.Errorf("expected base '%s', got '%s'", exp, ret.String())
	}
	// Dir
	exp = filepath.Dir(p)
	ret = fm.filepath_Dir(runtime.String(p))
	if ret.String() != exp {
		t.Errorf("expected dir '%s', got '%s'", exp, ret.String())
	}
	// Ext
	exp = filepath.Ext(p)
	ret = fm.filepath_Ext(runtime.String(p))
	if ret.String() != exp {
		t.Errorf("expected extension '%s', got '%s'", exp, ret.String())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:27,代码来源:filepath_test.go


示例10: TestStringsToUpper

func TestStringsToUpper(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_ToUpper(runtime.String("this"), runtime.String("Is"), runtime.String("A"), runtime.String("... strInG"))
	exp := "THISISA... STRING"
	if ret.String() != exp {
		t.Errorf("expected %s, got %s", exp, ret)
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:10,代码来源:strings_test.go


示例11: TestTimeSleep

func TestTimeSleep(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	tm := new(TimeMod)
	tm.SetCtx(ctx)
	n := time.Now()
	tm.time_Sleep(runtime.Number(100))
	if diff := time.Now().Sub(n); diff < 100*time.Millisecond {
		t.Errorf("expected at least 100ms, got %f", diff.Seconds()*1000)
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:10,代码来源:time_test.go


示例12: TestStringsConcat

func TestStringsConcat(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_Concat(runtime.String("hello"), runtime.Number(12), runtime.Bool(true), runtime.String("end"))
	exp := "hello12trueend"
	if ret.String() != exp {
		t.Errorf("expected %s, got %s", exp, ret)
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:10,代码来源:strings_test.go


示例13: TestFmtScanint

func TestFmtScanint(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	buf := bytes.NewBuffer([]byte("12\n"))
	ctx.Stdin = buf
	fm := new(FmtMod)
	fm.SetCtx(ctx)
	ret := fm.fmt_Scanint()
	if ret.Int() != 12 {
		t.Errorf("expected 12, got %d", ret.Int())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:11,代码来源:fmt_test.go


示例14: TestOsExec

func TestOsExec(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	om := new(OsMod)
	om.SetCtx(ctx)
	exp := "hello"
	ret := om.os_Exec(runtime.String("echo"), runtime.String(exp))
	// Shell adds a \n after output
	if ret.String() != exp+"\n" {
		t.Errorf("expected '%s', got '%s'", exp, ret)
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:11,代码来源:os_test.go


示例15: TestMathNaN

func TestMathNaN(t *testing.T) {
	// This is just an interface to Go's function, so just a quick simple test
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)
	ret := mm.math_NaN()
	exp := math.NaN()
	if math.IsNaN(ret.Float()) != math.IsNaN(exp) {
		t.Errorf("expected NaN, got %f", ret.Float())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:11,代码来源:math_test.go


示例16: TestMathIsNaN

func TestMathIsNaN(t *testing.T) {
	// This is just an interface to Go's function, so just a quick simple test
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)
	val := 0.12
	ret := mm.math_IsNaN(runtime.Number(val))
	exp := math.IsNaN(val)
	if ret.Bool() != exp {
		t.Errorf("expected %v, got %v", exp, ret.Bool())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:12,代码来源:math_test.go


示例17: TestMathTanh

func TestMathTanh(t *testing.T) {
	// This is just an interface to Go's function, so just a quick simple test
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)
	val := 1.12
	ret := mm.math_Tanh(runtime.Number(val))
	exp := math.Tanh(val)
	if ret.Float() != exp {
		t.Errorf("expected %f, got %f", exp, ret.Float())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:12,代码来源:math_test.go


示例18: TestStringsHasSuffix

func TestStringsHasSuffix(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_HasSuffix(runtime.String("suffix, you say"), runtime.String("ay"), runtime.Nil, runtime.Number(3), runtime.String("wh"))
	if !ret.Bool() {
		t.Errorf("expected true, got false")
	}
	ret = sm.strings_HasSuffix(runtime.String("suffix, you say"), runtime.String("no"), runtime.Nil, runtime.Number(3), runtime.String("hw"))
	if ret.Bool() {
		t.Errorf("expected false, got true")
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:13,代码来源:strings_test.go


示例19: TestStringsContains

func TestStringsContains(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_Contains(runtime.String("contains something"), runtime.String("what"), runtime.Nil, runtime.Number(3), runtime.String("some"))
	if !ret.Bool() {
		t.Errorf("expected true, got false")
	}
	ret = sm.strings_Contains(runtime.String("contains something"), runtime.String("no"), runtime.Nil, runtime.Number(3), runtime.String("hw"))
	if ret.Bool() {
		t.Errorf("expected false, got true")
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:13,代码来源:strings_test.go


示例20: TestOsGetwd

func TestOsGetwd(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	om := new(OsMod)
	om.SetCtx(ctx)
	exp, e := os.Getwd()
	if e != nil {
		panic(e)
	}
	ret := om.os_Getwd()
	if ret.String() != exp {
		t.Errorf("expected '%s', got '%s'", exp, ret.String())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:13,代码来源:os_test.go



注:本文中的github.com/PuerkitoBio/agora/runtime.NewCtx函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang runtime.Number函数代码示例发布时间:2022-05-28
下一篇:
Golang runtime.ExpectAtLeastNArgs函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap