mirror of https://github.com/stretchr/testify.git
Allow Contains and NotContains to check slices and arrays
parent
da775f0337
commit
ab1fdba54e
|
@ -399,14 +399,48 @@ func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contains asserts that the specified string contains the specified substring.
|
// containsElement try loop over the list check if the list includes the element.
|
||||||
|
// return (false, false) if impossible.
|
||||||
|
// return (true, false) if element was not found.
|
||||||
|
// return (true, true) if element was found.
|
||||||
|
func includeElement(list interface{}, element interface{}) (ok, found bool) {
|
||||||
|
|
||||||
|
listValue := reflect.ValueOf(list)
|
||||||
|
elementValue := reflect.ValueOf(element)
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
ok = false
|
||||||
|
found = false
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if reflect.TypeOf(list).Kind() == reflect.String {
|
||||||
|
return true, strings.Contains(listValue.String(), elementValue.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < listValue.Len(); i++ {
|
||||||
|
if listValue.Index(i).Interface() == element {
|
||||||
|
return true, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, false
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains asserts that the specified string or list(array, slice...) 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"], "World", "But ["Hello", "World"] does contain 'World'")
|
||||||
//
|
//
|
||||||
// Returns whether the assertion was successful (true) or not (false).
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
func Contains(t TestingT, s, contains string, msgAndArgs ...interface{}) bool {
|
func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
|
||||||
if !strings.Contains(s, contains) {
|
ok, found := includeElement(s, contains)
|
||||||
|
if !ok {
|
||||||
|
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", s), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", s, contains), msgAndArgs...)
|
return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", s, contains), msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,14 +448,20 @@ func Contains(t TestingT, s, contains string, msgAndArgs ...interface{}) bool {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotContains asserts that the specified string does NOT contain the specified substring.
|
// NotContains asserts that the specified string or list(array, slice...) 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).
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
func NotContains(t TestingT, s, contains string, msgAndArgs ...interface{}) bool {
|
func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
|
||||||
if strings.Contains(s, contains) {
|
ok, found := includeElement(s, contains)
|
||||||
|
if !ok {
|
||||||
|
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", s), msgAndArgs...)
|
||||||
|
}
|
||||||
|
if found {
|
||||||
return Fail(t, fmt.Sprintf("\"%s\" should not contain \"%s\"", s, contains), msgAndArgs...)
|
return Fail(t, fmt.Sprintf("\"%s\" should not contain \"%s\"", s, contains), msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -200,6 +200,7 @@ func TestNotEqual(t *testing.T) {
|
||||||
func TestContains(t *testing.T) {
|
func TestContains(t *testing.T) {
|
||||||
|
|
||||||
mockT := new(testing.T)
|
mockT := new(testing.T)
|
||||||
|
list := []string{"Foo", "Bar"}
|
||||||
|
|
||||||
if !Contains(mockT, "Hello World", "Hello") {
|
if !Contains(mockT, "Hello World", "Hello") {
|
||||||
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
|
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
|
||||||
|
@ -208,11 +209,19 @@ func TestContains(t *testing.T) {
|
||||||
t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
|
t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !Contains(mockT, list, "Bar") {
|
||||||
|
t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Bar\"")
|
||||||
|
}
|
||||||
|
if Contains(mockT, list, "Salut") {
|
||||||
|
t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotContains(t *testing.T) {
|
func TestNotContains(t *testing.T) {
|
||||||
|
|
||||||
mockT := new(testing.T)
|
mockT := new(testing.T)
|
||||||
|
list := []string{"Foo", "Bar"}
|
||||||
|
|
||||||
if !NotContains(mockT, "Hello World", "Hello!") {
|
if !NotContains(mockT, "Hello World", "Hello!") {
|
||||||
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
|
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
|
||||||
|
@ -221,6 +230,56 @@ func TestNotContains(t *testing.T) {
|
||||||
t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
|
t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !NotContains(mockT, list, "Foo!") {
|
||||||
|
t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
|
||||||
|
}
|
||||||
|
if NotContains(mockT, list, "Foo") {
|
||||||
|
t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_includeElement(t *testing.T) {
|
||||||
|
|
||||||
|
list1 := []string{"Foo", "Bar"}
|
||||||
|
list2 := []int{1, 2}
|
||||||
|
|
||||||
|
ok, found := includeElement("Hello World", "World")
|
||||||
|
True(t, ok)
|
||||||
|
True(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list1, "Foo")
|
||||||
|
True(t, ok)
|
||||||
|
True(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list1, "Bar")
|
||||||
|
True(t, ok)
|
||||||
|
True(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list2, 1)
|
||||||
|
True(t, ok)
|
||||||
|
True(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list2, 2)
|
||||||
|
True(t, ok)
|
||||||
|
True(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list1, "Foo!")
|
||||||
|
True(t, ok)
|
||||||
|
False(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list2, 3)
|
||||||
|
True(t, ok)
|
||||||
|
False(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(list2, "1")
|
||||||
|
True(t, ok)
|
||||||
|
False(t, found)
|
||||||
|
|
||||||
|
ok, found = includeElement(1433, "1")
|
||||||
|
False(t, ok)
|
||||||
|
False(t, found)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDidPanic(t *testing.T) {
|
func TestDidPanic(t *testing.T) {
|
||||||
|
|
|
@ -73,9 +73,9 @@
|
||||||
//
|
//
|
||||||
// assert.IsType(t, expectedObject, actualObject [, message [, format-args]])
|
// assert.IsType(t, expectedObject, actualObject [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.Contains(t, string, substring [, message [, format-args]])
|
// assert.Contains(t, stringOrSlice, substringOrElement [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.NotContains(t, string, substring [, message [, format-args]])
|
// assert.NotContains(t, stringOrSlice, substringOrElement [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.Panics(t, func(){
|
// assert.Panics(t, func(){
|
||||||
//
|
//
|
||||||
|
@ -126,9 +126,9 @@
|
||||||
//
|
//
|
||||||
// assert.IsType(expectedObject, actualObject [, message [, format-args]])
|
// assert.IsType(expectedObject, actualObject [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.Contains(string, substring [, message [, format-args]])
|
// assert.Contains(stringOrSlice, substringOrElement [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.NotContains(string, substring [, message [, format-args]])
|
// assert.NotContains(stringOrSlice, substringOrElement [, message [, format-args]])
|
||||||
//
|
//
|
||||||
// assert.Panics(func(){
|
// assert.Panics(func(){
|
||||||
//
|
//
|
||||||
|
|
|
@ -129,7 +129,7 @@ func (a *Assertions) NotEqual(expected, actual interface{}, msgAndArgs ...interf
|
||||||
// assert.Contains("Hello World", "World", "But 'Hello World' does contain 'World'")
|
// assert.Contains("Hello World", "World", "But 'Hello World' does contain 'World'")
|
||||||
//
|
//
|
||||||
// Returns whether the assertion was successful (true) or not (false).
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
func (a *Assertions) Contains(s, contains string, msgAndArgs ...interface{}) bool {
|
func (a *Assertions) Contains(s, contains interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return Contains(a.t, s, contains, msgAndArgs...)
|
return Contains(a.t, s, contains, msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ func (a *Assertions) Contains(s, contains string, msgAndArgs ...interface{}) boo
|
||||||
// assert.NotContains("Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
|
// assert.NotContains("Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
|
||||||
//
|
//
|
||||||
// Returns whether the assertion was successful (true) or not (false).
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
func (a *Assertions) NotContains(s, contains string, msgAndArgs ...interface{}) bool {
|
func (a *Assertions) NotContains(s, contains interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return NotContains(a.t, s, contains, msgAndArgs...)
|
return NotContains(a.t, s, contains, msgAndArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,6 +148,7 @@ func TestNotEqualWrapper(t *testing.T) {
|
||||||
func TestContainsWrapper(t *testing.T) {
|
func TestContainsWrapper(t *testing.T) {
|
||||||
|
|
||||||
assert := New(new(testing.T))
|
assert := New(new(testing.T))
|
||||||
|
list := []string{"Foo", "Bar"}
|
||||||
|
|
||||||
if !assert.Contains("Hello World", "Hello") {
|
if !assert.Contains("Hello World", "Hello") {
|
||||||
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
|
t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
|
||||||
|
@ -156,11 +157,19 @@ func TestContainsWrapper(t *testing.T) {
|
||||||
t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
|
t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !assert.Contains(list, "Foo") {
|
||||||
|
t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
|
||||||
|
}
|
||||||
|
if assert.Contains(list, "Salut") {
|
||||||
|
t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotContainsWrapper(t *testing.T) {
|
func TestNotContainsWrapper(t *testing.T) {
|
||||||
|
|
||||||
assert := New(new(testing.T))
|
assert := New(new(testing.T))
|
||||||
|
list := []string{"Foo", "Bar"}
|
||||||
|
|
||||||
if !assert.NotContains("Hello World", "Hello!") {
|
if !assert.NotContains("Hello World", "Hello!") {
|
||||||
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
|
t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
|
||||||
|
@ -169,6 +178,13 @@ func TestNotContainsWrapper(t *testing.T) {
|
||||||
t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
|
t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !assert.NotContains(list, "Foo!") {
|
||||||
|
t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
|
||||||
|
}
|
||||||
|
if assert.NotContains(list, "Foo") {
|
||||||
|
t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDidPanicWrapper(t *testing.T) {
|
func TestDidPanicWrapper(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue