123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- * 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 (
- "encoding/json"
- "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
- // Test AST serialization
- if err == nil {
- var unmarshaledJSONObject map[string]interface{}
- astString, err := json.Marshal(n.ToJSONObject())
- if err != nil {
- return nil, fmt.Errorf("Could not marshal AST: %v", err)
- }
- if err := json.Unmarshal(astString, &unmarshaledJSONObject); err != nil {
- return nil, fmt.Errorf("Could not unmarshal JSON object: %v", err)
- }
- unmarshaledAST, err := ASTFromJSONObject(unmarshaledJSONObject)
- if err != nil {
- return nil, fmt.Errorf("Could not create AST from unmarshaled JSON object: %v", err)
- }
- // String compare the ASTs
- if ok, msg := n.Equals(unmarshaledAST); !ok {
- return nil, fmt.Errorf(
- "Parsed AST is different from the unmarshaled AST.\n%v\n",
- msg)
- }
- }
- 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
- }
|