main_test.go 3.7 KB

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