aboutsummaryrefslogtreecommitdiffstats
path: root/jsre/jsre_test.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-03-15 14:13:39 +0800
committerzelig <viktor.tron@gmail.com>2015-03-15 14:13:39 +0800
commitda44097800caf2e20ec8e067f8700e59a43a5e06 (patch)
tree59caea58032056fbc42395cf3a1eb53f5f75e46c /jsre/jsre_test.go
parent2a5fbced7fb1fe976367f3f9f334c6ca657e7917 (diff)
downloaddexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar.gz
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar.bz2
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar.lz
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar.xz
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.tar.zst
dexon-da44097800caf2e20ec8e067f8700e59a43a5e06.zip
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
Diffstat (limited to 'jsre/jsre_test.go')
-rw-r--r--jsre/jsre_test.go85
1 files changed, 85 insertions, 0 deletions
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)
+ }
+
+}