parser_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Logf("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. */
  32. parser := NewParserFromString(prog)
  33. parser.SetLogger(&testLogger{"", 0, test})
  34. ast := parsefunc(parser)
  35. HelperFailOnErrors(ast, expected, test)
  36. if ast != nil {
  37. test.Logf("AST OK")
  38. test.Logf("\nAst: %s\n", ast.Dump())
  39. }
  40. }
  41. func TestParser1(test *testing.T) {
  42. com := `puts "hello"
  43. say ( add 5 10 ) .`
  44. parser := NewParserFromString(com)
  45. ast := parser.Parse()
  46. if ast != nil {
  47. test.Logf("AST OK")
  48. // ast.Display()
  49. }
  50. }
  51. func TestParser2(test *testing.T) {
  52. com := `puts "hello"
  53. say ( add 5 10 ) .`
  54. HelperParseAndFailOnErrors(com, 0, (*Parser).Parse, test)
  55. }
  56. func TestParenthesis(test *testing.T) {
  57. HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
  58. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  59. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  60. }
  61. func TestClosed(test *testing.T) {
  62. HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
  63. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  64. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  65. }
  66. func TestClosedWithError(test *testing.T) {
  67. HelperParseAndFailOnErrors(`( add 5 10 ) ) `, 1, (*Parser).Parse, test)
  68. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  69. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  70. }
  71. func TestParseDesignFile(test *testing.T) {
  72. parser, err := NewParserFromFilename("design_muesli.muesli")
  73. if err != nil {
  74. test.Errorf("Error parsingfile : %s", err)
  75. return
  76. }
  77. parser.SetLogger(&testLogger{"", 0, test})
  78. ast := parser.Parse()
  79. if ast != nil {
  80. test.Logf("Ast OK.")
  81. test.Logf("\nAst: %s\n", ast.Dump())
  82. }
  83. }