mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.7 KiB
84 lines
1.7 KiB
package jsre
|
|
|
|
import (
|
|
"github.com/robertkrimen/otto"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
type testNativeObjectBinding struct {
|
|
toVal func(interface{}) otto.Value
|
|
}
|
|
|
|
type msg struct {
|
|
Msg string
|
|
}
|
|
|
|
func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
|
|
m, err := call.Argument(0).ToString()
|
|
if err != nil {
|
|
return otto.UndefinedValue()
|
|
}
|
|
return no.toVal(&msg{m})
|
|
}
|
|
|
|
func TestExec(t *testing.T) {
|
|
jsre := New("/tmp")
|
|
|
|
common.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`))
|
|
err := jsre.Exec("test.js")
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
val, err := jsre.Run("msg")
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
if !val.IsString() {
|
|
t.Errorf("expected string value, got %v", val)
|
|
}
|
|
exp := "testMsg"
|
|
got, _ := val.ToString()
|
|
if exp != got {
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
|
}
|
|
}
|
|
|
|
func TestBind(t *testing.T) {
|
|
jsre := New("/tmp")
|
|
|
|
jsre.Bind("no", &testNativeObjectBinding{jsre.ToVal})
|
|
|
|
val, err := jsre.Run(`no.TestMethod("testMsg")`)
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
pp, err := jsre.PrettyPrint(val)
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
t.Logf("no: %v", pp)
|
|
}
|
|
|
|
func TestLoadScript(t *testing.T) {
|
|
jsre := New("/tmp")
|
|
|
|
common.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`))
|
|
_, err := jsre.Run(`loadScript("test.js")`)
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
val, err := jsre.Run("msg")
|
|
if err != nil {
|
|
t.Errorf("expected no error, got %v", err)
|
|
}
|
|
if !val.IsString() {
|
|
t.Errorf("expected string value, got %v", val)
|
|
}
|
|
exp := "testMsg"
|
|
got, _ := val.ToString()
|
|
if exp != got {
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
|
}
|
|
}
|
|
|