Skip to content

Commit

Permalink
Merge pull request stretchr#165 from tuvistavie/master
Browse files Browse the repository at this point in the history
Support map keys for Contains/NotContains assertion.
  • Loading branch information
ernesto-jimenez committed Nov 1, 2015
2 parents 9a4de4d + 1e710e5 commit 40b02b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
16 changes: 14 additions & 2 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ func includeElement(list interface{}, element interface{}) (ok, found bool) {
return true, strings.Contains(listValue.String(), elementValue.String())
}

if reflect.TypeOf(list).Kind() == reflect.Map {
mapKeys := listValue.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if ObjectsAreEqual(mapKeys[i].Interface(), element) {
return true, true
}
}
return true, false
}

for i := 0; i < listValue.Len(); i++ {
if ObjectsAreEqual(listValue.Index(i).Interface(), element) {
return true, true
Expand All @@ -508,11 +518,12 @@ func includeElement(list interface{}, element interface{}) (ok, found bool) {

}

// Contains asserts that the specified string or list(array, slice...) contains the
// Contains asserts that the specified string, list(array, slice...) or map contains the
// specified substring or element.
//
// assert.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'")
// assert.Contains(t, ["Hello", "World"], "World", "But ["Hello", "World"] does contain 'World'")
// assert.Contains(t, {"Hello": "World"}, "Hello", "But {'Hello': 'World'} does contain 'Hello'")
//
// Returns whether the assertion was successful (true) or not (false).
func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
Expand All @@ -529,11 +540,12 @@ func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bo

}

// NotContains asserts that the specified string or list(array, slice...) does NOT contain the
// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the
// specified substring or element.
//
// assert.NotContains(t, "Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
// assert.NotContains(t, ["Hello", "World"], "Earth", "But ['Hello', 'World'] does NOT contain 'Earth'")
// assert.NotContains(t, {"Hello": "World"}, "Earth", "But {'Hello': 'World'} does NOT contain 'Earth'")
//
// Returns whether the assertion was successful (true) or not (false).
func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
Expand Down
28 changes: 26 additions & 2 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func TestContains(t *testing.T) {
{"g", "h"},
{"j", "k"},
}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}

if !Contains(mockT, "Hello World", "Hello") {
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
Expand All @@ -350,12 +351,22 @@ func TestContains(t *testing.T) {
if Contains(mockT, complexList, &A{"g", "e"}) {
t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
}
if Contains(mockT, complexList, &A{"g", "e"}) {
t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
}
if !Contains(mockT, simpleMap, "Foo") {
t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
}
if Contains(mockT, simpleMap, "Bar") {
t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
}
}

func TestNotContains(t *testing.T) {

mockT := new(testing.T)
list := []string{"Foo", "Bar"}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}

if !NotContains(mockT, "Hello World", "Hello!") {
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
Expand All @@ -370,13 +381,19 @@ func TestNotContains(t *testing.T) {
if NotContains(mockT, list, "Foo") {
t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
}

if NotContains(mockT, simpleMap, "Foo") {
t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")
}
if !NotContains(mockT, simpleMap, "Bar") {
t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")
}
}

func Test_includeElement(t *testing.T) {

list1 := []string{"Foo", "Bar"}
list2 := []int{1, 2}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}

ok, found := includeElement("Hello World", "World")
True(t, ok)
Expand Down Expand Up @@ -410,10 +427,17 @@ func Test_includeElement(t *testing.T) {
True(t, ok)
False(t, found)

ok, found = includeElement(simpleMap, "Foo")
True(t, ok)
True(t, found)

ok, found = includeElement(simpleMap, "Bar")
True(t, ok)
False(t, found)

ok, found = includeElement(1433, "1")
False(t, ok)
False(t, found)

}

func TestCondition(t *testing.T) {
Expand Down

0 comments on commit 40b02b9

Please sign in to comment.