mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.3 KiB
138 lines
3.3 KiB
10 years ago
|
// Copyright 2015 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
10 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
10 years ago
|
// 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.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
10 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
|
||
10 years ago
|
package jsre
|
||
|
|
||
|
import (
|
||
10 years ago
|
"io/ioutil"
|
||
|
"os"
|
||
9 years ago
|
"path"
|
||
10 years ago
|
"testing"
|
||
10 years ago
|
"time"
|
||
10 years ago
|
|
||
|
"github.com/robertkrimen/otto"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
type testNativeObjectBinding struct{}
|
||
10 years ago
|
|
||
|
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()
|
||
|
}
|
||
10 years ago
|
v, _ := call.Otto.ToValue(&msg{m})
|
||
|
return v
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
9 years ago
|
return New(dir, os.Stdout), dir
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
func TestExec(t *testing.T) {
|
||
9 years ago
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
||
|
defer os.RemoveAll(dir)
|
||
10 years ago
|
|
||
|
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)
|
||
|
}
|
||
10 years ago
|
exp := "testMsg"
|
||
|
got, _ := val.ToString()
|
||
|
if exp != got {
|
||
|
t.Errorf("expected '%v', got '%v'", exp, got)
|
||
10 years ago
|
}
|
||
10 years ago
|
jsre.Stop(false)
|
||
|
}
|
||
|
|
||
|
func TestNatto(t *testing.T) {
|
||
9 years ago
|
jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
|
||
|
defer os.RemoveAll(dir)
|
||
10 years ago
|
|
||
|
err := jsre.Exec("test.js")
|
||
|
if err != nil {
|
||
|
t.Errorf("expected no error, got %v", err)
|
||
|
}
|
||
9 years ago
|
time.Sleep(100 * time.Millisecond)
|
||
10 years ago
|
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)
|
||
10 years ago
|
}
|
||
|
|
||
|
func TestBind(t *testing.T) {
|
||
9 years ago
|
jsre := New("", os.Stdout)
|
||
9 years ago
|
defer jsre.Stop(false)
|
||
10 years ago
|
|
||
10 years ago
|
jsre.Bind("no", &testNativeObjectBinding{})
|
||
10 years ago
|
|
||
9 years ago
|
_, err := jsre.Run(`no.TestMethod("testMsg")`)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("expected no error, got %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func TestLoadScript(t *testing.T) {
|
||
9 years ago
|
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
||
|
defer os.RemoveAll(dir)
|
||
10 years ago
|
|
||
10 years ago
|
_, err := jsre.Run(`loadScript("test.js")`)
|
||
|
if err != nil {
|
||
|
t.Errorf("expected no error, got %v", err)
|
||
10 years ago
|
}
|
||
10 years ago
|
val, err := jsre.Run("msg")
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("expected no error, got %v", err)
|
||
|
}
|
||
10 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
jsre.Stop(false)
|
||
9 years ago
|
}
|