2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-03-15 06:13:39 +00:00
|
|
|
package jsre
|
|
|
|
|
|
|
|
import (
|
2015-03-25 14:58:52 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-08-06 14:19:09 +00:00
|
|
|
"path"
|
2015-03-15 06:13:39 +00:00
|
|
|
"testing"
|
2015-04-22 00:31:59 +00:00
|
|
|
"time"
|
2015-05-25 00:27:37 +00:00
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2015-03-15 06:13:39 +00:00
|
|
|
)
|
|
|
|
|
2015-05-25 00:27:37 +00:00
|
|
|
type testNativeObjectBinding struct{}
|
2015-03-15 06:13:39 +00:00
|
|
|
|
|
|
|
type msg struct {
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
|
|
|
|
m, err := call.Argument(0).ToString()
|
|
|
|
if err != nil {
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
2015-05-25 00:27:37 +00:00
|
|
|
v, _ := call.Otto.ToValue(&msg{m})
|
|
|
|
return v
|
2015-03-15 06:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 14:19:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-15 06:13:39 +00:00
|
|
|
func TestExec(t *testing.T) {
|
2015-08-06 14:19:09 +00:00
|
|
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
|
|
|
defer os.RemoveAll(dir)
|
2015-03-15 06:13:39 +00:00
|
|
|
|
|
|
|
err := jsre.Exec("test.js")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
val, err := jsre.Run("msg")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
if !val.IsString() {
|
|
|
|
t.Errorf("expected string value, got %v", val)
|
|
|
|
}
|
2015-03-15 11:42:17 +00:00
|
|
|
exp := "testMsg"
|
|
|
|
got, _ := val.ToString()
|
|
|
|
if exp != got {
|
|
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
2015-03-15 06:13:39 +00:00
|
|
|
}
|
2015-04-22 00:31:59 +00:00
|
|
|
jsre.Stop(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatto(t *testing.T) {
|
2015-08-06 14:19:09 +00:00
|
|
|
jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
|
|
|
|
defer os.RemoveAll(dir)
|
2015-04-22 00:31:59 +00:00
|
|
|
|
|
|
|
err := jsre.Exec("test.js")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
val, err := jsre.Run("msg")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
if !val.IsString() {
|
|
|
|
t.Errorf("expected string value, got %v", val)
|
|
|
|
}
|
|
|
|
exp := "testMsg"
|
|
|
|
got, _ := val.ToString()
|
|
|
|
if exp != got {
|
|
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
|
|
|
}
|
|
|
|
jsre.Stop(false)
|
2015-03-15 06:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
2015-08-06 14:19:09 +00:00
|
|
|
jsre := New("")
|
2015-03-15 06:13:39 +00:00
|
|
|
|
2015-05-25 00:27:37 +00:00
|
|
|
jsre.Bind("no", &testNativeObjectBinding{})
|
2015-03-15 06:13:39 +00:00
|
|
|
|
2015-03-20 12:48:23 +00:00
|
|
|
val, err := jsre.Run(`no.TestMethod("testMsg")`)
|
2015-03-15 06:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
pp, err := jsre.PrettyPrint(val)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("no: %v", pp)
|
2015-04-22 00:31:59 +00:00
|
|
|
jsre.Stop(false)
|
2015-03-15 06:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 11:42:17 +00:00
|
|
|
func TestLoadScript(t *testing.T) {
|
2015-08-06 14:19:09 +00:00
|
|
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
|
|
|
defer os.RemoveAll(dir)
|
2015-03-15 06:13:39 +00:00
|
|
|
|
2015-03-15 11:42:17 +00:00
|
|
|
_, err := jsre.Run(`loadScript("test.js")`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
2015-03-15 06:13:39 +00:00
|
|
|
}
|
2015-03-15 11:42:17 +00:00
|
|
|
val, err := jsre.Run("msg")
|
2015-03-15 06:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
2015-03-15 11:42:17 +00:00
|
|
|
if !val.IsString() {
|
|
|
|
t.Errorf("expected string value, got %v", val)
|
|
|
|
}
|
|
|
|
exp := "testMsg"
|
|
|
|
got, _ := val.ToString()
|
|
|
|
if exp != got {
|
|
|
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
|
|
|
}
|
2015-04-22 00:31:59 +00:00
|
|
|
jsre.Stop(false)
|
2015-08-06 14:19:09 +00:00
|
|
|
}
|