123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package muesli
- import (
- _ "strings"
- "testing"
- )
- func HelperFailOnErrors(ast * Ast, expected int, test *testing.T) {
- if ast == nil {
- test.Errorf("Parse failed, AST is nil.")
- return
- }
- if ast.IsNone() {
- test.Errorf("Parse failed, %d parse errors expected", expected)
- }
- errors := ast.Errors()
- if len(errors) != expected {
- test.Log("Parse errors:\n")
- for _, err := range errors {
- test.Logf("%s\n", err.String())
- }
- test.Errorf("There were %d parse errors, %d expected", len(errors), expected)
- }
- }
- func HelperParseAndFailOnErrors(prog string, expected int,
- parsefunc func(*Parser) *Ast, test *testing.T) {
- defer func(){
- err := recover()
- if err != nil {
- test.Errorf("Parse error: %s", err)
- }
- }()
-
- parser := NewParserFromString(prog)
- parser.SetLogger(&testLogger{})
- ast := parsefunc(parser)
- HelperFailOnErrors(ast, expected, test)
- if ast != nil {
- ast.Display()
- }
- }
- func TestParser(test *testing.T) {
- com := `puts "hello"
- say ( add 5 10 ) .`
- parser := NewParserFromString(com)
- ast := parser.Parse()
- if ast != nil {
- ast.Display()
- }
- }
- func TestParser2(test *testing.T) {
- com := `puts "hello"
- say ( add 5 10 ) .`
- HelperParseAndFailOnErrors(com, 0, (*Parser).Parse, test)
- }
- func TestParenthesis(test *testing.T) {
- HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).Parse, test)
- // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
- // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
- }
- func TestClosed(test *testing.T) {
- HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseStatement, test)
- // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
- // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
- }
- func TestClosedWithError(test *testing.T) {
- HelperParseAndFailOnErrors(`( add 5 10 ) ) `, 0, (*Parser).ParseStatement, test)
- // HelperParseAndFailOnErrors(`( add 5 10 ) `, 0, (*Parser).ParseParenthesis, test)
- // HelperParseAndFailOnErrors(`add 5 10 `, 0, (*Parser).ParseCommand, test)
- }
- func TestParseDesignFile(test *testing.T) {
-
- parser, err := NewParserFromFilename("design_muesli.muesli")
- if err != nil {
- test.Errorf("Error parsingfile : %s", err)
- return
- }
- parser.SetLogger(&testLogger{})
- ast := parser.Parse()
- if ast != nil {
- ast.Display()
- }
-
- }
|