Skip to content

Commit 55ce24e

Browse files
Improve docs and add a test
1 parent 1a1ed62 commit 55ce24e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

deep.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ const (
6161
// pass it to Equal; if you do, it does nothing.
6262
FLAG_NONE byte = iota
6363

64-
// FLAG_IGNORE_SLICE_ORDER causes Equal to ignore slice order and, instead,
65-
// compare value counts. For example, []{1, 2} and []{2, 1} are equal
66-
// because each value has the same count. But []{1, 2, 2} and []{1, 2}
67-
// are not equal because the first slice has two occurrences of value 2.
64+
// FLAG_IGNORE_SLICE_ORDER causes Equal to ignore slice order so that
65+
// []int{1, 2} and []int{2, 1} are equal. Only slices of primitive scalars
66+
// like numbers and strings are supported. Slices of complex types,
67+
// like []T where T is a struct, are undefined because Equal does not
68+
// recurse into the slice value when this flag is enabled.
6869
FLAG_IGNORE_SLICE_ORDER
6970
)
7071

deep_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,3 +1561,23 @@ func TestSliceOrderString(t *testing.T) {
15611561
t.Errorf("got %s, expected '(unordered) slice[]=x: value count: 0 != 1'", diff[2])
15621562
}
15631563
}
1564+
1565+
func TestSliceOrderStruct(t *testing.T) {
1566+
// https://github.com/go-test/deep/issues/28
1567+
// This is NOT supported but Go is so wonderful that it just happens to work.
1568+
// But again: not supported. So if this test starts to fail or be a problem,
1569+
// it can and should be removed becuase the docs say it's not supported.
1570+
type T struct{ i int }
1571+
a := []T{
1572+
{i: 1},
1573+
{i: 2},
1574+
}
1575+
b := []T{
1576+
{i: 2},
1577+
{i: 1},
1578+
}
1579+
diff := deep.Equal(a, b, deep.FLAG_IGNORE_SLICE_ORDER)
1580+
if len(diff) != 0 {
1581+
t.Fatalf("expected 0 diff, got %d: %s", len(diff), diff)
1582+
}
1583+
}

0 commit comments

Comments
 (0)