New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow IEqualityComparer in string equality assertion #2339
Comments
For the provided example you should be able to use the existing: logLines.Should().MatchEquivalentOf(expected); Note: the With v6 you can e.g. write these extension method. public static class StringAssertionsExtensions
{
[CustomAssertion]
public static AndConstraint<TAssertions> Be<TAssertions>(this StringAssertions<TAssertions> parent,
string expected, IEqualityComparer<string> comparer, string because = "", params object[] becauseArgs)
where TAssertions : StringAssertions<TAssertions>
{
Execute.Assertion
.ForCondition(comparer.Equals(parent.Subject, expected))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:string} to be {0}{reason}, but found {1}.", expected, parent.Subject);
return new AndConstraint<TAssertions>((TAssertions)parent);
}
[CustomAssertion]
public static AndConstraint<TAssertions> Be2<TAssertions>(this StringAssertions<TAssertions> parent,
string expected, IEqualityComparer<string> comparer, string because = "", params object[] becauseArgs)
where TAssertions : StringAssertions<TAssertions>
{
((object)parent.Subject).Should().Be(expected, comparer, because, becauseArgs);
return new AndConstraint<TAssertions>((TAssertions)parent);
}
[CustomAssertion]
public static AndConstraint<TAssertions> BeEquivalentTo<TAssertions>(this StringAssertions<TAssertions> parent,
string expected, IEqualityComparer<string> comparer, string because = "", params object[] becauseArgs)
where TAssertions : StringAssertions<TAssertions>
{
((object)parent.Subject).Should().BeEquivalentTo(expected, opt => opt.Using(comparer), because, becauseArgs);
return new AndConstraint<TAssertions>((TAssertions)parent);
}
} That on failure give these failure messages, respectively.
The downside of these is that they won't provide as pretty formatted failure messages as Back to the API suggestion. I think we might also want look at the entire With the changes in #2296 it should be easy (famous last words) to built an adapter for Related discussion for generic collections: #699 |
I also think we should rename |
@jnyrup Thanks for looking into this.
This doesn't actually work, due to the bug here. But more importantly,
The only way I can think of to address that is to backtrack on match failure, which would be expensive if not vectorized. |
In C# terms |
#2307 is working on improving the failure message when comparing strings. |
I'm dreaming of somethings like this var subject = """
Du
bist
süß
""";
subject.Should().BeEquivalentTo("du bist süss", opt => opt
.IgnoringLeadingWhitespace()
.IgnoringTrailingWhitespace()
.IgnoringNewlines()
.IgnoringCase()
.Using(new CultureInfo("de-DE"))); |
I meant figuratively |
Make it a separate issue. I'd love that. |
I would be happy to work on such extensions for Should I create a separate issue to discuss the possible options and API structure first? |
Yes! |
Background and motivation
I'm trying to assert two strings are equal using a custom comparer. The code below works, though it would be nice if the cast to
object
wouldn't be necessary andBe()
could be used instead ofBeEquivalentTo()
.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?
No
The text was updated successfully, but these errors were encountered: