github.com/klaytn/klaytn@v1.12.1/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 "os" 25 "path" 26 "reflect" 27 "testing" 28 "time" 29 30 "github.com/dop251/goja" 31 ) 32 33 type testNativeObjectBinding struct { 34 vm *goja.Runtime 35 } 36 37 type msg struct { 38 Msg string 39 } 40 41 func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value { 42 m := call.Argument(0).ToString().String() 43 return no.vm.ToValue(&msg{m}) 44 } 45 46 func newWithTestJS(t *testing.T, testjs string) *JSRE { 47 dir := t.TempDir() 48 if testjs != "" { 49 if err := os.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { 50 t.Fatal("cannot create test.js:", err) 51 } 52 } 53 jsre := New(dir, os.Stdout) 54 return jsre 55 } 56 57 func TestExec(t *testing.T) { 58 jsre := newWithTestJS(t, `msg = "testMsg"`) 59 60 err := jsre.Exec("test.js") 61 if err != nil { 62 t.Errorf("expected no error, got %v", err) 63 } 64 val, err := jsre.Run("msg") 65 if err != nil { 66 t.Errorf("expected no error, got %v", err) 67 } 68 if val.ExportType().Kind() != reflect.String { 69 t.Errorf("expected string value, got %v", val) 70 } 71 exp := "testMsg" 72 got := val.ToString().String() 73 if exp != got { 74 t.Errorf("expected '%v', got '%v'", exp, got) 75 } 76 jsre.Stop(false) 77 } 78 79 func TestNatto(t *testing.T) { 80 jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) 81 82 err := jsre.Exec("test.js") 83 if err != nil { 84 t.Fatalf("expected no error, got %v", err) 85 } 86 time.Sleep(100 * time.Millisecond) 87 val, err := jsre.Run("msg") 88 if err != nil { 89 t.Fatalf("expected no error, got %v", err) 90 } 91 if val.ExportType().Kind() != reflect.String { 92 t.Fatalf("expected string value, got %v", val) 93 } 94 exp := "testMsg" 95 got := val.ToString().String() 96 if exp != got { 97 t.Fatalf("expected '%v', got '%v'", exp, got) 98 } 99 jsre.Stop(false) 100 } 101 102 func TestBind(t *testing.T) { 103 jsre := New("", os.Stdout) 104 defer jsre.Stop(false) 105 106 jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm}) 107 108 _, err := jsre.Run(`no.TestMethod("testMsg")`) 109 if err != nil { 110 t.Errorf("expected no error, got %v", err) 111 } 112 } 113 114 func TestLoadScript(t *testing.T) { 115 jsre := newWithTestJS(t, `msg = "testMsg"`) 116 117 _, err := jsre.Run(`loadScript("test.js")`) 118 if err != nil { 119 t.Errorf("expected no error, got %v", err) 120 } 121 val, err := jsre.Run("msg") 122 if err != nil { 123 t.Errorf("expected no error, got %v", err) 124 } 125 if val.ExportType().Kind() != reflect.String { 126 t.Errorf("expected string value, got %v", val) 127 } 128 exp := "testMsg" 129 got := val.ToString().String() 130 if exp != got { 131 t.Errorf("expected '%v', got '%v'", exp, got) 132 } 133 jsre.Stop(false) 134 }