Skip to content

Commit

Permalink
ThrowIfArgumentIsNullOrEmpty should throw ArgumentNullException for n…
Browse files Browse the repository at this point in the history
…ulls
  • Loading branch information
jnyrup committed Dec 22, 2022
1 parent 0c0046f commit 91070ab
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 36 deletions.
6 changes: 4 additions & 2 deletions Src/FluentAssertions/Common/Guard.cs
Expand Up @@ -26,15 +26,17 @@ public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, s
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(paramName);
ThrowIfArgumentIsNull(str, paramName);
throw new ArgumentException("The value cannot be an empty string.", paramName);
}
}

public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, string paramName, string message)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(paramName, message);
ThrowIfArgumentIsNull(str, paramName, message);
throw new ArgumentException(message, paramName);
}
}

Expand Down
Expand Up @@ -15,8 +15,10 @@ internal class MappedPathMatchingRule : IMemberMatchingRule
/// <summary>
/// Initializes a new instance of the <see cref="MappedPathMatchingRule"/> class.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="expectationMemberPath"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="subjectMemberPath"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="expectationMemberPath"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="expectationMemberPath"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="subjectMemberPath"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="subjectMemberPath"/> is empty.</exception>
public MappedPathMatchingRule(string expectationMemberPath, string subjectMemberPath)
{
Guard.ThrowIfArgumentIsNullOrEmpty(expectationMemberPath,
Expand Down
3 changes: 2 additions & 1 deletion Src/FluentAssertions/Specialized/AssemblyAssertions.cs
Expand Up @@ -110,7 +110,8 @@ public AndConstraint<AssemblyAssertions> Reference(Assembly assembly, string bec
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndWhichConstraint<AssemblyAssertions, Type> DefineType(string @namespace, string name, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNullOrEmpty(name, nameof(name));
Expand Down
39 changes: 26 additions & 13 deletions Src/FluentAssertions/Types/TypeAssertions.cs
Expand Up @@ -852,7 +852,8 @@ public AndConstraint<TypeAssertions> NotBeStatic(string because = "", params obj
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="propertyType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndWhichConstraint<TypeAssertions, PropertyInfo> HaveProperty(
Type propertyType, string name, string because = "", params object[] becauseArgs)
{
Expand Down Expand Up @@ -897,7 +898,8 @@ public AndConstraint<TypeAssertions> NotBeStatic(string because = "", params obj
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndWhichConstraint<TypeAssertions, PropertyInfo> HaveProperty<TProperty>(
string name, string because = "", params object[] becauseArgs)
{
Expand All @@ -915,7 +917,8 @@ public AndConstraint<TypeAssertions> NotBeStatic(string because = "", params obj
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNullOrEmpty(name, nameof(name));
Expand Down Expand Up @@ -953,7 +956,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="interfaceType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndConstraint<TypeAssertions> HaveExplicitProperty(
Type interfaceType, string name, string because = "", params object[] becauseArgs)
{
Expand Down Expand Up @@ -996,7 +1000,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndConstraint<TypeAssertions> HaveExplicitProperty<TInterface>(
string name, string because = "", params object[] becauseArgs)
where TInterface : class
Expand All @@ -1018,7 +1023,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="interfaceType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndConstraint<TypeAssertions> NotHaveExplicitProperty(
Type interfaceType, string name, string because = "", params object[] becauseArgs)
{
Expand Down Expand Up @@ -1062,7 +1068,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
public AndConstraint<TypeAssertions> NotHaveExplicitProperty<TInterface>(
string name, string because = "", params object[] becauseArgs)
where TInterface : class
Expand All @@ -1085,7 +1092,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="interfaceType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndConstraint<TypeAssertions> HaveExplicitMethod(
Type interfaceType, string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand Down Expand Up @@ -1132,7 +1140,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndConstraint<TypeAssertions> HaveExplicitMethod<TInterface>(
string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand All @@ -1156,7 +1165,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="interfaceType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndConstraint<TypeAssertions> NotHaveExplicitMethod(
Type interfaceType, string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand Down Expand Up @@ -1203,7 +1213,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndConstraint<TypeAssertions> NotHaveExplicitMethod<TInterface>(
string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand Down Expand Up @@ -1314,7 +1325,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndWhichConstraint<TypeAssertions, MethodInfo> HaveMethod(
string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand Down Expand Up @@ -1359,7 +1371,8 @@ public AndConstraint<TypeAssertions> NotHaveProperty(string name, string because
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="parameterTypes"/> is <see langword="null"/>.</exception>
public AndConstraint<TypeAssertions> NotHaveMethod(
string name, IEnumerable<Type> parameterTypes, string because = "", params object[] becauseArgs)
Expand Down
6 changes: 4 additions & 2 deletions Src/FluentAssertions/Xml/XElementAssertions.cs
Expand Up @@ -167,7 +167,8 @@ public AndConstraint<XElementAssertions> HaveValue(string expected, string becau
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expectedName"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="expectedName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="expectedName"/> is empty.</exception>
public AndConstraint<XElementAssertions> HaveAttribute(string expectedName, string expectedValue, string because = "",
params object[] becauseArgs)
{
Expand Down Expand Up @@ -239,7 +240,8 @@ public AndConstraint<XElementAssertions> HaveValue(string expected, string becau
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="expected"/> is empty.</exception>
public AndWhichConstraint<XElementAssertions, XElement> HaveElement(string expected, string because = "",
params object[] becauseArgs)
{
Expand Down
Expand Up @@ -238,7 +238,7 @@ public void When_an_assembly_defining_a_type_with_an_empty_name_it_should_throw(
Action act = () => thisAssembly.Should().DefineType(GetType().Namespace, string.Empty);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down
Expand Up @@ -179,7 +179,7 @@ public void When_asserting_a_type_has_an_explicit_method_with_an_empty_name_it_s
type.Should().HaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public void When_asserting_a_type_has_an_explicit_methodOfT_with_an_empty_name_i
type.Should().HaveExplicitMethod<IExplicitInterface>(string.Empty, new Type[0]);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ public void When_asserting_a_type_does_not_have_an_explicit_method_with_an_empty
type.Should().NotHaveExplicitMethod(typeof(IExplicitInterface), string.Empty, new Type[0]);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down Expand Up @@ -519,7 +519,7 @@ public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_an_em
type.Should().NotHaveExplicitMethod<IExplicitInterface>(string.Empty, new Type[0]);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down
Expand Up @@ -164,7 +164,7 @@ public void When_asserting_a_type_has_an_explicit_property_with_an_empty_name_it
type.Should().HaveExplicitProperty(typeof(IExplicitInterface), string.Empty);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public void When_asserting_a_type_has_an_explicitlyOfT_property_with_an_empty_na
type.Should().HaveExplicitProperty<IExplicitInterface>(string.Empty);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}

Expand Down Expand Up @@ -390,7 +390,7 @@ public void When_asserting_a_type_does_not_have_an_explicit_property_with_an_emp
type.Should().NotHaveExplicitProperty(typeof(IExplicitInterface), string.Empty);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down Expand Up @@ -459,7 +459,7 @@ public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_a
type.Should().NotHaveExplicitProperty<IExplicitInterface>(string.Empty);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}
}
Expand Down
Expand Up @@ -107,7 +107,7 @@ public void When_asserting_a_type_has_a_method_with_an_empty_name_it_should_thro
type.Should().HaveMethod(string.Empty, new[] { typeof(string) });

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}

Expand Down Expand Up @@ -213,7 +213,7 @@ public void When_asserting_a_type_does_not_have_a_method_with_an_empty_name_it_s
type.Should().NotHaveMethod(string.Empty, new[] { typeof(string) });

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
act.Should().ThrowExactly<ArgumentException>()
.WithParameterName("name");
}

Expand Down

0 comments on commit 91070ab

Please sign in to comment.