Skip to content

Commit 66eef0e

Browse files
authored
fix: assert.MapSubset (or just support maps in assert.Subset) (#1178)
* WIP: added map key value check in subset * upgraded subset & notsubset to check handle maps
1 parent 2fab6df commit 66eef0e

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

assert/assertions.go

+40-6
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok
816816
return true // we consider nil to be equal to the nil set
817817
}
818818

819-
subsetValue := reflect.ValueOf(subset)
820819
defer func() {
821820
if e := recover(); e != nil {
822821
ok = false
@@ -826,14 +825,32 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok
826825
listKind := reflect.TypeOf(list).Kind()
827826
subsetKind := reflect.TypeOf(subset).Kind()
828827

829-
if listKind != reflect.Array && listKind != reflect.Slice {
828+
if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
830829
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
831830
}
832831

833-
if subsetKind != reflect.Array && subsetKind != reflect.Slice {
832+
if subsetKind != reflect.Array && subsetKind != reflect.Slice && listKind != reflect.Map {
834833
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
835834
}
836835

836+
subsetValue := reflect.ValueOf(subset)
837+
if subsetKind == reflect.Map && listKind == reflect.Map {
838+
listValue := reflect.ValueOf(list)
839+
subsetKeys := subsetValue.MapKeys()
840+
841+
for i := 0; i < len(subsetKeys); i++ {
842+
subsetKey := subsetKeys[i]
843+
subsetElement := subsetValue.MapIndex(subsetKey).Interface()
844+
listElement := listValue.MapIndex(subsetKey).Interface()
845+
846+
if !ObjectsAreEqual(subsetElement, listElement) {
847+
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", list, subsetElement), msgAndArgs...)
848+
}
849+
}
850+
851+
return true
852+
}
853+
837854
for i := 0; i < subsetValue.Len(); i++ {
838855
element := subsetValue.Index(i).Interface()
839856
ok, found := containsElement(list, element)
@@ -860,7 +877,6 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
860877
return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...)
861878
}
862879

863-
subsetValue := reflect.ValueOf(subset)
864880
defer func() {
865881
if e := recover(); e != nil {
866882
ok = false
@@ -870,14 +886,32 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
870886
listKind := reflect.TypeOf(list).Kind()
871887
subsetKind := reflect.TypeOf(subset).Kind()
872888

873-
if listKind != reflect.Array && listKind != reflect.Slice {
889+
if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
874890
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
875891
}
876892

877-
if subsetKind != reflect.Array && subsetKind != reflect.Slice {
893+
if subsetKind != reflect.Array && subsetKind != reflect.Slice && listKind != reflect.Map {
878894
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
879895
}
880896

897+
subsetValue := reflect.ValueOf(subset)
898+
if subsetKind == reflect.Map && listKind == reflect.Map {
899+
listValue := reflect.ValueOf(list)
900+
subsetKeys := subsetValue.MapKeys()
901+
902+
for i := 0; i < len(subsetKeys); i++ {
903+
subsetKey := subsetKeys[i]
904+
subsetElement := subsetValue.MapIndex(subsetKey).Interface()
905+
listElement := listValue.MapIndex(subsetKey).Interface()
906+
907+
if !ObjectsAreEqual(subsetElement, listElement) {
908+
return true
909+
}
910+
}
911+
912+
return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
913+
}
914+
881915
for i := 0; i < subsetValue.Len(); i++ {
882916
element := subsetValue.Index(i).Interface()
883917
ok, found := containsElement(list, element)

assert/assertions_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,27 @@ func TestSubsetNotSubset(t *testing.T) {
685685
{[]int{1, 2, 3}, []int{1, 2}, true, "[1, 2, 3] contains [1, 2]"},
686686
{[]int{1, 2, 3}, []int{1, 2, 3}, true, "[1, 2, 3] contains [1, 2, 3"},
687687
{[]string{"hello", "world"}, []string{"hello"}, true, "[\"hello\", \"world\"] contains [\"hello\"]"},
688+
{map[string]string{
689+
"a": "x",
690+
"c": "z",
691+
"b": "y",
692+
}, map[string]string{
693+
"a": "x",
694+
"b": "y",
695+
}, true, `{ "a": "x", "b": "y", "c": "z"} contains { "a": "x", "b": "y"}`},
688696

689697
// cases that are expected not to contain
690698
{[]string{"hello", "world"}, []string{"hello", "testify"}, false, "[\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]"},
691699
{[]int{1, 2, 3}, []int{4, 5}, false, "[1, 2, 3] does not contain [4, 5"},
692700
{[]int{1, 2, 3}, []int{1, 5}, false, "[1, 2, 3] does not contain [1, 5]"},
701+
{map[string]string{
702+
"a": "x",
703+
"c": "z",
704+
"b": "y",
705+
}, map[string]string{
706+
"a": "x",
707+
"b": "z",
708+
}, false, `{ "a": "x", "b": "y", "c": "z"} does not contain { "a": "x", "b": "z"}`},
693709
}
694710

695711
for _, c := range cases {

0 commit comments

Comments
 (0)