Skip to content

Commit fc52959

Browse files
committed
Use custom byte dumper for equality errors.
1 parent 51bf270 commit fc52959

3 files changed

Lines changed: 75 additions & 15 deletions

File tree

pkg/check/equal.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,30 +267,32 @@ func equalError(want, have any, opts ...Option) *notice.Notice {
267267
}
268268

269269
ops := DefaultOptions(opts...)
270-
msg := notice.New("expected values to be equal").
271-
Trail(ops.Trail)
272-
273-
if b, ok := want.(byte); ok && isPrintableChar(b) {
274-
_ = msg.Want("%#v ('%s')", want, string(b))
275-
} else {
276-
_ = msg.Want("%s", ops.Dumper.Any(want))
270+
if _, ok := ops.Dumper.Dumpers[typByte]; !ok {
271+
ops.Dumper.Dumpers[typByte] = dumpByte
277272
}
278273

279-
if b, ok := have.(byte); ok && isPrintableChar(b) {
280-
_ = msg.Have("%#v ('%s')", have, string(b))
281-
} else {
282-
_ = msg.Have("%s", ops.Dumper.Any(have))
283-
}
274+
msg := notice.New("expected values to be equal").
275+
Trail(ops.Trail).
276+
Want("%s", ops.Dumper.Any(want)).
277+
Have("%s", ops.Dumper.Any(have))
284278

285279
if wTyp != "" {
286-
// nolint: govet
287280
_ = msg.
288281
Append("want type", "%s", wTyp).
289282
Append("have type", "%s", hTyp)
290283
}
291284
return msg
292285
}
293286

294-
func dumpByte(dnp dump.Dump, lvl int, val reflect.Value) string {
295-
return ""
287+
// dumpByte is a custom bumper for bytes.
288+
func dumpByte(dmp dump.Dump, lvl int, val reflect.Value) string {
289+
v := val.Interface().(byte) // nolint: forcetypeassert
290+
var str string
291+
if isPrintableChar(v) {
292+
str = fmt.Sprintf("0x%02x ('%s')", v, string(v))
293+
} else {
294+
str = fmt.Sprintf("0x%02x", v)
295+
}
296+
prn := dump.NewPrinter(dmp)
297+
return prn.Tab(dmp.Indent + lvl).Write(str).String()
296298
}

pkg/check/equal_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package check
33
import (
44
"errors"
55
"fmt"
6+
"reflect"
67
"testing"
78
"time"
89

910
"github.com/ctx42/testing/internal/affirm"
1011
"github.com/ctx42/testing/internal/cases"
1112
"github.com/ctx42/testing/internal/types"
13+
"github.com/ctx42/testing/pkg/dump"
1214
"github.com/ctx42/testing/pkg/must"
1315
)
1416

@@ -1297,6 +1299,23 @@ func Test_equalError(t *testing.T) {
12971299
affirm.Equal(t, wMsg, err.Error())
12981300
})
12991301

1302+
t.Run("does not override already set byte dumper", func(t *testing.T) {
1303+
// --- Given ---
1304+
w := byte('A')
1305+
h := byte('B')
1306+
fn := func(_ dump.Dump, _ int, _ reflect.Value) string { return "abc" }
1307+
ops := DefaultOptions(WithDumper(dump.WithDumper(byte(0), fn)))
1308+
1309+
// --- When ---
1310+
err := equalError(w, h, WithOptions(ops))
1311+
1312+
// --- Then ---
1313+
wMsg := "expected values to be equal:\n" +
1314+
" want: abc\n" +
1315+
" have: abc"
1316+
affirm.Equal(t, wMsg, err.Error())
1317+
})
1318+
13001319
t.Run("different types", func(t *testing.T) {
13011320
// --- Given ---
13021321
w := byte('A')
@@ -1315,3 +1334,41 @@ func Test_equalError(t *testing.T) {
13151334
affirm.Equal(t, wMsg, err.Error())
13161335
})
13171336
}
1337+
1338+
func Test_dumpByte(t *testing.T) {
1339+
t.Run("success printable", func(t *testing.T) {
1340+
// --- Given ---
1341+
dmp := dump.New()
1342+
val := reflect.ValueOf(byte(42))
1343+
1344+
// --- When ---
1345+
have := dumpByte(dmp, 0, val)
1346+
1347+
// --- Then ---
1348+
affirm.Equal(t, "0x2a ('*')", have)
1349+
})
1350+
1351+
t.Run("success not printable", func(t *testing.T) {
1352+
// --- Given ---
1353+
dmp := dump.New()
1354+
val := reflect.ValueOf(byte(1))
1355+
1356+
// --- When ---
1357+
have := dumpByte(dmp, 0, val)
1358+
1359+
// --- Then ---
1360+
affirm.Equal(t, "0x01", have)
1361+
})
1362+
1363+
t.Run("uses indent and level", func(t *testing.T) {
1364+
// --- Given ---
1365+
dmp := dump.New(dump.WithIndent(2))
1366+
val := reflect.ValueOf(byte(1))
1367+
1368+
// --- When ---
1369+
have := dumpByte(dmp, 1, val)
1370+
1371+
// --- Then ---
1372+
affirm.Equal(t, " 0x01", have)
1373+
})
1374+
}

pkg/check/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
typTime = reflect.TypeOf(time.Time{})
2121
typTimeLoc = reflect.TypeOf(time.Location{})
2222
typTimeLocPtr = reflect.TypeOf(&time.Location{})
23+
typByte = reflect.TypeOf(byte(0))
2324
)
2425

2526
// typeString returns type of the value as a string.

0 commit comments

Comments
 (0)