📖 7 min read (~ 1400 words).

Collection

Asserting Slices And Maps

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 7 functionalities.

Contains

Contains asserts that the specified string, list(array, slice…) or map contains the specified substring or element.

Examples
	assertions.Contains(t, "Hello World", "World")
	assertions.Contains(t, []string{"Hello", "World"}, "World")
	assertions.Contains(t, map[string]string{"Hello": "World"}, "Hello")
	success: []string{"A","B"}, "A"
	failure: []string{"A","B"}, "C"

ElementsMatch

ElementsMatch asserts that the specified listA(array, slice…) is equal to specified listB(array, slice…) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.

Examples
	assertions.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])
	success: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
	failure: []int{1, 2, 3}, []int{1, 2, 4}

Len

Len asserts that the specified object has specific length.

Len also fails if the object has a type that len() does not accept.

The asserted object can be a string, a slice, a map, an array or a channel.

See also reflect.Len.

Examples
	assertions.Len(t, mySlice, 3)
	assertions.Len(t, myString, 4)
	assertions.Len(t, myMap, 5)
	success: []string{"A","B"}, 2
	failure: []string{"A","B"}, 1
SignatureUsage
assertions.Len(t T, object any, length int, msgAndArgs ...any) boolinternal implementation

Source: github.com/go-openapi/testify/v2/internal/assertions#Len

Maintainer Note The implementation is based on [reflect.Len]. The potential panic is handled with recover. A better approach could be to check for the [reflect.Type] before calling [reflect.Len].

Note (proposals) this does not currently support iterators, or collection objects that have a Len() method.

NotContains

NotContains asserts that the specified string, list(array, slice…) or map does NOT contain the specified substring or element.

Examples
	assertions.NotContains(t, "Hello World", "Earth")
	assertions.NotContains(t, ["Hello", "World"], "Earth")
	assertions.NotContains(t, {"Hello": "World"}, "Earth")
	success: []string{"A","B"}, "C"
	failure: []string{"A","B"}, "B"

NotElementsMatch

NotElementsMatch asserts that the specified listA(array, slice…) is NOT equal to specified listB(array, slice…) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should not match. This is an inverse of ElementsMatch.

Examples
	assertions.NotElementsMatch(t, [1, 1, 2, 3], [1, 1, 2, 3]) -> false
	assertions.NotElementsMatch(t, [1, 1, 2, 3], [1, 2, 3]) -> true
	assertions.NotElementsMatch(t, [1, 2, 3], [1, 2, 4]) -> true
	success: []int{1, 2, 3}, []int{1, 2, 4}
	failure: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}

NotSubset

NotSubset asserts that the list (array, slice, or map) does NOT contain all elements given in the subset (array, slice, or map). Map elements are key-value pairs unless compared with an array or slice where only the map key is evaluated.

Examples
	assertions.NotSubset(t, [1, 3, 4], [1, 2])
	assertions.NotSubset(t, {"x": 1, "y": 2}, {"z": 3})
	assertions.NotSubset(t, [1, 3, 4], {1: "one", 2: "two"})
	assertions.NotSubset(t, {"x": 1, "y": 2}, ["z"])
	success: []int{1, 2, 3}, []int{4, 5}
	failure: []int{1, 2, 3}, []int{1, 2}

Subset

Subset asserts that the list (array, slice, or map) contains all elements given in the subset (array, slice, or map).

Map elements are key-value pairs unless compared with an array or slice where only the map key is evaluated.

Examples
	assertions.Subset(t, [1, 2, 3], [1, 2])
	assertions.Subset(t, {"x": 1, "y": 2}, {"x": 1})
	assertions.Subset(t, [1, 2, 3], {1: "one", 2: "two"})
	assertions.Subset(t, {"x": 1, "y": 2}, ["x"])
	success: []int{1, 2, 3}, []int{1, 2}
	failure: []int{1, 2, 3}, []int{4, 5}


Generated with github.com/go-openapi/testify/v2/codegen