parser_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package muesli
  2. import (
  3. _ "strings"
  4. "testing"
  5. )
  6. func HelperFailOnErrors(ast * Ast, expected int, test *testing.T) {
  7. if ast == nil {
  8. test.Errorf("Parse failed, AST is nil.")
  9. return
  10. }
  11. if ast.IsNone() {
  12. test.Errorf("Parse failed, %d parse errors expected", expected)
  13. }
  14. errors := ast.Errors()
  15. if len(errors) != expected {
  16. test.Log("Parse errors:\n")
  17. for _, err := range errors {
  18. test.Logf("%s\n", err.String())
  19. }
  20. test.Errorf("There were %d parse errors, %d expected", len(errors), expected)
  21. }
  22. }
  23. func HelperParseAndFailOnErrors(prog string, expected int,
  24. parsefunc func(*Parser) *Ast, test *testing.T) {
  25. defer func(){
  26. err := recover()
  27. if err != nil {
  28. test.Errorf("Parse error: %s", err)
  29. }
  30. }()
  31. parser := NewParserFromString(prog)
  32. parser.SetLogger(&testLogger{"", 0, test})
  33. ast := parsefunc(parser)
  34. HelperFailOnErrors(ast, expected, test)
  35. if ast != nil {
  36. test.Logf("AST OK")
  37. // ast.Display()
  38. }
  39. }
  40. func TestParser(test *testing.T) {
  41. com := `puts "hello"
  42. say ( add 5 10 ) .`
  43. parser := NewParserFromString(com)
  44. ast := parser.Parse()
  45. if ast != nil {
  46. test.Logf("AST OK")
  47. // ast.Display()
  48. }
  49. }
  50. func TestParser2(test *testing.T) {
  51. com := `puts "hello"
  52. say ( add 5 10 ) .`
  53. HelperParseAndFailOnErrors(com, 0, (*Parser).Parse, test)
  54. }
  55. func TestParenthesis(test *testing.T) {
  56. HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
  57. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  58. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  59. }
  60. func TestClosed(test *testing.T) {
  61. HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
  62. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  63. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  64. }
  65. func TestClosedWithError(test *testing.T) {
  66. HelperParseAndFailOnErrors(`( add 5 10 ) ) `, 1, (*Parser).Parse, test)
  67. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  68. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  69. }
  70. func TestParseDesignFile(test *testing.T) {
  71. parser, err := NewParserFromFilename("design_muesli.muesli")
  72. if err != nil {
  73. test.Errorf("Error parsingfile : %s", err)
  74. return
  75. }
  76. parser.SetLogger(&testLogger{"", 0, test})
  77. ast := parser.Parse()
  78. if ast != nil {
  79. test.Logf("Ast OK.")
  80. // ast.Display()
  81. }
  82. }