rt_general_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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), 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. }