format_test.go 2.8 KB

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