Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Source/aweXpect/Helpers/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace aweXpect.Helpers;

internal static class ThrowHelper
{
public static ArgumentException EmptyCollection()
=> new("You have to provide at least one expected value!");
}
2 changes: 1 addition & 1 deletion Source/aweXpect/That/Chars/ThatChar.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ConstraintResult IsMetBy(char actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect/That/Chars/ThatNullableChar.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ConstraintResult IsMetBy(char? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect/That/DateOnlys/ThatDateOnly.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ConstraintResult IsMetBy(DateOnly actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ConstraintResult IsMetBy(DateOnly? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public ConstraintResult IsMetBy(DateTimeOffset actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ConstraintResult IsMetBy(DateTimeOffset? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect/That/DateTimes/ThatDateTime.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public ConstraintResult IsMetBy(DateTime actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ConstraintResult IsMetBy(DateTime? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
18 changes: 17 additions & 1 deletion Source/aweXpect/That/Enums/ThatEnum.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ private sealed class IsOneOfConstraint<TEnum>(string it, ExpectationGrammars gra
public ConstraintResult IsMetBy(TEnum actual)
{
Actual = actual;
Outcome = expected.Any(value => actual.Equals(value)) ? Outcome.Success : Outcome.Failure;
bool hasValues = false;
foreach (TEnum? value in expected)
{
hasValues = true;
if (actual.Equals(value))
{
Outcome = Outcome.Success;
return this;
}
}

if (!hasValues)
{
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
return this;
}

Expand Down
18 changes: 17 additions & 1 deletion Source/aweXpect/That/Enums/ThatNullableEnum.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ private sealed class IsOneOfConstraint<TEnum>(string it, ExpectationGrammars gra
public ConstraintResult IsMetBy(TEnum? actual)
{
Actual = actual;
Outcome = expected.Any(value => actual.Equals(value)) ? Outcome.Success : Outcome.Failure;
bool hasValues = false;
foreach (TEnum? value in expected)
{
hasValues = true;
if (actual.Equals(value))
{
Outcome = Outcome.Success;
return this;
}
}

if (!hasValues)
{
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
return this;
}

Expand Down
25 changes: 12 additions & 13 deletions Source/aweXpect/That/Numbers/ThatNumber.IsOneOf.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using aweXpect.Core;
using aweXpect.Core.Constraints;
using aweXpect.Helpers;
Expand Down Expand Up @@ -217,7 +216,7 @@ public ConstraintResult IsMetBy(TNumber actual)
{
Actual = actual;
bool hasValues = false;
foreach (TNumber? value in expected)
foreach (TNumber value in expected)
{
hasValues = true;
if (options.IsWithinTolerance(actual, value))
Expand All @@ -229,7 +228,7 @@ public ConstraintResult IsMetBy(TNumber actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -273,7 +272,7 @@ public ConstraintResult IsMetBy(TNumber? actual)
{
Actual = actual;
bool hasValues = false;
foreach (TNumber? value in expected)
foreach (TNumber value in expected)
{
hasValues = true;
if (options.IsWithinTolerance(actual, value))
Expand All @@ -285,7 +284,7 @@ public ConstraintResult IsMetBy(TNumber? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -341,7 +340,7 @@ public ConstraintResult IsMetBy(TNumber actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -397,7 +396,7 @@ public ConstraintResult IsMetBy(TNumber? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -2421,7 +2420,7 @@ public ConstraintResult IsMetBy(TNumber actual)
{
Actual = actual;
bool hasValues = false;
foreach (TNumber? value in expected)
foreach (TNumber value in expected)
{
hasValues = true;
if (options.IsWithinTolerance(actual, value))
Expand All @@ -2433,7 +2432,7 @@ public ConstraintResult IsMetBy(TNumber actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -2477,7 +2476,7 @@ public ConstraintResult IsMetBy(TNumber? actual)
{
Actual = actual;
bool hasValues = false;
foreach (TNumber? value in expected)
foreach (TNumber value in expected)
{
hasValues = true;
if (options.IsWithinTolerance(actual, value))
Expand All @@ -2489,7 +2488,7 @@ public ConstraintResult IsMetBy(TNumber? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -2545,7 +2544,7 @@ public ConstraintResult IsMetBy(TNumber actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down Expand Up @@ -2601,7 +2600,7 @@ public ConstraintResult IsMetBy(TNumber? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
21 changes: 17 additions & 4 deletions Source/aweXpect/That/Objects/ThatObject.IsOneOf.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using aweXpect.Core;
using aweXpect.Core.Constraints;
Expand Down Expand Up @@ -87,9 +86,23 @@ private sealed class IsOneOfConstraint<TSubject, TExpected>(
public ConstraintResult IsMetBy(TSubject actual)
{
Actual = actual;
Outcome = expected.Any(value => options.AreConsideredEqual(actual, value))
? Outcome.Success
: Outcome.Failure;
bool hasValues = false;
foreach (TExpected? value in expected)
{
hasValues = true;
if (options.AreConsideredEqual(actual, value))
{
Outcome = Outcome.Success;
return this;
}
}

if (!hasValues)
{
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
return this;
}

Expand Down
23 changes: 18 additions & 5 deletions Source/aweXpect/That/Strings/ThatString.IsOneOf.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using aweXpect.Core;
using aweXpect.Core.Constraints;
using aweXpect.Helpers;
Expand Down Expand Up @@ -82,10 +81,24 @@ public ConstraintResult IsMetBy(string? actual)
{
Actual = actual;
StringEqualityOptions stringEqualityOptions = options;
Outcome = expectedValues.Any(expectedValue => stringEqualityOptions
.AreConsideredEqual(actual, expectedValue))
? Outcome.Success
: Outcome.Failure;
bool hasValues = false;
foreach (string? value in expectedValues)
{
hasValues = true;
if (stringEqualityOptions
.AreConsideredEqual(actual, value))
{
Outcome = Outcome.Success;
return this;
}
}

if (!hasValues)
{
throw ThrowHelper.EmptyCollection();
}
Comment thread
vbreuss marked this conversation as resolved.

Outcome = Outcome.Failure;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ConstraintResult IsMetBy(TimeOnly? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect/That/TimeOnlys/ThatTimeOnly.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public ConstraintResult IsMetBy(TimeOnly actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public ConstraintResult IsMetBy(TimeSpan? actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect/That/TimeSpans/ThatTimeSpan.IsOneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ConstraintResult IsMetBy(TimeSpan actual)

if (!hasValues)
{
throw new ArgumentException("You have to provide at least one expected value!");
throw ThrowHelper.EmptyCollection();
}

Outcome = Outcome.Failure;
Expand Down
4 changes: 2 additions & 2 deletions Tests/aweXpect.Tests/Chars/ThatChar.IsNotOneOf.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await That(Act).Throws<ArgumentException>()
[InlineData('\t')]
public async Task WhenExpectedOnlyContainsNull_ShouldSucceed(char subject)
{
IEnumerable<char?> expected = [null];
IEnumerable<char?> expected = [null,];

async Task Act()
=> await That(subject).IsNotOneOf(expected);
Expand All @@ -57,7 +57,7 @@ await That(Act).Throws<ArgumentException>()
public async Task WhenSubjectIsContained_ShouldFail(char subject,
params char[] otherValues)
{
IEnumerable<char> expected = [..otherValues, subject];
IEnumerable<char> expected = [..otherValues, subject,];

async Task Act()
=> await That(subject).IsNotOneOf(expected);
Expand Down
4 changes: 2 additions & 2 deletions Tests/aweXpect.Tests/Chars/ThatChar.IsOneOf.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await That(Act).Throws<ArgumentException>()
[InlineData('\t')]
public async Task WhenExpectedOnlyContainsNull_ShouldFail(char subject)
{
IEnumerable<char?> expected = [null];
IEnumerable<char?> expected = [null,];

async Task Act()
=> await That(subject).IsOneOf(expected);
Expand Down Expand Up @@ -62,7 +62,7 @@ await That(Act).Throws<ArgumentException>()
public async Task WhenSubjectIsContained_ShouldSucceed(char subject,
params char[] otherValues)
{
IEnumerable<char> expected = [..otherValues, subject];
IEnumerable<char> expected = [..otherValues, subject,];

async Task Act()
=> await That(subject).IsOneOf(expected);
Expand Down
Loading
Loading