jsre_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package jsre
  2. import (
  3. "github.com/robertkrimen/otto"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. )
  9. type testNativeObjectBinding struct {
  10. toVal func(interface{}) otto.Value
  11. }
  12. type msg struct {
  13. Msg string
  14. }
  15. func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
  16. m, err := call.Argument(0).ToString()
  17. if err != nil {
  18. return otto.UndefinedValue()
  19. }
  20. return no.toVal(&msg{m})
  21. }
  22. func TestExec(t *testing.T) {
  23. jsre := New("/tmp")
  24. ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
  25. err := jsre.Exec("test.js")
  26. if err != nil {
  27. t.Errorf("expected no error, got %v", err)
  28. }
  29. val, err := jsre.Run("msg")
  30. if err != nil {
  31. t.Errorf("expected no error, got %v", err)
  32. }
  33. if !val.IsString() {
  34. t.Errorf("expected string value, got %v", val)
  35. }
  36. exp := "testMsg"
  37. got, _ := val.ToString()
  38. if exp != got {
  39. t.Errorf("expected '%v', got '%v'", exp, got)
  40. }
  41. jsre.Stop(false)
  42. }
  43. func TestNatto(t *testing.T) {
  44. jsre := New("/tmp")
  45. ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm)
  46. err := jsre.Exec("test.js")
  47. if err != nil {
  48. t.Errorf("expected no error, got %v", err)
  49. }
  50. time.Sleep(time.Millisecond * 10)
  51. val, err := jsre.Run("msg")
  52. if err != nil {
  53. t.Errorf("expected no error, got %v", err)
  54. }
  55. if !val.IsString() {
  56. t.Errorf("expected string value, got %v", val)
  57. }
  58. exp := "testMsg"
  59. got, _ := val.ToString()
  60. if exp != got {
  61. t.Errorf("expected '%v', got '%v'", exp, got)
  62. }
  63. jsre.Stop(false)
  64. }
  65. func TestBind(t *testing.T) {
  66. jsre := New("/tmp")
  67. jsre.Bind("no", &testNativeObjectBinding{jsre.ToVal})
  68. val, err := jsre.Run(`no.TestMethod("testMsg")`)
  69. if err != nil {
  70. t.Errorf("expected no error, got %v", err)
  71. }
  72. pp, err := jsre.PrettyPrint(val)
  73. if err != nil {
  74. t.Errorf("expected no error, got %v", err)
  75. }
  76. t.Logf("no: %v", pp)
  77. jsre.Stop(false)
  78. }
  79. func TestLoadScript(t *testing.T) {
  80. jsre := New("/tmp")
  81. ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
  82. _, err := jsre.Run(`loadScript("test.js")`)
  83. if err != nil {
  84. t.Errorf("expected no error, got %v", err)
  85. }
  86. val, err := jsre.Run("msg")
  87. if err != nil {
  88. t.Errorf("expected no error, got %v", err)
  89. }
  90. if !val.IsString() {
  91. t.Errorf("expected string value, got %v", val)
  92. }
  93. exp := "testMsg"
  94. got, _ := val.ToString()
  95. if exp != got {
  96. t.Errorf("expected '%v', got '%v'", exp, got)
  97. }
  98. jsre.Stop(false)
  99. }