parser_main_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 TestStatementParsing(t *testing.T) {
  15. // Comment parsing without statements
  16. input := `a := 1
  17. b := 2; c:= 3`
  18. expectedOutput := `
  19. statements
  20. :=
  21. identifier: a
  22. number: 1
  23. :=
  24. identifier: b
  25. number: 2
  26. :=
  27. identifier: c
  28. number: 3
  29. `[1:]
  30. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  31. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  32. return
  33. }
  34. }
  35. func TestIdentifierParsing(t *testing.T) {
  36. input := `a := 1
  37. a.foo := 2
  38. a.b.c.foo := a.b
  39. `
  40. expectedOutput := `
  41. statements
  42. :=
  43. identifier: a
  44. number: 1
  45. :=
  46. identifier: a
  47. identifier: foo
  48. number: 2
  49. :=
  50. identifier: a
  51. identifier: b
  52. identifier: c
  53. identifier: foo
  54. identifier: a
  55. identifier: b
  56. `[1:]
  57. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  58. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  59. return
  60. }
  61. input = `a := 1
  62. a().foo := x2.foo()
  63. a.b.c().foo := a()
  64. `
  65. expectedOutput = `
  66. statements
  67. :=
  68. identifier: a
  69. number: 1
  70. :=
  71. identifier: a
  72. funccall
  73. identifier: foo
  74. identifier: x2
  75. identifier: foo
  76. funccall
  77. :=
  78. identifier: a
  79. identifier: b
  80. identifier: c
  81. funccall
  82. identifier: foo
  83. identifier: a
  84. funccall
  85. `[1:]
  86. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  87. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  88. return
  89. }
  90. input = `a(1+2).foo := x2.foo(foo)
  91. a.b.c(x()).foo := a(1,a(),3, x, y) + 1
  92. `
  93. expectedOutput = `
  94. statements
  95. :=
  96. identifier: a
  97. funccall
  98. plus
  99. number: 1
  100. number: 2
  101. identifier: foo
  102. identifier: x2
  103. identifier: foo
  104. funccall
  105. identifier: foo
  106. :=
  107. identifier: a
  108. identifier: b
  109. identifier: c
  110. funccall
  111. identifier: x
  112. funccall
  113. identifier: foo
  114. plus
  115. identifier: a
  116. funccall
  117. number: 1
  118. identifier: a
  119. funccall
  120. number: 3
  121. identifier: x
  122. identifier: y
  123. number: 1
  124. `[1:]
  125. if res, err := UnitTestParseWithPPResult("mytest", input, ""); err != nil || fmt.Sprint(res) != expectedOutput {
  126. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  127. return
  128. }
  129. }
  130. func TestCommentParsing(t *testing.T) {
  131. // Comment parsing without statements
  132. input := `/* This is a comment*/ a := 1 + 1 # foo bar`
  133. expectedOutput := `
  134. :=
  135. identifier: a # This is a comment
  136. plus
  137. number: 1
  138. number: 1 # foo bar
  139. `[1:]
  140. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  141. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  142. return
  143. }
  144. input = `/* foo */ 1 # foo bar`
  145. expectedOutput = `
  146. number: 1 # foo foo bar
  147. `[1:]
  148. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  149. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  150. return
  151. }
  152. }
  153. func TestSimpleExpressionParsing(t *testing.T) {
  154. // Test error output
  155. input := `"bl\*a"conversion`
  156. if _, err := UnitTestParse("mytest", input); err.Error() !=
  157. "Parse error in mytest: Lexical error (invalid syntax while parsing string) (Line:1 Pos:1)" {
  158. t.Error(err)
  159. return
  160. }
  161. // Test incomplete expression
  162. input = `a *`
  163. if _, err := UnitTestParse("mytest", input); err.Error() !=
  164. "Parse error in mytest: Unexpected end" {
  165. t.Error(err)
  166. return
  167. }
  168. input = `not ==`
  169. if _, err := UnitTestParse("mytest", input); err.Error() !=
  170. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:5)" {
  171. t.Error(err)
  172. return
  173. }
  174. input = `(==)`
  175. if _, err := UnitTestParse("mytest", input); err.Error() !=
  176. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:2)" {
  177. t.Error(err)
  178. return
  179. }
  180. input = "5 ( 5"
  181. if _, err := UnitTestParse("mytest", input); err.Error() !=
  182. "Parse error in mytest: Term can only start an expression (() (Line:1 Pos:3)" {
  183. t.Error(err)
  184. return
  185. }
  186. input = "5 + \""
  187. if _, err := UnitTestParse("mytest", input); err.Error() !=
  188. "Parse error in mytest: Lexical error (Unexpected end while reading string value (unclosed quotes)) (Line:1 Pos:5)" {
  189. t.Error(err)
  190. return
  191. }
  192. // Test prefix operator
  193. input = ` + a - -5`
  194. expectedOutput := `
  195. minus
  196. plus
  197. identifier: a
  198. minus
  199. number: 5
  200. `[1:]
  201. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  202. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  203. return
  204. }
  205. }
  206. func TestArithmeticParsing(t *testing.T) {
  207. input := "a + b * 5 /2"
  208. expectedOutput := `
  209. plus
  210. identifier: a
  211. div
  212. times
  213. identifier: b
  214. number: 5
  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. // Test brackets
  222. input = "a + 1 * (5 + 6)"
  223. expectedOutput = `
  224. plus
  225. identifier: a
  226. times
  227. number: 1
  228. plus
  229. number: 5
  230. number: 6
  231. `[1:]
  232. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  233. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  234. return
  235. }
  236. // Test needless brackets
  237. input = "(a + 1) * (5 / (6 - 2))"
  238. expectedOutput = `
  239. times
  240. plus
  241. identifier: a
  242. number: 1
  243. div
  244. number: 5
  245. minus
  246. number: 6
  247. number: 2
  248. `[1:]
  249. // Pretty printer should get rid of the needless brackets
  250. res, err := UnitTestParseWithPPResult("mytest", input, "(a + 1) * 5 / (6 - 2)")
  251. if err != nil || fmt.Sprint(res) != expectedOutput {
  252. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  253. return
  254. }
  255. }
  256. func TestLogicParsing(t *testing.T) {
  257. input := "not (a + 1) * 5 and tRue == false or not 1 - 5 != test"
  258. expectedOutput := `
  259. or
  260. and
  261. not
  262. times
  263. plus
  264. identifier: a
  265. number: 1
  266. number: 5
  267. ==
  268. true
  269. false
  270. not
  271. !=
  272. minus
  273. number: 1
  274. number: 5
  275. identifier: test
  276. `[1:]
  277. res, err := UnitTestParseWithPPResult("mytest", input, "not (a + 1) * 5 and true == false or not 1 - 5 != test")
  278. if err != nil || fmt.Sprint(res) != expectedOutput {
  279. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  280. return
  281. }
  282. input = "a > b or a <= p or b hasSuffix 'test' or c hasPrefix 'test' and x < 4 or x >= 10"
  283. expectedOutput = `
  284. or
  285. or
  286. or
  287. or
  288. >
  289. identifier: a
  290. identifier: b
  291. <=
  292. identifier: a
  293. identifier: p
  294. hassuffix
  295. identifier: b
  296. string: 'test'
  297. and
  298. hasprefix
  299. identifier: c
  300. string: 'test'
  301. <
  302. identifier: x
  303. number: 4
  304. >=
  305. identifier: x
  306. number: 10
  307. `[1:]
  308. res, err = UnitTestParseWithPPResult("mytest", input, `a > b or a <= p or b hassuffix "test" or c hasprefix "test" and x < 4 or x >= 10`)
  309. if err != nil || fmt.Sprint(res) != expectedOutput {
  310. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  311. return
  312. }
  313. input = "(a in null or c notin d) and false like 9 or x // 6 > 2 % 1"
  314. expectedOutput = `
  315. or
  316. and
  317. or
  318. in
  319. identifier: a
  320. null
  321. notin
  322. identifier: c
  323. identifier: d
  324. like
  325. false
  326. number: 9
  327. >
  328. divint
  329. identifier: x
  330. number: 6
  331. modint
  332. number: 2
  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. }