parser_statement_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 TestTryContext(t *testing.T) {
  46. input := `
  47. try {
  48. raise("test", [1,2,3])
  49. } except "test", "bla" as e {
  50. print(1)
  51. } except e {
  52. print(1)
  53. } except {
  54. print(1)
  55. } finally {
  56. print(2)
  57. }
  58. `
  59. expectedOutput := `
  60. try
  61. statements
  62. identifier: raise
  63. funccall
  64. string: 'test'
  65. list
  66. number: 1
  67. number: 2
  68. number: 3
  69. except
  70. string: 'test'
  71. string: 'bla'
  72. as
  73. identifier: e
  74. statements
  75. identifier: print
  76. funccall
  77. number: 1
  78. except
  79. identifier: e
  80. statements
  81. identifier: print
  82. funccall
  83. number: 1
  84. except
  85. statements
  86. identifier: print
  87. funccall
  88. number: 1
  89. finally
  90. statements
  91. identifier: print
  92. funccall
  93. number: 2
  94. `[1:]
  95. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  96. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  97. return
  98. }
  99. input = `
  100. try {
  101. raise("test", [1,2,3])
  102. }
  103. `
  104. expectedOutput = `
  105. try
  106. statements
  107. identifier: raise
  108. funccall
  109. string: 'test'
  110. list
  111. number: 1
  112. number: 2
  113. number: 3
  114. `[1:]
  115. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  116. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  117. return
  118. }
  119. input = `
  120. try {
  121. raise("test", [1,2,3])
  122. } finally {
  123. }
  124. `
  125. expectedOutput = `
  126. try
  127. statements
  128. identifier: raise
  129. funccall
  130. string: 'test'
  131. list
  132. number: 1
  133. number: 2
  134. number: 3
  135. finally
  136. statements
  137. `[1:]
  138. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  139. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  140. return
  141. }
  142. }
  143. func TestLoopParsing(t *testing.T) {
  144. input := `
  145. for a != null {
  146. print(1);
  147. print(2);
  148. break
  149. continue
  150. }
  151. `
  152. expectedOutput := `
  153. loop
  154. guard
  155. !=
  156. identifier: a
  157. null
  158. statements
  159. identifier: print
  160. funccall
  161. number: 1
  162. identifier: print
  163. funccall
  164. number: 2
  165. break
  166. continue
  167. `[1:]
  168. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  169. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  170. return
  171. }
  172. input = `
  173. for a in range(1,2) {
  174. print(1);
  175. print(2)
  176. }
  177. `
  178. expectedOutput = `
  179. loop
  180. in
  181. identifier: a
  182. identifier: range
  183. funccall
  184. number: 1
  185. number: 2
  186. statements
  187. identifier: print
  188. funccall
  189. number: 1
  190. identifier: print
  191. funccall
  192. number: 2
  193. `[1:]
  194. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  195. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  196. return
  197. }
  198. input = `
  199. for a < 1 and b > 2 {
  200. print(1)
  201. print(2)
  202. }
  203. `
  204. expectedOutput = `
  205. loop
  206. guard
  207. and
  208. <
  209. identifier: a
  210. number: 1
  211. >
  212. identifier: b
  213. number: 2
  214. statements
  215. identifier: print
  216. funccall
  217. number: 1
  218. identifier: print
  219. funccall
  220. number: 2
  221. `[1:]
  222. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  223. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  224. return
  225. }
  226. input = `
  227. for a in range(1,2,3) {
  228. ==
  229. }
  230. `
  231. if _, err := UnitTestParse("mytest", input); err.Error() !=
  232. "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:2)" {
  233. t.Error(err)
  234. return
  235. }
  236. input = `
  237. for a in == {
  238. @print(1)
  239. }
  240. `
  241. if _, err := UnitTestParse("mytest", input); err.Error() !=
  242. "Parse error in mytest: Term cannot start an expression (==) (Line:2 Pos:10)" {
  243. t.Error(err)
  244. return
  245. }
  246. }
  247. func TestConditionalParsing(t *testing.T) {
  248. input := `
  249. if a == b or c < d {
  250. print(1);
  251. foo := 1
  252. } elif x or y {
  253. x := 1; y := 2; p := {
  254. 1:2
  255. }
  256. } elif true {
  257. x := 1; y := 2
  258. } else {
  259. x := 1
  260. }
  261. `
  262. expectedOutput := `
  263. if
  264. guard
  265. or
  266. ==
  267. identifier: a
  268. identifier: b
  269. <
  270. identifier: c
  271. identifier: d
  272. statements
  273. identifier: print
  274. funccall
  275. number: 1
  276. :=
  277. identifier: foo
  278. number: 1
  279. guard
  280. or
  281. identifier: x
  282. identifier: y
  283. statements
  284. :=
  285. identifier: x
  286. number: 1
  287. :=
  288. identifier: y
  289. number: 2
  290. :=
  291. identifier: p
  292. map
  293. kvp
  294. number: 1
  295. number: 2
  296. guard
  297. true
  298. statements
  299. :=
  300. identifier: x
  301. number: 1
  302. :=
  303. identifier: y
  304. number: 2
  305. guard
  306. true
  307. statements
  308. :=
  309. identifier: x
  310. number: 1
  311. `[1:]
  312. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  313. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  314. return
  315. }
  316. input = `
  317. if a {
  318. print(1)
  319. } elif b {
  320. print(2)
  321. }
  322. `
  323. expectedOutput = `
  324. if
  325. guard
  326. identifier: a
  327. statements
  328. identifier: print
  329. funccall
  330. number: 1
  331. guard
  332. identifier: b
  333. statements
  334. identifier: print
  335. funccall
  336. number: 2
  337. `[1:]
  338. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  339. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  340. return
  341. }
  342. input = `
  343. if a {
  344. print(1)
  345. } else {
  346. print(2)
  347. }
  348. `
  349. expectedOutput = `
  350. if
  351. guard
  352. identifier: a
  353. statements
  354. identifier: print
  355. funccall
  356. number: 1
  357. guard
  358. true
  359. statements
  360. identifier: print
  361. funccall
  362. number: 2
  363. `[1:]
  364. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  365. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  366. return
  367. }
  368. // Test error output
  369. input = `else { b }`
  370. if _, err := UnitTestParse("mytest", input); err.Error() !=
  371. "Parse error in mytest: Term cannot start an expression (<ELSE>) (Line:1 Pos:1)" {
  372. t.Error(err)
  373. return
  374. }
  375. input = `elif { b }`
  376. if _, err := UnitTestParse("mytest", input); err.Error() !=
  377. "Parse error in mytest: Term cannot start an expression (<ELIF>) (Line:1 Pos:1)" {
  378. t.Error(err)
  379. return
  380. }
  381. input = `if { b }`
  382. if _, err := UnitTestParse("mytest", input); err.Error() !=
  383. "Parse error in mytest: Unexpected end (Line:1 Pos:8)" {
  384. t.Error(err)
  385. return
  386. }
  387. input = `if == { b }`
  388. if _, err := UnitTestParse("mytest", input); err.Error() !=
  389. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:4)" {
  390. t.Error(err)
  391. return
  392. }
  393. input = `if x { b } elif == { c }`
  394. if _, err := UnitTestParse("mytest", input); err.Error() !=
  395. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:17)" {
  396. t.Error(err)
  397. return
  398. }
  399. }