format_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 i, tt := range tests {
  30. testFormatRegexp(t, i, 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 i, tt := range tests {
  54. testFormatRegexp(t, i, 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. Wrap(io.EOF, "error"),
  82. "%v",
  83. "error: EOF",
  84. }, {
  85. Wrap(io.EOF, "error"),
  86. "%+v",
  87. "EOF\n" +
  88. "error\n" +
  89. "github.com/pkg/errors.TestFormatWrap\n" +
  90. "\t.+/github.com/pkg/errors/format_test.go:91",
  91. }, {
  92. Wrap(Wrap(io.EOF, "error1"), "error2"),
  93. "%+v",
  94. "EOF\n" +
  95. "error1\n" +
  96. "github.com/pkg/errors.TestFormatWrap\n" +
  97. "\t.+/github.com/pkg/errors/format_test.go:98\n",
  98. }, {
  99. Wrap(New("error with space"), "context"),
  100. "%q",
  101. `"context: error with space"`,
  102. }}
  103. for i, tt := range tests {
  104. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  105. }
  106. }
  107. func TestFormatWrapf(t *testing.T) {
  108. tests := []struct {
  109. error
  110. format string
  111. want string
  112. }{{
  113. Wrapf(io.EOF, "error%d", 2),
  114. "%s",
  115. "error2: EOF",
  116. }, {
  117. Wrapf(io.EOF, "error%d", 2),
  118. "%v",
  119. "error2: EOF",
  120. }, {
  121. Wrapf(io.EOF, "error%d", 2),
  122. "%+v",
  123. "EOF\n" +
  124. "error2\n" +
  125. "github.com/pkg/errors.TestFormatWrapf\n" +
  126. "\t.+/github.com/pkg/errors/format_test.go:129",
  127. }, {
  128. Wrapf(New("error"), "error%d", 2),
  129. "%s",
  130. "error2: error",
  131. }, {
  132. Wrapf(New("error"), "error%d", 2),
  133. "%v",
  134. "error2: error",
  135. }, {
  136. Wrapf(New("error"), "error%d", 2),
  137. "%+v",
  138. "error\n" +
  139. "github.com/pkg/errors.TestFormatWrapf\n" +
  140. "\t.+/github.com/pkg/errors/format_test.go:144",
  141. }}
  142. for i, tt := range tests {
  143. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  144. }
  145. }
  146. func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
  147. got := fmt.Sprintf(format, arg)
  148. lines := strings.SplitN(got, "\n", -1)
  149. for i, w := range strings.SplitN(want, "\n", -1) {
  150. match, err := regexp.MatchString(w, lines[i])
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. if !match {
  155. t.Errorf("test %d: line %d: fmt.Sprintf(%q, err): got: %q, want: %q", n+1, i+1, format, got, want)
  156. }
  157. }
  158. }