Skip to content

StringAssertions - Count regex matches #1868

@ITaluone

Description

@ITaluone

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions