parser_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package muesli
  2. import (
  3. _ "strings"
  4. "testing"
  5. )
  6. func HelperFailOnErrors(ast Ast, expected int, test *testing.T) {
  7. if ast.IsNone() {
  8. test.Errorf("Parse failed, %d parse errors expected", expected)
  9. }
  10. errors := ast.Errors()
  11. if len(errors) != expected {
  12. test.Log("Parse errors:\n")
  13. for _, err := range errors {
  14. test.Logf("%s\n", err.String())
  15. }
  16. test.Errorf("There were %d parse errors, %d expected", len(errors), expected)
  17. }
  18. }
  19. func HelperParseAndFailOnErrors(prog string, expected int,
  20. parsefunc func(*Parser) Ast, test *testing.T) {
  21. parser := NewParserFromString(prog)
  22. parser.SetLogger(&testLogger{})
  23. ast := parsefunc(parser)
  24. HelperFailOnErrors(ast, expected, test)
  25. ast.Display()
  26. }
  27. func TestParser(test *testing.T) {
  28. com := `puts "hello"
  29. say ( add 5 10 ) .`
  30. parser := NewParserFromString(com)
  31. ast := parser.Parse()
  32. ast.Display()
  33. }
  34. func TestParser2(test *testing.T) {
  35. com := `puts "hello"
  36. say ( add 5 10 ) .`
  37. HelperParseAndFailOnErrors(com, 0, (*Parser).Parse, test)
  38. }
  39. func TestParenthesis(test *testing.T) {
  40. HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
  41. // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
  42. // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
  43. }
  44. func TestParseLexerDesignFile(test *testing.T) {
  45. parser, err := NewParserFromFilename("design_muesli.muesli")
  46. if err != nil {
  47. test.Errorf("Error parsingfile : %s", err)
  48. return
  49. }
  50. parser.SetLogger(&testLogger{})
  51. ast := parser.Parse()
  52. ast.Display()
  53. }