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
New string-specific options for string equivalency assertions #2364
Comments
StringAssertions.BeEquivalentTo
StringAssertions.BeEquivalentTo
I like 1 and 3, but I don't like 2. Instead, we also need to add these options to the general |
@dennisdoomen : I adjusted the API proposal to exclude point 2. I am not sure, I understand what you mean with "to add these options to the general |
Yes, and all the others that take a |
@dennisdoomen : I would split it into two pull requests:
Is this okay for you? |
Yeah, that sounds like a great plan. |
@jnyrup I'm fine marking this as "api approved" |
Love this proposal :) |
@dennisdoomen : As requested in the #2372 I also added the two API changes for |
I've been thinking about if this needs some more upfront designing here? BeEquivalentTo(string expected, ...)
ContainEquivalentOf(string expected, ...)
ContainEquivalentOf(string expected, OccurrenceConstraint occurrenceConstraint, ...)
EndWithEquivalentOf(string expected, ...)
MatchEquivalentOf(string wildcardPattern, ...)
NotBeEquivalentTo(string unexpected, ...)
NotContainEquivalentOf(string unexpected, ...)
NotEndWithEquivalentOf(string unexpected, ...)
NotMatchEquivalentOf(string wildcardPattern, ...)
NotStartWithEquivalentOf(string unexpected, ...)
StartWithEquivalentOf(string expected, ...)
Doesn't one currently has do some extra work to make the general new { Value = "abc" }.Should().BeEquivalentTo(new { Value = "ABC" }, opt => opt
.Using<string>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation))
.WhenTypeIs<string>()); Which with the options overload to new { Value = "abc" }.Should().BeEquivalentTo(new { Value = "ABC" }, opt => opt
.Using<string>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation, o => o.IgnoringNewlines()))
.WhenTypeIs<string>()); Or are you saying that one should have the same/similar "abc".Should().BeEquivalentTo("ABC", opt => opt.IgnoringNewlines())
new { Value = "abc" }.Should().BeEquivalentTo(new { Value = "ABC" }, opt => opt.IgnoringNewlines()) |
@jnyrup :
The idea is exactly this, that I add the additional methods ( |
@jnyrup : Do you mean, that I should also add the corresponding overload with |
It was meant as an open question. So instead of looking solely at I also just recalled that with #1284 we have support for passing a custom new { Value = "abc" }.Should().BeEquivalentTo(new { Value = "ABC" }, opt => opt
.Using(StringComparer.OrdinalIgnoreCase)); |
I often had the case, that I needed to compare strings case sensitive or case insensitive and for me it would read better to specify this explicitely than to rely on the difference between "foo".Should().Be("FOO", StringComparer.OrdinalIgnoreCase); vs.
In the latter example I often got review comments, why this works, as it is not immediately clear, that "BeEquivalentTo" is case-insensitive. So for me adding the |
Oof, this "enhancement" is blowing out of proportions. Happy that @vbreuss is taken the lead here. |
@dennisdoomen , @jnyrup So I would add the suggested additional overloads also to
Is this okay? |
I might be opening up an earlier decision (my brain has been very pre-occupied with conferences and some personal stuff), but what if you just added an overload to the methods @jnyrup listed that takes an |
The difficulty here is, that I can't get access to this comparer. The Using-Method just adds an But I will give your idea a try and create a pull request! |
@dennisdoomen If you want me to continue this approach, I would add the same overloads to the other string assertion methods. |
I love it! What do you think @jnyrup ? |
Primarily looked at the tests and they look great! One thing I have never given a thought so far is whether the new options should be applied to only the subject, only the expectation or both 🤔 |
I would vote for "only subject". Why one would construct an expectation where he adds leading/trailing whitespaces manually? That makes no sense to me. |
StringAssertions.BeEquivalentTo
I updated the description of this issue to reflect the latest suggestion, which is according to the suggested implementation in #2413. |
Why not just assume it applies to both? I can't imagine a scenario where you ignore newlines on the subject, but do care about them in the expectation. That just doesn't make sense. |
What I had in mind was something like this, but I realize that was just an example where it doesn't need to ignore newlines from expectation, but also doesn't hurt to do so. var subject = GetMessage(); // returns "some\r\n value" due to some line breaking logic
subject.Should().BeEquivalentTo("some value", opt => opt.IgnoringNewlines()); // in this particular test I don't care about the line breaks, but that the message contains "some value". So I'm good with applying it to both 👍 |
Is this proposal okay now? I implemented it in #2413, but currently the issue doesn't yet have the "api-approved" label... |
In the draft PR If we add |
I think so, but then I would have to also change |
I agree, but as this has nothing to do with the config changes, I implemented it in a separate pull request (#2436), to keep the changes in #2413 more focused. |
I changed it accordingly in #2413! |
Some more thinking on the behavior of #1247 is about considering "\n" and My example in #2339 (comment) was going one step further and considered The current implementation removes newlines, such that
is is considered equivalent to
Any proper line-breaking algorithm would insert a hyphen when breaking a line inside a word
What do you think? |
I think, that when I read BTW, when I look at the example in #1247, it could also be fixed with |
I'm not entirely sure what you mean to say with the last example (about the hyphen). |
That I don't think |
So the question is, does |
What about adding two options:
|
I honestly don't know 🤷♂️ |
Background and motivation
Initial discussion in #2339 and #2064
I would like to add some overloads to most string equivalency assertions to tackle various use cases (e.g. ignore white space, ignore casing, ignore newline, etc.) with the goal to tackle most existing use cases and to allow for extensibility of the provided mechanism. In order to achieve this, I would add an overload with a
Func<EquivalencyAssertionOptions<string>, EquivalencyAssertionOptions<string>>
to specify additional options on the string comparison like ignoring leading or trailing white space or newlines to the following string assertions:This also allows specifying an
IEqualityComparer<string>
as requested in #2339 via the.Using
method on the assertion options.API Proposal
Example for
BeEquivalentTo
:and add the following methods to the
EquivalencyAssertionOptions<T>
class:API Usage
Alternative Designs
No response
Risks
As I would support arbitrary
IEqualityComparer<string>
, I can no longer use theIndexOfFirstMismatch
extension method, but would have to rewrite it, e.g. as follows:The difference is, that I have to slice the strings and get substrings, instead of comparing char by char. This could have a performance impact...
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?
Yes, please assign this issue to me.
The text was updated successfully, but these errors were encountered: