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

StringAssertions - Count regex matches #1868

Closed
ITaluone opened this issue Mar 28, 2022 · 7 comments · Fixed by #1869
Closed

StringAssertions - Count regex matches #1868

ITaluone opened this issue Mar 28, 2022 · 7 comments · Fixed by #1869

Comments

@ITaluone
Copy link
Contributor

ITaluone commented Mar 28, 2022

Description

I am using the FA string assertions MatchRegex() to check strings. But because the regex pattern have to be really generic to match all future strings, it turned out to be a test case that will mostly ever pass (and therefore is useless).

Now if I could restrict the assertion to a predictable count of regex matches, this could be (for me) a better solution.

Complete minimal example reproducing the issue

class A
{
    public string LongLongString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est (100,00€)Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore (104,23€)magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
}

[Fact]
public void Test_Regex_matches()
{
    // Arrange
    var a = new A();
    
    // Act / Assert
    // Matching e.g. "(100,00€)"
    a.LongLongString.Should().MatchRegex(@"(.*\(\d+,\d{2}€\))+", Exactly.Twice());
}

Expected behavior:

I think an overload like the string.Contains("bla", AtLeast.Once()) should be good, so:
a.LongLongString().Should().MatchRegex("bla", Exactly.Twice());

Actual behavior:

Cannot be done (except I am missing some way to achieve this)

Versions

  • Which version of Fluent Assertions are you using? -> 6.4.0
  • Which .NET runtime and version are you targeting? .NET Core 3.1

Additional Information

Unfortunately string.Contains("bla", AtLeast.Once()) and similar assertions can not be done, because I am trying to match price values inside the text.

@jnyrup
Copy link
Member

jnyrup commented Mar 28, 2022

You can achieve it using the Match() assertion, but it's not pretty and the failure message will be poor.

a.LongLongString.Should().Match(subject => Regex.Matches(subject, @"(.*\(\d+,\d{2}€\))+").Count == 2);

@ITaluone
Copy link
Contributor Author

Ok, thank you

I will take that for the moment to be able to go further in development. Helps a lot :)

@IT-VBFK
Copy link
Contributor

IT-VBFK commented Mar 28, 2022

If you have a small hint for me how to start I would give it a shot

@jnyrup
Copy link
Member

jnyrup commented Mar 28, 2022

@IT-VBFK Have a look at how the Contain overload that takes an OccurrenceConstraint works.
To calculate the number of occurrences to pass to ForConstraint use Regex.Matches().Count instead of CountSubstring.

@IT-VBFK
Copy link
Contributor

IT-VBFK commented Mar 28, 2022

Ok will try to implement this :)

Should this be an extra overload for both of the MatchRegex() methods?

@jnyrup
Copy link
Member

jnyrup commented Mar 28, 2022

Should this be an extra overload for both of the MatchRegex() methods?

Yes please.

@jnyrup
Copy link
Member

jnyrup commented Mar 28, 2022

If it's of any help Contain(string, OccurrenceConstraint) was added in #1145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants