306055ad65
* add helper functions for builtin functions and builtin modules * fix a builtin function bug for modules
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package objects_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/d5/tengo/assert"
|
|
"github.com/d5/tengo/objects"
|
|
)
|
|
|
|
func TestGetBuiltinFunctions(t *testing.T) {
|
|
testGetBuiltinFunctions(t)
|
|
testGetBuiltinFunctions(t, "print")
|
|
testGetBuiltinFunctions(t, "int", "float")
|
|
testGetBuiltinFunctions(t, "int", "float", "printf")
|
|
testGetBuiltinFunctions(t, "int", "int") // duplicate names ignored
|
|
}
|
|
|
|
func TestGetAllBuiltinFunctions(t *testing.T) {
|
|
funcs := objects.GetAllBuiltinFunctions()
|
|
if !assert.Equal(t, len(objects.Builtins), len(funcs)) {
|
|
return
|
|
}
|
|
|
|
namesM := make(map[string]bool)
|
|
for _, bf := range objects.Builtins {
|
|
namesM[bf.Name] = true
|
|
}
|
|
|
|
for _, bf := range funcs {
|
|
assert.True(t, namesM[bf.Name], "name: %s", bf.Name)
|
|
}
|
|
}
|
|
|
|
func TestAllBuiltinFunctionNames(t *testing.T) {
|
|
names := objects.AllBuiltinFunctionNames()
|
|
if !assert.Equal(t, len(objects.Builtins), len(names)) {
|
|
return
|
|
}
|
|
|
|
namesM := make(map[string]bool)
|
|
for _, name := range names {
|
|
namesM[name] = true
|
|
}
|
|
|
|
for _, bf := range objects.Builtins {
|
|
assert.True(t, namesM[bf.Name], "name: %s", bf.Name)
|
|
}
|
|
}
|
|
|
|
func testGetBuiltinFunctions(t *testing.T, names ...string) {
|
|
// remove duplicates
|
|
namesM := make(map[string]bool)
|
|
for _, name := range names {
|
|
namesM[name] = true
|
|
}
|
|
|
|
funcs := objects.GetBuiltinFunctions(names...)
|
|
if !assert.Equal(t, len(namesM), len(funcs)) {
|
|
return
|
|
}
|
|
|
|
for _, bf := range funcs {
|
|
assert.True(t, namesM[bf.Name], "name: %s", bf.Name)
|
|
}
|
|
}
|