|
|
|
@ -19,6 +19,7 @@ package jsre |
|
|
|
|
import ( |
|
|
|
|
"io/ioutil" |
|
|
|
|
"os" |
|
|
|
|
"path" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
@ -40,10 +41,23 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value |
|
|
|
|
return v |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) { |
|
|
|
|
dir, err := ioutil.TempDir("", "jsre-test") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal("cannot create temporary directory:", err) |
|
|
|
|
} |
|
|
|
|
if testjs != "" { |
|
|
|
|
if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { |
|
|
|
|
t.Fatal("cannot create test.js:", err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return New(dir), dir |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestExec(t *testing.T) { |
|
|
|
|
jsre := New("/tmp") |
|
|
|
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`) |
|
|
|
|
defer os.RemoveAll(dir) |
|
|
|
|
|
|
|
|
|
ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm) |
|
|
|
|
err := jsre.Exec("test.js") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err) |
|
|
|
@ -64,9 +78,9 @@ func TestExec(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestNatto(t *testing.T) { |
|
|
|
|
jsre := New("/tmp") |
|
|
|
|
jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) |
|
|
|
|
defer os.RemoveAll(dir) |
|
|
|
|
|
|
|
|
|
ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm) |
|
|
|
|
err := jsre.Exec("test.js") |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err) |
|
|
|
@ -88,7 +102,7 @@ func TestNatto(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) { |
|
|
|
|
jsre := New("/tmp") |
|
|
|
|
jsre := New("") |
|
|
|
|
|
|
|
|
|
jsre.Bind("no", &testNativeObjectBinding{}) |
|
|
|
|
|
|
|
|
@ -105,9 +119,9 @@ func TestBind(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestLoadScript(t *testing.T) { |
|
|
|
|
jsre := New("/tmp") |
|
|
|
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`) |
|
|
|
|
defer os.RemoveAll(dir) |
|
|
|
|
|
|
|
|
|
ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm) |
|
|
|
|
_, err := jsre.Run(`loadScript("test.js")`) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err) |
|
|
|
|