StringAssertions - Count regex matches
See original GitHub issueDescription
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.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How do I count the number of matches by a regex?
The MatchCollection instance returned from a call to the Matches method on RegEx will give you the count of the number of matches....
Read more >How to Count the Number of Matches for a Regex?
In this tutorial, we'll take a look at how to use regular expressions to count the number of matches in some text.
Read more >Strings
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit...
Read more >MatchCollection.Count Property (System.Text. ...
The following example uses the Count property to determine whether the call to the Regex.Matches(String, String) method found any matches.
Read more >Regex.Count Method (System.Text.RegularExpressions)
Count (String, String, RegexOptions, TimeSpan). Searches an input string for all occurrences of a regular expression and returns the number of matches.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok, thank you
I will take that for the moment to be able to go further in development. Helps a lot 😃
If it’s of any help
Contain(string, OccurrenceConstraint)
was added in #1145