You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.7 KiB
78 lines
2.7 KiB
using FluentAssertions.Collections;
|
|
using FluentAssertions.Execution;
|
|
|
|
namespace Recyclarr.TestLibrary.FluentAssertions;
|
|
|
|
public static class FluentAssertionsExtensions
|
|
{
|
|
public static AndWhichConstraint<TAssertions, string> ContainRegexMatch<TCollection, TAssertions>(
|
|
this StringCollectionAssertions<TCollection, TAssertions> assert,
|
|
string regexPattern,
|
|
string because = "",
|
|
params object[] becauseArgs
|
|
)
|
|
where TCollection : IEnumerable<string>
|
|
where TAssertions : StringCollectionAssertions<TCollection, TAssertions>
|
|
{
|
|
bool ContainsRegexMatch()
|
|
{
|
|
return assert.Subject.Any(item =>
|
|
{
|
|
using var scope = new AssertionScope();
|
|
item.Should().MatchRegex(regexPattern);
|
|
return scope.Discard().Length == 0;
|
|
});
|
|
}
|
|
|
|
Execute.Assertion
|
|
.BecauseOf(because, becauseArgs)
|
|
.ForCondition(ContainsRegexMatch())
|
|
.FailWith("Expected {context:collection} {0} to contain a regex match of {1}{reason}.", assert.Subject,
|
|
regexPattern);
|
|
|
|
var matched = assert.Subject.Where(item =>
|
|
{
|
|
using var scope = new AssertionScope();
|
|
item.Should().MatchRegex(regexPattern);
|
|
return scope.Discard().Length == 0;
|
|
});
|
|
|
|
return new AndWhichConstraint<TAssertions, string>((TAssertions) assert, matched);
|
|
}
|
|
|
|
public static AndWhichConstraint<TAssertions, string> NotContainRegexMatch<TCollection, TAssertions>(
|
|
this StringCollectionAssertions<TCollection, TAssertions> assert,
|
|
string regexPattern,
|
|
string because = "",
|
|
params object[] becauseArgs
|
|
)
|
|
where TCollection : IEnumerable<string>
|
|
where TAssertions : StringCollectionAssertions<TCollection, TAssertions>
|
|
{
|
|
bool NotContainsRegexMatch()
|
|
{
|
|
return assert.Subject.Any(item =>
|
|
{
|
|
using var scope = new AssertionScope();
|
|
item.Should().NotMatchRegex(regexPattern);
|
|
return scope.Discard().Length == 0;
|
|
});
|
|
}
|
|
|
|
Execute.Assertion
|
|
.BecauseOf(because, becauseArgs)
|
|
.ForCondition(NotContainsRegexMatch())
|
|
.FailWith("Expected {context:collection} {0} to not contain a regex match of {1}{reason}.", assert.Subject,
|
|
regexPattern);
|
|
|
|
var matched = assert.Subject.Where(item =>
|
|
{
|
|
using var scope = new AssertionScope();
|
|
item.Should().NotMatchRegex(regexPattern);
|
|
return scope.Discard().Length == 0;
|
|
});
|
|
|
|
return new AndWhichConstraint<TAssertions, string>((TAssertions) assert, matched);
|
|
}
|
|
}
|