jsre_test.go 3.2 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
	"os"
21
	"path"
22
	"reflect"
zelig's avatar
zelig committed
23
	"testing"
24
	"time"
25

26
	"github.com/dop251/goja"
zelig's avatar
zelig committed
27 28
)

29 30 31
type testNativeObjectBinding struct {
	vm *goja.Runtime
}
zelig's avatar
zelig committed
32 33 34 35 36

type msg struct {
	Msg string
}

37 38 39
func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
	m := call.Argument(0).ToString().String()
	return no.vm.ToValue(&msg{m})
zelig's avatar
zelig committed
40 41
}

42 43
func newWithTestJS(t *testing.T, testjs string) *JSRE {
	dir := t.TempDir()
44
	if testjs != "" {
45
		if err := os.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
46 47 48
			t.Fatal("cannot create test.js:", err)
		}
	}
49
	jsre := New(dir, os.Stdout)
50
	return jsre
51 52
}

zelig's avatar
zelig committed
53
func TestExec(t *testing.T) {
54
	jsre := newWithTestJS(t, `msg = "testMsg"`)
zelig's avatar
zelig committed
55 56 57 58 59 60 61 62 63

	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)
	}
64
	if val.ExportType().Kind() != reflect.String {
zelig's avatar
zelig committed
65 66
		t.Errorf("expected string value, got %v", val)
	}
67
	exp := "testMsg"
68
	got := val.ToString().String()
69 70
	if exp != got {
		t.Errorf("expected '%v', got '%v'", exp, got)
zelig's avatar
zelig committed
71
	}
72 73 74 75
	jsre.Stop(false)
}

func TestNatto(t *testing.T) {
76
	jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
77 78 79

	err := jsre.Exec("test.js")
	if err != nil {
80
		t.Fatalf("expected no error, got %v", err)
81
	}
82
	time.Sleep(100 * time.Millisecond)
83 84
	val, err := jsre.Run("msg")
	if err != nil {
85
		t.Fatalf("expected no error, got %v", err)
86
	}
87
	if val.ExportType().Kind() != reflect.String {
88
		t.Fatalf("expected string value, got %v", val)
89 90
	}
	exp := "testMsg"
91
	got := val.ToString().String()
92
	if exp != got {
93
		t.Fatalf("expected '%v', got '%v'", exp, got)
94 95
	}
	jsre.Stop(false)
zelig's avatar
zelig committed
96 97 98
}

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

102
	jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
zelig's avatar
zelig committed
103

104
	_, err := jsre.Run(`no.TestMethod("testMsg")`)
zelig's avatar
zelig committed
105 106 107 108 109
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
}

110
func TestLoadScript(t *testing.T) {
111
	jsre := newWithTestJS(t, `msg = "testMsg"`)
zelig's avatar
zelig committed
112

113 114 115
	_, err := jsre.Run(`loadScript("test.js")`)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
zelig's avatar
zelig committed
116
	}
117
	val, err := jsre.Run("msg")
zelig's avatar
zelig committed
118 119 120
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
121
	if val.ExportType().Kind() != reflect.String {
122 123 124
		t.Errorf("expected string value, got %v", val)
	}
	exp := "testMsg"
125
	got := val.ToString().String()
126 127 128
	if exp != got {
		t.Errorf("expected '%v', got '%v'", exp, got)
	}
129
	jsre.Stop(false)
130
}