main_test.go 2.6 KB

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