main_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. n, err := ParseWithRuntime(name, input, &DummyRuntimeProvider{})
  44. // TODO Test pretty printing
  45. // Test AST serialization
  46. if err == nil {
  47. var unmarshaledJSONObject map[string]interface{}
  48. astString, err := json.Marshal(n.ToJSONObject())
  49. if err != nil {
  50. return nil, fmt.Errorf("Could not marshal AST: %v", err)
  51. }
  52. if err := json.Unmarshal(astString, &unmarshaledJSONObject); err != nil {
  53. return nil, fmt.Errorf("Could not unmarshal JSON object: %v", err)
  54. }
  55. unmarshaledAST, err := ASTFromJSONObject(unmarshaledJSONObject)
  56. if err != nil {
  57. return nil, fmt.Errorf("Could not create AST from unmarshaled JSON object: %v", err)
  58. }
  59. // String compare the ASTs
  60. if ok, msg := n.Equals(unmarshaledAST); !ok {
  61. return nil, fmt.Errorf(
  62. "Parsed AST is different from the unmarshaled AST.\n%v\n",
  63. msg)
  64. }
  65. }
  66. return n, err
  67. }
  68. // Used nodes map which is filled during unit testing. Prefilled with tokens which
  69. // will not be generated by the parser
  70. //
  71. var usedPrettyPrinterNodes = map[string]bool{}
  72. func UnitTestPrettyPrinting(input, astOutput, ppOutput string) error {
  73. var visitAST func(*ASTNode)
  74. astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
  75. if err != nil || fmt.Sprint(astres) != astOutput {
  76. return fmt.Errorf("Unexpected parser output:\n%v expected was:\n%v Error: %v", astres, astOutput, err)
  77. }
  78. visitAST = func(n *ASTNode) {
  79. // Make the encountered node as used
  80. numChildren := len(n.Children)
  81. if numChildren > 0 {
  82. usedPrettyPrinterNodes[fmt.Sprintf("%v_%v", n.Name, numChildren)] = true
  83. } else {
  84. usedPrettyPrinterNodes[n.Name] = true
  85. }
  86. for _, c := range n.Children {
  87. visitAST(c)
  88. }
  89. }
  90. visitAST(astres)
  91. ppres, err := PrettyPrint(astres)
  92. if err != nil || ppres != ppOutput {
  93. return fmt.Errorf("Unexpected result: %v (expected: %v) error: %v", ppres, ppOutput, err)
  94. }
  95. // Make sure the pretty printed result is valid and gets the same parse tree
  96. astres2, err := ParseWithRuntime("mytest", ppres, &DummyRuntimeProvider{})
  97. if err != nil || fmt.Sprint(astres2) != astOutput {
  98. return fmt.Errorf("Unexpected parser output from pretty print string:\n%v expected was:\n%v Error: %v", astres2, astOutput, err)
  99. }
  100. return nil
  101. }
  102. // Helper objects
  103. type DummyRuntimeProvider struct {
  104. }
  105. func (d *DummyRuntimeProvider) Runtime(n *ASTNode) Runtime {
  106. // Make the encountered node as used
  107. usedNodes[n.Name] = true
  108. return nil
  109. }