jsre_test.go 3.33 KB
Newer Older
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// 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
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

zelig's avatar
zelig committed
17 18 19
package jsre

import (
20 21
	"io/ioutil"
	"os"
22
	"path"
zelig's avatar
zelig committed
23
	"testing"
24
	"time"
25 26

	"github.com/robertkrimen/otto"
zelig's avatar
zelig committed
27 28
)

29
type testNativeObjectBinding struct{}
zelig's avatar
zelig committed
30 31 32 33 34 35 36 37 38 39

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()
	}
40 41
	v, _ := call.Otto.ToValue(&msg{m})
	return v
zelig's avatar
zelig committed
42 43
}

44 45 46 47 48 49 50 51 52 53
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)
		}
	}
54
	return New(dir, os.Stdout), dir
55 56
}

zelig's avatar
zelig committed
57
func TestExec(t *testing.T) {
58 59
	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
	defer os.RemoveAll(dir)
zelig's avatar
zelig committed
60 61 62 63 64 65 66 67 68 69 70 71

	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)
	}
72 73 74 75
	exp := "testMsg"
	got, _ := val.ToString()
	if exp != got {
		t.Errorf("expected '%v', got '%v'", exp, got)
zelig's avatar
zelig committed
76
	}
77 78 79 80
	jsre.Stop(false)
}

func TestNatto(t *testing.T) {
81 82
	jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
	defer os.RemoveAll(dir)
83 84 85 86 87

	err := jsre.Exec("test.js")
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
88
	time.Sleep(100 * time.Millisecond)
89 90 91 92 93 94 95 96 97 98 99 100 101
	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)
zelig's avatar
zelig committed
102 103 104
}

func TestBind(t *testing.T) {
105
	jsre := New("", os.Stdout)
106
	defer jsre.Stop(false)
zelig's avatar
zelig committed
107

108
	jsre.Bind("no", &testNativeObjectBinding{})
zelig's avatar
zelig committed
109

110
	_, err := jsre.Run(`no.TestMethod("testMsg")`)
zelig's avatar
zelig committed
111 112 113 114 115
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
}

116
func TestLoadScript(t *testing.T) {
117 118
	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
	defer os.RemoveAll(dir)
zelig's avatar
zelig committed
119

120 121 122
	_, err := jsre.Run(`loadScript("test.js")`)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
zelig's avatar
zelig committed
123
	}
124
	val, err := jsre.Run("msg")
zelig's avatar
zelig committed
125 126 127
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
128 129 130 131 132 133 134 135
	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)
	}
136
	jsre.Stop(false)
137
}