Skip to content

Commit

Permalink
Merge pull request #170 from hpidcock/unhashable-samecontents
Browse files Browse the repository at this point in the history
Allow SameContents to work with unhashable contents.
  • Loading branch information
hpidcock authored Jul 14, 2023
2 parents 0e4da91 + 334b536 commit 9af10d6
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions checkers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ type sameContents struct {

// SameContents checks that the obtained slice contains all the values (and
// same number of values) of the expected slice and vice versa, without respect
// to order or duplicates. Uses DeepEquals on mapped contents to compare.
// to order or duplicates. Uses DeepEquals on contents to compare. Content types
// do not need to be hashable, but must satisfy reflect.DeepEquals.
var SameContents gc.Checker = &sameContents{
&gc.CheckerInfo{Name: "SameContents", Params: []string{"obtained", "expected"}},
}
Expand Down Expand Up @@ -190,16 +191,29 @@ func (checker *sameContents) Check(params []interface{}, names []string) (result
return false, ""
}

// spin up maps with the entries as keys and the counts as values
mob := make(map[interface{}]int, length)
mexp := make(map[interface{}]int, length)
// left is the expected
left := make([]any, 0, length)
// right is the obtained
right := make([]any, 0, length)

for i := 0; i < length; i++ {
mexp[reflect.Indirect(vexp.Index(i)).Interface()]++
mob[reflect.Indirect(vob.Index(i)).Interface()]++
left = append(left, reflect.Indirect(vexp.Index(i)).Interface())
right = append(right, reflect.Indirect(vob.Index(i)).Interface())
}

return reflect.DeepEqual(mob, mexp), ""
outer:
for i := 0; i < len(left); i++ {
for j, r := range right {
if reflect.DeepEqual(left[i], r) {
left = append(left[:i], left[i+1:]...)
right = append(right[:j], right[j+1:]...)
i--
continue outer
}
}
}

return len(left) == 0 && len(right) == 0, ""
}

type errorIsNilChecker struct {
Expand All @@ -211,8 +225,7 @@ type errorIsNilChecker struct {
//
// For example:
//
// c.Assert(err, ErrorIsNil)
//
// c.Assert(err, ErrorIsNil)
var ErrorIsNil gc.Checker = &errorIsNilChecker{
&gc.CheckerInfo{Name: "ErrorIsNil", Params: []string{"value"}},
}
Expand Down

0 comments on commit 9af10d6

Please sign in to comment.