rt_assign_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. `let a := 42`, vs,
  19. `
  20. :=
  21. let
  22. identifier: a
  23. number: 42
  24. `[1:])
  25. if vsRes := vs.String(); vsRes != `GlobalScope {
  26. a (float64) : 42
  27. }` {
  28. t.Error("Unexpected result: ", vsRes, res, err)
  29. return
  30. }
  31. res, err = UnitTestEvalAndAST(
  32. `a := "test"`, vs,
  33. `
  34. :=
  35. identifier: a
  36. string: 'test'
  37. `[1:])
  38. if vsRes := vs.String(); vsRes != `GlobalScope {
  39. a (string) : test
  40. }` {
  41. t.Error("Unexpected result: ", vsRes, res, err)
  42. return
  43. }
  44. res, err = UnitTestEvalAndAST(
  45. `a := [1,2,3]`, vs,
  46. `
  47. :=
  48. identifier: a
  49. list
  50. number: 1
  51. number: 2
  52. number: 3
  53. `[1:])
  54. if vsRes := vs.String(); vsRes != `GlobalScope {
  55. a ([]interface {}) : [1,2,3]
  56. }` {
  57. t.Error("Unexpected result: ", vsRes, res, err)
  58. return
  59. }
  60. res, err = UnitTestEvalAndAST(
  61. `a := {
  62. 1 : "foo",
  63. "2" : "bar",
  64. "foobar" : 3,
  65. x : 4,
  66. }`, vs,
  67. `
  68. :=
  69. identifier: a
  70. map
  71. kvp
  72. number: 1
  73. string: 'foo'
  74. kvp
  75. string: '2'
  76. string: 'bar'
  77. kvp
  78. string: 'foobar'
  79. number: 3
  80. kvp
  81. identifier: x
  82. number: 4
  83. `[1:])
  84. if vsRes := vs.String(); vsRes != `GlobalScope {
  85. a (map[interface {}]interface {}) : {"1":"foo","2":"bar","foobar":3,"null":4}
  86. }` {
  87. t.Error("Unexpected result: ", vsRes, res, err)
  88. return
  89. }
  90. _, err = UnitTestEval(
  91. `1 := [1, 2]`, vs)
  92. if 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)" {
  93. t.Error("Unexpected result:", err)
  94. return
  95. }
  96. _, err = UnitTestEval(
  97. `[1] := [1, 2]`, vs)
  98. if 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)" {
  99. t.Error("Unexpected result:", err)
  100. return
  101. }
  102. _, err = UnitTestEval(
  103. `[a, b] := [1, 2, 3]`, vs)
  104. if 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)" {
  105. t.Error("Unexpected result:", err)
  106. return
  107. }
  108. _, err = UnitTestEval(
  109. `[a, b] := 1`, vs)
  110. if err.Error() != "ECAL error in ECALTestRuntime: Invalid state (Result is not a list (value is 1)) (Line:1 Pos:8)" {
  111. t.Error("Unexpected result:", err)
  112. return
  113. }
  114. _, err = UnitTestEval(
  115. `[a, b.c, c] := [1, 2, 3]`, vs)
  116. if err.Error() != "ECAL error in ECALTestRuntime: Cannot access variable (Variable b is not a container) (Line:1 Pos:13)" {
  117. t.Error("Unexpected result:", err)
  118. return
  119. }
  120. }
  121. func TestComplexAssignments(t *testing.T) {
  122. vs := scope.NewScope(scope.GlobalScope)
  123. res, err := UnitTestEvalAndAST(
  124. `let [a, b] := ["test", [1,2,3]]`, vs,
  125. `
  126. :=
  127. let
  128. list
  129. identifier: a
  130. identifier: b
  131. list
  132. string: 'test'
  133. list
  134. number: 1
  135. number: 2
  136. number: 3
  137. `[1:])
  138. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  139. a (string) : test
  140. b ([]interface {}) : [1,2,3]
  141. }` {
  142. t.Error("Unexpected result: ", vsRes, res, err)
  143. return
  144. }
  145. res, err = UnitTestEvalAndAST(`
  146. a := {
  147. "b" : [ 0, {
  148. "c" : [ 0, 1, 2, {
  149. "test" : 1
  150. }]
  151. }]
  152. }
  153. a.b[1].c["3"]["test"] := 3
  154. `, vs,
  155. `
  156. statements
  157. :=
  158. identifier: a
  159. map
  160. kvp
  161. string: 'b'
  162. list
  163. number: 0
  164. map
  165. kvp
  166. string: 'c'
  167. list
  168. number: 0
  169. number: 1
  170. number: 2
  171. map
  172. kvp
  173. string: 'test'
  174. number: 1
  175. :=
  176. identifier: a
  177. identifier: b
  178. compaccess
  179. number: 1
  180. identifier: c
  181. compaccess
  182. string: '3'
  183. compaccess
  184. string: 'test'
  185. number: 3
  186. `[1:])
  187. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  188. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  189. b ([]interface {}) : [1,2,3]
  190. }` {
  191. t.Error("Unexpected result: ", vsRes, res, err)
  192. return
  193. }
  194. res, err = UnitTestEvalAndAST(`
  195. z := a.b[1].c["3"]["test"]
  196. [x, y] := a.b
  197. `, vs,
  198. `
  199. statements
  200. :=
  201. identifier: z
  202. identifier: a
  203. identifier: b
  204. compaccess
  205. number: 1
  206. identifier: c
  207. compaccess
  208. string: '3'
  209. compaccess
  210. string: 'test'
  211. :=
  212. list
  213. identifier: x
  214. identifier: y
  215. identifier: a
  216. identifier: b
  217. `[1:])
  218. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  219. a (map[interface {}]interface {}) : {"b":[0,{"c":[0,1,2,{"test":3}]}]}
  220. b ([]interface {}) : [1,2,3]
  221. x (float64) : 0
  222. y (map[interface {}]interface {}) : {"c":[0,1,2,{"test":3}]}
  223. z (float64) : 3
  224. }` {
  225. t.Error("Unexpected result: ", vsRes, res, err)
  226. return
  227. }
  228. }
  229. func TestScopedDeclaration(t *testing.T) {
  230. vs := scope.NewScope(scope.GlobalScope)
  231. res, err := UnitTestEval(`
  232. a := 5
  233. func foo() {
  234. let a := 2
  235. }
  236. foo()`, vs)
  237. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  238. a (float64) : 5
  239. foo (*interpreter.function) : ecal.function: foo (Line 3, Pos 1)
  240. }` {
  241. t.Error("Unexpected result: ", vsRes, res, err)
  242. return
  243. }
  244. res, err = UnitTestEval(`
  245. a := 5
  246. func foo() {
  247. let [a, b, c]
  248. a := 2
  249. }
  250. foo()`, vs)
  251. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  252. a (float64) : 5
  253. foo (*interpreter.function) : ecal.function: foo (Line 3, Pos 1)
  254. }` {
  255. t.Error("Unexpected result: ", vsRes, res, err)
  256. return
  257. }
  258. res, err = UnitTestEval(`let [1]`, vs)
  259. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Let can only declare variables within a list) (Line:1 Pos:1)" {
  260. t.Error("Unexpected result: ", res, err)
  261. return
  262. }
  263. res, err = UnitTestEval(`let 1`, vs)
  264. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Let must declare a variable or list of variables) (Line:1 Pos:1)" {
  265. t.Error("Unexpected result: ", res, err)
  266. return
  267. }
  268. res, err = UnitTestEval(`let a.b`, vs)
  269. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Let can only declare simple variables) (Line:1 Pos:1)" {
  270. t.Error("Unexpected result: ", res, err)
  271. return
  272. }
  273. }