gitee.com/liu-zhao234568/cntest@v1.0.0/internal/jsre/jsre_test.go (about)

     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  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     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  // GNU Lesser General Public License for more details.
    13  //
    14  // 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  
    17  package jsre
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path"
    23  	"reflect"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/dop251/goja"
    28  )
    29  
    30  type testNativeObjectBinding struct {
    31  	vm *goja.Runtime
    32  }
    33  
    34  type msg struct {
    35  	Msg string
    36  }
    37  
    38  func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
    39  	m := call.Argument(0).ToString().String()
    40  	return no.vm.ToValue(&msg{m})
    41  }
    42  
    43  func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
    44  	dir, err := ioutil.TempDir("", "jsre-test")
    45  	if err != nil {
    46  		t.Fatal("cannot create temporary directory:", err)
    47  	}
    48  	if testjs != "" {
    49  		if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
    50  			t.Fatal("cannot create test.js:", err)
    51  		}
    52  	}
    53  	jsre := New(dir, os.Stdout)
    54  	return jsre, dir
    55  }
    56  
    57  func TestExec(t *testing.T) {
    58  	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
    59  	defer os.RemoveAll(dir)
    60  
    61  	err := jsre.Exec("test.js")
    62  	if err != nil {
    63  		t.Errorf("expected no error, got %v", err)
    64  	}
    65  	val, err := jsre.Run("msg")
    66  	if err != nil {
    67  		t.Errorf("expected no error, got %v", err)
    68  	}
    69  	if val.ExportType().Kind() != reflect.String {
    70  		t.Errorf("expected string value, got %v", val)
    71  	}
    72  	exp := "testMsg"
    73  	got := val.ToString().String()
    74  	if exp != got {
    75  		t.Errorf("expected '%v', got '%v'", exp, got)
    76  	}
    77  	jsre.Stop(false)
    78  }
    79  
    80  func TestNatto(t *testing.T) {
    81  	jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
    82  	defer os.RemoveAll(dir)
    83  
    84  	err := jsre.Exec("test.js")
    85  	if err != nil {
    86  		t.Errorf("expected no error, got %v", err)
    87  	}
    88  	time.Sleep(100 * time.Millisecond)
    89  	val, err := jsre.Run("msg")
    90  	if err != nil {
    91  		t.Errorf("expected no error, got %v", err)
    92  	}
    93  	if val.ExportType().Kind() != reflect.String {
    94  		t.Errorf("expected string value, got %v", val)
    95  	}
    96  	exp := "testMsg"
    97  	got := val.ToString().String()
    98  	if exp != got {
    99  		t.Errorf("expected '%v', got '%v'", exp, got)
   100  	}
   101  	jsre.Stop(false)
   102  }
   103  
   104  func TestBind(t *testing.T) {
   105  	jsre := New("", os.Stdout)
   106  	defer jsre.Stop(false)
   107  
   108  	jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
   109  
   110  	_, err := jsre.Run(`no.TestMethod("testMsg")`)
   111  	if err != nil {
   112  		t.Errorf("expected no error, got %v", err)
   113  	}
   114  }
   115  
   116  func TestLoadScript(t *testing.T) {
   117  	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
   118  	defer os.RemoveAll(dir)
   119  
   120  	_, err := jsre.Run(`loadScript("test.js")`)
   121  	if err != nil {
   122  		t.Errorf("expected no error, got %v", err)
   123  	}
   124  	val, err := jsre.Run("msg")
   125  	if err != nil {
   126  		t.Errorf("expected no error, got %v", err)
   127  	}
   128  	if val.ExportType().Kind() != reflect.String {
   129  		t.Errorf("expected string value, got %v", val)
   130  	}
   131  	exp := "testMsg"
   132  	got := val.ToString().String()
   133  	if exp != got {
   134  		t.Errorf("expected '%v', got '%v'", exp, got)
   135  	}
   136  	jsre.Stop(false)
   137  }