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