init
This commit is contained in:
1 parent
bdbb3216ce
commit
c0775acb8a
25 files changed
+2252
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoja_AddFunction(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
const SCRIPT = `
|
||||
var response = {};
|
||||
try {
|
||||
response["test"] = f("string", "unknown", "test");
|
||||
} catch (e) {
|
||||
if (!(e instanceof GoError)) {
|
||||
throw(e);
|
||||
}
|
||||
if (e.value.Error() !== "TEST") {
|
||||
throw("Unexpected value: " + e.value.Error());
|
||||
}
|
||||
}
|
||||
return response;
|
||||
`
|
||||
|
||||
f := func(varchar string, integer int, object string) (interface{}, error) {
|
||||
a.Equal("string", varchar)
|
||||
a.Equal(0, integer)
|
||||
a.Equal("test", object)
|
||||
return "test", nil
|
||||
}
|
||||
vm := goja.New()
|
||||
vm.Set("f", f)
|
||||
resp, err := vm.RunString(fmt.Sprintf("(function() { %s })();", SCRIPT))
|
||||
a.NoError(err)
|
||||
a.Equal(resp.Export(), map[string]interface{}{"test": "test"})
|
||||
|
||||
f2 := func(varchar string, integer int, object string) (interface{}, error) {
|
||||
a.Equal("string", varchar)
|
||||
a.Equal(0, integer)
|
||||
a.Equal("test", object)
|
||||
return "test", errors.New("TEST")
|
||||
}
|
||||
vm = goja.New()
|
||||
vm.Set("f", f2)
|
||||
resp, err = vm.RunString(fmt.Sprintf("(function() { %s })();", SCRIPT))
|
||||
a.NoError(err)
|
||||
a.NoError(err)
|
||||
a.Equal(resp.Export(), map[string]interface{}{})
|
||||
}
|
||||
Reference in new issue
Block a user