Skip to content

Commit

Permalink
Merge pull request #382 from GlenDC/feat-test-subset
Browse files Browse the repository at this point in the history
add Subset and NotSubset assertions
  • Loading branch information
ernesto-jimenez authored May 28, 2017
2 parents e68c5c5 + 3a59a58 commit aa10ab5
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
86 changes: 86 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,92 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{})

}

// Subset asserts that the specified list(array, slice...) contains all
// elements given in the specified subset(array, slice...).
//
// assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
if subset == nil {
return true // we consider nil to be equal to the nil set
}

subsetValue := reflect.ValueOf(subset)
defer func() {
if e := recover(); e != nil {
ok = false
}
}()

listKind := reflect.TypeOf(list).Kind()
subsetKind := reflect.TypeOf(subset).Kind()

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

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

for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
if !found {
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", list, element), msgAndArgs...)
}
}

return true
}

// NotSubset asserts that the specified list(array, slice...) contains not all
// elements given in the specified subset(array, slice...).
//
// assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
//
// Returns whether the assertion was successful (true) or not (false).
func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
if subset == nil {
return false // we consider nil to be equal to the nil set
}

subsetValue := reflect.ValueOf(subset)
defer func() {
if e := recover(); e != nil {
ok = false
}
}()

listKind := reflect.TypeOf(list).Kind()
subsetKind := reflect.TypeOf(subset).Kind()

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

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

for i := 0; i < subsetValue.Len(); i++ {
element := subsetValue.Index(i).Interface()
ok, found := includeElement(list, element)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
}
if !found {
return true
}
}

return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
}

// Condition uses a Comparison to assert a complex condition.
func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
result := comp()
Expand Down
60 changes: 60 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,66 @@ func TestNotContains(t *testing.T) {
}
}

func TestSubset(t *testing.T) {
mockT := new(testing.T)

if !Subset(mockT, []int{1, 2, 3}, nil) {
t.Error("Subset should return true: given subset is nil")
}
if !Subset(mockT, []int{1, 2, 3}, []int{}) {
t.Error("Subset should return true: any set contains the nil set")
}
if !Subset(mockT, []int{1, 2, 3}, []int{1, 2}) {
t.Error("Subset should return true: [1, 2, 3] contains [1, 2]")
}
if !Subset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
t.Error("Subset should return true: [1, 2, 3] contains [1, 2, 3]")
}
if !Subset(mockT, []string{"hello", "world"}, []string{"hello"}) {
t.Error("Subset should return true: [\"hello\", \"world\"] contains [\"hello\"]")
}

if Subset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
t.Error("Subset should return false: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
}
if Subset(mockT, []int{1, 2, 3}, []int{4, 5}) {
t.Error("Subset should return false: [1, 2, 3] does not contain [4, 5]")
}
if Subset(mockT, []int{1, 2, 3}, []int{1, 5}) {
t.Error("Subset should return false: [1, 2, 3] does not contain [1, 5]")
}
}

func TestNotSubset(t *testing.T) {
mockT := new(testing.T)

if NotSubset(mockT, []int{1, 2, 3}, nil) {
t.Error("NotSubset should return false: given subset is nil")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{}) {
t.Error("NotSubset should return false: any set contains the nil set")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2}) {
t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2]")
}
if NotSubset(mockT, []int{1, 2, 3}, []int{1, 2, 3}) {
t.Error("NotSubset should return false: [1, 2, 3] contains [1, 2, 3]")
}
if NotSubset(mockT, []string{"hello", "world"}, []string{"hello"}) {
t.Error("NotSubset should return false: [\"hello\", \"world\"] contains [\"hello\"]")
}

if !NotSubset(mockT, []string{"hello", "world"}, []string{"hello", "testify"}) {
t.Error("NotSubset should return true: [\"hello\", \"world\"] does not contain [\"hello\", \"testify\"]")
}
if !NotSubset(mockT, []int{1, 2, 3}, []int{4, 5}) {
t.Error("NotSubset should return true: [1, 2, 3] does not contain [4, 5]")
}
if !NotSubset(mockT, []int{1, 2, 3}, []int{1, 5}) {
t.Error("NotSubset should return true: [1, 2, 3] does not contain [1, 5]")
}
}

func Test_includeElement(t *testing.T) {

list1 := []string{"Foo", "Bar"}
Expand Down

0 comments on commit aa10ab5

Please sign in to comment.