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