parser_main_test.go 5.3 KB

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