parser_statement_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 TestLoopParsing(t *testing.T) {
  15. input := `
  16. for a != null {
  17. print(1);
  18. print(2);
  19. break
  20. continue
  21. }
  22. `
  23. expectedOutput := `
  24. loop
  25. guard
  26. !=
  27. identifier: a
  28. null
  29. statements
  30. identifier: print
  31. funccall
  32. number: 1
  33. identifier: print
  34. funccall
  35. number: 2
  36. break
  37. continue
  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. input = `
  44. for a in range(1,2) {
  45. print(1);
  46. print(2)
  47. }
  48. `
  49. expectedOutput = `
  50. loop
  51. in
  52. identifier: a
  53. identifier: range
  54. funccall
  55. number: 1
  56. number: 2
  57. statements
  58. identifier: print
  59. funccall
  60. number: 1
  61. identifier: print
  62. funccall
  63. number: 2
  64. `[1:]
  65. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  66. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  67. return
  68. }
  69. input = `
  70. for a < 1 and b > 2 {
  71. print(1)
  72. print(2)
  73. }
  74. `
  75. expectedOutput = `
  76. loop
  77. guard
  78. and
  79. <
  80. identifier: a
  81. number: 1
  82. >
  83. identifier: b
  84. number: 2
  85. statements
  86. identifier: print
  87. funccall
  88. number: 1
  89. identifier: print
  90. funccall
  91. number: 2
  92. `[1:]
  93. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  94. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  95. return
  96. }
  97. input = `
  98. for a in range(1,2,3) {
  99. ==
  100. }
  101. `
  102. if _, err := UnitTestParse("mytest", input); err.Error() !=
  103. "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:2)" {
  104. t.Error(err)
  105. return
  106. }
  107. input = `
  108. for a in == {
  109. @print(1)
  110. }
  111. `
  112. if _, err := UnitTestParse("mytest", input); err.Error() !=
  113. "Parse error in mytest: Term cannot start an expression (==) (Line:2 Pos:10)" {
  114. t.Error(err)
  115. return
  116. }
  117. }
  118. func TestConditionalParsing(t *testing.T) {
  119. input := `
  120. if a == b or c < d {
  121. print(1);
  122. foo := 1
  123. } elif x or y {
  124. x := 1; y := 2; p := {
  125. 1:2
  126. }
  127. } elif true {
  128. x := 1; y := 2
  129. } else {
  130. x := 1
  131. }
  132. `
  133. expectedOutput := `
  134. if
  135. guard
  136. or
  137. ==
  138. identifier: a
  139. identifier: b
  140. <
  141. identifier: c
  142. identifier: d
  143. statements
  144. identifier: print
  145. funccall
  146. number: 1
  147. :=
  148. identifier: foo
  149. number: 1
  150. guard
  151. or
  152. identifier: x
  153. identifier: y
  154. statements
  155. :=
  156. identifier: x
  157. number: 1
  158. :=
  159. identifier: y
  160. number: 2
  161. :=
  162. identifier: p
  163. map
  164. kvp
  165. number: 1
  166. number: 2
  167. guard
  168. true
  169. statements
  170. :=
  171. identifier: x
  172. number: 1
  173. :=
  174. identifier: y
  175. number: 2
  176. guard
  177. true
  178. statements
  179. :=
  180. identifier: x
  181. number: 1
  182. `[1:]
  183. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  184. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  185. return
  186. }
  187. input = `
  188. if a {
  189. print(1)
  190. } elif b {
  191. print(2)
  192. }
  193. `
  194. expectedOutput = `
  195. if
  196. guard
  197. identifier: a
  198. statements
  199. identifier: print
  200. funccall
  201. number: 1
  202. guard
  203. identifier: b
  204. statements
  205. identifier: print
  206. funccall
  207. number: 2
  208. `[1:]
  209. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  210. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  211. return
  212. }
  213. input = `
  214. if a {
  215. print(1)
  216. } else {
  217. print(2)
  218. }
  219. `
  220. expectedOutput = `
  221. if
  222. guard
  223. identifier: a
  224. statements
  225. identifier: print
  226. funccall
  227. number: 1
  228. guard
  229. true
  230. statements
  231. identifier: print
  232. funccall
  233. number: 2
  234. `[1:]
  235. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  236. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  237. return
  238. }
  239. // Test error output
  240. input = `else { b }`
  241. if _, err := UnitTestParse("mytest", input); err.Error() !=
  242. "Parse error in mytest: Term cannot start an expression (<ELSE>) (Line:1 Pos:1)" {
  243. t.Error(err)
  244. return
  245. }
  246. input = `elif { b }`
  247. if _, err := UnitTestParse("mytest", input); err.Error() !=
  248. "Parse error in mytest: Term cannot start an expression (<ELIF>) (Line:1 Pos:1)" {
  249. t.Error(err)
  250. return
  251. }
  252. input = `if { b }`
  253. if _, err := UnitTestParse("mytest", input); err.Error() !=
  254. "Parse error in mytest: Unexpected end (Line:1 Pos:8)" {
  255. t.Error(err)
  256. return
  257. }
  258. input = `if == { b }`
  259. if _, err := UnitTestParse("mytest", input); err.Error() !=
  260. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:4)" {
  261. t.Error(err)
  262. return
  263. }
  264. input = `if x { b } elif == { c }`
  265. if _, err := UnitTestParse("mytest", input); err.Error() !=
  266. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:17)" {
  267. t.Error(err)
  268. return
  269. }
  270. }