parser_helper_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "flag"
  12. "fmt"
  13. "os"
  14. "testing"
  15. )
  16. // Main function for all tests in this package
  17. func TestMain(m *testing.M) {
  18. flag.Parse()
  19. res := m.Run()
  20. // Check if all nodes have been tested
  21. for _, n := range astNodeMap {
  22. if _, ok := usedNodes[n.Name]; !ok {
  23. fmt.Println("Not tested node: ", n.Name)
  24. }
  25. }
  26. os.Exit(res)
  27. }
  28. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  29. // will not be generated by the parser
  30. //
  31. var usedNodes = map[string]bool{
  32. NodeEOF: true, // Only used as end term
  33. "": true, // No node e.g. semicolon - These nodes should never be part of an AST
  34. }
  35. func UnitTestParse(name string, input string) (*ASTNode, error) {
  36. n, err := ParseWithRuntime(name, input, &DummyRuntimeProvider{})
  37. return n, err
  38. }
  39. // Helper objects
  40. type DummyRuntimeProvider struct {
  41. }
  42. func (d *DummyRuntimeProvider) Runtime(n *ASTNode) Runtime {
  43. // Make the encountered node as used
  44. usedNodes[n.Name] = true
  45. return nil
  46. }