main_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "encoding/json"
  12. "flag"
  13. "fmt"
  14. "os"
  15. "testing"
  16. )
  17. // Main function for all tests in this package
  18. func TestMain(m *testing.M) {
  19. flag.Parse()
  20. res := m.Run()
  21. // Check if all nodes have been tested
  22. for _, n := range astNodeMap {
  23. if _, ok := usedNodes[n.Name]; !ok {
  24. fmt.Println("Not tested node: ", n.Name)
  25. }
  26. }
  27. // Check if all nodes have been pretty printed
  28. for k := range prettyPrinterMap {
  29. if _, ok := usedPrettyPrinterNodes[k]; !ok {
  30. fmt.Println("Not tested pretty printer: ", k)
  31. }
  32. }
  33. os.Exit(res)
  34. }
  35. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  36. // will not be generated by the parser
  37. //
  38. var usedNodes = map[string]bool{
  39. NodeEOF: true, // Only used as end term
  40. "": true, // No node e.g. semicolon - These nodes should never be part of an AST
  41. }
  42. func UnitTestParse(name string, input string) (*ASTNode, error) {
  43. return UnitTestParseWithPPResult(name, input, "")
  44. }
  45. func UnitTestParseWithPPResult(name string, input string, expectedPPRes string) (*ASTNode, error) {
  46. n, err := ParseWithRuntime(name, input, &DummyRuntimeProvider{})
  47. // Test AST serialization
  48. if err == nil {
  49. var unmarshaledJSONObject map[string]interface{}
  50. astString, err := json.Marshal(n.ToJSONObject())
  51. if err != nil {
  52. return nil, fmt.Errorf("Could not marshal AST: %v", err)
  53. }
  54. if err := json.Unmarshal(astString, &unmarshaledJSONObject); err != nil {
  55. return nil, fmt.Errorf("Could not unmarshal JSON object: %v", err)
  56. }
  57. unmarshaledAST, err := ASTFromJSONObject(unmarshaledJSONObject)
  58. if err != nil {
  59. return nil, fmt.Errorf("Could not create AST from unmarshaled JSON object: %v", err)
  60. }
  61. // Compare the ASTs
  62. if ok, msg := n.Equals(unmarshaledAST, false); !ok {
  63. return nil, fmt.Errorf(
  64. "Parsed AST is different from the unmarshaled AST.\n%v\n",
  65. msg)
  66. }
  67. }
  68. // Test Pretty printing
  69. if err == nil {
  70. ppres, err := PrettyPrint(n)
  71. if err != nil {
  72. return nil, fmt.Errorf("Error while pretty printing: %v (input: %v)", err, input)
  73. }
  74. if expectedPPRes == "" {
  75. n2, err := ParseWithRuntime(name, ppres, &DummyRuntimeProvider{})
  76. if err != nil {
  77. return nil, fmt.Errorf("Error while parsing pretty print result: %v (result: %v)", err, ppres)
  78. }
  79. // Compare the ASTs
  80. if ok, msg := n.Equals(n2, true); !ok {
  81. return nil, fmt.Errorf(
  82. "Parsed AST from pretty printer is different from the originally parsed AST."+
  83. "\nOriginal input: %v\nPretty printed: %v\nPretty AST: %v\n%v\n",
  84. input, ppres, n2, msg)
  85. }
  86. } else if ppres != expectedPPRes {
  87. return nil, fmt.Errorf("Expected pretty printer result is different:\nExpected "+
  88. "result: %v\nActual result: %v\n", expectedPPRes, ppres)
  89. }
  90. markASTNodesAsPrettyPrinted(n)
  91. }
  92. return n, err
  93. }
  94. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  95. // will not be generated by the parser
  96. //
  97. var usedPrettyPrinterNodes = map[string]bool{}
  98. func markASTNodesAsPrettyPrinted(n *ASTNode) {
  99. // Make the encountered node as used
  100. numChildren := len(n.Children)
  101. if numChildren > 0 {
  102. usedPrettyPrinterNodes[fmt.Sprintf("%v_%v", n.Name, numChildren)] = true
  103. } else {
  104. usedPrettyPrinterNodes[n.Name] = true
  105. }
  106. for _, c := range n.Children {
  107. markASTNodesAsPrettyPrinted(c)
  108. }
  109. }
  110. func UnitTestPrettyPrinting(input, astOutput, ppOutput string) error {
  111. astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
  112. if err != nil || fmt.Sprint(astres) != astOutput {
  113. return fmt.Errorf("Unexpected parser output:\n%v expected was:\n%v Error: %v", astres, astOutput, err)
  114. }
  115. markASTNodesAsPrettyPrinted(astres)
  116. ppres, err := PrettyPrint(astres)
  117. if err != nil || ppres != ppOutput {
  118. return fmt.Errorf("Unexpected result: %v (expected: %v) error: %v", ppres, ppOutput, err)
  119. }
  120. // Make sure the pretty printed result is valid and gets the same parse tree
  121. astres2, err := ParseWithRuntime("mytest", ppres, &DummyRuntimeProvider{})
  122. if err != nil || fmt.Sprint(astres2) != astOutput {
  123. return fmt.Errorf("Unexpected parser output from pretty print string:\n%v expected was:\n%v Error: %v", astres2, astOutput, err)
  124. }
  125. return nil
  126. }
  127. // Helper objects
  128. type DummyRuntimeProvider struct {
  129. }
  130. func (d *DummyRuntimeProvider) Runtime(n *ASTNode) Runtime {
  131. // Make the encountered node as used
  132. usedNodes[n.Name] = true
  133. return nil
  134. }