jsre_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package jsre
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "testing"
  21. "time"
  22. "github.com/robertkrimen/otto"
  23. )
  24. type testNativeObjectBinding struct{}
  25. type msg struct {
  26. Msg string
  27. }
  28. func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
  29. m, err := call.Argument(0).ToString()
  30. if err != nil {
  31. return otto.UndefinedValue()
  32. }
  33. v, _ := call.Otto.ToValue(&msg{m})
  34. return v
  35. }
  36. func TestExec(t *testing.T) {
  37. jsre := New("/tmp")
  38. ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
  39. err := jsre.Exec("test.js")
  40. if err != nil {
  41. t.Errorf("expected no error, got %v", err)
  42. }
  43. val, err := jsre.Run("msg")
  44. if err != nil {
  45. t.Errorf("expected no error, got %v", err)
  46. }
  47. if !val.IsString() {
  48. t.Errorf("expected string value, got %v", val)
  49. }
  50. exp := "testMsg"
  51. got, _ := val.ToString()
  52. if exp != got {
  53. t.Errorf("expected '%v', got '%v'", exp, got)
  54. }
  55. jsre.Stop(false)
  56. }
  57. func TestNatto(t *testing.T) {
  58. jsre := New("/tmp")
  59. ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm)
  60. err := jsre.Exec("test.js")
  61. if err != nil {
  62. t.Errorf("expected no error, got %v", err)
  63. }
  64. time.Sleep(time.Millisecond * 10)
  65. val, err := jsre.Run("msg")
  66. if err != nil {
  67. t.Errorf("expected no error, got %v", err)
  68. }
  69. if !val.IsString() {
  70. t.Errorf("expected string value, got %v", val)
  71. }
  72. exp := "testMsg"
  73. got, _ := val.ToString()
  74. if exp != got {
  75. t.Errorf("expected '%v', got '%v'", exp, got)
  76. }
  77. jsre.Stop(false)
  78. }
  79. func TestBind(t *testing.T) {
  80. jsre := New("/tmp")
  81. jsre.Bind("no", &testNativeObjectBinding{})
  82. val, err := jsre.Run(`no.TestMethod("testMsg")`)
  83. if err != nil {
  84. t.Errorf("expected no error, got %v", err)
  85. }
  86. pp, err := jsre.PrettyPrint(val)
  87. if err != nil {
  88. t.Errorf("expected no error, got %v", err)
  89. }
  90. t.Logf("no: %v", pp)
  91. jsre.Stop(false)
  92. }
  93. func TestLoadScript(t *testing.T) {
  94. jsre := New("/tmp")
  95. ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
  96. _, err := jsre.Run(`loadScript("test.js")`)
  97. if err != nil {
  98. t.Errorf("expected no error, got %v", err)
  99. }
  100. val, err := jsre.Run("msg")
  101. if err != nil {
  102. t.Errorf("expected no error, got %v", err)
  103. }
  104. if !val.IsString() {
  105. t.Errorf("expected string value, got %v", val)
  106. }
  107. exp := "testMsg"
  108. got, _ := val.ToString()
  109. if exp != got {
  110. t.Errorf("expected '%v', got '%v'", exp, got)
  111. }
  112. jsre.Stop(false)
  113. }