Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit f36fdcc

Browse files
committed
use testFormatRegexp for all Sprintf tests
1 parent e172069 commit f36fdcc

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

format_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func TestFormatWrapf(t *testing.T) {
126126
}
127127
}
128128

129-
func testFormatRegexp(t *testing.T, err error, format, want string) {
130-
got := fmt.Sprintf(format, err)
129+
func testFormatRegexp(t *testing.T, arg interface{}, format, want string) {
130+
got := fmt.Sprintf(format, arg)
131131
lines := strings.SplitN(got, "\n", -1)
132132
for i, w := range strings.SplitN(want, "\n", -1) {
133133
match, err := regexp.MatchString(w, lines[i])

stack_test.go

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestFrameFormat(t *testing.T) {
9393
return x.ptr()
9494
}(),
9595
"%n",
96-
"(*X).ptr",
96+
`\(\*X\).ptr`,
9797
}, {
9898
func() Frame {
9999
var x X
@@ -113,19 +113,15 @@ func TestFrameFormat(t *testing.T) {
113113
Frame(initpc),
114114
"%+v",
115115
"github.com/pkg/errors.init\n" +
116-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:9",
116+
"\t/.+/github.com/pkg/errors/stack_test.go:9",
117117
}, {
118118
Frame(0),
119119
"%v",
120120
"unknown:0",
121121
}}
122122

123123
for _, tt := range tests {
124-
got := fmt.Sprintf(tt.format, tt.Frame)
125-
want := tt.want
126-
if want != got {
127-
t.Errorf("%v %q: want: %q, got: %q", tt.Frame, tt.format, want, got)
128-
}
124+
testFormatRegexp(t, tt.Frame, tt.format, tt.want)
129125
}
130126
}
131127

@@ -178,24 +174,24 @@ func TestStacktrace(t *testing.T) {
178174
}{{
179175
New("ooh"), []string{
180176
"github.com/pkg/errors.TestStacktrace\n" +
181-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:179",
177+
"\t/.+/github.com/pkg/errors/stack_test.go:175",
182178
},
183179
}, {
184180
Wrap(New("ooh"), "ahh"), []string{
185181
"github.com/pkg/errors.TestStacktrace\n" +
186-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:184", // this is the stack of Wrap, not New
182+
"\t/.+/github.com/pkg/errors/stack_test.go:180", // this is the stack of Wrap, not New
187183
},
188184
}, {
189185
Cause(Wrap(New("ooh"), "ahh")), []string{
190186
"github.com/pkg/errors.TestStacktrace\n" +
191-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:189", // this is the stack of New
187+
"\t/.+/github.com/pkg/errors/stack_test.go:185", // this is the stack of New
192188
},
193189
}, {
194190
func() error { return New("ooh") }(), []string{
195191
"github.com/pkg/errors.TestStacktrace.func1\n" +
196-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:194", // this is the stack of New
192+
"\t/.+/github.com/pkg/errors/stack_test.go:190", // this is the stack of New
197193
"github.com/pkg/errors.TestStacktrace\n" +
198-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:194", // this is the stack of New's caller
194+
"\t/.+/github.com/pkg/errors/stack_test.go:190", // this is the stack of New's caller
199195
},
200196
}, {
201197
Cause(func() error {
@@ -204,14 +200,14 @@ func TestStacktrace(t *testing.T) {
204200
}()
205201
}()), []string{
206202
"github.com/pkg/errors.TestStacktrace.func2.1\n" +
207-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:203", // this is the stack of Errorf
203+
"\t/.+/github.com/pkg/errors/stack_test.go:199", // this is the stack of Errorf
208204
"github.com/pkg/errors.TestStacktrace.func2\n" +
209-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:204", // this is the stack of Errorf's caller
205+
"\t/.+/github.com/pkg/errors/stack_test.go:200", // this is the stack of Errorf's caller
210206
"github.com/pkg/errors.TestStacktrace\n" +
211-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:205", // this is the stack of Errorf's caller's caller
207+
"\t/.+/github.com/pkg/errors/stack_test.go:201", // this is the stack of Errorf's caller's caller
212208
},
213209
}}
214-
for i, tt := range tests {
210+
for _, tt := range tests {
215211
x, ok := tt.err.(interface {
216212
Stacktrace() Stacktrace
217213
})
@@ -221,11 +217,7 @@ func TestStacktrace(t *testing.T) {
221217
}
222218
st := x.Stacktrace()
223219
for j, want := range tt.want {
224-
frame := st[j]
225-
got := fmt.Sprintf("%+v", frame)
226-
if got != want {
227-
t.Errorf("test %d: frame %d: got %q, want %q", i, j, got, want)
228-
}
220+
testFormatRegexp(t, st[j], "%+v", want)
229221
}
230222
}
231223
}
@@ -246,61 +238,58 @@ func TestStacktraceFormat(t *testing.T) {
246238
}{{
247239
nil,
248240
"%s",
249-
"[]",
241+
`\[\]`,
250242
}, {
251243
nil,
252244
"%v",
253-
"[]",
245+
`\[\]`,
254246
}, {
255247
nil,
256248
"%+v",
257249
"",
258250
}, {
259251
nil,
260252
"%#v",
261-
"[]errors.Frame(nil)",
253+
`\[\]errors.Frame\(nil\)`,
262254
}, {
263255
make(Stacktrace, 0),
264256
"%s",
265-
"[]",
257+
`\[\]`,
266258
}, {
267259
make(Stacktrace, 0),
268260
"%v",
269-
"[]",
261+
`\[\]`,
270262
}, {
271263
make(Stacktrace, 0),
272264
"%+v",
273265
"",
274266
}, {
275267
make(Stacktrace, 0),
276268
"%#v",
277-
"[]errors.Frame{}",
269+
`\[\]errors.Frame{}`,
278270
}, {
279271
stacktrace()[:2],
280272
"%s",
281-
"[stack_test.go stack_test.go]",
273+
`\[stack_test.go stack_test.go\]`,
282274
}, {
283275
stacktrace()[:2],
284276
"%v",
285-
"[stack_test.go:236 stack_test.go:283]",
277+
`\[stack_test.go:228 stack_test.go:275\]`,
286278
}, {
287279
stacktrace()[:2],
288280
"%+v",
289281
"\n" +
290282
"github.com/pkg/errors.stacktrace\n" +
291-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:236\n" +
283+
"\t/.+/github.com/pkg/errors/stack_test.go:228\n" +
292284
"github.com/pkg/errors.TestStacktraceFormat\n" +
293-
"\t/home/dfc/src/github.com/pkg/errors/stack_test.go:287",
285+
"\t/.+/github.com/pkg/errors/stack_test.go:279",
294286
}, {
295287
stacktrace()[:2],
296288
"%#v",
297-
"[]errors.Frame{stack_test.go:236, stack_test.go:295}",
289+
`\[\]errors.Frame{stack_test.go:228, stack_test.go:287}`,
298290
}}
299291

300-
for i, tt := range tests {
301-
got := fmt.Sprintf(tt.format, tt.Stacktrace)
302-
if got != tt.want {
303-
t.Errorf("test %d: got: %q, want: %q", i+1, got, tt.want)
304-
}
292+
for _, tt := range tests {
293+
testFormatRegexp(t, tt.Stacktrace, tt.format, tt.want)
305294
}
306295
}

0 commit comments

Comments
 (0)