github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/internal/jsre/jsre_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package jsre 19 20 import ( 21 "io/ioutil" 22 "os" 23 "path" 24 "testing" 25 "time" 26 27 "github.com/robertkrimen/otto" 28 ) 29 30 type testNativeObjectBinding struct{} 31 32 type msg struct { 33 Msg string 34 } 35 36 func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value { 37 m, err := call.Argument(0).ToString() 38 if err != nil { 39 return otto.UndefinedValue() 40 } 41 v, _ := call.Otto.ToValue(&msg{m}) 42 return v 43 } 44 45 func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) { 46 dir, err := ioutil.TempDir("", "jsre-test") 47 if err != nil { 48 t.Fatal("cannot create temporary directory:", err) 49 } 50 if testjs != "" { 51 if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { 52 t.Fatal("cannot create test.js:", err) 53 } 54 } 55 return New(dir, os.Stdout), dir 56 } 57 58 func TestExec(t *testing.T) { 59 jsre, dir := newWithTestJS(t, `msg = "testMsg"`) 60 defer os.RemoveAll(dir) 61 62 err := jsre.Exec("test.js") 63 if err != nil { 64 t.Errorf("expected no error, got %v", err) 65 } 66 val, err := jsre.Run("msg") 67 if err != nil { 68 t.Errorf("expected no error, got %v", err) 69 } 70 if !val.IsString() { 71 t.Errorf("expected string value, got %v", val) 72 } 73 exp := "testMsg" 74 got, _ := val.ToString() 75 if exp != got { 76 t.Errorf("expected '%v', got '%v'", exp, got) 77 } 78 jsre.Stop(false) 79 } 80 81 func TestNatto(t *testing.T) { 82 jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) 83 defer os.RemoveAll(dir) 84 85 err := jsre.Exec("test.js") 86 if err != nil { 87 t.Errorf("expected no error, got %v", err) 88 } 89 time.Sleep(100 * time.Millisecond) 90 val, err := jsre.Run("msg") 91 if err != nil { 92 t.Errorf("expected no error, got %v", err) 93 } 94 if !val.IsString() { 95 t.Errorf("expected string value, got %v", val) 96 } 97 exp := "testMsg" 98 got, _ := val.ToString() 99 if exp != got { 100 t.Errorf("expected '%v', got '%v'", exp, got) 101 } 102 jsre.Stop(false) 103 } 104 105 func TestBind(t *testing.T) { 106 jsre := New("", os.Stdout) 107 defer jsre.Stop(false) 108 109 jsre.Bind("no", &testNativeObjectBinding{}) 110 111 _, err := jsre.Run(`no.TestMethod("testMsg")`) 112 if err != nil { 113 t.Errorf("expected no error, got %v", err) 114 } 115 } 116 117 func TestLoadScript(t *testing.T) { 118 jsre, dir := newWithTestJS(t, `msg = "testMsg"`) 119 defer os.RemoveAll(dir) 120 121 _, err := jsre.Run(`loadScript("test.js")`) 122 if err != nil { 123 t.Errorf("expected no error, got %v", err) 124 } 125 val, err := jsre.Run("msg") 126 if err != nil { 127 t.Errorf("expected no error, got %v", err) 128 } 129 if !val.IsString() { 130 t.Errorf("expected string value, got %v", val) 131 } 132 exp := "testMsg" 133 got, _ := val.ToString() 134 if exp != got { 135 t.Errorf("expected '%v', got '%v'", exp, got) 136 } 137 jsre.Stop(false) 138 }