format_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. New("error"),
  30. "%q",
  31. `"error"`,
  32. }}
  33. for i, tt := range tests {
  34. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  35. }
  36. }
  37. func TestFormatErrorf(t *testing.T) {
  38. tests := []struct {
  39. error
  40. format string
  41. want string
  42. }{{
  43. Errorf("%s", "error"),
  44. "%s",
  45. "error",
  46. }, {
  47. Errorf("%s", "error"),
  48. "%v",
  49. "error",
  50. }, {
  51. Errorf("%s", "error"),
  52. "%+v",
  53. "error\n" +
  54. "github.com/pkg/errors.TestFormatErrorf\n" +
  55. "\t.+/github.com/pkg/errors/format_test.go:55",
  56. }}
  57. for i, tt := range tests {
  58. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  59. }
  60. }
  61. func TestFormatWrap(t *testing.T) {
  62. tests := []struct {
  63. error
  64. format string
  65. want string
  66. }{{
  67. Wrap(New("error"), "error2"),
  68. "%s",
  69. "error2: error",
  70. }, {
  71. Wrap(New("error"), "error2"),
  72. "%v",
  73. "error2: error",
  74. }, {
  75. Wrap(New("error"), "error2"),
  76. "%+v",
  77. "error\n" +
  78. "github.com/pkg/errors.TestFormatWrap\n" +
  79. "\t.+/github.com/pkg/errors/format_test.go:81",
  80. }, {
  81. Wrap(io.EOF, "error"),
  82. "%s",
  83. "error: EOF",
  84. }, {
  85. Wrap(io.EOF, "error"),
  86. "%v",
  87. "error: EOF",
  88. }, {
  89. Wrap(io.EOF, "error"),
  90. "%+v",
  91. "EOF\n" +
  92. "error\n" +
  93. "github.com/pkg/errors.TestFormatWrap\n" +
  94. "\t.+/github.com/pkg/errors/format_test.go:95",
  95. }, {
  96. Wrap(Wrap(io.EOF, "error1"), "error2"),
  97. "%+v",
  98. "EOF\n" +
  99. "error1\n" +
  100. "github.com/pkg/errors.TestFormatWrap\n" +
  101. "\t.+/github.com/pkg/errors/format_test.go:102\n",
  102. }, {
  103. Wrap(New("error with space"), "context"),
  104. "%q",
  105. `"context: error with space"`,
  106. }}
  107. for i, tt := range tests {
  108. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  109. }
  110. }
  111. func TestFormatWrapf(t *testing.T) {
  112. tests := []struct {
  113. error
  114. format string
  115. want string
  116. }{{
  117. Wrapf(io.EOF, "error%d", 2),
  118. "%s",
  119. "error2: EOF",
  120. }, {
  121. Wrapf(io.EOF, "error%d", 2),
  122. "%v",
  123. "error2: EOF",
  124. }, {
  125. Wrapf(io.EOF, "error%d", 2),
  126. "%+v",
  127. "EOF\n" +
  128. "error2\n" +
  129. "github.com/pkg/errors.TestFormatWrapf\n" +
  130. "\t.+/github.com/pkg/errors/format_test.go:133",
  131. }, {
  132. Wrapf(New("error"), "error%d", 2),
  133. "%s",
  134. "error2: error",
  135. }, {
  136. Wrapf(New("error"), "error%d", 2),
  137. "%v",
  138. "error2: error",
  139. }, {
  140. Wrapf(New("error"), "error%d", 2),
  141. "%+v",
  142. "error\n" +
  143. "github.com/pkg/errors.TestFormatWrapf\n" +
  144. "\t.+/github.com/pkg/errors/format_test.go:148",
  145. }}
  146. for i, tt := range tests {
  147. testFormatRegexp(t, i, tt.error, tt.format, tt.want)
  148. }
  149. }
  150. func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) {
  151. got := fmt.Sprintf(format, arg)
  152. lines := strings.SplitN(got, "\n", -1)
  153. for i, w := range strings.SplitN(want, "\n", -1) {
  154. match, err := regexp.MatchString(w, lines[i])
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. if !match {
  159. t.Errorf("test %d: line %d: fmt.Sprintf(%q, err): got: %q, want: %q", n+1, i+1, format, got, want)
  160. }
  161. }
  162. }