-
-
Notifications
You must be signed in to change notification settings - Fork 113
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Problem Statement
It would be nice if GenerateAssertion could generate assertions similar to HasSingleItemAssertion, where the assertion is "terminal" and returns the matched item. Currently this requires building the entire assertion class by hand.
Proposed Solution
Something like this:
public class UnitTest1
{
[Test]
public async Task TestWithMemberData()
{
string[] items = new[] { "food", "bard" };
var matched = await Assert.That(items).ContainsMessage("foo", exact: false);
// Do other stuff with `matched`, whose value will be "food".
}
}
public static partial class Assertions
{
[EditorBrowsable(EditorBrowsableState.Never)]
[GenerateAssertion(ExpectationMessage = "to contain message '{needle}'")]
// New type: AssertionResult<T>
public static AssertionResult<string> ContainsMessage(this IEnumerable<string> strings, string needle, bool exact = true)
{
var result = strings.FirstOrDefault(x => exact ? x == needle : x.Contains(needle));
if (result is not null)
return AssertionResult.Passed(result); // New method: Produces an AssertionResult<T>
else
// AssertionResult would have an implicit conversion to AssertionResult<T>,
// allowing this to work? The conversion from AssertionResult => AssertionResult<T>
// should throw if AssertionResult.Passed because there's no suitable `Result` to use?
return AssertionResult.Failed(exact
? $"{needle} not found (exact matching)"
: $"{needle} not found (loose matching)");
}
}Alternatives Considered
Build the Assertion by hand
Feature Category
Assertions
How important is this feature to you?
Nice to have - would improve my experience
Additional Context
No response
Contribution
- I'm willing to submit a pull request for this feature
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request