parser_statement_test.go 8.2 KB

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