diff options
author | Felix Lange <fjl@twurst.com> | 2015-03-18 04:15:23 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-03-18 04:15:23 +0800 |
commit | 786a58d8b0188700edec824f74606a3980132f50 (patch) | |
tree | cf247ed9ca2038e9a11d000ad03b2c75b9056195 /jsre/jsre_test.go | |
parent | 9663493ba0d493ec62153e725b5d30340f2aa525 (diff) | |
parent | 17cd7a5817ce4acbfb85ebe1977952027167bd1c (diff) | |
download | dexon-786a58d8b0188700edec824f74606a3980132f50.tar dexon-786a58d8b0188700edec824f74606a3980132f50.tar.gz dexon-786a58d8b0188700edec824f74606a3980132f50.tar.bz2 dexon-786a58d8b0188700edec824f74606a3980132f50.tar.lz dexon-786a58d8b0188700edec824f74606a3980132f50.tar.xz dexon-786a58d8b0188700edec824f74606a3980132f50.tar.zst dexon-786a58d8b0188700edec824f74606a3980132f50.zip |
Merge pull request #485 from ethersphere/frontier/nodeadmin.js
Frontier console node admin interface
Diffstat (limited to 'jsre/jsre_test.go')
-rw-r--r-- | jsre/jsre_test.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go new file mode 100644 index 000000000..f01854b51 --- /dev/null +++ b/jsre/jsre_test.go @@ -0,0 +1,84 @@ +package jsre + +import ( + "github.com/obscuren/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) + } +} |