From da44097800caf2e20ec8e067f8700e59a43a5e06 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 15 Mar 2015 13:13:39 +0700 Subject: jsre: - require became loadScript(), no require is supplied - bignumber_js.go: heredoc v2.0.3 minified fixed for otto Regexp incompatibility https://github.com/robertkrimen/otto#regular-expression-incompatibility - bignumber.min.js also updated in mist/assets/ext - ethereum_js.go: latest master minified - assetPath in constructor - Eval/Exec/Handle/ToVal nice API - jsre tests --- jsre/jsre_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 jsre/jsre_test.go (limited to 'jsre/jsre_test.go') diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go new file mode 100644 index 000000000..67b5ea906 --- /dev/null +++ b/jsre/jsre_test.go @@ -0,0 +1,85 @@ +package jsre + +import ( + "github.com/obscuren/otto" + "os" + "path" + "testing" + + "github.com/ethereum/go-ethereum/ethutil" +) + +var defaultAssetPath = path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") + +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") + + ethutil.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) + } + + // this errors + err = jsre.Exec(path.Join(defaultAssetPath, "bignumber.min.js")) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + _, err = jsre.Run("x = new BigNumber(123.4567);") + if err != nil { + t.Errorf("expected no error, got %v", err) + } +} + +func TestBind(t *testing.T) { + jsre := New(defaultAssetPath) + + 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 TestRequire(t *testing.T) { + jsre := New(defaultAssetPath) + + _, err := jsre.Run("x = new BigNumber(123.4567);") + if err == nil { + t.Errorf("expected error, got nothing") + } + _, err = jsre.Run(`loadScript("bignumber.min.js"); x = new BigNumber(123.4567)`) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + +} -- cgit From 7bc40aa963626383dbb6a5cb6fe10736dc1d74fa Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 15 Mar 2015 18:42:17 +0700 Subject: jsre: remove assetpath using GOPATH from test --- jsre/jsre_test.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'jsre/jsre_test.go') diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go index 67b5ea906..e4decf2f4 100644 --- a/jsre/jsre_test.go +++ b/jsre/jsre_test.go @@ -2,15 +2,11 @@ package jsre import ( "github.com/obscuren/otto" - "os" - "path" "testing" "github.com/ethereum/go-ethereum/ethutil" ) -var defaultAssetPath = path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") - type testNativeObjectBinding struct { toVal func(interface{}) otto.Value } @@ -42,20 +38,15 @@ func TestExec(t *testing.T) { if !val.IsString() { t.Errorf("expected string value, got %v", val) } - - // this errors - err = jsre.Exec(path.Join(defaultAssetPath, "bignumber.min.js")) - if err != nil { - t.Errorf("expected no error, got %v", err) - } - _, err = jsre.Run("x = new BigNumber(123.4567);") - if err != nil { - t.Errorf("expected no error, got %v", err) + exp := "testMsg" + got, _ := val.ToString() + if exp != got { + t.Errorf("expected '%v', got '%v'", exp, got) } } func TestBind(t *testing.T) { - jsre := New(defaultAssetPath) + jsre := New("/tmp") jsre.Bind("no", &testNativeObjectBinding{jsre.ToVal}) @@ -70,16 +61,24 @@ func TestBind(t *testing.T) { t.Logf("no: %v", pp) } -func TestRequire(t *testing.T) { - jsre := New(defaultAssetPath) +func TestLoadScript(t *testing.T) { + jsre := New("/tmp") - _, err := jsre.Run("x = new BigNumber(123.4567);") - if err == nil { - t.Errorf("expected error, got nothing") + ethutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) + _, err := jsre.Run(`loadScript("test.js")`) + if err != nil { + t.Errorf("expected no error, got %v", err) } - _, err = jsre.Run(`loadScript("bignumber.min.js"); x = new BigNumber(123.4567)`) + 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) + } } -- cgit From b4060da9eb717f43346f24822f5b25c3385a8519 Mon Sep 17 00:00:00 2001 From: zelig Date: Mon, 16 Mar 2015 22:48:54 +0700 Subject: ethutil -> common --- jsre/jsre_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'jsre/jsre_test.go') diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go index e4decf2f4..f01854b51 100644 --- a/jsre/jsre_test.go +++ b/jsre/jsre_test.go @@ -4,7 +4,7 @@ import ( "github.com/obscuren/otto" "testing" - "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/common" ) type testNativeObjectBinding struct { @@ -26,7 +26,7 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value func TestExec(t *testing.T) { jsre := New("/tmp") - ethutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) + common.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) err := jsre.Exec("test.js") if err != nil { t.Errorf("expected no error, got %v", err) @@ -64,7 +64,7 @@ func TestBind(t *testing.T) { func TestLoadScript(t *testing.T) { jsre := New("/tmp") - ethutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) + 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) -- cgit