github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/internal/jsre/jsre_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2015 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package jsre
    26  
    27  import (
    28  	"io/ioutil"
    29  	"os"
    30  	"path"
    31  	"testing"
    32  	"time"
    33  
    34  	"github.com/robertkrimen/otto"
    35  )
    36  
    37  type testNativeObjectBinding struct{}
    38  
    39  type msg struct {
    40  	Msg string
    41  }
    42  
    43  func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
    44  	m, err := call.Argument(0).ToString()
    45  	if err != nil {
    46  		return otto.UndefinedValue()
    47  	}
    48  	v, _ := call.Otto.ToValue(&msg{m})
    49  	return v
    50  }
    51  
    52  func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
    53  	dir, err := ioutil.TempDir("", "jsre-test")
    54  	if err != nil {
    55  		t.Fatal("cannot create temporary directory:", err)
    56  	}
    57  	if testjs != "" {
    58  		if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
    59  			t.Fatal("cannot create test.js:", err)
    60  		}
    61  	}
    62  	return New(dir, os.Stdout), dir
    63  }
    64  
    65  func TestExec(t *testing.T) {
    66  	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
    67  	defer os.RemoveAll(dir)
    68  
    69  	err := jsre.Exec("test.js")
    70  	if err != nil {
    71  		t.Errorf("expected no error, got %v", err)
    72  	}
    73  	val, err := jsre.Run("msg")
    74  	if err != nil {
    75  		t.Errorf("expected no error, got %v", err)
    76  	}
    77  	if !val.IsString() {
    78  		t.Errorf("expected string value, got %v", val)
    79  	}
    80  	exp := "testMsg"
    81  	got, _ := val.ToString()
    82  	if exp != got {
    83  		t.Errorf("expected '%v', got '%v'", exp, got)
    84  	}
    85  	jsre.Stop(false)
    86  }
    87  
    88  func TestNatto(t *testing.T) {
    89  	jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
    90  	defer os.RemoveAll(dir)
    91  
    92  	err := jsre.Exec("test.js")
    93  	if err != nil {
    94  		t.Errorf("expected no error, got %v", err)
    95  	}
    96  	time.Sleep(100 * time.Millisecond)
    97  	val, err := jsre.Run("msg")
    98  	if err != nil {
    99  		t.Errorf("expected no error, got %v", err)
   100  	}
   101  	if !val.IsString() {
   102  		t.Errorf("expected string value, got %v", val)
   103  	}
   104  	exp := "testMsg"
   105  	got, _ := val.ToString()
   106  	if exp != got {
   107  		t.Errorf("expected '%v', got '%v'", exp, got)
   108  	}
   109  	jsre.Stop(false)
   110  }
   111  
   112  func TestBind(t *testing.T) {
   113  	jsre := New("", os.Stdout)
   114  	defer jsre.Stop(false)
   115  
   116  	jsre.Bind("no", &testNativeObjectBinding{})
   117  
   118  	_, err := jsre.Run(`no.TestMethod("testMsg")`)
   119  	if err != nil {
   120  		t.Errorf("expected no error, got %v", err)
   121  	}
   122  }
   123  
   124  func TestLoadScript(t *testing.T) {
   125  	jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
   126  	defer os.RemoveAll(dir)
   127  
   128  	_, err := jsre.Run(`loadScript("test.js")`)
   129  	if err != nil {
   130  		t.Errorf("expected no error, got %v", err)
   131  	}
   132  	val, err := jsre.Run("msg")
   133  	if err != nil {
   134  		t.Errorf("expected no error, got %v", err)
   135  	}
   136  	if !val.IsString() {
   137  		t.Errorf("expected string value, got %v", val)
   138  	}
   139  	exp := "testMsg"
   140  	got, _ := val.ToString()
   141  	if exp != got {
   142  		t.Errorf("expected '%v', got '%v'", exp, got)
   143  	}
   144  	jsre.Stop(false)
   145  }