github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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 {
    44  	dir := t.TempDir()
    45  	if testjs != "" {
    46  		if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
    47  			t.Fatal("cannot create test.js:", err)
    48  		}
    49  	}
    50  	jsre := New(dir, os.Stdout)
    51  	return jsre
    52  }
    53  
    54  func TestExec(t *testing.T) {
    55  	jsre := newWithTestJS(t, `msg = "testMsg"`)
    56  
    57  	err := jsre.Exec("test.js")
    58  	if err != nil {
    59  		t.Errorf("expected no error, got %v", err)
    60  	}
    61  	val, err := jsre.Run("msg")
    62  	if err != nil {
    63  		t.Errorf("expected no error, got %v", err)
    64  	}
    65  	if val.ExportType().Kind() != reflect.String {
    66  		t.Errorf("expected string value, got %v", val)
    67  	}
    68  	exp := "testMsg"
    69  	got := val.ToString().String()
    70  	if exp != got {
    71  		t.Errorf("expected '%v', got '%v'", exp, got)
    72  	}
    73  	jsre.Stop(false)
    74  }
    75  
    76  func TestNatto(t *testing.T) {
    77  	jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
    78  
    79  	err := jsre.Exec("test.js")
    80  	if err != nil {
    81  		t.Fatalf("expected no error, got %v", err)
    82  	}
    83  	time.Sleep(100 * time.Millisecond)
    84  	val, err := jsre.Run("msg")
    85  	if err != nil {
    86  		t.Fatalf("expected no error, got %v", err)
    87  	}
    88  	if val.ExportType().Kind() != reflect.String {
    89  		t.Fatalf("expected string value, got %v", val)
    90  	}
    91  	exp := "testMsg"
    92  	got := val.ToString().String()
    93  	if exp != got {
    94  		t.Fatalf("expected '%v', got '%v'", exp, got)
    95  	}
    96  	jsre.Stop(false)
    97  }
    98  
    99  func TestBind(t *testing.T) {
   100  	jsre := New("", os.Stdout)
   101  	defer jsre.Stop(false)
   102  
   103  	jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
   104  
   105  	_, err := jsre.Run(`no.TestMethod("testMsg")`)
   106  	if err != nil {
   107  		t.Errorf("expected no error, got %v", err)
   108  	}
   109  }
   110  
   111  func TestLoadScript(t *testing.T) {
   112  	jsre := newWithTestJS(t, `msg = "testMsg"`)
   113  
   114  	_, err := jsre.Run(`loadScript("test.js")`)
   115  	if err != nil {
   116  		t.Errorf("expected no error, got %v", err)
   117  	}
   118  	val, err := jsre.Run("msg")
   119  	if err != nil {
   120  		t.Errorf("expected no error, got %v", err)
   121  	}
   122  	if val.ExportType().Kind() != reflect.String {
   123  		t.Errorf("expected string value, got %v", val)
   124  	}
   125  	exp := "testMsg"
   126  	got := val.ToString().String()
   127  	if exp != got {
   128  		t.Errorf("expected '%v', got '%v'", exp, got)
   129  	}
   130  	jsre.Stop(false)
   131  }