parser_statement_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 parser
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestAssignmentParsing(t *testing.T) {
  16. input := `
  17. z := a.b[1].c["3"]["test"]
  18. [x, y] := a.b
  19. `
  20. expectedOutput := `
  21. statements
  22. :=
  23. identifier: z
  24. identifier: a
  25. identifier: b
  26. compaccess
  27. number: 1
  28. identifier: c
  29. compaccess
  30. string: '3'
  31. compaccess
  32. string: 'test'
  33. :=
  34. list
  35. identifier: x
  36. identifier: y
  37. identifier: a
  38. identifier: b
  39. `[1:]
  40. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  41. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  42. return
  43. }
  44. }
  45. func TestLoopParsing(t *testing.T) {
  46. input := `
  47. for a != null {
  48. print(1);
  49. print(2);
  50. break
  51. continue
  52. }
  53. `
  54. expectedOutput := `
  55. loop
  56. guard
  57. !=
  58. identifier: a
  59. null
  60. statements
  61. identifier: print
  62. funccall
  63. number: 1
  64. identifier: print
  65. funccall
  66. number: 2
  67. break
  68. continue
  69. `[1:]
  70. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  71. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  72. return
  73. }
  74. input = `
  75. for a in range(1,2) {
  76. print(1);
  77. print(2)
  78. }
  79. `
  80. expectedOutput = `
  81. loop
  82. in
  83. identifier: a
  84. identifier: range
  85. funccall
  86. number: 1
  87. number: 2
  88. statements
  89. identifier: print
  90. funccall
  91. number: 1
  92. identifier: print
  93. funccall
  94. number: 2
  95. `[1:]
  96. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  97. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  98. return
  99. }
  100. input = `
  101. for a < 1 and b > 2 {
  102. print(1)
  103. print(2)
  104. }
  105. `
  106. expectedOutput = `
  107. loop
  108. guard
  109. and
  110. <
  111. identifier: a
  112. number: 1
  113. >
  114. identifier: b
  115. number: 2
  116. statements
  117. identifier: print
  118. funccall
  119. number: 1
  120. identifier: print
  121. funccall
  122. number: 2
  123. `[1:]
  124. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  125. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  126. return
  127. }
  128. input = `
  129. for a in range(1,2,3) {
  130. ==
  131. }
  132. `
  133. if _, err := UnitTestParse("mytest", input); err.Error() !=
  134. "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:2)" {
  135. t.Error(err)
  136. return
  137. }
  138. input = `
  139. for a in == {
  140. @print(1)
  141. }
  142. `
  143. if _, err := UnitTestParse("mytest", input); err.Error() !=
  144. "Parse error in mytest: Term cannot start an expression (==) (Line:2 Pos:10)" {
  145. t.Error(err)
  146. return
  147. }
  148. }
  149. func TestConditionalParsing(t *testing.T) {
  150. input := `
  151. if a == b or c < d {
  152. print(1);
  153. foo := 1
  154. } elif x or y {
  155. x := 1; y := 2; p := {
  156. 1:2
  157. }
  158. } elif true {
  159. x := 1; y := 2
  160. } else {
  161. x := 1
  162. }
  163. `
  164. expectedOutput := `
  165. if
  166. guard
  167. or
  168. ==
  169. identifier: a
  170. identifier: b
  171. <
  172. identifier: c
  173. identifier: d
  174. statements
  175. identifier: print
  176. funccall
  177. number: 1
  178. :=
  179. identifier: foo
  180. number: 1
  181. guard
  182. or
  183. identifier: x
  184. identifier: y
  185. statements
  186. :=
  187. identifier: x
  188. number: 1
  189. :=
  190. identifier: y
  191. number: 2
  192. :=
  193. identifier: p
  194. map
  195. kvp
  196. number: 1
  197. number: 2
  198. guard
  199. true
  200. statements
  201. :=
  202. identifier: x
  203. number: 1
  204. :=
  205. identifier: y
  206. number: 2
  207. guard
  208. true
  209. statements
  210. :=
  211. identifier: x
  212. number: 1
  213. `[1:]
  214. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  215. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  216. return
  217. }
  218. input = `
  219. if a {
  220. print(1)
  221. } elif b {
  222. print(2)
  223. }
  224. `
  225. expectedOutput = `
  226. if
  227. guard
  228. identifier: a
  229. statements
  230. identifier: print
  231. funccall
  232. number: 1
  233. guard
  234. identifier: b
  235. statements
  236. identifier: print
  237. funccall
  238. number: 2
  239. `[1:]
  240. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  241. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  242. return
  243. }
  244. input = `
  245. if a {
  246. print(1)
  247. } else {
  248. print(2)
  249. }
  250. `
  251. expectedOutput = `
  252. if
  253. guard
  254. identifier: a
  255. statements
  256. identifier: print
  257. funccall
  258. number: 1
  259. guard
  260. true
  261. statements
  262. identifier: print
  263. funccall
  264. number: 2
  265. `[1:]
  266. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  267. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  268. return
  269. }
  270. // Test error output
  271. input = `else { b }`
  272. if _, err := UnitTestParse("mytest", input); err.Error() !=
  273. "Parse error in mytest: Term cannot start an expression (<ELSE>) (Line:1 Pos:1)" {
  274. t.Error(err)
  275. return
  276. }
  277. input = `elif { b }`
  278. if _, err := UnitTestParse("mytest", input); err.Error() !=
  279. "Parse error in mytest: Term cannot start an expression (<ELIF>) (Line:1 Pos:1)" {
  280. t.Error(err)
  281. return
  282. }
  283. input = `if { b }`
  284. if _, err := UnitTestParse("mytest", input); err.Error() !=
  285. "Parse error in mytest: Unexpected end (Line:1 Pos:8)" {
  286. t.Error(err)
  287. return
  288. }
  289. input = `if == { b }`
  290. if _, err := UnitTestParse("mytest", input); err.Error() !=
  291. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:4)" {
  292. t.Error(err)
  293. return
  294. }
  295. input = `if x { b } elif == { c }`
  296. if _, err := UnitTestParse("mytest", input); err.Error() !=
  297. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:17)" {
  298. t.Error(err)
  299. return
  300. }
  301. }