Merge pull request #100 from victorkryukov/issue-99-contains-complex-types

Fix #99: Contains doesn't work for complex types
pull/103/head
Mat Ryer 2014-11-18 15:38:11 -08:00
commit 8ce79b9f0b
2 changed files with 17 additions and 2 deletions

View File

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

View File

@ -214,10 +214,20 @@ func TestNotEqual(t *testing.T) {
}
}
type A struct {
Name, Value string
}
func TestContains(t *testing.T) {
mockT := new(testing.T)
list := []string{"Foo", "Bar"}
complexList := []*A{
&A{"b", "c"},
&A{"d", "e"},
&A{"g", "h"},
&A{"j", "k"},
}
if !Contains(mockT, "Hello World", "Hello") {
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
@ -232,7 +242,12 @@ func TestContains(t *testing.T) {
if Contains(mockT, list, "Salut") {
t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
}
if !Contains(mockT, complexList, &A{"g", "h"}) {
t.Error("Contains should return true: complexList contains {\"g\", \"h\"}")
}
if Contains(mockT, complexList, &A{"g", "e"}) {
t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")
}
}
func TestNotContains(t *testing.T) {