parser_statement_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 TestMutexBlock(t *testing.T) {
  144. input := `
  145. mutex foo {
  146. print(1)
  147. print(2)
  148. }
  149. `
  150. expectedOutput := `
  151. mutex
  152. identifier: foo
  153. statements
  154. identifier: print
  155. funccall
  156. number: 1
  157. identifier: print
  158. funccall
  159. number: 2
  160. `[1:]
  161. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  162. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  163. return
  164. }
  165. }
  166. func TestLoopParsing(t *testing.T) {
  167. input := `
  168. for a != null {
  169. print(1);
  170. print(2);
  171. break
  172. continue
  173. }
  174. `
  175. expectedOutput := `
  176. loop
  177. guard
  178. !=
  179. identifier: a
  180. null
  181. statements
  182. identifier: print
  183. funccall
  184. number: 1
  185. identifier: print
  186. funccall
  187. number: 2
  188. break
  189. continue
  190. `[1:]
  191. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  192. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  193. return
  194. }
  195. input = `
  196. for a in range(1,2) {
  197. print(1);
  198. print(2)
  199. }
  200. `
  201. expectedOutput = `
  202. loop
  203. in
  204. identifier: a
  205. identifier: range
  206. funccall
  207. number: 1
  208. number: 2
  209. statements
  210. identifier: print
  211. funccall
  212. number: 1
  213. identifier: print
  214. funccall
  215. number: 2
  216. `[1:]
  217. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  218. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  219. return
  220. }
  221. input = `
  222. for a < 1 and b > 2 {
  223. print(1)
  224. print(2)
  225. }
  226. `
  227. expectedOutput = `
  228. loop
  229. guard
  230. and
  231. <
  232. identifier: a
  233. number: 1
  234. >
  235. identifier: b
  236. number: 2
  237. statements
  238. identifier: print
  239. funccall
  240. number: 1
  241. identifier: print
  242. funccall
  243. number: 2
  244. `[1:]
  245. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  246. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  247. return
  248. }
  249. input = `
  250. for a in range(1,2,3) {
  251. ==
  252. }
  253. `
  254. if _, err := UnitTestParse("mytest", input); err.Error() !=
  255. "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:2)" {
  256. t.Error(err)
  257. return
  258. }
  259. input = `
  260. for a in == {
  261. @print(1)
  262. }
  263. `
  264. if _, err := UnitTestParse("mytest", input); err.Error() !=
  265. "Parse error in mytest: Term cannot start an expression (==) (Line:2 Pos:10)" {
  266. t.Error(err)
  267. return
  268. }
  269. }
  270. func TestConditionalParsing(t *testing.T) {
  271. input := `
  272. if a == b or c < d {
  273. print(1);
  274. foo := 1
  275. } elif x or y {
  276. x := 1; y := 2; p := {
  277. 1:2
  278. }
  279. } elif true {
  280. x := 1; y := 2
  281. } else {
  282. x := 1
  283. }
  284. `
  285. expectedOutput := `
  286. if
  287. guard
  288. or
  289. ==
  290. identifier: a
  291. identifier: b
  292. <
  293. identifier: c
  294. identifier: d
  295. statements
  296. identifier: print
  297. funccall
  298. number: 1
  299. :=
  300. identifier: foo
  301. number: 1
  302. guard
  303. or
  304. identifier: x
  305. identifier: y
  306. statements
  307. :=
  308. identifier: x
  309. number: 1
  310. :=
  311. identifier: y
  312. number: 2
  313. :=
  314. identifier: p
  315. map
  316. kvp
  317. number: 1
  318. number: 2
  319. guard
  320. true
  321. statements
  322. :=
  323. identifier: x
  324. number: 1
  325. :=
  326. identifier: y
  327. number: 2
  328. guard
  329. true
  330. statements
  331. :=
  332. identifier: x
  333. number: 1
  334. `[1:]
  335. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  336. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  337. return
  338. }
  339. input = `
  340. if a {
  341. print(1)
  342. } elif b {
  343. print(2)
  344. }
  345. `
  346. expectedOutput = `
  347. if
  348. guard
  349. identifier: a
  350. statements
  351. identifier: print
  352. funccall
  353. number: 1
  354. guard
  355. identifier: b
  356. statements
  357. identifier: print
  358. funccall
  359. number: 2
  360. `[1:]
  361. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  362. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  363. return
  364. }
  365. input = `
  366. if a {
  367. print(1)
  368. } else {
  369. print(2)
  370. }
  371. `
  372. expectedOutput = `
  373. if
  374. guard
  375. identifier: a
  376. statements
  377. identifier: print
  378. funccall
  379. number: 1
  380. guard
  381. true
  382. statements
  383. identifier: print
  384. funccall
  385. number: 2
  386. `[1:]
  387. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  388. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  389. return
  390. }
  391. // Test error output
  392. input = `else { b }`
  393. if _, err := UnitTestParse("mytest", input); err.Error() !=
  394. "Parse error in mytest: Term cannot start an expression (<ELSE>) (Line:1 Pos:1)" {
  395. t.Error(err)
  396. return
  397. }
  398. input = `elif { b }`
  399. if _, err := UnitTestParse("mytest", input); err.Error() !=
  400. "Parse error in mytest: Term cannot start an expression (<ELIF>) (Line:1 Pos:1)" {
  401. t.Error(err)
  402. return
  403. }
  404. input = `if { b }`
  405. if _, err := UnitTestParse("mytest", input); err.Error() !=
  406. "Parse error in mytest: Unexpected end (Line:1 Pos:8)" {
  407. t.Error(err)
  408. return
  409. }
  410. input = `if == { b }`
  411. if _, err := UnitTestParse("mytest", input); err.Error() !=
  412. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:4)" {
  413. t.Error(err)
  414. return
  415. }
  416. input = `if x { b } elif == { c }`
  417. if _, err := UnitTestParse("mytest", input); err.Error() !=
  418. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:17)" {
  419. t.Error(err)
  420. return
  421. }
  422. }