// PrintAST returns string representation of parsed template. func (tpl *Template) PrintAST() string { if err := tpl.parse(); err != nil { return fmt.Sprintf("PARSER ERROR: %s", err) } return ast.Print(tpl.program) }
func TestParser(t *testing.T) { t.Parallel() for _, test := range parserTests { output := "" node, err := Parse(test.input) if err == nil { output = ast.Print(node) } if (err != nil) || (test.output != output) { t.Errorf("Test '%s' failed\ninput:\n\t'%s'\nexpected\n\t%q\ngot\n\t%q\nerror:\n\t%s", test.name, test.input, test.output, output, err) } } }
// package example func Example() { source := "You know {{nothing}} John Snow" // parse template program, err := Parse(source) if err != nil { panic(err) } // print AST output := ast.Print(program) fmt.Print(output) // CONTENT[ 'You know ' ] // {{ PATH:nothing [] }} // CONTENT[ ' John Snow' ] }
func TestParserErrors(t *testing.T) { t.Parallel() for _, test := range parserErrorTests { node, err := Parse(test.input) if err == nil { output := ast.Print(node) tokens := lexer.Collect(test.input) t.Errorf("Test '%s' failed - Error expected\ninput:\n\t'%s'\ngot\n\t%q\ntokens:\n\t%q", test.name, test.input, output, tokens) } else if test.output != "" { matched, errMatch := regexp.MatchString(regexp.QuoteMeta(test.output), fmt.Sprint(err)) if errMatch != nil { panic("Failed to match regexp") } if !matched { t.Errorf("Test '%s' failed - Incorrect error returned\ninput:\n\t'%s'\nexpected\n\t%q\ngot\n\t%q", test.name, test.input, test.output, err) } } } }