Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,14 +1013,14 @@ func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok
return true // we consider nil to be equal to the nil set
}

listKind := reflect.TypeOf(list).Kind()
listKind := kindOfList(list)
if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
return Fail(t, unsupportedListTypeMessage(list, listKind), msgAndArgs...)
}

subsetKind := reflect.TypeOf(subset).Kind()
subsetKind := kindOfList(subset)
if subsetKind != reflect.Array && subsetKind != reflect.Slice && subsetKind != reflect.Map {
return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
return Fail(t, unsupportedListTypeMessage(subset, subsetKind), msgAndArgs...)
}

if subsetKind == reflect.Map && listKind == reflect.Map {
Expand Down Expand Up @@ -1081,14 +1081,14 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...)
}

listKind := reflect.TypeOf(list).Kind()
listKind := kindOfList(list)
if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
return Fail(t, fmt.Sprintf("%#v has an unsupported type %s", list, listKind), msgAndArgs...)
return Fail(t, unsupportedListTypeMessage(list, listKind), msgAndArgs...)
}

subsetKind := reflect.TypeOf(subset).Kind()
subsetKind := kindOfList(subset)
if subsetKind != reflect.Array && subsetKind != reflect.Slice && subsetKind != reflect.Map {
return Fail(t, fmt.Sprintf("%#v has an unsupported type %s", subset, subsetKind), msgAndArgs...)
return Fail(t, unsupportedListTypeMessage(subset, subsetKind), msgAndArgs...)
}

if subsetKind == reflect.Map && listKind == reflect.Map {
Expand Down Expand Up @@ -1132,6 +1132,21 @@ func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...)
}

func kindOfList(list interface{}) reflect.Kind {
listType := reflect.TypeOf(list)
if listType == nil {
return reflect.Invalid
}
return listType.Kind()
}

func unsupportedListTypeMessage(list interface{}, kind reflect.Kind) string {
if kind == reflect.Invalid {
return fmt.Sprintf("%#v has an unsupported type <nil>", list)
}
return fmt.Sprintf("%#v has an unsupported type %s", list, kind)
}

// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified
// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
// the number of appearances of each of them in both lists should match.
Expand Down
38 changes: 38 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,44 @@ func TestNotSubsetNil(t *testing.T) {
}
}

func TestSubsetNotSubsetWithNilList(t *testing.T) {
t.Parallel()

cases := []struct {
name string
assertion func(TestingT, interface{}, interface{}, ...interface{}) bool
}{
{"Subset", Subset},
{"NotSubset", NotSubset},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
mockT := new(mockTestingT)
var result bool

funcDidPanic, panicValue, _ := didPanic(func() {
result = tc.assertion(mockT, nil, []int{1})
})

False(t, funcDidPanic, "%s should not panic: %#v", tc.name, panicValue)
False(t, result)
Contains(t, mockT.errorString(), "<nil> has an unsupported type <nil>")
})
}
}

func TestSubsetFormatsUnsupportedNonStringListCorrectly(t *testing.T) {
t.Parallel()

mockT := new(mockTestingT)
Subset(mockT, true, []bool{true})

errStr := mockT.errorString()
NotContains(t, errStr, "%!q", "Subset error message should not contain %%q formatting artifacts")
Contains(t, errStr, "true has an unsupported type bool")
}

func Test_containsElement(t *testing.T) {
t.Parallel()

Expand Down