errorutil_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package errorutil
  10. import (
  11. "errors"
  12. "testing"
  13. )
  14. func TestAssertOk(t *testing.T) {
  15. defer func() {
  16. if r := recover(); r == nil {
  17. t.Error("Giving AssertOk an error should cause a panic.")
  18. }
  19. }()
  20. AssertOk(errors.New("test"))
  21. }
  22. func TestAssertTrue(t *testing.T) {
  23. defer func() {
  24. if r := recover(); r == nil {
  25. t.Error("Giving AssertTrue a negative condition should cause a panic.")
  26. }
  27. }()
  28. AssertTrue(false, "bla")
  29. }
  30. func TestCompositeError(t *testing.T) {
  31. ce := NewCompositeError()
  32. if ce.HasErrors() {
  33. t.Error("CompositeError object shouldn't have any errors yet")
  34. return
  35. }
  36. ce.Add(errors.New("test1"))
  37. if !ce.HasErrors() {
  38. t.Error("CompositeError object should have one error by now")
  39. return
  40. }
  41. ce.Add(errors.New("test2"))
  42. // Add a CompositeError to a CompositeError
  43. ce2 := NewCompositeError()
  44. ce2.Add(errors.New("test3"))
  45. ce.Add(ce2)
  46. if ce.Error() != "test1; test2; test3" {
  47. t.Error("Unexpected output:", ce.Error())
  48. }
  49. }