jsre_test.go 3.2 KB

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