gojs/gojs_test.go

190 lines
3.8 KiB
Go
Raw Normal View History

2024-02-17 12:55:08 +08:00
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"
2024-02-17 12:55:08 +08:00
"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()
}
2024-02-17 12:55:08 +08:00
func init() {
defaultObject := Object{id: "o-00"}
plg := plugin.Plugin{
2024-02-17 12:55:08 +08:00
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,
2024-02-17 12:55:08 +08:00
"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)
2024-02-17 12:55:08 +08:00
},
"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{},
2024-02-17 12:55:08 +08:00
})
}
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)"))
}
2024-02-17 12:55:08 +08:00
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,
})
2024-02-17 12:55:08 +08:00
test(t, "call", u.Int(r) == 11, r)
}
func TestPlugin(t *testing.T) {
r, _, _ := gojs.Run("return obj.getId()", nil)
2024-02-17 12:55:08 +08:00
test(t, "obj.getId()", u.String(r) == "o-00", r)
2024-02-18 13:20:58 +08:00
r, _, _ = gojs.Run(`
2024-02-17 12:55:08 +08:00
o = obj.new('o-01')
return o.getId()
`, nil)
2024-02-17 12:55:08 +08:00
test(t, "new obj.getId()", u.String(r) == "o-01", r)
t1 := time.Now()
2024-02-18 13:20:58 +08:00
r, _, _ = gojs.Run(`
2024-02-17 12:55:08 +08:00
out = ''
obj.echo('123', function(text){
out = text
}, null)
2024-02-17 12:55:08 +08:00
return out
`, nil)
2024-02-17 12:55:08 +08:00
t2 := time.Now()
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
2024-02-17 12:55:08 +08:00
test(t, "callback", u.String(r) == "123", r)
t1 = time.Now()
2024-02-18 13:20:58 +08:00
r, _, _ = gojs.Run(`
2024-02-17 12:55:08 +08:00
out = ''
obj.echoTimes(function(text){
out += text
})
return out
`, nil)
2024-02-17 12:55:08 +08:00
t2 = time.Now()
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
2024-02-17 12:55:08 +08:00
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)
2024-02-17 12:55:08 +08:00
}
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(`
out = ''
obj.echoTimes(function(text){
out += text
})
return out
`, nil)
2024-02-17 12:55:08 +08:00
}
tb.StopTimer()
ms2 := runtime.MemStats{}
runtime.ReadMemStats(&ms2)
runtime.GC()
ms3 := runtime.MemStats{}
runtime.ReadMemStats(&ms3)
fmt.Println(">>", ms1.HeapInuse, ms2.HeapInuse, ms3.HeapInuse)
}