xgo/stdlib/stdlib_test.go

243 lines
5 KiB
Go
Raw Normal View History

package stdlib_test
import (
"fmt"
"testing"
2019-01-30 04:39:25 +03:00
"time"
2019-12-30 00:38:51 +03:00
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/require"
"github.com/d5/tengo/v2/stdlib"
)
type ARR = []interface{}
type MAP = map[string]interface{}
type IARR []interface{}
type IMAP map[string]interface{}
func TestAllModuleNames(t *testing.T) {
names := stdlib.AllModuleNames()
2019-12-20 22:40:38 +03:00
require.Equal(t,
len(stdlib.BuiltinModules)+len(stdlib.SourceModules),
len(names))
}
func TestModulesRun(t *testing.T) {
// os.File
expect(t, `
os := import("os")
out := ""
write_file := func(filename, data) {
file := os.create(filename)
if !file { return file }
if res := file.write(bytes(data)); is_error(res) {
return res
}
return file.close()
}
read_file := func(filename) {
file := os.open(filename)
if !file { return file }
data := bytes(100)
cnt := file.read(data)
if is_error(cnt) {
return cnt
}
file.close()
return data[:cnt]
}
if write_file("./temp", "foobar") {
out = string(read_file("./temp"))
}
os.remove("./temp")
`, "foobar")
// exec.command
expect(t, `
out := ""
os := import("os")
cmd := os.exec("echo", "foo", "bar")
if !is_error(cmd) {
out = cmd.output()
}
`, []byte("foo bar\n"))
}
func TestGetModules(t *testing.T) {
mods := stdlib.GetModuleMap()
2019-12-20 22:40:38 +03:00
require.Equal(t, 0, mods.Len())
mods = stdlib.GetModuleMap("os")
2019-12-20 22:40:38 +03:00
require.Equal(t, 1, mods.Len())
require.NotNil(t, mods.Get("os"))
mods = stdlib.GetModuleMap("os", "rand")
2019-12-20 22:40:38 +03:00
require.Equal(t, 2, mods.Len())
require.NotNil(t, mods.Get("os"))
require.NotNil(t, mods.Get("rand"))
mods = stdlib.GetModuleMap("text", "text")
2019-12-20 22:40:38 +03:00
require.Equal(t, 1, mods.Len())
require.NotNil(t, mods.Get("text"))
mods = stdlib.GetModuleMap("nonexisting", "text")
2019-12-20 22:40:38 +03:00
require.Equal(t, 1, mods.Len())
require.NotNil(t, mods.Get("text"))
}
type callres struct {
t *testing.T
o interface{}
e error
}
func (c callres) call(funcName string, args ...interface{}) callres {
if c.e != nil {
return c
}
2019-12-20 22:40:38 +03:00
var oargs []tengo.Object
for _, v := range args {
oargs = append(oargs, object(v))
}
switch o := c.o.(type) {
2019-12-20 22:40:38 +03:00
case *tengo.BuiltinModule:
m, ok := o.Attrs[funcName]
if !ok {
2019-12-20 22:40:38 +03:00
return callres{t: c.t, e: fmt.Errorf(
"function not found: %s", funcName)}
}
2019-12-20 22:40:38 +03:00
f, ok := m.(*tengo.UserFunction)
if !ok {
2019-12-20 22:40:38 +03:00
return callres{t: c.t, e: fmt.Errorf(
"non-callable: %s", funcName)}
}
res, err := f.Value(oargs...)
return callres{t: c.t, o: res, e: err}
2019-12-20 22:40:38 +03:00
case *tengo.UserFunction:
res, err := o.Value(oargs...)
return callres{t: c.t, o: res, e: err}
2019-12-20 22:40:38 +03:00
case *tengo.ImmutableMap:
m, ok := o.Value[funcName]
if !ok {
return callres{t: c.t, e: fmt.Errorf("function not found: %s", funcName)}
}
2019-12-20 22:40:38 +03:00
f, ok := m.(*tengo.UserFunction)
if !ok {
return callres{t: c.t, e: fmt.Errorf("non-callable: %s", funcName)}
}
res, err := f.Value(oargs...)
return callres{t: c.t, o: res, e: err}
default:
panic(fmt.Errorf("unexpected object: %v (%T)", o, o))
}
}
2019-12-20 22:40:38 +03:00
func (c callres) expect(expected interface{}, msgAndArgs ...interface{}) {
require.NoError(c.t, c.e, msgAndArgs...)
require.Equal(c.t, object(expected), c.o, msgAndArgs...)
}
2019-12-20 22:40:38 +03:00
func (c callres) expectError() {
require.Error(c.t, c.e)
}
func module(t *testing.T, moduleName string) callres {
mod := stdlib.GetModuleMap(moduleName).GetBuiltinModule(moduleName)
if mod == nil {
return callres{t: t, e: fmt.Errorf("module not found: %s", moduleName)}
}
return callres{t: t, o: mod}
}
2019-12-20 22:40:38 +03:00
func object(v interface{}) tengo.Object {
switch v := v.(type) {
2019-12-20 22:40:38 +03:00
case tengo.Object:
return v
case string:
2019-12-20 22:40:38 +03:00
return &tengo.String{Value: v}
case int64:
2019-12-20 22:40:38 +03:00
return &tengo.Int{Value: v}
case int: // for convenience
2019-12-20 22:40:38 +03:00
return &tengo.Int{Value: int64(v)}
case bool:
if v {
2019-12-20 22:40:38 +03:00
return tengo.TrueValue
}
2019-12-20 22:40:38 +03:00
return tengo.FalseValue
case rune:
2019-12-20 22:40:38 +03:00
return &tengo.Char{Value: v}
case byte: // for convenience
2019-12-20 22:40:38 +03:00
return &tengo.Char{Value: rune(v)}
case float64:
2019-12-20 22:40:38 +03:00
return &tengo.Float{Value: v}
case []byte:
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: v}
case MAP:
2019-12-20 22:40:38 +03:00
objs := make(map[string]tengo.Object)
for k, v := range v {
objs[k] = object(v)
}
2019-12-20 22:40:38 +03:00
return &tengo.Map{Value: objs}
case ARR:
2019-12-20 22:40:38 +03:00
var objs []tengo.Object
for _, e := range v {
objs = append(objs, object(e))
}
2019-12-20 22:40:38 +03:00
return &tengo.Array{Value: objs}
case IMAP:
2019-12-20 22:40:38 +03:00
objs := make(map[string]tengo.Object)
for k, v := range v {
objs[k] = object(v)
}
2019-12-20 22:40:38 +03:00
return &tengo.ImmutableMap{Value: objs}
case IARR:
2019-12-20 22:40:38 +03:00
var objs []tengo.Object
for _, e := range v {
objs = append(objs, object(e))
}
2019-12-20 22:40:38 +03:00
return &tengo.ImmutableArray{Value: objs}
2019-01-30 04:39:25 +03:00
case time.Time:
2019-12-20 22:40:38 +03:00
return &tengo.Time{Value: v}
2019-02-09 23:33:35 +03:00
case []int:
2019-12-20 22:40:38 +03:00
var objs []tengo.Object
2019-02-09 23:33:35 +03:00
for _, e := range v {
2019-12-20 22:40:38 +03:00
objs = append(objs, &tengo.Int{Value: int64(e)})
2019-02-09 23:33:35 +03:00
}
2019-12-20 22:40:38 +03:00
return &tengo.Array{Value: objs}
}
panic(fmt.Errorf("unknown type: %T", v))
}
func expect(t *testing.T, input string, expected interface{}) {
2019-12-20 22:40:38 +03:00
s := tengo.NewScript([]byte(input))
s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
c, err := s.Run()
2019-12-20 22:40:38 +03:00
require.NoError(t, err)
require.NotNil(t, c)
v := c.Get("out")
2019-12-20 22:40:38 +03:00
require.NotNil(t, v)
require.Equal(t, expected, v.Value())
}