parser_main_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }