main_test.go 4.6 KB

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