rt_assign_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/scope"
  14. )
  15. func TestSimpleAssignments(t *testing.T) {
  16. vs := scope.NewScope(scope.GlobalScope)
  17. res, err := UnitTestEvalAndAST(
  18. `a := 42`, vs,
  19. `
  20. :=
  21. identifier: a
  22. number: 42
  23. `[1:])
  24. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  25. a (float64) : 42
  26. }` {
  27. t.Error("Unexpected result: ", vsRes, res, err)
  28. return
  29. }
  30. res, err = UnitTestEvalAndAST(
  31. `a := "test"`, vs,
  32. `
  33. :=
  34. identifier: a
  35. string: 'test'
  36. `[1:])
  37. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  38. a (string) : test
  39. }` {
  40. t.Error("Unexpected result: ", vsRes, res, err)
  41. return
  42. }
  43. res, err = UnitTestEvalAndAST(
  44. `a := [1,2,3]`, vs,
  45. `
  46. :=
  47. identifier: a
  48. list
  49. number: 1
  50. number: 2
  51. number: 3
  52. `[1:])
  53. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  54. a ([]interface {}) : [1,2,3]
  55. }` {
  56. t.Error("Unexpected result: ", vsRes, res, err)
  57. return
  58. }
  59. res, err = UnitTestEvalAndAST(
  60. `a := {
  61. 1 : "foo",
  62. "2" : "bar",
  63. "foobar" : 3,
  64. x : 4,
  65. }`, vs,
  66. `
  67. :=
  68. identifier: a
  69. map
  70. kvp
  71. number: 1
  72. string: 'foo'
  73. kvp
  74. string: '2'
  75. string: 'bar'
  76. kvp
  77. string: 'foobar'
  78. number: 3
  79. kvp
  80. identifier: x
  81. number: 4
  82. `[1:])
  83. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  84. a (map[interface {}]interface {}) : {"1":"foo","2":"bar","foobar":3,"null":4}
  85. }` {
  86. t.Error("Unexpected result: ", vsRes, res, err)
  87. return
  88. }
  89. _, err = UnitTestEval(
  90. `1 := [1, 2]`, vs)
  91. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Cannot access variable (Must have a variable or list of variables on the left side of the assignment) (Line:1 Pos:3)" {
  92. t.Error("Unexpected result:", err)
  93. return
  94. }
  95. _, err = UnitTestEval(
  96. `[1] := [1, 2]`, vs)
  97. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Cannot access variable (Must have a list of variables on the left side of the assignment) (Line:1 Pos:5)" {
  98. t.Error("Unexpected result:", err)
  99. return
  100. }
  101. _, err = UnitTestEval(
  102. `[a, b] := [1, 2, 3]`, vs)
  103. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid state (Assigned number of variables is different to number of values (2 variables vs 3 values)) (Line:1 Pos:8)" {
  104. t.Error("Unexpected result:", err)
  105. return
  106. }
  107. _, err = UnitTestEval(
  108. `[a, b] := 1`, vs)
  109. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid state (Result is not a list (value is 1)) (Line:1 Pos:8)" {
  110. t.Error("Unexpected result:", err)
  111. return
  112. }
  113. _, err = UnitTestEval(
  114. `[a, b.c, c] := [1, 2, 3]`, vs)
  115. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Cannot access variable (Variable b is not a container) (Line:1 Pos:13)" {
  116. t.Error("Unexpected result:", err)
  117. return
  118. }
  119. }
  120. func TestComplexAssignments(t *testing.T) {
  121. vs := scope.NewScope(scope.GlobalScope)
  122. res, err := UnitTestEvalAndAST(
  123. `[a, b] := ["test", [1,2,3]]`, vs,
  124. `
  125. :=
  126. list
  127. identifier: a
  128. identifier: b
  129. list
  130. string: 'test'
  131. list
  132. number: 1
  133. number: 2
  134. number: 3
  135. `[1:])
  136. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  137. a (string) : test
  138. b ([]interface {}) : [1,2,3]
  139. }` {
  140. t.Error("Unexpected result: ", vsRes, res, err)
  141. return
  142. }
  143. res, err = UnitTestEvalAndAST(`
  144. a := {
  145. "b" : [ 0, {
  146. "c" : [ 0, 1, 2, {
  147. "test" : 1
  148. }]
  149. }]
  150. }
  151. a.b[1].c["3"]["test"] := 3
  152. `, vs,
  153. `
  154. statements
  155. :=
  156. identifier: a
  157. map
  158. kvp
  159. string: 'b'
  160. list
  161. number: 0
  162. map
  163. kvp
  164. string: 'c'
  165. list
  166. number: 0
  167. number: 1
  168. number: 2
  169. map
  170. kvp
  171. string: 'test'
  172. number: 1
  173. :=
  174. identifier: a
  175. identifier: b
  176. compaccess
  177. number: 1
  178. identifier: c
  179. compaccess
  180. string: '3'
  181. compaccess
  182. string: 'test'
  183. number: 3
  184. `[1:])
  185. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  186. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  187. b ([]interface {}) : [1,2,3]
  188. }` {
  189. t.Error("Unexpected result: ", vsRes, res, err)
  190. return
  191. }
  192. res, err = UnitTestEvalAndAST(`
  193. z := a.b[1].c["3"]["test"]
  194. [x, y] := a.b
  195. `, vs,
  196. `
  197. statements
  198. :=
  199. identifier: z
  200. identifier: a
  201. identifier: b
  202. compaccess
  203. number: 1
  204. identifier: c
  205. compaccess
  206. string: '3'
  207. compaccess
  208. string: 'test'
  209. :=
  210. list
  211. identifier: x
  212. identifier: y
  213. identifier: a
  214. identifier: b
  215. `[1:])
  216. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  217. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  218. b ([]interface {}) : [1,2,3]
  219. x (float64) : 0
  220. y (map[interface {}]interface {}) : {"c":[0,1,2,{"test":3}]}
  221. z (float64) : 3
  222. }` {
  223. t.Error("Unexpected result: ", vsRes, res, err)
  224. return
  225. }
  226. }