jsre_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "testing"
  22. "time"
  23. "github.com/robertkrimen/otto"
  24. )
  25. type testNativeObjectBinding struct{}
  26. type msg struct {
  27. Msg string
  28. }
  29. func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
  30. m, err := call.Argument(0).ToString()
  31. if err != nil {
  32. return otto.UndefinedValue()
  33. }
  34. v, _ := call.Otto.ToValue(&msg{m})
  35. return v
  36. }
  37. func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
  38. dir, err := ioutil.TempDir("", "jsre-test")
  39. if err != nil {
  40. t.Fatal("cannot create temporary directory:", err)
  41. }
  42. if testjs != "" {
  43. if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
  44. t.Fatal("cannot create test.js:", err)
  45. }
  46. }
  47. return New(dir), 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.IsString() {
  61. t.Errorf("expected string value, got %v", val)
  62. }
  63. exp := "testMsg"
  64. got, _ := val.ToString()
  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(time.Millisecond * 10)
  78. val, err := jsre.Run("msg")
  79. if err != nil {
  80. t.Errorf("expected no error, got %v", err)
  81. }
  82. if !val.IsString() {
  83. t.Errorf("expected string value, got %v", val)
  84. }
  85. exp := "testMsg"
  86. got, _ := val.ToString()
  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("")
  94. jsre.Bind("no", &testNativeObjectBinding{})
  95. val, err := jsre.Run(`no.TestMethod("testMsg")`)
  96. if err != nil {
  97. t.Errorf("expected no error, got %v", err)
  98. }
  99. pp, err := jsre.PrettyPrint(val)
  100. if err != nil {
  101. t.Errorf("expected no error, got %v", err)
  102. }
  103. t.Logf("no: %v", pp)
  104. jsre.Stop(false)
  105. }
  106. func TestLoadScript(t *testing.T) {
  107. jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
  108. defer os.RemoveAll(dir)
  109. _, err := jsre.Run(`loadScript("test.js")`)
  110. if err != nil {
  111. t.Errorf("expected no error, got %v", err)
  112. }
  113. val, err := jsre.Run("msg")
  114. if err != nil {
  115. t.Errorf("expected no error, got %v", err)
  116. }
  117. if !val.IsString() {
  118. t.Errorf("expected string value, got %v", val)
  119. }
  120. exp := "testMsg"
  121. got, _ := val.ToString()
  122. if exp != got {
  123. t.Errorf("expected '%v', got '%v'", exp, got)
  124. }
  125. jsre.Stop(false)
  126. }