parser_statement_test.go 5.9 KB

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