2019-01-29 05:30:26 +03:00
|
|
|
package stdlib_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-01-30 04:39:25 +03:00
|
|
|
"time"
|
2019-01-29 05:30:26 +03:00
|
|
|
|
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"
|
2019-01-29 05:30:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type ARR = []interface{}
|
|
|
|
type MAP = map[string]interface{}
|
|
|
|
type IARR []interface{}
|
|
|
|
type IMAP map[string]interface{}
|
|
|
|
|
2019-03-01 05:41:29 +03:00
|
|
|
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))
|
2019-03-01 05:41:29 +03:00
|
|
|
}
|
|
|
|
|
2019-03-04 21:21:39 +03:00
|
|
|
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"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:41:29 +03:00
|
|
|
func TestGetModules(t *testing.T) {
|
2019-03-20 11:28:40 +03:00
|
|
|
mods := stdlib.GetModuleMap()
|
2019-12-20 22:40:38 +03:00
|
|
|
require.Equal(t, 0, mods.Len())
|
2019-03-01 05:41:29 +03:00
|
|
|
|
2019-03-20 11:28:40 +03:00
|
|
|
mods = stdlib.GetModuleMap("os")
|
2019-12-20 22:40:38 +03:00
|
|
|
require.Equal(t, 1, mods.Len())
|
|
|
|
require.NotNil(t, mods.Get("os"))
|
2019-03-01 05:41:29 +03:00
|
|
|
|
2019-03-20 11:28:40 +03:00
|
|
|
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"))
|
2019-03-01 05:41:29 +03:00
|
|
|
|
2019-03-20 11:28:40 +03:00
|
|
|
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"))
|
2019-03-01 05:41:29 +03:00
|
|
|
|
2019-03-20 11:28:40 +03:00
|
|
|
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"))
|
2019-03-01 05:41:29 +03:00
|
|
|
}
|
|
|
|
|
2019-01-29 05:30:26 +03:00
|
|
|
type callres struct {
|
|
|
|
t *testing.T
|
2019-03-18 18:15:26 +03:00
|
|
|
o interface{}
|
2019-01-29 05:30:26 +03:00
|
|
|
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
|
2019-01-29 05:30:26 +03:00
|
|
|
for _, v := range args {
|
|
|
|
oargs = append(oargs, object(v))
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
switch o := c.o.(type) {
|
2019-12-20 22:40:38 +03:00
|
|
|
case *tengo.BuiltinModule:
|
2019-03-18 18:15:26 +03:00
|
|
|
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-03-18 18:15:26 +03:00
|
|
|
}
|
2019-01-29 05:30:26 +03:00
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
f, ok := m.(*tengo.UserFunction)
|
2019-03-18 18:15:26 +03:00
|
|
|
if !ok {
|
2019-12-20 22:40:38 +03:00
|
|
|
return callres{t: c.t, e: fmt.Errorf(
|
|
|
|
"non-callable: %s", funcName)}
|
2019-03-18 18:15:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2019-03-18 18:15:26 +03:00
|
|
|
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:
|
2019-03-18 18:15:26 +03:00
|
|
|
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)
|
2019-03-18 18:15:26 +03:00
|
|
|
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-01-29 05:30:26 +03:00
|
|
|
}
|
|
|
|
|
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-01-29 05:30:26 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
func (c callres) expectError() {
|
|
|
|
require.Error(c.t, c.e)
|
2019-01-29 05:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func module(t *testing.T, moduleName string) callres {
|
2019-03-20 11:28:40 +03:00
|
|
|
mod := stdlib.GetModuleMap(moduleName).GetBuiltinModule(moduleName)
|
|
|
|
if mod == nil {
|
2019-01-29 05:30:26 +03:00
|
|
|
return callres{t: t, e: fmt.Errorf("module not found: %s", moduleName)}
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:21:39 +03:00
|
|
|
return callres{t: t, o: mod}
|
2019-01-29 05:30:26 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
func object(v interface{}) tengo.Object {
|
2019-01-29 05:30:26 +03:00
|
|
|
switch v := v.(type) {
|
2019-12-20 22:40:38 +03:00
|
|
|
case tengo.Object:
|
2019-01-29 05:30:26 +03:00
|
|
|
return v
|
|
|
|
case string:
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.String{Value: v}
|
2019-01-29 05:30:26 +03:00
|
|
|
case int64:
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Int{Value: v}
|
2019-01-29 05:30:26 +03:00
|
|
|
case int: // for convenience
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Int{Value: int64(v)}
|
2019-01-29 05:30:26 +03:00
|
|
|
case bool:
|
|
|
|
if v {
|
2019-12-20 22:40:38 +03:00
|
|
|
return tengo.TrueValue
|
2019-01-29 05:30:26 +03:00
|
|
|
}
|
2019-12-20 22:40:38 +03:00
|
|
|
return tengo.FalseValue
|
2019-01-29 05:30:26 +03:00
|
|
|
case rune:
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Char{Value: v}
|
2019-01-29 05:30:26 +03:00
|
|
|
case byte: // for convenience
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Char{Value: rune(v)}
|
2019-01-29 05:30:26 +03:00
|
|
|
case float64:
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Float{Value: v}
|
2019-01-29 05:30:26 +03:00
|
|
|
case []byte:
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Bytes{Value: v}
|
2019-01-29 05:30:26 +03:00
|
|
|
case MAP:
|
2019-12-20 22:40:38 +03:00
|
|
|
objs := make(map[string]tengo.Object)
|
2019-01-29 05:30:26 +03:00
|
|
|
for k, v := range v {
|
|
|
|
objs[k] = object(v)
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Map{Value: objs}
|
2019-01-29 05:30:26 +03:00
|
|
|
case ARR:
|
2019-12-20 22:40:38 +03:00
|
|
|
var objs []tengo.Object
|
2019-01-29 05:30:26 +03:00
|
|
|
for _, e := range v {
|
|
|
|
objs = append(objs, object(e))
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.Array{Value: objs}
|
2019-01-29 05:30:26 +03:00
|
|
|
case IMAP:
|
2019-12-20 22:40:38 +03:00
|
|
|
objs := make(map[string]tengo.Object)
|
2019-01-29 05:30:26 +03:00
|
|
|
for k, v := range v {
|
|
|
|
objs[k] = object(v)
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
return &tengo.ImmutableMap{Value: objs}
|
2019-01-29 05:30:26 +03:00
|
|
|
case IARR:
|
2019-12-20 22:40:38 +03:00
|
|
|
var objs []tengo.Object
|
2019-01-29 05:30:26 +03:00
|
|
|
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}
|
2019-01-29 05:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("unknown type: %T", v))
|
|
|
|
}
|
2019-03-04 21:21:39 +03:00
|
|
|
|
|
|
|
func expect(t *testing.T, input string, expected interface{}) {
|
2019-12-20 22:40:38 +03:00
|
|
|
s := tengo.NewScript([]byte(input))
|
2019-03-20 11:28:40 +03:00
|
|
|
s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
|
2019-03-04 21:21:39 +03:00
|
|
|
c, err := s.Run()
|
2019-12-20 22:40:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, c)
|
2019-03-04 21:21:39 +03:00
|
|
|
v := c.Get("out")
|
2019-12-20 22:40:38 +03:00
|
|
|
require.NotNil(t, v)
|
|
|
|
require.Equal(t, expected, v.Value())
|
2019-03-04 21:21:39 +03:00
|
|
|
}
|