Skip to content

Commit 3b4d428

Browse files
dsnetneild
authored andcommitted
encoding/json: modernize tests
There are no changes to what is being tested. No test cases were removed or added. Changes made: * Use a local implementation of test case position marking. See #52751. * Use consistent names for all test tables and variables. * Generally speaking, follow modern Go style guide for tests. * Move global tables local to the test function if possible. * Make every table entry run in a distinct testing.T.Run. The purpose of this change is to make it easier to perform v1-to-v2 development where we want v2 to support close to bug-for-bug compatibility when running in v1 mode. Annotating each test case with the location of the test data makes it easier to jump directly to the test data itself and understand why this particular case is failing. Having every test case run in its own t.Run makes it easier to isolate a particular failing test and work on fixing the code until that test case starts to pass again. Unfortunately, many tests are annotated with an empty name. An empty name is better than nothing, since the testing framework auto assigns a numeric ID for duplicate names. It is not worth the trouble to give descriptive names to each of the thousands of test cases. Change-Id: I43905f35249b3d77dfca234b9c7808d40e225de8 Reviewed-on: https://go-review.googlesource.com/c/go/+/522880 Auto-Submit: Joseph Tsai <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 882a356 commit 3b4d428

File tree

7 files changed

+1528
-1445
lines changed

7 files changed

+1528
-1445
lines changed

src/encoding/json/bench_test.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func BenchmarkCodeEncoder(b *testing.B) {
9393
enc := NewEncoder(io.Discard)
9494
for pb.Next() {
9595
if err := enc.Encode(&codeStruct); err != nil {
96-
b.Fatal("Encode:", err)
96+
b.Fatalf("Encode error: %v", err)
9797
}
9898
}
9999
})
@@ -120,10 +120,10 @@ func BenchmarkCodeEncoderError(b *testing.B) {
120120
enc := NewEncoder(io.Discard)
121121
for pb.Next() {
122122
if err := enc.Encode(&codeStruct); err != nil {
123-
b.Fatal("Encode:", err)
123+
b.Fatalf("Encode error: %v", err)
124124
}
125125
if _, err := Marshal(dummy); err == nil {
126-
b.Fatal("expect an error here")
126+
b.Fatal("Marshal error: got nil, want non-nil")
127127
}
128128
}
129129
})
@@ -140,7 +140,7 @@ func BenchmarkCodeMarshal(b *testing.B) {
140140
b.RunParallel(func(pb *testing.PB) {
141141
for pb.Next() {
142142
if _, err := Marshal(&codeStruct); err != nil {
143-
b.Fatal("Marshal:", err)
143+
b.Fatalf("Marshal error: %v", err)
144144
}
145145
}
146146
})
@@ -166,10 +166,10 @@ func BenchmarkCodeMarshalError(b *testing.B) {
166166
b.RunParallel(func(pb *testing.PB) {
167167
for pb.Next() {
168168
if _, err := Marshal(&codeStruct); err != nil {
169-
b.Fatal("Marshal:", err)
169+
b.Fatalf("Marshal error: %v", err)
170170
}
171171
if _, err := Marshal(dummy); err == nil {
172-
b.Fatal("expect an error here")
172+
b.Fatal("Marshal error: got nil, want non-nil")
173173
}
174174
}
175175
})
@@ -188,7 +188,7 @@ func benchMarshalBytes(n int) func(*testing.B) {
188188
return func(b *testing.B) {
189189
for i := 0; i < b.N; i++ {
190190
if _, err := Marshal(v); err != nil {
191-
b.Fatal("Marshal:", err)
191+
b.Fatalf("Marshal error: %v", err)
192192
}
193193
}
194194
}
@@ -215,10 +215,10 @@ func benchMarshalBytesError(n int) func(*testing.B) {
215215
return func(b *testing.B) {
216216
for i := 0; i < b.N; i++ {
217217
if _, err := Marshal(v); err != nil {
218-
b.Fatal("Marshal:", err)
218+
b.Fatalf("Marshal error: %v", err)
219219
}
220220
if _, err := Marshal(dummy); err == nil {
221-
b.Fatal("expect an error here")
221+
b.Fatal("Marshal error: got nil, want non-nil")
222222
}
223223
}
224224
}
@@ -280,7 +280,7 @@ func BenchmarkCodeDecoder(b *testing.B) {
280280
buf.WriteByte('\n')
281281
buf.WriteByte('\n')
282282
if err := dec.Decode(&r); err != nil {
283-
b.Fatal("Decode:", err)
283+
b.Fatalf("Decode error: %v", err)
284284
}
285285
}
286286
})
@@ -297,7 +297,7 @@ func BenchmarkUnicodeDecoder(b *testing.B) {
297297
b.ResetTimer()
298298
for i := 0; i < b.N; i++ {
299299
if err := dec.Decode(&out); err != nil {
300-
b.Fatal("Decode:", err)
300+
b.Fatalf("Decode error: %v", err)
301301
}
302302
r.Seek(0, 0)
303303
}
@@ -311,7 +311,7 @@ func BenchmarkDecoderStream(b *testing.B) {
311311
buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n")
312312
var x any
313313
if err := dec.Decode(&x); err != nil {
314-
b.Fatal("Decode:", err)
314+
b.Fatalf("Decode error: %v", err)
315315
}
316316
ones := strings.Repeat(" 1\n", 300000) + "\n\n\n"
317317
b.StartTimer()
@@ -320,8 +320,11 @@ func BenchmarkDecoderStream(b *testing.B) {
320320
buf.WriteString(ones)
321321
}
322322
x = nil
323-
if err := dec.Decode(&x); err != nil || x != 1.0 {
324-
b.Fatalf("Decode: %v after %d", err, i)
323+
switch err := dec.Decode(&x); {
324+
case err != nil:
325+
b.Fatalf("Decode error: %v", err)
326+
case x != 1.0:
327+
b.Fatalf("Decode: got %v want 1.0", i)
325328
}
326329
}
327330
}
@@ -337,7 +340,7 @@ func BenchmarkCodeUnmarshal(b *testing.B) {
337340
for pb.Next() {
338341
var r codeResponse
339342
if err := Unmarshal(codeJSON, &r); err != nil {
340-
b.Fatal("Unmarshal:", err)
343+
b.Fatalf("Unmarshal error: %v", err)
341344
}
342345
}
343346
})
@@ -355,7 +358,7 @@ func BenchmarkCodeUnmarshalReuse(b *testing.B) {
355358
var r codeResponse
356359
for pb.Next() {
357360
if err := Unmarshal(codeJSON, &r); err != nil {
358-
b.Fatal("Unmarshal:", err)
361+
b.Fatalf("Unmarshal error: %v", err)
359362
}
360363
}
361364
})
@@ -369,7 +372,7 @@ func BenchmarkUnmarshalString(b *testing.B) {
369372
var s string
370373
for pb.Next() {
371374
if err := Unmarshal(data, &s); err != nil {
372-
b.Fatal("Unmarshal:", err)
375+
b.Fatalf("Unmarshal error: %v", err)
373376
}
374377
}
375378
})
@@ -382,7 +385,7 @@ func BenchmarkUnmarshalFloat64(b *testing.B) {
382385
var f float64
383386
for pb.Next() {
384387
if err := Unmarshal(data, &f); err != nil {
385-
b.Fatal("Unmarshal:", err)
388+
b.Fatalf("Unmarshal error: %v", err)
386389
}
387390
}
388391
})
@@ -395,7 +398,7 @@ func BenchmarkUnmarshalInt64(b *testing.B) {
395398
var x int64
396399
for pb.Next() {
397400
if err := Unmarshal(data, &x); err != nil {
398-
b.Fatal("Unmarshal:", err)
401+
b.Fatalf("Unmarshal error: %v", err)
399402
}
400403
}
401404
})
@@ -408,7 +411,7 @@ func BenchmarkUnmarshalMap(b *testing.B) {
408411
x := make(map[string]string, 3)
409412
for pb.Next() {
410413
if err := Unmarshal(data, &x); err != nil {
411-
b.Fatal("Unmarshal:", err)
414+
b.Fatalf("Unmarshal error: %v", err)
412415
}
413416
}
414417
})
@@ -421,7 +424,7 @@ func BenchmarkIssue10335(b *testing.B) {
421424
var s struct{}
422425
for pb.Next() {
423426
if err := Unmarshal(j, &s); err != nil {
424-
b.Fatal(err)
427+
b.Fatalf("Unmarshal error: %v", err)
425428
}
426429
}
427430
})
@@ -437,7 +440,7 @@ func BenchmarkIssue34127(b *testing.B) {
437440
b.RunParallel(func(pb *testing.PB) {
438441
for pb.Next() {
439442
if _, err := Marshal(&j); err != nil {
440-
b.Fatal(err)
443+
b.Fatalf("Marshal error: %v", err)
441444
}
442445
}
443446
})
@@ -450,7 +453,7 @@ func BenchmarkUnmapped(b *testing.B) {
450453
var s struct{}
451454
for pb.Next() {
452455
if err := Unmarshal(j, &s); err != nil {
453-
b.Fatal(err)
456+
b.Fatalf("Unmarshal error: %v", err)
454457
}
455458
}
456459
})
@@ -533,7 +536,7 @@ func BenchmarkEncodeMarshaler(b *testing.B) {
533536

534537
for pb.Next() {
535538
if err := enc.Encode(&m); err != nil {
536-
b.Fatal("Encode:", err)
539+
b.Fatalf("Encode error: %v", err)
537540
}
538541
}
539542
})
@@ -548,7 +551,7 @@ func BenchmarkEncoderEncode(b *testing.B) {
548551
b.RunParallel(func(pb *testing.PB) {
549552
for pb.Next() {
550553
if err := NewEncoder(io.Discard).Encode(v); err != nil {
551-
b.Fatal(err)
554+
b.Fatalf("Encode error: %v", err)
552555
}
553556
}
554557
})

0 commit comments

Comments
 (0)