129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2015 The go-ethereum Authors
 | |
| // This file is part of go-ethereum.
 | |
| //
 | |
| // go-ethereum 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.
 | |
| //
 | |
| // go-ethereum 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 go-ethereum.  If not, see <http://www.gnu.org/licenses/>.
 | |
| 
 | |
| package jsre
 | |
| 
 | |
| import (
 | |
| 	"io/ioutil"
 | |
| 	"os"
 | |
| 	"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 TestExec(t *testing.T) {
 | |
| 	jsre := New("/tmp")
 | |
| 
 | |
| 	ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
 | |
| 	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 := New("/tmp")
 | |
| 
 | |
| 	ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm)
 | |
| 	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)
 | |
| }
 | |
| 
 | |
| func TestBind(t *testing.T) {
 | |
| 	jsre := New("/tmp")
 | |
| 
 | |
| 	jsre.Bind("no", &testNativeObjectBinding{})
 | |
| 
 | |
| 	val, err := jsre.Run(`no.TestMethod("testMsg")`)
 | |
| 	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)
 | |
| 	jsre.Stop(false)
 | |
| }
 | |
| 
 | |
| func TestLoadScript(t *testing.T) {
 | |
| 	jsre := New("/tmp")
 | |
| 
 | |
| 	ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
 | |
| 	_, 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)
 | |
| }
 |