diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index 16b62b9117..152dcb217d 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -26,7 +26,8 @@ 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); } } @@ -34,7 +35,8 @@ public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, s { if (string.IsNullOrEmpty(str)) { - throw new ArgumentNullException(paramName, message); + ThrowIfArgumentIsNull(str, paramName, message); + throw new ArgumentException(message, paramName); } } diff --git a/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs b/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs index 0fc39f42af..25a69e1f20 100644 --- a/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs +++ b/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs @@ -15,8 +15,10 @@ internal class MappedPathMatchingRule : IMemberMatchingRule /// /// Initializes a new instance of the class. /// - /// is or empty. - /// is or empty. + /// is . + /// is empty. + /// is . + /// is empty. public MappedPathMatchingRule(string expectationMemberPath, string subjectMemberPath) { Guard.ThrowIfArgumentIsNullOrEmpty(expectationMemberPath, diff --git a/Src/FluentAssertions/Specialized/AssemblyAssertions.cs b/Src/FluentAssertions/Specialized/AssemblyAssertions.cs index f1b4ad384c..c9159b8972 100644 --- a/Src/FluentAssertions/Specialized/AssemblyAssertions.cs +++ b/Src/FluentAssertions/Specialized/AssemblyAssertions.cs @@ -110,7 +110,8 @@ public AndConstraint Reference(Assembly assembly, string bec /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndWhichConstraint DefineType(string @namespace, string name, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNullOrEmpty(name, nameof(name)); diff --git a/Src/FluentAssertions/Types/TypeAssertions.cs b/Src/FluentAssertions/Types/TypeAssertions.cs index 072248f4fb..5a5deb0513 100644 --- a/Src/FluentAssertions/Types/TypeAssertions.cs +++ b/Src/FluentAssertions/Types/TypeAssertions.cs @@ -852,7 +852,8 @@ public AndConstraint NotBeStatic(string because = "", params obj /// Zero or more objects to format using the placeholders in . /// /// is . - /// is or empty. + /// is . + /// is empty. public AndWhichConstraint HaveProperty( Type propertyType, string name, string because = "", params object[] becauseArgs) { @@ -897,7 +898,8 @@ public AndWhichConstraint HaveProperty( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndWhichConstraint HaveProperty( string name, string because = "", params object[] becauseArgs) { @@ -915,7 +917,8 @@ public AndWhichConstraint HaveProperty( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndConstraint NotHaveProperty(string name, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNullOrEmpty(name, nameof(name)); @@ -953,7 +956,8 @@ public AndConstraint NotHaveProperty(string name, string because /// Zero or more objects to format using the placeholders in . /// /// is . - /// is or empty. + /// is . + /// is empty. public AndConstraint HaveExplicitProperty( Type interfaceType, string name, string because = "", params object[] becauseArgs) { @@ -996,7 +1000,8 @@ public AndConstraint HaveExplicitProperty( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndConstraint HaveExplicitProperty( string name, string because = "", params object[] becauseArgs) where TInterface : class @@ -1018,7 +1023,8 @@ public AndConstraint HaveExplicitProperty( /// Zero or more objects to format using the placeholders in . /// /// is . - /// is or empty. + /// is . + /// is empty. public AndConstraint NotHaveExplicitProperty( Type interfaceType, string name, string because = "", params object[] becauseArgs) { @@ -1062,7 +1068,8 @@ public AndConstraint NotHaveExplicitProperty( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndConstraint NotHaveExplicitProperty( string name, string because = "", params object[] becauseArgs) where TInterface : class @@ -1085,7 +1092,8 @@ public AndConstraint NotHaveExplicitProperty( /// Zero or more objects to format using the placeholders in . /// /// is . - /// is or empty. + /// is . + /// is empty. /// is . public AndConstraint HaveExplicitMethod( Type interfaceType, string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) @@ -1132,7 +1140,8 @@ public AndConstraint HaveExplicitMethod( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. /// is . public AndConstraint HaveExplicitMethod( string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) @@ -1156,7 +1165,8 @@ public AndConstraint HaveExplicitMethod( /// Zero or more objects to format using the placeholders in . /// /// is . - /// is or empty. + /// is . + /// is empty. /// is . public AndConstraint NotHaveExplicitMethod( Type interfaceType, string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) @@ -1203,7 +1213,8 @@ public AndConstraint NotHaveExplicitMethod( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. /// is . public AndConstraint NotHaveExplicitMethod( string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) @@ -1314,7 +1325,8 @@ public AndConstraint NotHaveIndexer( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. /// is . public AndWhichConstraint HaveMethod( string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) @@ -1359,7 +1371,8 @@ public AndWhichConstraint HaveMethod( /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. /// is . public AndConstraint NotHaveMethod( string name, IEnumerable parameterTypes, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/Xml/XElementAssertions.cs b/Src/FluentAssertions/Xml/XElementAssertions.cs index 52501848dc..81fb5653a7 100644 --- a/Src/FluentAssertions/Xml/XElementAssertions.cs +++ b/Src/FluentAssertions/Xml/XElementAssertions.cs @@ -167,7 +167,8 @@ public AndConstraint HaveValue(string expected, string becau /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndConstraint HaveAttribute(string expectedName, string expectedValue, string because = "", params object[] becauseArgs) { @@ -239,7 +240,8 @@ public AndConstraint HaveAttribute(XName expectedName, strin /// /// Zero or more objects to format using the placeholders in . /// - /// is or empty. + /// is . + /// is empty. public AndWhichConstraint HaveElement(string expected, string because = "", params object[] becauseArgs) { diff --git a/Tests/FluentAssertions.Specs/Specialized/AssemblyAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Specialized/AssemblyAssertionSpecs.cs index 82c28ef184..0f298777fd 100644 --- a/Tests/FluentAssertions.Specs/Specialized/AssemblyAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Specialized/AssemblyAssertionSpecs.cs @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs index 7d4342d5bb..6861b1a8d4 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitMethod.cs @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -260,7 +260,7 @@ public void When_asserting_a_type_has_an_explicit_methodOfT_with_an_empty_name_i type.Should().HaveExplicitMethod(string.Empty, new Type[0]); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -519,7 +519,7 @@ public void When_asserting_a_type_does_not_have_an_explicit_methodOfT_with_an_em type.Should().NotHaveExplicitMethod(string.Empty, new Type[0]); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs index fc78bd5b15..ae3305305f 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveExplicitProperty.cs @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -212,7 +212,7 @@ public void When_asserting_a_type_has_an_explicitlyOfT_property_with_an_empty_na type.Should().HaveExplicitProperty(string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -459,7 +459,7 @@ public void When_asserting_a_type_does_not_have_an_explicitlyOfT_property_with_a type.Should().NotHaveExplicitProperty(string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs index 7a81e3f6a9..70c64d593a 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveMethod.cs @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } @@ -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() + act.Should().ThrowExactly() .WithParameterName("name"); } diff --git a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs index 290a2b73d9..fa7c2c4075 100644 --- a/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs +++ b/Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.HaveProperty.cs @@ -119,7 +119,7 @@ public void When_asserting_a_type_has_a_property_with_an_empty_name_it_should_th type.Should().HaveProperty(typeof(string), string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -170,7 +170,7 @@ public void When_asserting_a_type_has_a_propertyOfT_with_an_empty_name_it_should type.Should().HaveProperty(string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } @@ -249,7 +249,7 @@ public void When_asserting_a_type_does_not_have_a_property_with_an_empty_name_it type.Should().NotHaveProperty(string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("name"); } } diff --git a/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs index dc8d53de8f..47f174b46a 100644 --- a/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Xml/XElementAssertionSpecs.cs @@ -1056,7 +1056,7 @@ public void When_asserting_element_has_an_attribute_with_an_empty_name_it_should theElement.Should().HaveAttribute(string.Empty, "value"); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("expectedName"); } } @@ -1228,7 +1228,7 @@ public void When_asserting_element_has_a_child_element_with_an_empty_name_it_sho theElement.Should().HaveElement(string.Empty); // Assert - act.Should().ThrowExactly() + act.Should().ThrowExactly() .WithParameterName("expected"); } } diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index ef1a285352..2d5b76018f 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -21,6 +21,7 @@ sidebar: ### Fixes * Quering properties on classes, e.g. `typeof(MyClass).Properties()`, now also includes static properties - [#2054](https://github.com/fluentassertions/fluentassertions/pull/2054) * Nested AssertionScopes now print the inner scope reportables - [#2044](https://github.com/fluentassertions/fluentassertions/pull/2044) +* Throw `ArgumentException` instead of `ArgumentNullException` when a required `string` argument is empty - [#2023](https://github.com/fluentassertions/fluentassertions/pull/2023) ## 6.8.0