package gojs_test import ( "apigo.cloud/git/apigo/gojs" "apigo.cloud/git/apigo/plugin" "fmt" "github.com/ssgo/log" "github.com/ssgo/u" "runtime" "strings" "testing" "time" ) type Object struct { id string } func (obj *Object) GetId() string { return obj.id } type TestBirthday struct { Year int Month int Day int } type TestBaseUser struct { Id int Name string } type TestUser struct { TestBaseUser Birthday *TestBirthday } func (b *TestBirthday) String() string { return fmt.Sprintln(b.Year, b.Month, b.Day) } func (u *TestUser) GetBirthdayString() string { return u.Birthday.String() } func init() { defaultObject := Object{id: "o-00"} plg := plugin.Plugin{ Id: "obj", Name: "test obj plugin", Objects: map[string]interface{}{ "name": "1233", "list1": []interface{}{1, "2", map[string]interface{}{"aaa": 111, "bbb": "222"}, true}, "log": log.DefaultLogger.Info, "getId": defaultObject.GetId, "new": func(id string) interface{} { return &Object{id: id} }, "test1": func(id int, id2 uint16, id3 float64) *TestUser { return nil }, "echo": func(text string, echoFunc func(text string, t2 int) string, t3 []uint32, t4 *bool) interface{} { return echoFunc(text, 0) }, "echoTimes": func(echoFunc func(text string)) { for i := 0; i < 5; i++ { //time.Sleep(100 * time.Millisecond) echoFunc(fmt.Sprint(i)) } }, }, } plugin.Register(plg) gojs.SetPluginsConfig(map[string]plugin.Config{ "obj": plugin.Config{}, }) } func test(t *testing.T, name string, check bool, extArgs ...interface{}) { if check { fmt.Println(u.Green(name), u.BGreen("[OK]")) } else { fmt.Println(u.Red(name), u.BRed("[Failed]"), fmt.Sprintln(extArgs...)) t.Error(name) } } func TestTS(t *testing.T) { plg := plugin.Get("obj") plgCode := gojs.MakePluginCode(plg) test(t, "ts code", strings.Contains(plgCode, "(echoFunc: (text: string) => void)"), plgCode) } func TestGlobal(t *testing.T) { code := ` log('test', 'name', 'log') return plus(number,2) ` globals := map[string]interface{}{ "number": 9, "log": log.DefaultLogger.Info, "plus": func(i, j int) int { return i + j }, } r, _ := gojs.Run(code, &gojs.RuntimeOption{ Globals: globals, }) test(t, "call", u.Int(r) == 11, r) } func TestPlugin(t *testing.T) { rt := gojs.New(nil) rt.Exec("import obj from 'obj'") r, _ := rt.Run("return obj.getId()") test(t, "obj.getId()", u.String(r) == "o-00", r) r, _ = rt.Run(` o = obj.new('o-01') return o.getId() `) test(t, "new obj.getId()", u.String(r) == "o-01", r) t1 := time.Now() r, _ = rt.Run(` out = '' obj.echo('123', function(text){ out = text }, null) return out `) t2 := time.Now() fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) test(t, "callback", u.String(r) == "123", r) t1 = time.Now() r, _ = rt.Run(` out = '' obj.echoTimes(function(text){ out += text }) return out `) t2 = time.Now() fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) test(t, "callbacks", u.String(r) == "01234", r) } func BenchmarkEcho(tb *testing.B) { tb.StopTimer() ms1 := runtime.MemStats{} runtime.ReadMemStats(&ms1) tb.StartTimer() for i := 0; i < tb.N; i++ { gojs.Run(`return 1`, nil) } tb.StopTimer() ms2 := runtime.MemStats{} runtime.ReadMemStats(&ms2) runtime.GC() ms3 := runtime.MemStats{} runtime.ReadMemStats(&ms3) fmt.Println(">>", ms1.HeapInuse, ms2.HeapInuse, ms3.HeapInuse) } func BenchmarkCallback(tb *testing.B) { tb.StopTimer() ms1 := runtime.MemStats{} runtime.ReadMemStats(&ms1) tb.StartTimer() for i := 0; i < tb.N; i++ { gojs.Run(` import obj from 'obj' out = '' obj.echoTimes(function(text){ out += text }) return out `, nil) } tb.StopTimer() ms2 := runtime.MemStats{} runtime.ReadMemStats(&ms2) runtime.GC() ms3 := runtime.MemStats{} runtime.ReadMemStats(&ms3) fmt.Println(">>", ms1.HeapInuse, ms2.HeapInuse, ms3.HeapInuse) }