main_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // Check if all nodes have been pretty printed
  27. for k := range prettyPrinterMap {
  28. if _, ok := usedPrettyPrinterNodes[k]; !ok {
  29. fmt.Println("Not tested pretty printer: ", k)
  30. }
  31. }
  32. os.Exit(res)
  33. }
  34. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  35. // will not be generated by the parser
  36. //
  37. var usedNodes = map[string]bool{
  38. NodeEOF: true, // Only used as end term
  39. "": true, // No node e.g. semicolon - These nodes should never be part of an AST
  40. }
  41. func UnitTestParse(name string, input string) (*ASTNode, error) {
  42. n, err := ParseWithRuntime(name, input, &DummyRuntimeProvider{})
  43. // TODO Test pretty printing
  44. // TODO Test AST serialization
  45. return n, err
  46. }
  47. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  48. // will not be generated by the parser
  49. //
  50. var usedPrettyPrinterNodes = map[string]bool{}
  51. func UnitTestPrettyPrinting(input, astOutput, ppOutput string) error {
  52. var visitAST func(*ASTNode)
  53. astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
  54. if err != nil || fmt.Sprint(astres) != astOutput {
  55. return fmt.Errorf("Unexpected parser output:\n%v expected was:\n%v Error: %v", astres, astOutput, err)
  56. }
  57. visitAST = func(n *ASTNode) {
  58. // Make the encountered node as used
  59. numChildren := len(n.Children)
  60. if numChildren > 0 {
  61. usedPrettyPrinterNodes[fmt.Sprintf("%v_%v", n.Name, numChildren)] = true
  62. } else {
  63. usedPrettyPrinterNodes[n.Name] = true
  64. }
  65. for _, c := range n.Children {
  66. visitAST(c)
  67. }
  68. }
  69. visitAST(astres)
  70. ppres, err := PrettyPrint(astres)
  71. if err != nil || ppres != ppOutput {
  72. return fmt.Errorf("Unexpected result: %v (expected: %v) error: %v", ppres, ppOutput, err)
  73. }
  74. // Make sure the pretty printed result is valid and gets the same parse tree
  75. astres2, err := ParseWithRuntime("mytest", ppres, &DummyRuntimeProvider{})
  76. if err != nil || fmt.Sprint(astres2) != astOutput {
  77. return fmt.Errorf("Unexpected parser output from pretty print string:\n%v expected was:\n%v Error: %v", astres2, astOutput, err)
  78. }
  79. return nil
  80. }
  81. // Helper objects
  82. type DummyRuntimeProvider struct {
  83. }
  84. func (d *DummyRuntimeProvider) Runtime(n *ASTNode) Runtime {
  85. // Make the encountered node as used
  86. usedNodes[n.Name] = true
  87. return nil
  88. }