multi_error_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package common
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. )
  7. func TestCombine_AllNilReturnsNil(t *testing.T) {
  8. if err := Combine(); err != nil {
  9. t.Fatalf("Combine() with no args = %v, want nil", err)
  10. }
  11. if err := Combine(nil, nil, nil); err != nil {
  12. t.Fatalf("Combine(nil, nil, nil) = %v, want nil", err)
  13. }
  14. }
  15. func TestCombine_SkipsNilErrors(t *testing.T) {
  16. e1 := errors.New("boom one")
  17. e2 := errors.New("boom two")
  18. err := Combine(nil, e1, nil, e2, nil)
  19. if err == nil {
  20. t.Fatal("expected non-nil combined error")
  21. }
  22. msg := err.Error()
  23. if !strings.Contains(msg, "boom one") || !strings.Contains(msg, "boom two") {
  24. t.Fatalf("combined error %q does not contain both underlying messages", msg)
  25. }
  26. if !strings.HasPrefix(msg, "multierr: ") {
  27. t.Fatalf("combined error %q missing %q prefix", msg, "multierr: ")
  28. }
  29. }
  30. func TestCombine_SingleErrorStillWrapped(t *testing.T) {
  31. e := errors.New("only one")
  32. err := Combine(e)
  33. if err == nil {
  34. t.Fatal("expected non-nil error")
  35. }
  36. if !strings.Contains(err.Error(), "only one") {
  37. t.Fatalf("combined error %q missing underlying message", err.Error())
  38. }
  39. }