rt_general_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "testing"
  13. "devt.de/krotik/ecal/parser"
  14. "devt.de/krotik/ecal/scope"
  15. "devt.de/krotik/ecal/util"
  16. )
  17. func TestGeneralErrorCases(t *testing.T) {
  18. n, _ := parser.Parse("a", "a")
  19. inv := &invalidRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  20. if err := inv.Validate().Error(); err != "ECAL error in a: Invalid construct (Unknown node: identifier) (Line:1 Pos:1)" {
  21. t.Error("Unexpected result:", err)
  22. return
  23. }
  24. if _, err := inv.Eval(nil, nil); err.Error() != "ECAL error in a: Invalid construct (Unknown node: identifier) (Line:1 Pos:1)" {
  25. t.Error("Unexpected result:", err)
  26. return
  27. }
  28. n, _ = parser.Parse("a", "a")
  29. inv = &invalidRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  30. n.Runtime = inv
  31. n2, _ := parser.Parse("a", "a")
  32. inv2 := &invalidRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n2)}
  33. n2.Runtime = inv2
  34. n.Children = append(n.Children, n2)
  35. if err := inv.Validate().Error(); err != "ECAL error in a: Invalid construct (Unknown node: identifier) (Line:1 Pos:1)" {
  36. t.Error("Unexpected result:", err)
  37. return
  38. }
  39. n, _ = parser.Parse("a", "a")
  40. void := &voidRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  41. n.Runtime = void
  42. if res, err := void.Eval(nil, nil); err != nil || res != nil {
  43. t.Error("Unexpected result:", res, err)
  44. return
  45. }
  46. }
  47. func TestImporting(t *testing.T) {
  48. vs := scope.NewScope(scope.GlobalScope)
  49. il := &util.MemoryImportLocator{Files: make(map[string]string)}
  50. il.Files["foo/bar"] = `
  51. b := 123
  52. `
  53. res, err := UnitTestEvalAndASTAndImport(
  54. `
  55. import "foo/bar" as foobar
  56. a := foobar.b`, vs,
  57. `
  58. statements
  59. import
  60. string: 'foo/bar'
  61. identifier: foobar
  62. :=
  63. identifier: a
  64. identifier: foobar
  65. identifier: b
  66. `[1:], il)
  67. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  68. a (float64) : 123
  69. foobar (map[interface {}]interface {}) : {"b":123}
  70. }` {
  71. t.Error("Unexpected result: ", vsRes, res, err)
  72. return
  73. }
  74. n, _ := parser.Parse("a", "a")
  75. imp := &importRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  76. n.Runtime = imp
  77. imp.erp = NewECALRuntimeProvider("ECALTestRuntime", nil, nil)
  78. imp.erp.ImportLocator = nil
  79. if res, err := imp.Eval(nil, nil); err == nil || err.Error() != "ECAL error in ECALTestRuntime: Runtime error (No import locator was specified) (Line:1 Pos:1)" {
  80. t.Error("Unexpected result:", res, err)
  81. return
  82. }
  83. }
  84. func TestLogging(t *testing.T) {
  85. vs := scope.NewScope(scope.GlobalScope)
  86. _, err := UnitTestEvalAndAST(
  87. `
  88. log("Hello")
  89. debug("foo")
  90. error("bar")
  91. `, vs,
  92. `
  93. statements
  94. identifier: log
  95. funccall
  96. string: 'Hello'
  97. identifier: debug
  98. funccall
  99. string: 'foo'
  100. identifier: error
  101. funccall
  102. string: 'bar'
  103. `[1:])
  104. if err != nil {
  105. t.Error("Unexpected result: ", err)
  106. return
  107. }
  108. if testlogger.String() != `Hello
  109. debug: foo
  110. error: bar` {
  111. t.Error("Unexpected result: ", testlogger.String())
  112. return
  113. }
  114. }
  115. func TestOperatorRuntimeErrors(t *testing.T) {
  116. n, _ := parser.Parse("a", "a")
  117. op := &operatorRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  118. if res := op.errorDetailString(n.Token, "foo"); res != "a=foo" {
  119. t.Error("Unexpected result:", res)
  120. }
  121. n.Token.Identifier = false
  122. if res := op.errorDetailString(n.Token, "foo"); res != "a" {
  123. t.Error("Unexpected result:", res)
  124. }
  125. n.Token.Identifier = true
  126. res, err := UnitTestEval(
  127. `+ "a"`, nil)
  128. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a number (a) (Line:1 Pos:3)" {
  129. t.Error("Unexpected result: ", res, err)
  130. return
  131. }
  132. res, err = UnitTestEval(
  133. `x := "a"; + x`, nil)
  134. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a number (x=a) (Line:1 Pos:13)" {
  135. t.Error("Unexpected result: ", res, err)
  136. return
  137. }
  138. res, err = UnitTestEval(
  139. `not "a"`, nil)
  140. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a boolean (a) (Line:1 Pos:5)" {
  141. t.Error("Unexpected result: ", res, err)
  142. return
  143. }
  144. res, err = UnitTestEval(
  145. `a:= 5; a or 6`, nil)
  146. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a boolean (a=5) (Line:1 Pos:8)" {
  147. t.Error("Unexpected result: ", res, err)
  148. return
  149. }
  150. res, err = UnitTestEval(
  151. `true or 2`, nil)
  152. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a boolean (2) (Line:1 Pos:1)" {
  153. t.Error("Unexpected result: ", res, err)
  154. return
  155. }
  156. res, err = UnitTestEval(
  157. `a := "foo"; x in a`, nil)
  158. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Operand is not a list (a=foo) (Line:1 Pos:13)" {
  159. t.Error("Unexpected result: ", res, err)
  160. return
  161. }
  162. }