github.com/luckypickle/go-ethereum-vet@v1.14.2/internal/jsre/jsre_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package jsre 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path" 23 "testing" 24 "time" 25 26 "github.com/robertkrimen/otto" 27 ) 28 29 type testNativeObjectBinding struct{} 30 31 type msg struct { 32 Msg string 33 } 34 35 func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value { 36 m, err := call.Argument(0).ToString() 37 if err != nil { 38 return otto.UndefinedValue() 39 } 40 v, _ := call.Otto.ToValue(&msg{m}) 41 return v 42 } 43 44 func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) { 45 dir, err := ioutil.TempDir("", "jsre-test") 46 if err != nil { 47 t.Fatal("cannot create temporary directory:", err) 48 } 49 if testjs != "" { 50 if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { 51 t.Fatal("cannot create test.js:", err) 52 } 53 } 54 return New(dir, os.Stdout), dir 55 } 56 57 func TestExec(t *testing.T) { 58 jsre, dir := newWithTestJS(t, `msg = "testMsg"`) 59 defer os.RemoveAll(dir) 60 61 err := jsre.Exec("test.js") 62 if err != nil { 63 t.Errorf("expected no error, got %v", err) 64 } 65 val, err := jsre.Run("msg") 66 if err != nil { 67 t.Errorf("expected no error, got %v", err) 68 } 69 if !val.IsString() { 70 t.Errorf("expected string value, got %v", val) 71 } 72 exp := "testMsg" 73 got, _ := val.ToString() 74 if exp != got { 75 t.Errorf("expected '%v', got '%v'", exp, got) 76 } 77 jsre.Stop(false) 78 } 79 80 func TestNatto(t *testing.T) { 81 jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) 82 defer os.RemoveAll(dir) 83 84 err := jsre.Exec("test.js") 85 if err != nil { 86 t.Errorf("expected no error, got %v", err) 87 } 88 time.Sleep(100 * time.Millisecond) 89 val, err := jsre.Run("msg") 90 if err != nil { 91 t.Errorf("expected no error, got %v", err) 92 } 93 if !val.IsString() { 94 t.Errorf("expected string value, got %v", val) 95 } 96 exp := "testMsg" 97 got, _ := val.ToString() 98 if exp != got { 99 t.Errorf("expected '%v', got '%v'", exp, got) 100 } 101 jsre.Stop(false) 102 } 103 104 func TestBind(t *testing.T) { 105 jsre := New("", os.Stdout) 106 defer jsre.Stop(false) 107 108 jsre.Bind("no", &testNativeObjectBinding{}) 109 110 _, err := jsre.Run(`no.TestMethod("testMsg")`) 111 if err != nil { 112 t.Errorf("expected no error, got %v", err) 113 } 114 } 115 116 func TestLoadScript(t *testing.T) { 117 jsre, dir := newWithTestJS(t, `msg = "testMsg"`) 118 defer os.RemoveAll(dir) 119 120 _, err := jsre.Run(`loadScript("test.js")`) 121 if err != nil { 122 t.Errorf("expected no error, got %v", err) 123 } 124 val, err := jsre.Run("msg") 125 if err != nil { 126 t.Errorf("expected no error, got %v", err) 127 } 128 if !val.IsString() { 129 t.Errorf("expected string value, got %v", val) 130 } 131 exp := "testMsg" 132 got, _ := val.ToString() 133 if exp != got { 134 t.Errorf("expected '%v', got '%v'", exp, got) 135 } 136 jsre.Stop(false) 137 }