Skip to content
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

Added an EqualStringConstraint #4847

Closed
wants to merge 1 commit into from

Conversation

manfred-brands
Copy link
Member

@manfred-brands manfred-brands commented Sep 29, 2024

@stevenaw This is for discussion, to show issues we face.

Contributes to #53

I added a specific constraint for comparing strings (EqualStringConstraint)
The idea was that I then could remove the string specific modifiers (IgnoreCase, IgnoreWhiteSpace) from the actual EqualConstraint

This didn't work because the EqualConstraint modifiers, don't just apply to the type at hand, but to members of (nested) collection and dictionaries. Whether it applies is determined at runtime.

See the CollectionEqualsTests:

[TestCaseSource(nameof(IgnoreCaseData))]
public void HonorsIgnoreCase(IEnumerable expected, IEnumerable actual)
{
    Assert.That(expected, Is.EqualTo(actual).IgnoreCase);
}

The only thing this EqualsStringConstraint brings us is that this constraint doesn't allow passing in an Within constraint.
So the one test for that:

Assert.Throws<NotSupportedException>(() => Assert.That("abc", new EqualStringConstraint("abcd").Within(1)));

This no longer compiles. Hereby moving an error from runtime to compile time.

Fixes #4875

Copy link
Member

@OsirisTerje OsirisTerje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@manfred-brands
Copy link
Member Author

Due to merge conflicts superseded by combined PR #4882

@manfred-brands manfred-brands deleted the stringConstraint branch December 27, 2024 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String constraints should not allow using Within
2 participants