sentinel_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package xerrors
  2. import (
  3. "fmt"
  4. "io"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestSentinelWrapNil(t *testing.T) {
  9. sentinel := NewSentinel("sentinel")
  10. assert.Panics(t, func() { assert.NoError(t, sentinel.Wrap(nil)) })
  11. }
  12. func TestSentinelWrap(t *testing.T) {
  13. sentinel := NewSentinel("sentinel")
  14. assert.EqualError(t, sentinel.Wrap(New("err")), "sentinel: err")
  15. }
  16. func TestSentinelMultiWrap(t *testing.T) {
  17. top := NewSentinel("top")
  18. middle := NewSentinel("middle")
  19. err := top.Wrap(middle.Wrap(New("bottom")))
  20. assert.EqualError(t, err, "top: middle: bottom")
  21. }
  22. func TestSentinelIs(t *testing.T) {
  23. sentinel := NewSentinel("sentinel")
  24. assert.True(t, Is(sentinel, sentinel))
  25. assert.True(t, Is(sentinel.Wrap(New("err")), sentinel))
  26. assert.True(t, Is(NewSentinel("err").Wrap(sentinel), sentinel))
  27. assert.True(t, Is(Errorf("wrapper: %w", sentinel), sentinel))
  28. assert.True(t, Is(sentinel.WithStackTrace(), sentinel))
  29. assert.True(t, Is(Errorf("wrapper: %w", sentinel.WithStackTrace()), sentinel))
  30. }
  31. func TestSentinelMultiWrapIs(t *testing.T) {
  32. top := NewSentinel("top")
  33. middle := NewSentinel("middle")
  34. err := top.Wrap(middle.Wrap(io.EOF))
  35. assert.True(t, Is(err, top))
  36. assert.True(t, Is(err, middle))
  37. assert.True(t, Is(err, io.EOF))
  38. assert.False(t, Is(err, New("random")))
  39. }
  40. func TestSentinelAs(t *testing.T) {
  41. sentinel := NewSentinel("sentinel")
  42. var target *Sentinel
  43. assert.True(t, As(sentinel, &target))
  44. assert.NotNil(t, target)
  45. target = nil
  46. assert.True(t, As(sentinel.Wrap(New("err")), &target))
  47. assert.NotNil(t, target)
  48. target = nil
  49. assert.True(t, As(NewSentinel("err").Wrap(sentinel), &target))
  50. assert.NotNil(t, target)
  51. target = nil
  52. assert.True(t, As(Errorf("wrapper: %w", sentinel), &target))
  53. assert.NotNil(t, target)
  54. target = nil
  55. assert.True(t, As(sentinel.WithStackTrace(), &target))
  56. assert.NotNil(t, target)
  57. target = nil
  58. assert.True(t, As(Errorf("wrapper: %w", sentinel.WithStackTrace()), &target))
  59. assert.NotNil(t, target)
  60. target = nil
  61. }
  62. func TestSentinelMultiWrapAs(t *testing.T) {
  63. top := NewSentinel("top")
  64. middle := NewSentinel("middle")
  65. err := top.Wrap(middle.Wrap(io.EOF))
  66. var target *Sentinel
  67. assert.True(t, As(err, &target))
  68. assert.NotNil(t, target)
  69. }
  70. func TestSentinelFormatting(t *testing.T) {
  71. sentinel := NewSentinel("sentinel")
  72. assert.Equal(t, "sentinel", fmt.Sprintf("%s", sentinel))
  73. assert.Equal(t, "sentinel", fmt.Sprintf("%v", sentinel))
  74. assert.Equal(t, "sentinel", fmt.Sprintf("%+v", sentinel))
  75. }