format_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package errors
  2. import (
  3. "fmt"
  4. "io"
  5. "regexp"
  6. "strings"
  7. "testing"
  8. )
  9. func TestFormatNew(t *testing.T) {
  10. tests := []struct {
  11. error
  12. format string
  13. want string
  14. }{{
  15. New("error"),
  16. "%s",
  17. "error",
  18. }, {
  19. New("error"),
  20. "%v",
  21. "error",
  22. }, {
  23. New("error"),
  24. "%+v",
  25. "error\n" +
  26. "github.com/pkg/errors.TestFormatNew\n" +
  27. "\t.+/github.com/pkg/errors/format_test.go:25",
  28. }}
  29. for _, tt := range tests {
  30. testFormatRegexp(t, tt.error, tt.format, tt.want)
  31. }
  32. }
  33. func TestFormatErrorf(t *testing.T) {
  34. tests := []struct {
  35. error
  36. format string
  37. want string
  38. }{{
  39. Errorf("%s", "error"),
  40. "%s",
  41. "error",
  42. }, {
  43. Errorf("%s", "error"),
  44. "%v",
  45. "error",
  46. }, {
  47. Errorf("%s", "error"),
  48. "%+v",
  49. "error\n" +
  50. "github.com/pkg/errors.TestFormatErrorf\n" +
  51. "\t.+/github.com/pkg/errors/format_test.go:51",
  52. }}
  53. for _, tt := range tests {
  54. testFormatRegexp(t, tt.error, tt.format, tt.want)
  55. }
  56. }
  57. func TestFormatWrap(t *testing.T) {
  58. tests := []struct {
  59. error
  60. format string
  61. want string
  62. }{{
  63. Wrap(New("error"), "error2"),
  64. "%s",
  65. "error2: error",
  66. }, {
  67. Wrap(New("error"), "error2"),
  68. "%v",
  69. "error2: error",
  70. }, {
  71. Wrap(New("error"), "error2"),
  72. "%+v",
  73. "error\n" +
  74. "github.com/pkg/errors.TestFormatWrap\n" +
  75. "\t.+/github.com/pkg/errors/format_test.go:77",
  76. }, {
  77. Wrap(io.EOF, "error"),
  78. "%s",
  79. "error: EOF",
  80. }}
  81. for _, tt := range tests {
  82. testFormatRegexp(t, tt.error, tt.format, tt.want)
  83. }
  84. }
  85. func TestFormatWrapf(t *testing.T) {
  86. tests := []struct {
  87. error
  88. format string
  89. want string
  90. }{{
  91. Wrapf(New("error"), "error%d", 2),
  92. "%s",
  93. "error2: error",
  94. }, {
  95. Wrap(io.EOF, "error"),
  96. "%v",
  97. "error: EOF",
  98. }, {
  99. Wrap(io.EOF, "error"),
  100. "%+v",
  101. "EOF\n" +
  102. "github.com/pkg/errors.TestFormatWrapf\n" +
  103. "\t.+/github.com/pkg/errors/format_test.go:107: error",
  104. }, {
  105. Wrapf(New("error"), "error%d", 2),
  106. "%v",
  107. "error2: error",
  108. }, {
  109. Wrapf(New("error"), "error%d", 2),
  110. "%+v",
  111. "error\n" +
  112. "github.com/pkg/errors.TestFormatWrapf\n" +
  113. "\t.+/github.com/pkg/errors/format_test.go:117",
  114. }}
  115. for _, tt := range tests {
  116. testFormatRegexp(t, tt.error, tt.format, tt.want)
  117. }
  118. }
  119. func testFormatRegexp(t *testing.T, arg interface{}, format, want string) {
  120. got := fmt.Sprintf(format, arg)
  121. lines := strings.SplitN(got, "\n", -1)
  122. for i, w := range strings.SplitN(want, "\n", -1) {
  123. match, err := regexp.MatchString(w, lines[i])
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if !match {
  128. t.Errorf("fmt.Sprintf(%q, err): got: %q, want: %q", format, got, want)
  129. }
  130. }
  131. }