parser_main_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 TestCommentParsing(t *testing.T) {
  15. // Comment parsing without statements
  16. input := `/* This is a comment*/ a := 1 + 1 # foo bar`
  17. expectedOutput := `
  18. :=
  19. identifier: a # This is a comment
  20. plus
  21. number: 1
  22. number: 1 # foo bar
  23. `[1:]
  24. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  25. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  26. return
  27. }
  28. input = `/* foo */ 1 # foo bar`
  29. expectedOutput = `
  30. number: 1 # foo foo bar
  31. `[1:]
  32. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  33. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  34. return
  35. }
  36. }
  37. func TestSimpleExpressionParsing(t *testing.T) {
  38. // Test error output
  39. input := `"bl\*a"conversion`
  40. if _, err := UnitTestParse("mytest", input); err.Error() !=
  41. "Parse error in mytest: Lexical error (invalid syntax while parsing string) (Line:1 Pos:1)" {
  42. t.Error(err)
  43. return
  44. }
  45. // Test incomplete expression
  46. input = `a *`
  47. if _, err := UnitTestParse("mytest", input); err.Error() !=
  48. "Parse error in mytest: Unexpected end" {
  49. t.Error(err)
  50. return
  51. }
  52. input = `not ==`
  53. if _, err := UnitTestParse("mytest", input); err.Error() !=
  54. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:5)" {
  55. t.Error(err)
  56. return
  57. }
  58. input = `(==)`
  59. if _, err := UnitTestParse("mytest", input); err.Error() !=
  60. "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:2)" {
  61. t.Error(err)
  62. return
  63. }
  64. input = "5 ( 5"
  65. if _, err := UnitTestParse("mytest", input); err.Error() !=
  66. "Parse error in mytest: Term can only start an expression (() (Line:1 Pos:3)" {
  67. t.Error(err)
  68. return
  69. }
  70. input = "5 + \""
  71. if _, err := UnitTestParse("mytest", input); err.Error() !=
  72. "Parse error in mytest: Lexical error (Unexpected end while reading string value (unclosed quotes)) (Line:1 Pos:5)" {
  73. t.Error(err)
  74. return
  75. }
  76. // Test prefix operator
  77. input = ` + a - -5`
  78. expectedOutput := `
  79. minus
  80. plus
  81. identifier: a
  82. minus
  83. number: 5
  84. `[1:]
  85. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  86. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  87. return
  88. }
  89. }
  90. func TestArithmeticParsing(t *testing.T) {
  91. input := "a + b * 5 /2"
  92. expectedOutput := `
  93. plus
  94. identifier: a
  95. div
  96. times
  97. identifier: b
  98. number: 5
  99. number: 2
  100. `[1:]
  101. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  102. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  103. return
  104. }
  105. // Test brackets
  106. input = "a + 1 * (5 + 6)"
  107. expectedOutput = `
  108. plus
  109. identifier: a
  110. times
  111. number: 1
  112. plus
  113. number: 5
  114. number: 6
  115. `[1:]
  116. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  117. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  118. return
  119. }
  120. // Test needless brackets
  121. input = "(a + 1) * (5 / (6 - 2))"
  122. expectedOutput = `
  123. times
  124. plus
  125. identifier: a
  126. number: 1
  127. div
  128. number: 5
  129. minus
  130. number: 6
  131. number: 2
  132. `[1:]
  133. // Pretty printer should get rid of the needless brackets
  134. res, err := UnitTestParseWithPPResult("mytest", input, "(a + 1) * 5 / (6 - 2)")
  135. if err != nil || fmt.Sprint(res) != expectedOutput {
  136. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  137. return
  138. }
  139. }
  140. func TestLogicParsing(t *testing.T) {
  141. input := "not (a + 1) * 5 and tRue == false or not 1 - 5 != test"
  142. expectedOutput := `
  143. or
  144. and
  145. not
  146. times
  147. plus
  148. identifier: a
  149. number: 1
  150. number: 5
  151. ==
  152. true
  153. false
  154. not
  155. !=
  156. minus
  157. number: 1
  158. number: 5
  159. identifier: test
  160. `[1:]
  161. res, err := UnitTestParseWithPPResult("mytest", input, "not (a + 1) * 5 and true == false or not 1 - 5 != test")
  162. if err != nil || fmt.Sprint(res) != expectedOutput {
  163. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  164. return
  165. }
  166. input = "a > b or a <= p or b hasSuffix 'test' or c hasPrefix 'test' and x < 4 or x >= 10"
  167. expectedOutput = `
  168. or
  169. or
  170. or
  171. or
  172. >
  173. identifier: a
  174. identifier: b
  175. <=
  176. identifier: a
  177. identifier: p
  178. hassuffix
  179. identifier: b
  180. string: 'test'
  181. and
  182. hasprefix
  183. identifier: c
  184. string: 'test'
  185. <
  186. identifier: x
  187. number: 4
  188. >=
  189. identifier: x
  190. number: 10
  191. `[1:]
  192. res, err = UnitTestParseWithPPResult("mytest", input, `a > b or a <= p or b hassuffix "test" or c hasprefix "test" and x < 4 or x >= 10`)
  193. if err != nil || fmt.Sprint(res) != expectedOutput {
  194. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  195. return
  196. }
  197. input = "(a in null or c notin d) and false like 9 or x // 6 > 2 % 1"
  198. expectedOutput = `
  199. or
  200. and
  201. or
  202. in
  203. identifier: a
  204. null
  205. notin
  206. identifier: c
  207. identifier: d
  208. like
  209. false
  210. number: 9
  211. >
  212. divint
  213. identifier: x
  214. number: 6
  215. modint
  216. number: 2
  217. number: 1
  218. `[1:]
  219. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  220. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  221. return
  222. }
  223. }