Skip to content

Commit

Permalink
Argument exceptions (#1100)
Browse files Browse the repository at this point in the history
Argument exceptions
  • Loading branch information
jnyrup committed Aug 3, 2019
2 parents 8323a08 + 9197d21 commit f8251ea
Show file tree
Hide file tree
Showing 44 changed files with 977 additions and 220 deletions.
3 changes: 3 additions & 0 deletions Src/FluentAssertions/AssertionOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#region

using System;
using FluentAssertions.Common;
using FluentAssertions.Equivalency;

#endregion
Expand Down Expand Up @@ -33,6 +34,8 @@ public static EquivalencyAssertionOptions<T> CloneDefaults<T>()
public static void AssertEquivalencyUsing(
Func<EquivalencyAssertionOptions, EquivalencyAssertionOptions> defaultsConfigurer)
{
Guard.ThrowIfArgumentIsNull(defaultsConfigurer, nameof(defaultsConfigurer));

defaults = defaultsConfigurer(defaults);
}

Expand Down
71 changes: 27 additions & 44 deletions Src/FluentAssertions/Collections/CollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,16 @@ public AndConstraint<TAssertions> Equal(IEnumerable expected, string because = "
protected void AssertSubjectEquality<TActual, TExpected>(IEnumerable expectation, Func<TActual, TExpected, bool> equalityComparison,
string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(equalityComparison, nameof(equalityComparison));

bool subjectIsNull = ReferenceEquals(Subject, null);
bool expectationIsNull = expectation is null;
if (subjectIsNull && expectationIsNull)
{
return;
}

if (expectation is null)
{
throw new ArgumentNullException(nameof(expectation), "Cannot compare collection with <null>.");
}
Guard.ThrowIfArgumentIsNull(expectation, nameof(expectation), "Cannot compare collection with <null>.");

ICollection<TExpected> expectedItems = expectation.ConvertOrCastToCollection<TExpected>();

Expand Down Expand Up @@ -293,10 +292,7 @@ public AndConstraint<TAssertions> NotEqual(IEnumerable unexpected, string becaus
.FailWith("Expected collections not to be equal{reason}, but found <null>.");
}

if (unexpected is null)
{
throw new ArgumentNullException(nameof(unexpected), "Cannot compare collection with <null>.");
}
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare collection with <null>.");

if (ReferenceEquals(Subject, unexpected))
{
Expand Down Expand Up @@ -414,6 +410,8 @@ public AndConstraint<TAssertions> BeEquivalentTo(IEnumerable expectation,
Func<EquivalencyAssertionOptions<IEnumerable>, EquivalencyAssertionOptions<IEnumerable>> config, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config, nameof(config));

EquivalencyAssertionOptions<IEnumerable> options = config(AssertionOptions.CloneDefaults<IEnumerable>());

var context = new EquivalencyValidationContext
Expand Down Expand Up @@ -460,6 +458,8 @@ public AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(IEnumerable<TExpe
Func<EquivalencyAssertionOptions<TExpectation>, EquivalencyAssertionOptions<TExpectation>> config, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config, nameof(config));

EquivalencyAssertionOptions<IEnumerable<TExpectation>> options = config(AssertionOptions.CloneDefaults<TExpectation>()).AsCollection();

var context = new EquivalencyValidationContext
Expand Down Expand Up @@ -494,10 +494,7 @@ public AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(IEnumerable<TExpe
public AndConstraint<TAssertions> NotBeEquivalentTo(IEnumerable unexpected, string because = "",
params object[] becauseArgs)
{
if (unexpected is null)
{
throw new ArgumentNullException(nameof(unexpected), "Cannot verify inequivalence against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot verify inequivalence against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -593,6 +590,8 @@ public AndConstraint<TAssertions> ContainEquivalentOf<TExpectation>(TExpectation
public AndConstraint<TAssertions> ContainEquivalentOf<TExpectation>(TExpectation expectation, Func<EquivalencyAssertionOptions<TExpectation>,
EquivalencyAssertionOptions<TExpectation>> config, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config, nameof(config));

if (ReferenceEquals(Subject, null))
{
Execute.Assertion
Expand Down Expand Up @@ -710,10 +709,7 @@ private static List<T> GetMissingItems<T>(IEnumerable<T> expectedItems, IEnumera
/// </param>
public AndConstraint<TAssertions> Contain(IEnumerable expected, string because = "", params object[] becauseArgs)
{
if (expected is null)
{
throw new ArgumentNullException(nameof(expected), "Cannot verify containment against a <null> collection");
}
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify containment against a <null> collection");

ICollection<object> expectedObjects = expected.ConvertOrCastToCollection<object>();
if (!expectedObjects.Any())
Expand Down Expand Up @@ -790,10 +786,7 @@ public AndConstraint<TAssertions> ContainInOrder(params object[] expected)
public AndConstraint<TAssertions> ContainInOrder(IEnumerable expected, string because = "",
params object[] becauseArgs)
{
if (expected is null)
{
throw new ArgumentNullException(nameof(expected), "Cannot verify ordered containment against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify ordered containment against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1054,10 +1047,7 @@ private AndConstraint<TAssertions> NotBeInOrder(IComparer<object> comparer, Sort
public AndConstraint<TAssertions> BeSubsetOf(IEnumerable expectedSuperset, string because = "",
params object[] becauseArgs)
{
if (expectedSuperset is null)
{
throw new ArgumentNullException(nameof(expectedSuperset), "Cannot verify a subset against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(expectedSuperset, nameof(expectedSuperset), "Cannot verify a subset against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1139,10 +1129,7 @@ public AndConstraint<TAssertions> NotBeSubsetOf(IEnumerable unexpectedSuperset,
public AndConstraint<TAssertions> HaveSameCount(IEnumerable otherCollection, string because = "",
params object[] becauseArgs)
{
if (otherCollection is null)
{
throw new ArgumentNullException(nameof(otherCollection), "Cannot verify count against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1180,10 +1167,7 @@ public AndConstraint<TAssertions> HaveSameCount(IEnumerable otherCollection, str
public AndConstraint<TAssertions> NotHaveSameCount(IEnumerable otherCollection, string because = "",
params object[] becauseArgs)
{
if (otherCollection is null)
{
throw new ArgumentNullException(nameof(otherCollection), "Cannot verify count against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1275,10 +1259,7 @@ public AndWhichConstraint<TAssertions, object> HaveElementAt(int index, object e
/// </param>
public AndConstraint<TAssertions> NotContain(IEnumerable unexpected, string because = "", params object[] becauseArgs)
{
if (unexpected is null)
{
throw new ArgumentNullException(nameof(unexpected), "Cannot verify non-containment against a <null> collection");
}
Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot verify non-containment against a <null> collection");

ICollection<object> unexpectedObjects = unexpected.ConvertOrCastToCollection<object>();
if (!unexpectedObjects.Any())
Expand Down Expand Up @@ -1342,10 +1323,7 @@ public AndConstraint<TAssertions> NotContain(IEnumerable unexpected, string beca
public AndConstraint<TAssertions> IntersectWith(IEnumerable otherCollection, string because = "",
params object[] becauseArgs)
{
if (otherCollection is null)
{
throw new ArgumentNullException(nameof(otherCollection), "Cannot verify intersection against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify intersection against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1384,10 +1362,7 @@ public AndConstraint<TAssertions> IntersectWith(IEnumerable otherCollection, str
public AndConstraint<TAssertions> NotIntersectWith(IEnumerable otherCollection, string because = "",
params object[] becauseArgs)
{
if (otherCollection is null)
{
throw new ArgumentNullException(nameof(otherCollection), "Cannot verify intersection against a <null> collection.");
}
Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify intersection against a <null> collection.");

if (ReferenceEquals(Subject, null))
{
Expand Down Expand Up @@ -1443,6 +1418,8 @@ public AndConstraint<TAssertions> StartWith(object element, string because = "",

protected void AssertCollectionStartsWith<TActual, TExpected>(IEnumerable<TActual> actualItems, TExpected[] expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(equalityComparison, nameof(equalityComparison));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to start with {0}{reason}, ", expected)
Expand All @@ -1458,6 +1435,8 @@ protected void AssertCollectionStartsWith<TActual, TExpected>(IEnumerable<TActua

protected void AssertCollectionStartsWith<TActual, TExpected>(IEnumerable<TActual> actualItems, ICollection<TExpected> expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(equalityComparison, nameof(equalityComparison));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to start with {0}{reason}, ", expected)
Expand Down Expand Up @@ -1493,6 +1472,8 @@ public AndConstraint<TAssertions> EndWith(object element, string because = "", p

protected void AssertCollectionEndsWith<TActual, TExpected>(IEnumerable<TActual> actual, TExpected[] expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(equalityComparison, nameof(equalityComparison));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to end with {0}{reason}, ", expected)
Expand All @@ -1513,6 +1494,8 @@ protected void AssertCollectionEndsWith<TActual, TExpected>(IEnumerable<TActual>

protected void AssertCollectionEndsWith<TActual, TExpected>(IEnumerable<TActual> actual, ICollection<TExpected> expected, Func<TActual, TExpected, bool> equalityComparison, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(equalityComparison, nameof(equalityComparison));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to end with {0}{reason}, ", expected)
Expand Down
24 changes: 10 additions & 14 deletions Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public GenericCollectionAssertions(IEnumerable<T> actualValue)
public AndConstraint<GenericCollectionAssertions<T>> NotContainNulls<TKey>(Expression<Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class
{
Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate));

if (Subject is null)
{
Execute.Assertion
Expand Down Expand Up @@ -72,6 +74,8 @@ public AndConstraint<GenericCollectionAssertions<T>> NotContainNulls<TKey>(Expre
/// </param>
public AndConstraint<GenericCollectionAssertions<T>> OnlyHaveUniqueItems<TKey>(Expression<Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate));

if (Subject is null)
{
Execute.Assertion
Expand Down Expand Up @@ -238,11 +242,7 @@ public AndConstraint<GenericCollectionAssertions<T>> BeInDescendingOrder<TSelect
private AndConstraint<GenericCollectionAssertions<T>> BeOrderedBy<TSelector>(
Expression<Func<T, TSelector>> propertyExpression, IComparer<TSelector> comparer, SortOrder direction, string because, object[] args)
{
if (comparer is null)
{
throw new ArgumentNullException(nameof(comparer),
"Cannot assert collection ordering without specifying a comparer.");
}
Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer.");

if (IsValidProperty(propertyExpression, because, args))
{
Expand All @@ -269,11 +269,8 @@ private AndConstraint<GenericCollectionAssertions<T>> BeOrderedBy<TSelector>(

private bool IsValidProperty<TSelector>(Expression<Func<T, TSelector>> propertyExpression, string because, object[] args)
{
if (propertyExpression is null)
{
throw new ArgumentNullException(nameof(propertyExpression),
"Cannot assert collection ordering without specifying a property.");
}
Guard.ThrowIfArgumentIsNull(propertyExpression, nameof(propertyExpression),
"Cannot assert collection ordering without specifying a property.");

return Execute.Assertion
.ForCondition(!(Subject is null))
Expand Down Expand Up @@ -335,6 +332,8 @@ public void AllBeEquivalentTo<TExpectation>(TExpectation expectation,
string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config, nameof(config));

TExpectation[] repeatedExpectation = RepeatAsManyAs(expectation, Subject).ToArray();

// Because we have just manually created the collection based on single element
Expand Down Expand Up @@ -379,10 +378,7 @@ public AndConstraint<GenericCollectionAssertions<T>> SatisfyRespectively(params
/// </param>
public AndConstraint<GenericCollectionAssertions<T>> SatisfyRespectively(IEnumerable<Action<T>> expected, string because = "", params object[] becauseArgs)
{
if (expected is null)
{
throw new ArgumentNullException(nameof(expected), "Cannot verify against a <null> collection of inspectors");
}
Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify against a <null> collection of inspectors");

ICollection<Action<T>> elementInspectors = expected.ConvertOrCastToCollection();
if (!elementInspectors.Any())
Expand Down
Loading

0 comments on commit f8251ea

Please sign in to comment.