rt_general_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "testing"
  13. "devt.de/krotik/ecal/parser"
  14. "devt.de/krotik/ecal/scope"
  15. "devt.de/krotik/ecal/util"
  16. )
  17. func TestGeneralErrorCases(t *testing.T) {
  18. n, _ := parser.Parse("a", "a")
  19. inv := &invalidRuntime{newBaseRuntime(NewECALRuntimeProvider("a", nil, nil), n)}
  20. if err := inv.Validate().Error(); err != "ECAL error in a: Invalid construct (Unknown node: identifier) (Line:1 Pos:1)" {
  21. t.Error("Unexpected result:", err)
  22. return
  23. }
  24. if _, err := inv.Eval(nil, nil); err.Error() != "ECAL error in a: Invalid construct (Unknown node: identifier) (Line:1 Pos:1)" {
  25. t.Error("Unexpected result:", err)
  26. return
  27. }
  28. }
  29. func TestImporting(t *testing.T) {
  30. vs := scope.NewScope(scope.GlobalScope)
  31. il := &util.MemoryImportLocator{make(map[string]string)}
  32. il.Files["foo/bar"] = `
  33. b := 123
  34. `
  35. res, err := UnitTestEvalAndASTAndImport(
  36. `
  37. import "foo/bar" as foobar
  38. a := foobar.b`, vs,
  39. `
  40. statements
  41. import
  42. string: 'foo/bar'
  43. identifier: foobar
  44. :=
  45. identifier: a
  46. identifier: foobar
  47. identifier: b
  48. `[1:], il)
  49. if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
  50. a (float64) : 123
  51. foobar (map[interface {}]interface {}) : {"b":123}
  52. }` {
  53. t.Error("Unexpected result: ", vsRes, res, err)
  54. return
  55. }
  56. }
  57. func TestLogging(t *testing.T) {
  58. vs := scope.NewScope(scope.GlobalScope)
  59. _, err := UnitTestEvalAndAST(
  60. `
  61. log("Hello")
  62. debug("foo")
  63. error("bar")
  64. `, vs,
  65. `
  66. statements
  67. identifier: log
  68. funccall
  69. string: 'Hello'
  70. identifier: debug
  71. funccall
  72. string: 'foo'
  73. identifier: error
  74. funccall
  75. string: 'bar'
  76. `[1:])
  77. if err != nil {
  78. t.Error("Unexpected result: ", err)
  79. return
  80. }
  81. if testlogger.String() != `Hello
  82. debug: foo
  83. error: bar` {
  84. t.Error("Unexpected result: ", testlogger.String())
  85. return
  86. }
  87. }