@@ -816,7 +816,6 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok
816
816
return true // we consider nil to be equal to the nil set
817
817
}
818
818
819
- subsetValue := reflect .ValueOf (subset )
820
819
defer func () {
821
820
if e := recover (); e != nil {
822
821
ok = false
@@ -826,14 +825,32 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok
826
825
listKind := reflect .TypeOf (list ).Kind ()
827
826
subsetKind := reflect .TypeOf (subset ).Kind ()
828
827
829
- if listKind != reflect .Array && listKind != reflect .Slice {
828
+ if listKind != reflect .Array && listKind != reflect .Slice && listKind != reflect . Map {
830
829
return Fail (t , fmt .Sprintf ("%q has an unsupported type %s" , list , listKind ), msgAndArgs ... )
831
830
}
832
831
833
- if subsetKind != reflect .Array && subsetKind != reflect .Slice {
832
+ if subsetKind != reflect .Array && subsetKind != reflect .Slice && listKind != reflect . Map {
834
833
return Fail (t , fmt .Sprintf ("%q has an unsupported type %s" , subset , subsetKind ), msgAndArgs ... )
835
834
}
836
835
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
+
837
854
for i := 0 ; i < subsetValue .Len (); i ++ {
838
855
element := subsetValue .Index (i ).Interface ()
839
856
ok , found := containsElement (list , element )
@@ -860,7 +877,6 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
860
877
return Fail (t , "nil is the empty set which is a subset of every set" , msgAndArgs ... )
861
878
}
862
879
863
- subsetValue := reflect .ValueOf (subset )
864
880
defer func () {
865
881
if e := recover (); e != nil {
866
882
ok = false
@@ -870,14 +886,32 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
870
886
listKind := reflect .TypeOf (list ).Kind ()
871
887
subsetKind := reflect .TypeOf (subset ).Kind ()
872
888
873
- if listKind != reflect .Array && listKind != reflect .Slice {
889
+ if listKind != reflect .Array && listKind != reflect .Slice && listKind != reflect . Map {
874
890
return Fail (t , fmt .Sprintf ("%q has an unsupported type %s" , list , listKind ), msgAndArgs ... )
875
891
}
876
892
877
- if subsetKind != reflect .Array && subsetKind != reflect .Slice {
893
+ if subsetKind != reflect .Array && subsetKind != reflect .Slice && listKind != reflect . Map {
878
894
return Fail (t , fmt .Sprintf ("%q has an unsupported type %s" , subset , subsetKind ), msgAndArgs ... )
879
895
}
880
896
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
+
881
915
for i := 0 ; i < subsetValue .Len (); i ++ {
882
916
element := subsetValue .Index (i ).Interface ()
883
917
ok , found := containsElement (list , element )
0 commit comments