Closed SirSova closed 11 months ago
Like #1486 this use case can be rewritten using slices.ContainsFunc
, but this time by using it at 2 depth levels.
See https://go.dev/play/p/Y6d7YuRjRu1
expectedValues := []fooBar{ // the expected list that contains only fields that we wanna check, can be a struct type generated just for a specific test
{foo: "Three", bar: 3},
{foo: "One", bar: 1},
}
assert.True(t,
slices.ContainsFunc(
[]fooBarBuzz{ // it's suppose to be generated and have lots of fields
{foo: "One", bar: 1, buzz: time.Now()},
{foo: "Two", bar: 2, buzz: time.Now().Add(1 * time.Second)},
{foo: "Three", bar: 3, buzz: time.Now().Add(10 * time.Second)},
{foo: "Four", bar: 4, buzz: time.Now().Add(1 * time.Minute)},
},
func(actual fooBarBuzz) bool {
return slices.ContainsFunc(expectedValues, func(expected fooBar) bool {
return actual.foo == expected.foo && actual.bar == expected.bar
})
},
),
"should contain subset {foo: \"One\", bar: 1} and {foo: \"Three\", bar: 3}",
)
Like #1486 I think that this is more readable than introducing a new method in Testify. So I'm rejecting this feature request.
In addition to another issue: https://github.com/stretchr/testify/issues/1486, I suggest a new(more complex) assertion similar to
Subset
/ElementsMatch
. So, I face often situations when I need to check the elements, but not by all the properties (partially). For example, the actual objects contain too many fields, but I need to check 2-3 of them in each element. This is what I came to right now:It can be 2 different functions, for Subset + for all elements pass the condition (ElementsMatch alternative)