From d3556349ddf8a77ac312bc67df048c8fa8bdb0f8 Mon Sep 17 00:00:00 2001 From: Victor Kryukov Date: Tue, 18 Nov 2014 15:26:31 -0800 Subject: [PATCH] Fix #99: Contains doesn't work for complex types --- assert/assertions.go | 2 +- assert/assertions_test.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 5de1ab1..8af3157 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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 } } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index e5f1237..bb63be2 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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) {