parser_main_test.go 4.7 KB

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