main_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 interpreter
  11. import (
  12. "flag"
  13. "fmt"
  14. "os"
  15. "testing"
  16. "devt.de/krotik/common/datautil"
  17. "devt.de/krotik/ecal/engine"
  18. "devt.de/krotik/ecal/parser"
  19. "devt.de/krotik/ecal/scope"
  20. "devt.de/krotik/ecal/util"
  21. )
  22. // Main function for all tests in this package
  23. func TestMain(m *testing.M) {
  24. flag.Parse()
  25. // Run the tests
  26. res := m.Run()
  27. // Check if all nodes have been tested
  28. for n := range providerMap {
  29. if _, ok := usedNodes[n]; !ok {
  30. fmt.Println("Not tested node: ", n)
  31. }
  32. }
  33. os.Exit(res)
  34. }
  35. // Used nodes map which is filled during unit testing. Prefilled only with nodes
  36. // which should not be encountered in ASTs.
  37. //
  38. var usedNodes = map[string]bool{
  39. parser.NodeEOF: true,
  40. }
  41. // Last used logger
  42. //
  43. var testlogger *util.MemoryLogger
  44. // Last used processor
  45. //
  46. var testprocessor engine.Processor
  47. func UnitTestEval(input string, vs parser.Scope) (interface{}, error) {
  48. return UnitTestEvalAndAST(input, vs, "")
  49. }
  50. func UnitTestEvalAndAST(input string, vs parser.Scope, expectedAST string) (interface{}, error) {
  51. return UnitTestEvalAndASTAndImport(input, vs, expectedAST, nil)
  52. }
  53. func UnitTestEvalAndASTAndImport(input string, vs parser.Scope, expectedAST string, importLocator util.ECALImportLocator) (interface{}, error) {
  54. var traverseAST func(n *parser.ASTNode)
  55. traverseAST = func(n *parser.ASTNode) {
  56. if n.Name == "" {
  57. panic(fmt.Sprintf("Node found with empty string name: %s", n))
  58. }
  59. usedNodes[n.Name] = true
  60. for _, cn := range n.Children {
  61. traverseAST(cn)
  62. }
  63. }
  64. // Parse the input
  65. erp := NewECALRuntimeProvider("ECALTestRuntime", importLocator, nil)
  66. testlogger = erp.Logger.(*util.MemoryLogger)
  67. testprocessor = erp.Processor
  68. ast, err := parser.ParseWithRuntime("ECALEvalTest", input, erp)
  69. if err != nil {
  70. return nil, err
  71. }
  72. traverseAST(ast)
  73. if expectedAST != "" && ast.String() != expectedAST {
  74. return nil, fmt.Errorf("Unexpected AST result:\n%v", ast.String())
  75. }
  76. // Validate input
  77. if err := ast.Runtime.Validate(); err != nil {
  78. return nil, err
  79. }
  80. if vs == nil {
  81. vs = scope.NewScope(scope.GlobalScope)
  82. }
  83. return ast.Runtime.Eval(vs, make(map[string]interface{}))
  84. }
  85. /*
  86. addLogFunction adds a simple log function to a given Scope.
  87. */
  88. func addLogFunction(vs parser.Scope) *datautil.RingBuffer {
  89. buf := datautil.NewRingBuffer(20)
  90. vs.SetValue("testlog", &TestLogger{buf})
  91. return buf
  92. }
  93. /*
  94. TestLogger is a simple logger function which can be added to tess.
  95. */
  96. type TestLogger struct {
  97. buf *datautil.RingBuffer
  98. }
  99. func (tl *TestLogger) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  100. tl.buf.Add(fmt.Sprint(args...))
  101. return nil, nil
  102. }
  103. func (tl *TestLogger) DocString() (string, error) {
  104. return "testlogger docstring", nil
  105. }
  106. func (tl *TestLogger) String() string {
  107. return "TestLogger"
  108. }
  109. func (tl *TestLogger) MarshalJSON() ([]byte, error) {
  110. return []byte(tl.String()), nil
  111. }