parser_statement_test.go 8.2 KB

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