jsre_test.go 3.4 KB

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