Skip to content

Commit d956abb

Browse files
committed
tests
1 parent 288e73d commit d956abb

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

deep_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
v2 "github.com/go-test/deep/test/v2"
1515
)
1616

17+
const (
18+
multilineTestError = `wrong diff:
19+
got: %q
20+
expected: %q`
21+
)
22+
1723
func TestString(t *testing.T) {
1824
diff := deep.Equal("foo", "foo")
1925
if len(diff) > 0 {
@@ -1184,6 +1190,43 @@ func TestTimeUnexported(t *testing.T) {
11841190
}
11851191
}
11861192

1193+
func TestTimePrecision(t *testing.T) {
1194+
restoreTimePrecision := deep.TimePrecision
1195+
t.Cleanup(func() { deep.TimePrecision = restoreTimePrecision })
1196+
1197+
deep.TimePrecision = 1 * time.Microsecond
1198+
1199+
now := time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)
1200+
later := now.Add(123 * time.Nanosecond)
1201+
1202+
shouldBeEqual(t, deep.Equal(now, later))
1203+
1204+
d1 := 1 * time.Microsecond
1205+
d2 := d1 + 123*time.Nanosecond
1206+
1207+
shouldBeEqual(t, deep.Equal(d1, d2))
1208+
1209+
restoreCompareUnexportedFields := deep.CompareUnexportedFields
1210+
t.Cleanup(func() { deep.CompareUnexportedFields = restoreCompareUnexportedFields })
1211+
1212+
deep.CompareUnexportedFields = true
1213+
1214+
type S struct {
1215+
t time.Time
1216+
d time.Duration
1217+
}
1218+
1219+
s1 := &S{t: now, d: d1}
1220+
s2 := &S{t: later, d: d2}
1221+
1222+
// Since we cannot call `Truncate` on the unexported fields,
1223+
// we will show differences here.
1224+
shouldBeDiffs(t, deep.Equal(s1, s2),
1225+
"t.wall: 0 != 123",
1226+
"d: 1000 != 1123",
1227+
)
1228+
}
1229+
11871230
func TestInterface(t *testing.T) {
11881231
a := map[string]interface{}{
11891232
"foo": map[string]string{
@@ -1613,3 +1656,51 @@ func TestNilPointersAreZero(t *testing.T) {
16131656
t.Fatalf("expected 1 diff, got %d: %s", len(diff), diff)
16141657
}
16151658
}
1659+
1660+
func reportWrongDiff(t testing.TB, got, expect string) {
1661+
t.Helper()
1662+
1663+
output := fmt.Sprintf("wrong diff: got %q, expected %q", got, expect)
1664+
if len(output) > 120 {
1665+
output = fmt.Sprintf(multilineTestError, got, expect)
1666+
}
1667+
1668+
t.Error(output)
1669+
}
1670+
1671+
func shouldBeDiffs(t testing.TB, diff []string, head string, tail ...string) {
1672+
t.Helper()
1673+
1674+
if len(diff) == 0 {
1675+
t.Fatal("no diffs")
1676+
}
1677+
1678+
if len(diff) != len(tail)+1 {
1679+
t.Log("diff:", diff)
1680+
t.Errorf("wrong number of diffs: got %d, expected %d", len(diff), len(tail)+1)
1681+
}
1682+
1683+
if expect := head; diff[0] != expect {
1684+
reportWrongDiff(t, diff[0], expect)
1685+
}
1686+
1687+
for i, expect := range tail {
1688+
if i+1 >= len(diff) {
1689+
t.Errorf("missing diff: %q", expect)
1690+
continue
1691+
}
1692+
1693+
if got := diff[i+1]; got != expect {
1694+
reportWrongDiff(t, got, expect)
1695+
}
1696+
1697+
}
1698+
}
1699+
1700+
func shouldBeEqual(t testing.TB, diff []string) {
1701+
t.Helper()
1702+
1703+
if len(diff) > 0 {
1704+
t.Errorf("should be equal: %q", diff)
1705+
}
1706+
}

0 commit comments

Comments
 (0)