123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package parser
- import (
- "flag"
- "fmt"
- "os"
- "testing"
- )
- // Main function for all tests in this package
- func TestMain(m *testing.M) {
- flag.Parse()
- res := m.Run()
- // Check if all nodes have been tested
- for _, n := range astNodeMap {
- if _, ok := usedNodes[n.Name]; !ok {
- fmt.Println("Not tested node: ", n.Name)
- }
- }
- // Check if all nodes have been pretty printed
- for k := range prettyPrinterMap {
- if _, ok := usedPrettyPrinterNodes[k]; !ok {
- fmt.Println("Not tested pretty printer: ", k)
- }
- }
- os.Exit(res)
- }
- // Used nodes map which is filled during unit testing. Prefilled with tokens which
- // will not be generated by the parser
- //
- var usedNodes = map[string]bool{
- NodeEOF: true, // Only used as end term
- "": true, // No node e.g. semicolon - These nodes should never be part of an AST
- }
- func UnitTestParse(name string, input string) (*ASTNode, error) {
- n, err := ParseWithRuntime(name, input, &DummyRuntimeProvider{})
- // TODO Test pretty printing
- // TODO Test AST serialization
- return n, err
- }
- // Used nodes map which is filled during unit testing. Prefilled with tokens which
- // will not be generated by the parser
- //
- var usedPrettyPrinterNodes = map[string]bool{}
- func UnitTestPrettyPrinting(input, astOutput, ppOutput string) error {
- var visitAST func(*ASTNode)
- astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
- if err != nil || fmt.Sprint(astres) != astOutput {
- return fmt.Errorf("Unexpected parser output:\n%v expected was:\n%v Error: %v", astres, astOutput, err)
- }
- visitAST = func(n *ASTNode) {
- // Make the encountered node as used
- numChildren := len(n.Children)
- if numChildren > 0 {
- usedPrettyPrinterNodes[fmt.Sprintf("%v_%v", n.Name, numChildren)] = true
- } else {
- usedPrettyPrinterNodes[n.Name] = true
- }
- for _, c := range n.Children {
- visitAST(c)
- }
- }
- visitAST(astres)
- ppres, err := PrettyPrint(astres)
- if err != nil || ppres != ppOutput {
- return fmt.Errorf("Unexpected result: %v (expected: %v) error: %v", ppres, ppOutput, err)
- }
- // Make sure the pretty printed result is valid and gets the same parse tree
- astres2, err := ParseWithRuntime("mytest", ppres, &DummyRuntimeProvider{})
- if err != nil || fmt.Sprint(astres2) != astOutput {
- return fmt.Errorf("Unexpected parser output from pretty print string:\n%v expected was:\n%v Error: %v", astres2, astOutput, err)
- }
- return nil
- }
- // Helper objects
- type DummyRuntimeProvider struct {
- }
- func (d *DummyRuntimeProvider) Runtime(n *ASTNode) Runtime {
- // Make the encountered node as used
- usedNodes[n.Name] = true
- return nil
- }
|