Skip to content

Commit

Permalink
Improve docs and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-nichter committed Dec 9, 2022
1 parent 1a1ed62 commit 55ce24e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions deep.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ const (
// pass it to Equal; if you do, it does nothing.
FLAG_NONE byte = iota

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

Expand Down
20 changes: 20 additions & 0 deletions deep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,3 +1561,23 @@ func TestSliceOrderString(t *testing.T) {
t.Errorf("got %s, expected '(unordered) slice[]=x: value count: 0 != 1'", diff[2])
}
}

func TestSliceOrderStruct(t *testing.T) {
// https://github.com/go-test/deep/issues/28
// This is NOT supported but Go is so wonderful that it just happens to work.
// But again: not supported. So if this test starts to fail or be a problem,
// it can and should be removed becuase the docs say it's not supported.
type T struct{ i int }
a := []T{
{i: 1},
{i: 2},
}
b := []T{
{i: 2},
{i: 1},
}
diff := deep.Equal(a, b, deep.FLAG_IGNORE_SLICE_ORDER)
if len(diff) != 0 {
t.Fatalf("expected 0 diff, got %d: %s", len(diff), diff)
}
}

0 comments on commit 55ce24e

Please sign in to comment.