error_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 util
  11. import (
  12. "fmt"
  13. "strings"
  14. "testing"
  15. "devt.de/krotik/ecal/parser"
  16. )
  17. func TestRuntimeError(t *testing.T) {
  18. ast, _ := parser.Parse("foo", "a")
  19. err1 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  20. if err1.Error() != "ECAL error in foo: foo (bar) (Line:1 Pos:1)" {
  21. t.Error("Unexpected result:", err1)
  22. return
  23. }
  24. ast.Token = nil
  25. err2 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  26. if err2.Error() != "ECAL error in foo: foo (bar)" {
  27. t.Error("Unexpected result:", err2)
  28. return
  29. }
  30. ast, _ = parser.Parse("foo", "a:=1")
  31. err3 := NewRuntimeError("foo", fmt.Errorf("foo"), "bar", ast)
  32. ast, _ = parser.Parse("bar1", "print(b)")
  33. err3.(TraceableRuntimeError).AddTrace(ast)
  34. ast, _ = parser.Parse("bar2", "raise(c)")
  35. err3.(TraceableRuntimeError).AddTrace(ast)
  36. ast, _ = parser.Parse("bar3", "1 + d")
  37. err3.(TraceableRuntimeError).AddTrace(ast)
  38. trace := strings.Join(err3.(TraceableRuntimeError).GetTraceString(), "\n")
  39. if trace != `print(b) (bar1:1)
  40. raise(c) (bar2:1)
  41. 1 + d (bar3:1)` {
  42. t.Error("Unexpected result:", trace)
  43. return
  44. }
  45. }