github.com/theQRL/go-zond@v0.1.1/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  	"os"
    21  	"path"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/dop251/goja"
    27  )
    28  
    29  type testNativeObjectBinding struct {
    30  	vm *goja.Runtime
    31  }
    32  
    33  type msg struct {
    34  	Msg string
    35  }
    36  
    37  func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
    38  	m := call.Argument(0).ToString().String()
    39  	return no.vm.ToValue(&msg{m})
    40  }
    41  
    42  func newWithTestJS(t *testing.T, testjs string) *JSRE {
    43  	dir := t.TempDir()
    44  	if testjs != "" {
    45  		if err := os.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
    46  			t.Fatal("cannot create test.js:", err)
    47  		}
    48  	}
    49  	jsre := New(dir, os.Stdout)
    50  	return jsre
    51  }
    52  
    53  func TestExec(t *testing.T) {
    54  	jsre := newWithTestJS(t, `msg = "testMsg"`)
    55  
    56  	err := jsre.Exec("test.js")
    57  	if err != nil {
    58  		t.Errorf("expected no error, got %v", err)
    59  	}
    60  	val, err := jsre.Run("msg")
    61  	if err != nil {
    62  		t.Errorf("expected no error, got %v", err)
    63  	}
    64  	if val.ExportType().Kind() != reflect.String {
    65  		t.Errorf("expected string value, got %v", val)
    66  	}
    67  	exp := "testMsg"
    68  	got := val.ToString().String()
    69  	if exp != got {
    70  		t.Errorf("expected '%v', got '%v'", exp, got)
    71  	}
    72  	jsre.Stop(false)
    73  }
    74  
    75  func TestNatto(t *testing.T) {
    76  	jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
    77  
    78  	err := jsre.Exec("test.js")
    79  	if err != nil {
    80  		t.Fatalf("expected no error, got %v", err)
    81  	}
    82  	time.Sleep(100 * time.Millisecond)
    83  	val, err := jsre.Run("msg")
    84  	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)
    96  }
    97  
    98  func TestBind(t *testing.T) {
    99  	jsre := New("", os.Stdout)
   100  	defer jsre.Stop(false)
   101  
   102  	jsre.Set("no", &testNativeObjectBinding{vm: jsre.vm})
   103  
   104  	_, err := jsre.Run(`no.TestMethod("testMsg")`)
   105  	if err != nil {
   106  		t.Errorf("expected no error, got %v", err)
   107  	}
   108  }
   109  
   110  func TestLoadScript(t *testing.T) {
   111  	jsre := newWithTestJS(t, `msg = "testMsg"`)
   112  
   113  	_, err := jsre.Run(`loadScript("test.js")`)
   114  	if err != nil {
   115  		t.Errorf("expected no error, got %v", err)
   116  	}
   117  	val, err := jsre.Run("msg")
   118  	if err != nil {
   119  		t.Errorf("expected no error, got %v", err)
   120  	}
   121  	if val.ExportType().Kind() != reflect.String {
   122  		t.Errorf("expected string value, got %v", val)
   123  	}
   124  	exp := "testMsg"
   125  	got := val.ToString().String()
   126  	if exp != got {
   127  		t.Errorf("expected '%v', got '%v'", exp, got)
   128  	}
   129  	jsre.Stop(false)
   130  }