aboutsummaryrefslogtreecommitdiffstats
path: root/internal/jsre/jsre_test.go
blob: bcb6e0dd23fd7919dab243c05e0b48ed0270664b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package jsre

import (
    "io/ioutil"
    "os"
    "path"
    "testing"
    "time"

    "github.com/robertkrimen/otto"
)

type testNativeObjectBinding struct{}

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()
    }
    v, _ := call.Otto.ToValue(&msg{m})
    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, os.Stdout), dir
}

func TestExec(t *testing.T) {
    jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
    defer os.RemoveAll(dir)

    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)
    }
    exp := "testMsg"
    got, _ := val.ToString()
    if exp != got {
        t.Errorf("expected '%v', got '%v'", exp, got)
    }
    jsre.Stop(false)
}

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

    err := jsre.Exec("test.js")
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }
    time.Sleep(100 * time.Millisecond)
    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)
}

func TestBind(t *testing.T) {
    jsre := New("", os.Stdout)
    defer jsre.Stop(false)

    jsre.Bind("no", &testNativeObjectBinding{})

    _, err := jsre.Run(`no.TestMethod("testMsg")`)
    if err != nil {
        t.Errorf("expected no error, got %v", err)
    }
}

func TestLoadScript(t *testing.T) {
    jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
    defer os.RemoveAll(dir)

    _, err := jsre.Run(`loadScript("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)
    }
    exp := "testMsg"
    got, _ := val.ToString()
    if exp != got {
        t.Errorf("expected '%v', got '%v'", exp, got)
    }
    jsre.Stop(false)
}