Skip to content

Include extension methods for OccurrenceConstraint that reads more fluently #2045

@IT-VBFK

Description

@IT-VBFK

I was wondering if it would make sense to include an extension method for Exactly.Times like this:

myString.Should().Contain("something", 4.Times()); // Equivalent to Exactly.Times(4)

I think it reads a bit better than the existing Exactly.Times(4), but perhaps this has already been considered?

I understand there might be some confusion around AtLeast, AtMost etc., but that might be fixed like:

myString.Should().Contain("something", 4.TimesOrMore()); // Equivalent to AtLeast.Times(4) and MoreThan(3)
myString.Should().Contain("something", 4.TimesOrLess()); // Equivalent to AtMost.Times(4) and LessThan(5)

There seems to be some redundancy in with AtLeast/MoreThan and AtMost/LessThan, but they might be useful in scenarios I've never had.

I know creating such extensions is pretty straightforward:

public static class FluentOccurrenceConstraintExtensions
{
    public static OccurrenceConstraint Times(this int times)
    {
        return Exactly.Times(times);
    }

    public static OccurrenceConstraint TimesOrLess(this int times)
    {
        return AtLeast.Times(times);
    }

    public static OccurrenceConstraint TimesOrMore(this int times)
    {
        return AtMost.Times(times);
    }
}

Originally posted by @siewers in #2001

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved, it can be implemented

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions