From fff370904a64931c810ee2ee751e24428ca6212e Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 27 Oct 2022 19:51:19 +0200 Subject: [PATCH 1/6] Add missing exception xml documentation --- Src/FluentAssertions/AssertionOptions.cs | 1 + .../GenericCollectionAssertions.cs | 4 ++++ .../GenericDictionaryAssertions.cs | 8 ++++++++ .../Collections/StringCollectionAssertions.cs | 2 ++ .../Common/ExpressionExtensions.cs | 6 ++++++ Src/FluentAssertions/Common/MemberPath.cs | 8 ++++++++ .../ReadOnlyNonGenericCollectionWrapper.cs | 4 ++++ .../Data/DataColumnAssertions.cs | 1 + .../Data/DataRowAssertions.cs | 1 + .../Data/DataSetAssertions.cs | 1 + .../Data/DataTableAssertions.cs | 1 + ...DataColumnCollectionAssertionExtensions.cs | 4 ++++ .../DataRowCollectionAssertionExtensions.cs | 2 ++ .../DataTableCollectionAssertionExtensions.cs | 2 ++ .../Equivalency/ConversionSelector.cs | 10 ++++++++++ .../Matching/MappedPathMatchingRule.cs | 5 +++++ ...elfReferenceEquivalencyAssertionOptions.cs | 2 ++ .../EventRaisingExtensions.cs | 1 + .../Events/EventAssertions.cs | 2 +- .../Execution/GivenSelector.cs | 2 ++ .../Numeric/ComparableTypeAssertions.cs | 1 + .../Numeric/NullableNumericAssertions.cs | 1 + .../Numeric/NumericAssertions.cs | 3 +++ .../ObjectAssertionsExtensions.cs | 2 ++ .../Primitives/EnumAssertions.cs | 2 ++ .../Primitives/ObjectAssertions.cs | 2 ++ .../Primitives/StringAssertions.cs | 19 +++++++++++++++++++ .../Specialized/ExceptionAssertions.cs | 1 + .../Types/MethodInfoSelector.cs | 2 +- .../Types/MethodInfoSelectorAssertions.cs | 2 ++ .../Types/PropertyInfoSelector.cs | 2 +- Src/FluentAssertions/Types/TypeAssertions.cs | 6 ++++++ Src/FluentAssertions/Types/TypeSelector.cs | 4 ++++ .../Types/TypeSelectorAssertions.cs | 5 +++++ .../Xml/XDocumentAssertions.cs | 6 ++++++ .../Xml/XElementAssertions.cs | 2 ++ 36 files changed, 124 insertions(+), 3 deletions(-) diff --git a/Src/FluentAssertions/AssertionOptions.cs b/Src/FluentAssertions/AssertionOptions.cs index 0c7d295736..48459eab2a 100644 --- a/Src/FluentAssertions/AssertionOptions.cs +++ b/Src/FluentAssertions/AssertionOptions.cs @@ -36,6 +36,7 @@ public static EquivalencyAssertionOptions CloneDefaults() /// /// An action that is used to configure the defaults. /// + /// is . public static void AssertEquivalencyUsing( Func defaultsConfigurer) { diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index a8abb0783f..86fbb81e0c 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -176,6 +176,7 @@ public AndConstraint AllBeAssignableTo(Type expectedType, string be /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint AllBeEquivalentTo(TExpectation expectation, Func, EquivalencyAssertionOptions> config, string because = "", @@ -347,6 +348,7 @@ public AndConstraint BeEmpty(string because = "", params object[] b /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(IEnumerable expectation, Func, EquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) @@ -1185,6 +1187,7 @@ public AndConstraint EndWith(IEnumerable expectation, string bec /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint EndWith( IEnumerable expectation, Func equalityComparison, string because = "", params object[] becauseArgs) { @@ -1690,6 +1693,7 @@ public AndConstraint NotBeEmpty(string because = "", params object[ /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeEquivalentTo(IEnumerable unexpected, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs index d5850b841c..41167b4fc3 100644 --- a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs @@ -51,6 +51,7 @@ public GenericDictionaryAssertions(TCollection keyValuePairs) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint Equal(T expected, string because = "", params object[] becauseArgs) where T : IEnumerable> @@ -111,6 +112,7 @@ public GenericDictionaryAssertions(TCollection keyValuePairs) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotEqual(T unexpected, string because = "", params object[] becauseArgs) where T : IEnumerable> @@ -203,6 +205,7 @@ public GenericDictionaryAssertions(TCollection keyValuePairs) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(TExpectation expectation, Func, EquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) @@ -275,6 +278,7 @@ public AndConstraint ContainKeys(params TKey[] expected) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint ContainKeys(IEnumerable expected, string because = "", params object[] becauseArgs) { @@ -375,6 +379,7 @@ public AndConstraint NotContainKeys(params TKey[] unexpected) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotContainKeys(IEnumerable unexpected, string because = "", params object[] becauseArgs) { @@ -587,6 +592,7 @@ public AndConstraint NotContainValues(params TValue[] unexpected) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotContainValues(IEnumerable unexpected, string because = "", params object[] becauseArgs) { @@ -657,6 +663,7 @@ public AndConstraint Contain(params KeyValuePair[] ex /// /// Zero or more objects to format using the placeholders in . /// + /// is . public new AndConstraint Contain(IEnumerable> expected, string because = "", params object[] becauseArgs) { @@ -816,6 +823,7 @@ public AndConstraint NotContain(params KeyValuePair[] /// /// Zero or more objects to format using the placeholders in . /// + /// is . public new AndConstraint NotContain(IEnumerable> items, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs index 0136dd6108..b1da311a2f 100644 --- a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs @@ -120,6 +120,7 @@ public AndConstraint BeEquivalentTo(IEnumerable expectation /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(IEnumerable expectation, Func, EquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) @@ -180,6 +181,7 @@ public AndConstraint BeEquivalentTo(IEnumerable expectation /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint AllBe(string expectation, Func, EquivalencyAssertionOptions> config, string because = "", diff --git a/Src/FluentAssertions/Common/ExpressionExtensions.cs b/Src/FluentAssertions/Common/ExpressionExtensions.cs index ed5f65b61d..d66d9d3fea 100644 --- a/Src/FluentAssertions/Common/ExpressionExtensions.cs +++ b/Src/FluentAssertions/Common/ExpressionExtensions.cs @@ -8,6 +8,10 @@ namespace FluentAssertions.Common; internal static class ExpressionExtensions { + /// + /// Gets the of an returning a property. + /// + /// is . public static PropertyInfo GetPropertyInfo(this Expression> expression) { Guard.ThrowIfArgumentIsNull(expression, nameof(expression), "Expected a property expression, but found ."); @@ -32,6 +36,7 @@ internal static class ExpressionExtensions /// /// E.g. Parent.Child.Sibling.Name. /// + /// is . public static MemberPath GetMemberPath( this Expression> expression) { @@ -106,6 +111,7 @@ internal static class ExpressionExtensions /// /// Validates that the expression can be used to construct a . /// + /// is . public static void ValidateMemberPath( this Expression> expression) { diff --git a/Src/FluentAssertions/Common/MemberPath.cs b/Src/FluentAssertions/Common/MemberPath.cs index 7799141a61..19542bb153 100644 --- a/Src/FluentAssertions/Common/MemberPath.cs +++ b/Src/FluentAssertions/Common/MemberPath.cs @@ -24,6 +24,10 @@ public MemberPath(IMember member, string parentPath) { } + /// + /// Initializes a new instance of the class. + /// + /// is . public MemberPath(Type reflectedType, Type declaringType, string dottedPath) : this(dottedPath) { @@ -31,6 +35,10 @@ public MemberPath(Type reflectedType, Type declaringType, string dottedPath) this.declaringType = declaringType; } + /// + /// Initializes a new instance of the class. + /// + /// is . public MemberPath(string dottedPath) { Guard.ThrowIfArgumentIsNull( diff --git a/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs b/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs index b829f8b68f..158a5299a3 100644 --- a/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs +++ b/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs @@ -38,6 +38,10 @@ internal class ReadOnlyNonGenericCollectionWrapper : ICollec { public TCollection UnderlyingCollection { get; } + /// + /// Initializes a new instance of the class. + /// + /// is . public ReadOnlyNonGenericCollectionWrapper(TCollection collection) { Guard.ThrowIfArgumentIsNull(collection, nameof(collection)); diff --git a/Src/FluentAssertions/Data/DataColumnAssertions.cs b/Src/FluentAssertions/Data/DataColumnAssertions.cs index ccc1affcf4..bd82f727ef 100644 --- a/Src/FluentAssertions/Data/DataColumnAssertions.cs +++ b/Src/FluentAssertions/Data/DataColumnAssertions.cs @@ -109,6 +109,7 @@ public AndConstraint BeEquivalentTo(DataColumn expectation /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(DataColumn expectation, Func, IDataEquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(config, nameof(config)); diff --git a/Src/FluentAssertions/Data/DataRowAssertions.cs b/Src/FluentAssertions/Data/DataRowAssertions.cs index 2fc479a34a..213c968006 100644 --- a/Src/FluentAssertions/Data/DataRowAssertions.cs +++ b/Src/FluentAssertions/Data/DataRowAssertions.cs @@ -174,6 +174,7 @@ public AndConstraint> BeEquivalentTo(DataRow expecta /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint> BeEquivalentTo(DataRow expectation, Func, IDataEquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(config, nameof(config)); diff --git a/Src/FluentAssertions/Data/DataSetAssertions.cs b/Src/FluentAssertions/Data/DataSetAssertions.cs index 89b2a2f001..c7d71a6632 100644 --- a/Src/FluentAssertions/Data/DataSetAssertions.cs +++ b/Src/FluentAssertions/Data/DataSetAssertions.cs @@ -226,6 +226,7 @@ public AndConstraint> BeEquivalentTo(DataSet expecta /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint> BeEquivalentTo(DataSet expectation, Func, IDataEquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(config, nameof(config)); diff --git a/Src/FluentAssertions/Data/DataTableAssertions.cs b/Src/FluentAssertions/Data/DataTableAssertions.cs index e272e76218..b0b2bdc384 100644 --- a/Src/FluentAssertions/Data/DataTableAssertions.cs +++ b/Src/FluentAssertions/Data/DataTableAssertions.cs @@ -239,6 +239,7 @@ public AndConstraint> BeEquivalentTo(DataTable e /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint> BeEquivalentTo(DataTable expectation, Func, IDataEquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(config, nameof(config)); diff --git a/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs index 68f6e57166..72e2e9bfe7 100644 --- a/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs @@ -23,6 +23,7 @@ public static class DataColumnCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> BeSameAs( this GenericCollectionAssertions assertion, DataColumnCollection expected, string because = "", params object[] becauseArgs) @@ -66,6 +67,7 @@ public static class DataColumnCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> NotBeSameAs( this GenericCollectionAssertions assertion, DataColumnCollection unexpected, string because = "", params object[] becauseArgs) @@ -107,6 +109,7 @@ public static class DataColumnCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> HaveSameCount( this GenericCollectionAssertions assertion, DataColumnCollection otherCollection, string because = "", params object[] becauseArgs) @@ -143,6 +146,7 @@ public static class DataColumnCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> NotHaveSameCount( this GenericCollectionAssertions assertion, DataColumnCollection otherCollection, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs index ef82cee119..b694c4959c 100644 --- a/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs @@ -103,6 +103,7 @@ public static class DataRowCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> HaveSameCount( this GenericCollectionAssertions assertion, DataRowCollection otherCollection, string because = "", params object[] becauseArgs) @@ -138,6 +139,7 @@ public static class DataRowCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> NotHaveSameCount( this GenericCollectionAssertions assertion, DataRowCollection otherCollection, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs index e5364a53d5..8e508dfbc8 100644 --- a/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs @@ -138,6 +138,7 @@ public static class DataTableCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> HaveSameCount( this GenericCollectionAssertions assertion, DataTableCollection otherCollection, string because = "", params object[] becauseArgs) @@ -173,6 +174,7 @@ public static class DataTableCollectionAssertionExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint> NotHaveSameCount( this GenericCollectionAssertions assertion, DataTableCollection otherCollection, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/Equivalency/ConversionSelector.cs b/Src/FluentAssertions/Equivalency/ConversionSelector.cs index 105c5ffa1f..2b2e8bce5c 100644 --- a/Src/FluentAssertions/Equivalency/ConversionSelector.cs +++ b/Src/FluentAssertions/Equivalency/ConversionSelector.cs @@ -46,6 +46,11 @@ public void IncludeAll() inclusions.Add(new ConversionSelectorRule(_ => true, "Try conversion of all members. ")); } + /// + /// Instructs the equivalency comparison to try to convert the value of + /// a specific member on the expectation object before running any of the other steps. + /// + /// is . public void Include(Expression> predicate) { Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate)); @@ -55,6 +60,11 @@ public void Include(Expression> predicate) $"Try conversion of member {predicate.Body}. ")); } + /// + /// Instructs the equivalency comparison to prevent trying to convert the value of + /// a specific member on the expectation object before running any of the other steps. + /// + /// is . public void Exclude(Expression> predicate) { Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate)); diff --git a/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs b/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs index 2d83ae63d9..0fc39f42af 100644 --- a/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs +++ b/Src/FluentAssertions/Equivalency/Matching/MappedPathMatchingRule.cs @@ -12,6 +12,11 @@ internal class MappedPathMatchingRule : IMemberMatchingRule private readonly MemberPath expectationPath; private readonly MemberPath subjectPath; + /// + /// Initializes a new instance of the class. + /// + /// is or empty. + /// is or empty. public MappedPathMatchingRule(string expectationMemberPath, string subjectMemberPath) { Guard.ThrowIfArgumentIsNullOrEmpty(expectationMemberPath, diff --git a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs index ba3b13701d..9f5e13a600 100644 --- a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs +++ b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs @@ -636,6 +636,7 @@ public TSelf ComparingRecordsByMembers() /// Marks as a type that should be compared by its members even though it may override /// the method. /// + /// is . public TSelf ComparingByMembers(Type type) { Guard.ThrowIfArgumentIsNull(type, nameof(type)); @@ -666,6 +667,7 @@ public TSelf ComparingByMembers(Type type) /// Marks as a value type which must be compared using its /// method, regardless of it overriding it or not. /// + /// is . public TSelf ComparingByValue(Type type) { Guard.ThrowIfArgumentIsNull(type, nameof(type)); diff --git a/Src/FluentAssertions/EventRaisingExtensions.cs b/Src/FluentAssertions/EventRaisingExtensions.cs index 2d5b97a734..270675b45c 100644 --- a/Src/FluentAssertions/EventRaisingExtensions.cs +++ b/Src/FluentAssertions/EventRaisingExtensions.cs @@ -61,6 +61,7 @@ public static IEventRecording WithSender(this IEventRecording eventRecording, ob /// /// Returns only the events having some argument matching both type and predicate. /// + /// is . public static IEventRecording WithArgs(this IEventRecording eventRecording, Expression> predicate) { Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate)); diff --git a/Src/FluentAssertions/Events/EventAssertions.cs b/Src/FluentAssertions/Events/EventAssertions.cs index facad7f15f..986889cf1c 100644 --- a/Src/FluentAssertions/Events/EventAssertions.cs +++ b/Src/FluentAssertions/Events/EventAssertions.cs @@ -21,7 +21,7 @@ protected internal EventAssertions(IMonitor monitor) { this.Monitor = monitor; } - + /// /// Gets the which is being asserted. /// diff --git a/Src/FluentAssertions/Execution/GivenSelector.cs b/Src/FluentAssertions/Execution/GivenSelector.cs index 5ea76f292b..c0aee1edb6 100644 --- a/Src/FluentAssertions/Execution/GivenSelector.cs +++ b/Src/FluentAssertions/Execution/GivenSelector.cs @@ -33,6 +33,7 @@ internal GivenSelector(Func selector, AssertionScope predecessor, bool contin /// The condition will not be evaluated if the prior assertion failed, /// nor will throw any exceptions. /// + /// is . public GivenSelector ForCondition(Func predicate) { Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate)); @@ -50,6 +51,7 @@ public GivenSelector ForCondition(Func predicate) /// nor will throw any exceptions. /// /// + /// is . public GivenSelector Given(Func selector) { Guard.ThrowIfArgumentIsNull(selector, nameof(selector)); diff --git a/Src/FluentAssertions/Numeric/ComparableTypeAssertions.cs b/Src/FluentAssertions/Numeric/ComparableTypeAssertions.cs index 1b31a3f4a2..e562878c6b 100644 --- a/Src/FluentAssertions/Numeric/ComparableTypeAssertions.cs +++ b/Src/FluentAssertions/Numeric/ComparableTypeAssertions.cs @@ -102,6 +102,7 @@ public AndConstraint Be(T expected, string because = "", params obj /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(TExpectation expectation, Func, EquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/Numeric/NullableNumericAssertions.cs b/Src/FluentAssertions/Numeric/NullableNumericAssertions.cs index b6bd86b03c..08272e5321 100644 --- a/Src/FluentAssertions/Numeric/NullableNumericAssertions.cs +++ b/Src/FluentAssertions/Numeric/NullableNumericAssertions.cs @@ -109,6 +109,7 @@ public AndConstraint BeNull(string because = "", params object[] be /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint Match(Expression> predicate, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/Numeric/NumericAssertions.cs b/Src/FluentAssertions/Numeric/NumericAssertions.cs index 84c2f79ab4..7e20a7e227 100644 --- a/Src/FluentAssertions/Numeric/NumericAssertions.cs +++ b/Src/FluentAssertions/Numeric/NumericAssertions.cs @@ -401,6 +401,7 @@ public AndConstraint BeOneOf(params T[] validValues) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeOfType(Type expectedType, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expectedType, nameof(expectedType)); @@ -431,6 +432,7 @@ public AndConstraint BeOfType(Type expectedType, string because = " /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeOfType(Type unexpectedType, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpectedType, nameof(unexpectedType)); @@ -458,6 +460,7 @@ public AndConstraint NotBeOfType(Type unexpectedType, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint Match(Expression> predicate, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/ObjectAssertionsExtensions.cs b/Src/FluentAssertions/ObjectAssertionsExtensions.cs index 07a66dd8ea..0471708e95 100644 --- a/Src/FluentAssertions/ObjectAssertionsExtensions.cs +++ b/Src/FluentAssertions/ObjectAssertionsExtensions.cs @@ -48,6 +48,7 @@ public static class ObjectAssertionsExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint BeBinarySerializable(this ObjectAssertions assertions, Func, EquivalencyAssertionOptions> options, string because = "", params object[] becauseArgs) @@ -112,6 +113,7 @@ public static class ObjectAssertionsExtensions /// /// Zero or more objects to format using the placeholders in . /// + /// is . public static AndConstraint BeDataContractSerializable(this ObjectAssertions assertions, Func, EquivalencyAssertionOptions> options, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Primitives/EnumAssertions.cs b/Src/FluentAssertions/Primitives/EnumAssertions.cs index e7475505fa..c6bd3dcde1 100644 --- a/Src/FluentAssertions/Primitives/EnumAssertions.cs +++ b/Src/FluentAssertions/Primitives/EnumAssertions.cs @@ -413,6 +413,8 @@ public AndConstraint BeOneOf(params TEnum[] validValues) /// /// Zero or more objects to format using the placeholders in . /// + /// is . + /// is empty. public AndConstraint BeOneOf(IEnumerable validValues, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(validValues, nameof(validValues), "Cannot assert that an enum is one of a null list of enums"); diff --git a/Src/FluentAssertions/Primitives/ObjectAssertions.cs b/Src/FluentAssertions/Primitives/ObjectAssertions.cs index 8842d0babd..181f0840de 100644 --- a/Src/FluentAssertions/Primitives/ObjectAssertions.cs +++ b/Src/FluentAssertions/Primitives/ObjectAssertions.cs @@ -123,6 +123,7 @@ public AndConstraint NotBe(TSubject unexpected, string because = "" /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeEquivalentTo(TExpectation expectation, Func, EquivalencyAssertionOptions> config, string because = "", params object[] becauseArgs) @@ -199,6 +200,7 @@ public AndConstraint NotBe(TSubject unexpected, string because = "" /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeEquivalentTo( TExpectation unexpected, Func, EquivalencyAssertionOptions> config, diff --git a/Src/FluentAssertions/Primitives/StringAssertions.cs b/Src/FluentAssertions/Primitives/StringAssertions.cs index df9b9e4658..cd4f772704 100644 --- a/Src/FluentAssertions/Primitives/StringAssertions.cs +++ b/Src/FluentAssertions/Primitives/StringAssertions.cs @@ -406,6 +406,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint MatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { @@ -440,6 +441,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint MatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression, string because = "", params object[] becauseArgs) { @@ -479,6 +481,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint MatchRegex(Regex regularExpression, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { @@ -528,6 +531,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint MatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs) { @@ -573,6 +577,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotMatchRegex([RegexPattern][StringSyntax("Regex")] string regularExpression, string because = "", params object[] becauseArgs) { @@ -606,6 +611,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotMatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), @@ -649,6 +655,7 @@ public AndConstraint NotMatchRegex(Regex regularExpression, string /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint StartWith(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare start of string with ."); @@ -671,6 +678,7 @@ public AndConstraint StartWith(string expected, string because = "" /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotStartWith(string unexpected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare start of string with ."); @@ -693,6 +701,7 @@ public AndConstraint NotStartWith(string unexpected, string because /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint StartWithEquivalentOf(string expected, string because = "", params object[] becauseArgs) { @@ -716,6 +725,7 @@ public AndConstraint NotStartWith(string unexpected, string because /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotStartWithEquivalentOf(string unexpected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare start of string with ."); @@ -738,6 +748,7 @@ public AndConstraint NotStartWithEquivalentOf(string unexpected, st /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint EndWith(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare string end with ."); @@ -778,6 +789,7 @@ public AndConstraint EndWith(string expected, string because = "", /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotEndWith(string unexpected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare end of string with ."); @@ -810,6 +822,7 @@ public AndConstraint NotEndWith(string unexpected, string because = /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint EndWithEquivalentOf(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare string end equivalence with ."); @@ -855,6 +868,7 @@ public AndConstraint EndWithEquivalentOf(string expected, string be /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotEndWithEquivalentOf(string unexpected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot compare end of string with ."); @@ -892,6 +906,7 @@ public AndConstraint NotEndWithEquivalentOf(string unexpected, stri /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint Contain(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); @@ -928,6 +943,7 @@ public AndConstraint Contain(string expected, string because = "", /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint Contain(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); @@ -961,6 +977,7 @@ public AndConstraint Contain(string expected, OccurrenceConstraint /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint ContainEquivalentOf(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); @@ -998,6 +1015,7 @@ public AndConstraint ContainEquivalentOf(string expected, string be /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint ContainEquivalentOf(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); @@ -1105,6 +1123,7 @@ public AndConstraint ContainAny(params string[] values) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotContain(string unexpected, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Specialized/ExceptionAssertions.cs b/Src/FluentAssertions/Specialized/ExceptionAssertions.cs index cb998bac85..801e5f7bbc 100644 --- a/Src/FluentAssertions/Specialized/ExceptionAssertions.cs +++ b/Src/FluentAssertions/Specialized/ExceptionAssertions.cs @@ -176,6 +176,7 @@ public ExceptionAssertions(IEnumerable exceptions) /// /// Zero or more objects to format using the placeholders in . /// + /// is . public ExceptionAssertions Where(Expression> exceptionExpression, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Types/MethodInfoSelector.cs b/Src/FluentAssertions/Types/MethodInfoSelector.cs index 9af53699cb..507b1dfb30 100644 --- a/Src/FluentAssertions/Types/MethodInfoSelector.cs +++ b/Src/FluentAssertions/Types/MethodInfoSelector.cs @@ -28,7 +28,7 @@ public MethodInfoSelector(Type type) /// Initializes a new instance of the class. /// /// The types from which to select methods. - /// is . + /// is or contains . public MethodInfoSelector(IEnumerable types) { Guard.ThrowIfArgumentIsNull(types, nameof(types)); diff --git a/Src/FluentAssertions/Types/MethodInfoSelectorAssertions.cs b/Src/FluentAssertions/Types/MethodInfoSelectorAssertions.cs index f18552f702..d63c8d4937 100644 --- a/Src/FluentAssertions/Types/MethodInfoSelectorAssertions.cs +++ b/Src/FluentAssertions/Types/MethodInfoSelectorAssertions.cs @@ -182,6 +182,7 @@ public AndConstraint BeDecoratedWith(s /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -233,6 +234,7 @@ public AndConstraint NotBeDecoratedWith /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute diff --git a/Src/FluentAssertions/Types/PropertyInfoSelector.cs b/Src/FluentAssertions/Types/PropertyInfoSelector.cs index f75820ba7c..44035953c9 100644 --- a/Src/FluentAssertions/Types/PropertyInfoSelector.cs +++ b/Src/FluentAssertions/Types/PropertyInfoSelector.cs @@ -28,7 +28,7 @@ public PropertyInfoSelector(Type type) /// Initializes a new instance of the class. /// /// The types from which to select properties. - /// is . + /// is or contains . public PropertyInfoSelector(IEnumerable types) { Guard.ThrowIfArgumentIsNull(types, nameof(types)); diff --git a/Src/FluentAssertions/Types/TypeAssertions.cs b/Src/FluentAssertions/Types/TypeAssertions.cs index 7b76f1ad85..072248f4fb 100644 --- a/Src/FluentAssertions/Types/TypeAssertions.cs +++ b/Src/FluentAssertions/Types/TypeAssertions.cs @@ -89,6 +89,7 @@ public new AndConstraint BeAssignableTo(string because = "", /// Zero or more objects to format using the placeholders in . /// /// An which can be used to chain assertions. + /// is . public new AndConstraint BeAssignableTo(Type type, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(type, nameof(type)); @@ -134,6 +135,7 @@ public new AndConstraint NotBeAssignableTo(string because = " /// Zero or more objects to format using the placeholders in . /// /// An which can be used to chain assertions. + /// is . public new AndConstraint NotBeAssignableTo(Type type, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(type, nameof(type)); @@ -254,6 +256,7 @@ public AndConstraint NotBe(Type unexpected, string because = "", /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint BeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -313,6 +316,7 @@ public AndConstraint NotBe(Type unexpected, string because = "", /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint BeDecoratedWithOrInherit( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -369,6 +373,7 @@ public AndConstraint NotBeDecoratedWith(string becau /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -424,6 +429,7 @@ public AndConstraint NotBeDecoratedWith(string becau /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeDecoratedWithOrInherit( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute diff --git a/Src/FluentAssertions/Types/TypeSelector.cs b/Src/FluentAssertions/Types/TypeSelector.cs index d5e3cb62b8..70b4d2d8ef 100644 --- a/Src/FluentAssertions/Types/TypeSelector.cs +++ b/Src/FluentAssertions/Types/TypeSelector.cs @@ -19,6 +19,10 @@ public TypeSelector(Type type) { } + /// + /// Initializes a new instance of the class. + /// + /// is or contains . public TypeSelector(IEnumerable types) { Guard.ThrowIfArgumentIsNull(types, nameof(types)); diff --git a/Src/FluentAssertions/Types/TypeSelectorAssertions.cs b/Src/FluentAssertions/Types/TypeSelectorAssertions.cs index dfe6d0f75b..7c1604a9f1 100644 --- a/Src/FluentAssertions/Types/TypeSelectorAssertions.cs +++ b/Src/FluentAssertions/Types/TypeSelectorAssertions.cs @@ -20,6 +20,7 @@ public class TypeSelectorAssertions /// /// Initializes a new instance of the class. /// + /// is or contains . public TypeSelectorAssertions(params Type[] types) { Guard.ThrowIfArgumentIsNull(types, nameof(types)); @@ -76,6 +77,7 @@ public AndConstraint BeDecoratedWith(string /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -142,6 +144,7 @@ public AndConstraint BeDecoratedWithOrInherit /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint BeDecoratedWithOrInherit( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -208,6 +211,7 @@ public AndConstraint NotBeDecoratedWith(stri /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeDecoratedWith( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute @@ -274,6 +278,7 @@ public AndConstraint NotBeDecoratedWithOrInherit /// Zero or more objects to format using the placeholders in . /// + /// is . public AndConstraint NotBeDecoratedWithOrInherit( Expression> isMatchingAttributePredicate, string because = "", params object[] becauseArgs) where TAttribute : Attribute diff --git a/Src/FluentAssertions/Xml/XDocumentAssertions.cs b/Src/FluentAssertions/Xml/XDocumentAssertions.cs index 31ce1d22b0..76baf7d115 100644 --- a/Src/FluentAssertions/Xml/XDocumentAssertions.cs +++ b/Src/FluentAssertions/Xml/XDocumentAssertions.cs @@ -130,6 +130,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint HaveRoot(string expected, string because = "", params object[] becauseArgs) { @@ -151,6 +152,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint HaveRoot(XName expected, string because = "", params object[] becauseArgs) { if (Subject is null) @@ -187,6 +189,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint HaveElement(string expected, string because = "", params object[] becauseArgs) { @@ -213,6 +216,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint> HaveElement(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { @@ -236,6 +240,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint HaveElement(XName expected, string because = "", params object[] becauseArgs) { @@ -282,6 +287,7 @@ public AndConstraint NotBeEquivalentTo(XDocument unexpected /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint> HaveElement(XName expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) diff --git a/Src/FluentAssertions/Xml/XElementAssertions.cs b/Src/FluentAssertions/Xml/XElementAssertions.cs index 6fbfc5ccf2..52501848dc 100644 --- a/Src/FluentAssertions/Xml/XElementAssertions.cs +++ b/Src/FluentAssertions/Xml/XElementAssertions.cs @@ -307,6 +307,7 @@ public AndConstraint HaveValue(string expected, string becau /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint> HaveElement(XName expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) @@ -357,6 +358,7 @@ public AndConstraint HaveValue(string expected, string becau /// /// Zero or more objects to format using the placeholders in . /// + /// is . public AndWhichConstraint> HaveElement(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { From 158e31b5ae3ba919b35673904ea39a63c916df7c Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 27 Oct 2022 19:57:08 +0200 Subject: [PATCH 2/6] Move Guard checks --- .../Collections/GenericCollectionAssertions.cs | 16 ++++++++-------- .../Collections/GenericDictionaryAssertions.cs | 5 +++-- .../Collections/SubsequentOrderingAssertions.cs | 5 +++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index 86fbb81e0c..7154f6391e 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -419,6 +419,7 @@ public AndConstraint BeEmpty(string because = "", params object[] b public AndConstraint> BeInAscendingOrder( IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return BeInOrder(comparer, SortOrder.Ascending, because, becauseArgs); } @@ -446,6 +447,7 @@ public AndConstraint BeEmpty(string because = "", params object[] b public AndConstraint> BeInAscendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return BeOrderedBy(propertyExpression, comparer, SortOrder.Ascending, because, becauseArgs); } @@ -534,6 +536,7 @@ public AndConstraint> BeInAscendingOrder(Func> BeInDescendingOrder( IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return BeInOrder(comparer, SortOrder.Descending, because, becauseArgs); } @@ -561,6 +564,7 @@ public AndConstraint> BeInAscendingOrder(Func> BeInDescendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return BeOrderedBy(propertyExpression, comparer, SortOrder.Descending, because, becauseArgs); } @@ -1811,6 +1815,7 @@ public AndConstraint NotBeEmpty(string because = "", params object[ public AndConstraint NotBeInAscendingOrder( IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return NotBeInOrder(comparer, SortOrder.Ascending, because, becauseArgs); } @@ -1838,6 +1843,7 @@ public AndConstraint NotBeEmpty(string because = "", params object[ public AndConstraint NotBeInAscendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return NotBeOrderedBy(propertyExpression, comparer, SortOrder.Ascending, because, becauseArgs); } @@ -1925,6 +1931,7 @@ public AndConstraint NotBeInAscendingOrder(Func comparis public AndConstraint NotBeInDescendingOrder( IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return NotBeInOrder(comparer, SortOrder.Descending, because, becauseArgs); } @@ -1952,6 +1959,7 @@ public AndConstraint NotBeInAscendingOrder(Func comparis public AndConstraint NotBeInDescendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return NotBeOrderedBy(propertyExpression, comparer, SortOrder.Descending, because, becauseArgs); } @@ -3126,8 +3134,6 @@ public AndConstraint StartWith(T element, string because = "", para string because, object[] becauseArgs) { - Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); - if (IsValidProperty(propertyExpression, because, becauseArgs)) { ICollection unordered = Subject.ConvertOrCastToCollection(); @@ -3347,8 +3353,6 @@ private bool IsValidProperty(Expression> propertyE string because, object[] becauseArgs) { - Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); - if (IsValidProperty(propertyExpression, because, becauseArgs)) { ICollection unordered = Subject.ConvertOrCastToCollection(); @@ -3376,8 +3380,6 @@ private bool IsValidProperty(Expression> propertyE private AndConstraint> BeInOrder( IComparer comparer, SortOrder expectedOrder, string because = "", params object[] becauseArgs) { - Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); - string sortOrder = (expectedOrder == SortOrder.Ascending) ? "ascending" : "descending"; bool success = Execute.Assertion @@ -3423,8 +3425,6 @@ private bool IsValidProperty(Expression> propertyE /// private AndConstraint NotBeInOrder(IComparer comparer, SortOrder order, string because = "", params object[] becauseArgs) { - Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); - string sortOrder = (order == SortOrder.Ascending) ? "ascending" : "descending"; bool success = Execute.Assertion diff --git a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs index 41167b4fc3..a7a83f5f38 100644 --- a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs @@ -471,17 +471,18 @@ public AndConstraint ContainValues(params TValue[] expected) /// /// Zero or more objects to format using the placeholders in . /// + /// is . + /// is empty. public AndConstraint ContainValues(IEnumerable expected, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify value containment against a collection of values"); return ContainValuesAndWhich(expected, because, becauseArgs); } private AndWhichConstraint> ContainValuesAndWhich(IEnumerable expected, string because = "", params object[] becauseArgs) { - Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify value containment against a collection of values"); - ICollection expectedValues = expected.ConvertOrCastToCollection(); if (!expectedValues.Any()) diff --git a/Src/FluentAssertions/Collections/SubsequentOrderingAssertions.cs b/Src/FluentAssertions/Collections/SubsequentOrderingAssertions.cs index 0f795fd66c..01e9df5e3d 100644 --- a/Src/FluentAssertions/Collections/SubsequentOrderingAssertions.cs +++ b/Src/FluentAssertions/Collections/SubsequentOrderingAssertions.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Linq.Expressions; +using FluentAssertions.Common; namespace FluentAssertions.Collections; @@ -85,9 +86,11 @@ public SubsequentOrderingGenericCollectionAssertions(TCollection actualValue, IO /// /// Empty and single element collections are considered to be ordered both in ascending and descending order at the same time. /// + /// is . public AndConstraint> ThenBeInAscendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return ThenBeOrderedBy(propertyExpression, comparer, SortOrder.Ascending, because, becauseArgs); } @@ -134,9 +137,11 @@ public SubsequentOrderingGenericCollectionAssertions(TCollection actualValue, IO /// /// Empty and single element collections are considered to be ordered both in ascending and descending order at the same time. /// + /// is . public AndConstraint> ThenBeInDescendingOrder( Expression> propertyExpression, IComparer comparer, string because = "", params object[] becauseArgs) { + Guard.ThrowIfArgumentIsNull(comparer, nameof(comparer), "Cannot assert collection ordering without specifying a comparer."); return ThenBeOrderedBy(propertyExpression, comparer, SortOrder.Descending, because, becauseArgs); } From d6fac73aad0c2d37a46d94ee2f5a9e413f337f0d Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 27 Oct 2022 20:08:18 +0200 Subject: [PATCH 3/6] Use Guard.ThrowIfArgumentIsEmpty --- .../GenericCollectionAssertions.cs | 20 ++------- .../GenericDictionaryAssertions.cs | 42 +++++-------------- .../GenericDictionaryAssertionSpecs.cs | 2 +- 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index 7154f6391e..b5e57e59c0 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -756,10 +756,7 @@ public AndConstraint Contain(IEnumerable expected, string becaus Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify containment against a collection"); ICollection expectedObjects = expected.ConvertOrCastToCollection(); - if (!expectedObjects.Any()) - { - throw new ArgumentException("Cannot verify containment against an empty collection", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expectedObjects, nameof(expected), "Cannot verify containment against an empty collection"); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) @@ -2153,10 +2150,7 @@ public AndConstraint NotContain(IEnumerable unexpected, string b Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot verify non-containment against a collection"); ICollection unexpectedObjects = unexpected.ConvertOrCastToCollection(); - if (!unexpectedObjects.Any()) - { - throw new ArgumentException("Cannot verify non-containment against an empty collection", nameof(unexpected)); - } + Guard.ThrowIfArgumentIsEmpty(unexpectedObjects, nameof(unexpected), "Cannot verify non-containment against an empty collection"); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) @@ -2920,10 +2914,7 @@ public AndConstraint SatisfyRespectively(IEnumerable> exp Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify against a collection of inspectors"); ICollection> elementInspectors = expected.ConvertOrCastToCollection(); - if (!elementInspectors.Any()) - { - throw new ArgumentException("Cannot verify against an empty collection of inspectors", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(elementInspectors, nameof(expected), "Cannot verify against an empty collection of inspectors"); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) @@ -3009,10 +3000,7 @@ public AndConstraint Satisfy(IEnumerable>> Guard.ThrowIfArgumentIsNull(predicates, nameof(predicates), "Cannot verify against a collection of predicates"); IList>> predicatesList = predicates.ConvertOrCastToList(); - if (predicatesList.Count == 0) - { - throw new ArgumentException("Cannot verify against an empty collection of predicates", nameof(predicates)); - } + Guard.ThrowIfArgumentIsEmpty(predicatesList, nameof(predicates), "Cannot verify against an empty collection of predicates"); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) diff --git a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs index a7a83f5f38..c893f4cf1a 100644 --- a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs @@ -279,17 +279,14 @@ public AndConstraint ContainKeys(params TKey[] expected) /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint ContainKeys(IEnumerable expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot verify key containment against a collection of keys"); ICollection expectedKeys = expected.ConvertOrCastToCollection(); - - if (!expectedKeys.Any()) - { - throw new ArgumentException("Cannot verify key containment against an empty sequence", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expectedKeys, nameof(expected), "Cannot verify key containment against an empty sequence"); if (Subject is null) { @@ -380,17 +377,14 @@ public AndConstraint NotContainKeys(params TKey[] unexpected) /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint NotContainKeys(IEnumerable unexpected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot verify key containment against a collection of keys"); ICollection unexpectedKeys = unexpected.ConvertOrCastToCollection(); - - if (!unexpectedKeys.Any()) - { - throw new ArgumentException("Cannot verify key containment against an empty sequence", nameof(unexpected)); - } + Guard.ThrowIfArgumentIsEmpty(unexpectedKeys, nameof(unexpected), "Cannot verify key containment against an empty sequence"); if (Subject is null) { @@ -484,11 +478,7 @@ public AndConstraint ContainValues(params TValue[] expected) params object[] becauseArgs) { ICollection expectedValues = expected.ConvertOrCastToCollection(); - - if (!expectedValues.Any()) - { - throw new ArgumentException("Cannot verify value containment with an empty sequence", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expectedValues, nameof(expected), "Cannot verify value containment against an empty sequence"); if (Subject is null) { @@ -594,6 +584,7 @@ public AndConstraint NotContainValues(params TValue[] unexpected) /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint NotContainValues(IEnumerable unexpected, string because = "", params object[] becauseArgs) { @@ -601,10 +592,7 @@ public AndConstraint NotContainValues(params TValue[] unexpected) ICollection unexpectedValues = unexpected.ConvertOrCastToCollection(); - if (!unexpectedValues.Any()) - { - throw new ArgumentException("Cannot verify value containment with an empty sequence", nameof(unexpected)); - } + Guard.ThrowIfArgumentIsEmpty(unexpectedValues, nameof(unexpected), "Cannot verify value containment with an empty sequence"); if (Subject is null) { @@ -665,18 +653,14 @@ public AndConstraint Contain(params KeyValuePair[] ex /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public new AndConstraint Contain(IEnumerable> expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot compare dictionary with ."); ICollection> expectedKeyValuePairs = expected.ConvertOrCastToCollection(); - - if (!expectedKeyValuePairs.Any()) - { - throw new ArgumentException("Cannot verify key containment against an empty collection of key/value pairs", - nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expectedKeyValuePairs, nameof(expected), "Cannot verify key containment against an empty collection of key/value pairs"); if (Subject is null) { @@ -825,18 +809,14 @@ public AndConstraint NotContain(params KeyValuePair[] /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public new AndConstraint NotContain(IEnumerable> items, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(items, nameof(items), "Cannot compare dictionary with ."); ICollection> keyValuePairs = items.ConvertOrCastToCollection(); - - if (!keyValuePairs.Any()) - { - throw new ArgumentException("Cannot verify key containment against an empty collection of key/value pairs", - nameof(items)); - } + Guard.ThrowIfArgumentIsEmpty(keyValuePairs, nameof(items), "Cannot verify key containment against an empty collection of key/value pairs"); if (Subject is null) { diff --git a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs index 2a0f0da359..c3d3ca3fae 100644 --- a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs @@ -1732,7 +1732,7 @@ public void When_the_contents_of_a_dictionary_are_checked_against_an_empty_list_ // Assert act.Should().Throw().WithMessage( - "Cannot verify value containment with an empty sequence*"); + "Cannot verify value containment against an empty sequence*"); } } From c43ffbcb2a7996eed27fdbac7ba17b10da027e1b Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 27 Oct 2022 20:14:24 +0200 Subject: [PATCH 4/6] Add ThrowIfArgumentIsEmpty for strings --- .../Collections/StringCollectionAssertions.cs | 12 +-- Src/FluentAssertions/Common/Guard.cs | 8 ++ .../Primitives/StringAssertions.cs | 82 +++++-------------- 3 files changed, 29 insertions(+), 73 deletions(-) diff --git a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs index b1da311a2f..549ffccdab 100644 --- a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs @@ -242,11 +242,7 @@ public AndConstraint BeEquivalentTo(IEnumerable expectation params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against . Provide a wildcard pattern or use the Contain method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the Contain method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the Contain method."); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) @@ -327,11 +323,7 @@ private IEnumerable AllThatMatch(string wildcardPattern) params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against . Provide a wildcard pattern or use the NotContain method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the NotContain method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match strings in collection against an empty string. Provide a wildcard pattern or use the NotContain method."); bool success = Execute.Assertion .BecauseOf(because, becauseArgs) diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index d035ca344a..f5d55492d5 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -63,6 +63,14 @@ public static void ThrowIfArgumentIsEmpty(IEnumerable values, string param } } + public static void ThrowIfArgumentIsEmpty(string str, string paramName, string message) + { + if (str.Length == 0) + { + throw new ArgumentException(message, paramName); + } + } + /// /// Workaround to make dotnet_code_quality.null_check_validation_methods work /// https://github.com/dotnet/roslyn-analyzers/issues/3451#issuecomment-606690452 diff --git a/Src/FluentAssertions/Primitives/StringAssertions.cs b/Src/FluentAssertions/Primitives/StringAssertions.cs index cd4f772704..263aeb30e3 100644 --- a/Src/FluentAssertions/Primitives/StringAssertions.cs +++ b/Src/FluentAssertions/Primitives/StringAssertions.cs @@ -210,11 +210,7 @@ public AndConstraint NotBe(string unexpected, string because = "", public AndConstraint Match(string wildcardPattern, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against . Provide a wildcard pattern or use the BeNull method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method."); var stringWildcardMatchingValidator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs); stringWildcardMatchingValidator.Validate(); @@ -260,11 +256,7 @@ public AndConstraint Match(string wildcardPattern, string because = public AndConstraint NotMatch(string wildcardPattern, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against . Provide a wildcard pattern or use the NotBeNull method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method."); new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs) { @@ -313,11 +305,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against . Provide a wildcard pattern or use the BeNull method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the BeEmpty method."); var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs) { @@ -369,11 +357,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(wildcardPattern, nameof(wildcardPattern), "Cannot match string against . Provide a wildcard pattern or use the NotBeNull method."); - - if (wildcardPattern.Length == 0) - { - throw new ArgumentException("Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method.", nameof(wildcardPattern)); - } + Guard.ThrowIfArgumentIsEmpty(wildcardPattern, nameof(wildcardPattern), "Cannot match string against an empty string. Provide a wildcard pattern or use the NotBeEmpty method."); var validator = new StringWildcardMatchingValidator(Subject, wildcardPattern, because, becauseArgs) { @@ -482,6 +466,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint MatchRegex(Regex regularExpression, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { @@ -489,12 +474,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus "Cannot match string against . Provide a regex pattern or use the BeNull method."); var regexStr = regularExpression.ToString(); - if (regexStr.Length == 0) - { - throw new ArgumentException( - "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.", - nameof(regularExpression)); - } + Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method."); bool success = Execute.Assertion .ForCondition(Subject is not null) @@ -532,6 +512,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint MatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs) { @@ -539,12 +520,7 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus "Cannot match string against . Provide a regex pattern or use the BeNull method."); var regexStr = regularExpression.ToString(); - if (regexStr.Length == 0) - { - throw new ArgumentException( - "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method.", - nameof(regularExpression)); - } + Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty string. Provide a regex pattern or use the BeEmpty method."); bool success = Execute.Assertion .ForCondition(Subject is not null) @@ -612,18 +588,14 @@ public AndConstraint NotMatch(string wildcardPattern, string becaus /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint NotMatchRegex(Regex regularExpression, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(regularExpression, nameof(regularExpression), "Cannot match string against . Provide a regex pattern or use the NotBeNull method."); var regexStr = regularExpression.ToString(); - if (regexStr.Length == 0) - { - throw new ArgumentException( - "Cannot match string against an empty regex pattern. Provide a regex pattern or use the NotBeEmpty method.", - nameof(regularExpression)); - } + Guard.ThrowIfArgumentIsEmpty(regexStr, nameof(regularExpression), "Cannot match string against an empty regex pattern. Provide a regex pattern or use the NotBeEmpty method."); bool success = Execute.Assertion .ForCondition(Subject is not null) @@ -907,14 +879,11 @@ public AndConstraint NotEndWithEquivalentOf(string unexpected, stri /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint Contain(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); - - if (expected.Length == 0) - { - throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string."); Execute.Assertion .ForCondition(Contains(Subject, expected, StringComparison.Ordinal)) @@ -944,14 +913,11 @@ public AndConstraint Contain(string expected, string because = "", /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint Contain(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); - - if (expected.Length == 0) - { - throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string."); int actual = Subject.CountSubstring(expected, StringComparison.Ordinal); @@ -978,14 +944,11 @@ public AndConstraint Contain(string expected, OccurrenceConstraint /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint ContainEquivalentOf(string expected, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); - - if (expected.Length == 0) - { - throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string."); Execute.Assertion .ForCondition(Contains(Subject, expected, StringComparison.OrdinalIgnoreCase)) @@ -1016,14 +979,11 @@ public AndConstraint ContainEquivalentOf(string expected, string be /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint ContainEquivalentOf(string expected, OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(expected, nameof(expected), "Cannot assert string containment against ."); - - if (expected.Length == 0) - { - throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(expected)); - } + Guard.ThrowIfArgumentIsEmpty(expected, nameof(expected), "Cannot assert string containment against an empty string."); int actual = Subject.CountSubstring(expected, StringComparison.OrdinalIgnoreCase); @@ -1128,11 +1088,7 @@ public AndConstraint ContainAny(params string[] values) params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(unexpected, nameof(unexpected), "Cannot assert string containment against ."); - - if (unexpected.Length == 0) - { - throw new ArgumentException("Cannot assert string containment against an empty string.", nameof(unexpected)); - } + Guard.ThrowIfArgumentIsEmpty(unexpected, nameof(unexpected), "Cannot assert string containment against an empty string."); Execute.Assertion .ForCondition(!Contains(Subject, unexpected, StringComparison.Ordinal)) From 0c0046fc61b092371058168a8810827112c88916 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 27 Oct 2022 20:28:31 +0200 Subject: [PATCH 5/6] Add ThrowIfArgumentIsNegative --- Src/FluentAssertions/Common/Guard.cs | 8 ++++++++ .../Primitives/DateTimeAssertions.cs | 11 +++-------- .../Primitives/DateTimeOffsetAssertions.cs | 11 +++-------- .../Primitives/SimpleTimeSpanAssertions.cs | 11 +++-------- Src/FluentAssertions/Primitives/StringAssertions.cs | 1 + .../Specialized/AsyncFunctionAssertions.cs | 12 ++---------- .../Specialized/DelegateAssertions.cs | 11 ++--------- .../Specialized/ExecutionTimeAssertions.cs | 5 +---- .../Specialized/FunctionAssertionHelpers.cs | 11 ++--------- .../Specialized/GenericAsyncFunctionAssertions.cs | 12 ++---------- .../AsyncFunctionExceptionAssertionSpecs.cs | 6 ++++-- .../Exceptions/FunctionExceptionAssertionSpecs.cs | 6 ++++-- .../Exceptions/NotThrowSpecs.cs | 6 ++++-- .../Primitives/DateTimeAssertionSpecs.cs | 6 ++++-- .../Primitives/DateTimeOffsetAssertionSpecs.cs | 6 ++++-- .../Primitives/SimpleTimeSpanAssertionSpecs.cs | 6 ++++-- .../Specialized/ExecutionTimeAssertionsSpecs.cs | 3 ++- .../Specialized/TaskOfTAssertionSpecs.cs | 6 ++++-- 18 files changed, 57 insertions(+), 81 deletions(-) diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index f5d55492d5..16b62b9117 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -71,6 +71,14 @@ public static void ThrowIfArgumentIsEmpty(string str, string paramName, string m } } + public static void ThrowIfArgumentIsNegative(TimeSpan timeSpan, string paramName) + { + if (timeSpan < TimeSpan.Zero) + { + throw new ArgumentOutOfRangeException(paramName, "The value must be non-negative."); + } + } + /// /// Workaround to make dotnet_code_quality.null_check_validation_methods work /// https://github.com/dotnet/roslyn-analyzers/issues/3451#issuecomment-606690452 diff --git a/Src/FluentAssertions/Primitives/DateTimeAssertions.cs b/Src/FluentAssertions/Primitives/DateTimeAssertions.cs index b2320fa7bc..e85e5cc6a1 100644 --- a/Src/FluentAssertions/Primitives/DateTimeAssertions.cs +++ b/Src/FluentAssertions/Primitives/DateTimeAssertions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using FluentAssertions.Common; using FluentAssertions.Execution; namespace FluentAssertions.Primitives; @@ -157,10 +158,7 @@ public AndConstraint Be(DateTime? expected, string because = "", pa public AndConstraint BeCloseTo(DateTime nearbyTime, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); long distanceToMinInTicks = (nearbyTime - DateTime.MinValue).Ticks; DateTime minimumValue = nearbyTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks)); @@ -208,10 +206,7 @@ public AndConstraint Be(DateTime? expected, string because = "", pa public AndConstraint NotBeCloseTo(DateTime distantTime, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); long distanceToMinInTicks = (distantTime - DateTime.MinValue).Ticks; DateTime minimumValue = distantTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks)); diff --git a/Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs b/Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs index 149064c0fe..7713f15c87 100644 --- a/Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs +++ b/Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using FluentAssertions.Common; using FluentAssertions.Execution; namespace FluentAssertions.Primitives; @@ -293,10 +294,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value) string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); long distanceToMinInTicks = (nearbyTime - DateTimeOffset.MinValue).Ticks; DateTimeOffset minimumValue = nearbyTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks)); @@ -344,10 +342,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value) public AndConstraint NotBeCloseTo(DateTimeOffset distantTime, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); long distanceToMinInTicks = (distantTime - DateTimeOffset.MinValue).Ticks; DateTimeOffset minimumValue = distantTime.AddTicks(-Math.Min(precision.Ticks, distanceToMinInTicks)); diff --git a/Src/FluentAssertions/Primitives/SimpleTimeSpanAssertions.cs b/Src/FluentAssertions/Primitives/SimpleTimeSpanAssertions.cs index 34bd206aaa..601b187853 100644 --- a/Src/FluentAssertions/Primitives/SimpleTimeSpanAssertions.cs +++ b/Src/FluentAssertions/Primitives/SimpleTimeSpanAssertions.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Diagnostics; +using FluentAssertions.Common; using FluentAssertions.Execution; namespace FluentAssertions.Primitives; @@ -239,10 +240,7 @@ public AndConstraint BeGreaterThan(TimeSpan expected, string becaus public AndConstraint BeCloseTo(TimeSpan nearbyTime, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); TimeSpan minimumValue = nearbyTime - precision; TimeSpan maximumValue = nearbyTime + precision; @@ -281,10 +279,7 @@ public AndConstraint BeGreaterThan(TimeSpan expected, string becaus public AndConstraint NotBeCloseTo(TimeSpan distantTime, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); TimeSpan minimumValue = distantTime - precision; TimeSpan maximumValue = distantTime + precision; diff --git a/Src/FluentAssertions/Primitives/StringAssertions.cs b/Src/FluentAssertions/Primitives/StringAssertions.cs index 263aeb30e3..69622f4ec7 100644 --- a/Src/FluentAssertions/Primitives/StringAssertions.cs +++ b/Src/FluentAssertions/Primitives/StringAssertions.cs @@ -1084,6 +1084,7 @@ public AndConstraint ContainAny(params string[] values) /// Zero or more objects to format using the placeholders in . /// /// is . + /// is empty. public AndConstraint NotContain(string unexpected, string because = "", params object[] becauseArgs) { diff --git a/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs b/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs index 32471cecdb..a96ec788c9 100644 --- a/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs +++ b/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs @@ -251,16 +251,8 @@ public async Task> NotThrowAsync(string b /// Throws if waitTime or pollInterval are negative. public Task> NotThrowAfterAsync(TimeSpan waitTime, TimeSpan pollInterval, string because = "", params object[] becauseArgs) { - if (waitTime < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative."); - } - - if (pollInterval < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(pollInterval), - $"The value of {nameof(pollInterval)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime)); + Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval)); Execute.Assertion .ForCondition(Subject is not null) diff --git a/Src/FluentAssertions/Specialized/DelegateAssertions.cs b/Src/FluentAssertions/Specialized/DelegateAssertions.cs index 26d23a8b03..343cb0a32f 100644 --- a/Src/FluentAssertions/Specialized/DelegateAssertions.cs +++ b/Src/FluentAssertions/Specialized/DelegateAssertions.cs @@ -164,15 +164,8 @@ public AndConstraint NotThrowAfter(TimeSpan waitTime, TimeSpan poll .FailWith("Expected {context} not to throw after {0}{reason}, but found .", waitTime); FailIfSubjectIsAsyncVoid(); - if (waitTime < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative."); - } - - if (pollInterval < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime)); + Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval)); TimeSpan? invocationEndTime = null; Exception exception = null; diff --git a/Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs b/Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs index 3f122ccde1..df30d413ea 100644 --- a/Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs +++ b/Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs @@ -201,10 +201,7 @@ public AndConstraint BeGreaterThan(TimeSpan minDuration /// public AndConstraint BeCloseTo(TimeSpan expectedDuration, TimeSpan precision, string because = "", params object[] becauseArgs) { - if (precision < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(precision), $"The value of {nameof(precision)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(precision, nameof(precision)); TimeSpan minimumValue = expectedDuration - precision; TimeSpan maximumValue = expectedDuration + precision; diff --git a/Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs b/Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs index fd6d974d71..98045d2ec4 100644 --- a/Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs +++ b/Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs @@ -25,15 +25,8 @@ internal static T NotThrow(Func subject, string because, object[] becauseA internal static TResult NotThrowAfter(Func subject, IClock clock, TimeSpan waitTime, TimeSpan pollInterval, string because, object[] becauseArgs) { - if (waitTime < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative."); - } - - if (pollInterval < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime)); + Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval)); TimeSpan? invocationEndTime = null; Exception exception = null; diff --git a/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs b/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs index 43e2104a6f..73dc33583e 100644 --- a/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs +++ b/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs @@ -122,16 +122,8 @@ public GenericAsyncFunctionAssertions(Func> subject, IExtractExcep public new Task, TResult>> NotThrowAfterAsync( TimeSpan waitTime, TimeSpan pollInterval, string because = "", params object[] becauseArgs) { - if (waitTime < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative."); - } - - if (pollInterval < TimeSpan.Zero) - { - throw new ArgumentOutOfRangeException(nameof(pollInterval), - $"The value of {nameof(pollInterval)} must be non-negative."); - } + Guard.ThrowIfArgumentIsNegative(waitTime, nameof(waitTime)); + Guard.ThrowIfArgumentIsNegative(pollInterval, nameof(pollInterval)); Execute.Assertion .ForCondition(Subject is not null) diff --git a/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs index 5eb3688792..b7f468f00a 100644 --- a/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs @@ -1153,7 +1153,8 @@ public async Task When_wait_time_is_negative_for_async_func_it_should_throw() // Assert await act.Should().ThrowAsync() - .WithMessage("* value of waitTime must be non-negative*"); + .WithParameterName("waitTime") + .WithMessage("*must be non-negative*"); } [Fact] @@ -1171,7 +1172,8 @@ public async Task When_poll_interval_is_negative_for_async_func_it_should_throw( // Assert await act.Should().ThrowAsync() - .WithMessage("* value of pollInterval must be non-negative*"); + .WithParameterName("pollInterval") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Exceptions/FunctionExceptionAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Exceptions/FunctionExceptionAssertionSpecs.cs index 550b5b19e0..96f97b9eb2 100644 --- a/Tests/FluentAssertions.Specs/Exceptions/FunctionExceptionAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Exceptions/FunctionExceptionAssertionSpecs.cs @@ -472,7 +472,8 @@ public void When_wait_time_is_negative_it_should_throw() // Assert action.Should().Throw() - .WithMessage("* value of waitTime must be non-negative*"); + .WithParameterName("waitTime") + .WithMessage("*must be non-negative*"); } [Fact] @@ -489,7 +490,8 @@ public void When_poll_interval_is_negative_it_should_throw() // Assert action.Should().Throw() - .WithMessage("* value of pollInterval must be non-negative*"); + .WithParameterName("pollInterval") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs b/Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs index 535718afe8..3822d517d0 100644 --- a/Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs +++ b/Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs @@ -136,7 +136,8 @@ public void When_wait_time_is_negative_it_should_throw() // Assert action.Should().Throw() - .WithMessage("* value of waitTime must be non-negative*"); + .WithParameterName("waitTime") + .WithMessage("*must be non-negative*"); } [Fact] @@ -153,7 +154,8 @@ public void When_poll_interval_is_negative_it_should_throw() // Assert action.Should().Throw() - .WithMessage("* value of pollInterval must be non-negative*"); + .WithParameterName("pollInterval") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Primitives/DateTimeAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Primitives/DateTimeAssertionSpecs.cs index 3ef8c5d6e7..1b83c880d8 100644 --- a/Tests/FluentAssertions.Specs/Primitives/DateTimeAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Primitives/DateTimeAssertionSpecs.cs @@ -383,7 +383,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_ // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] @@ -590,7 +591,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Primitives/DateTimeOffsetAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Primitives/DateTimeOffsetAssertionSpecs.cs index 8a4cf5438d..ed7b1b9ce7 100644 --- a/Tests/FluentAssertions.Specs/Primitives/DateTimeOffsetAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Primitives/DateTimeOffsetAssertionSpecs.cs @@ -561,7 +561,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_ // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] @@ -768,7 +769,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Primitives/SimpleTimeSpanAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Primitives/SimpleTimeSpanAssertionSpecs.cs index 70ab3416bb..c1440c927f 100644 --- a/Tests/FluentAssertions.Specs/Primitives/SimpleTimeSpanAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Primitives/SimpleTimeSpanAssertionSpecs.cs @@ -542,7 +542,8 @@ public void When_asserting_that_time_is_close_to_a_negative_precision_it_should_ // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] @@ -666,7 +667,8 @@ public void When_asserting_that_time_is_not_close_to_a_negative_precision_it_sho // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Specialized/ExecutionTimeAssertionsSpecs.cs b/Tests/FluentAssertions.Specs/Specialized/ExecutionTimeAssertionsSpecs.cs index 92d7c340a2..5fca878e49 100644 --- a/Tests/FluentAssertions.Specs/Specialized/ExecutionTimeAssertionsSpecs.cs +++ b/Tests/FluentAssertions.Specs/Specialized/ExecutionTimeAssertionsSpecs.cs @@ -414,7 +414,8 @@ public void When_asserting_that_execution_time_is_close_to_a_negative_precision_ // Assert act.Should().Throw() - .WithMessage("* value of precision must be non-negative*"); + .WithParameterName("precision") + .WithMessage("*must be non-negative*"); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs index 8e18081243..589a3baff7 100644 --- a/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs @@ -330,7 +330,8 @@ public async Task When_wait_time_is_negative_it_should_fail() // Assert await act.Should().ThrowAsync() - .WithMessage("* value of waitTime must be non-negative*"); + .WithParameterName("waitTime") + .WithMessage("*must be non-negative*"); } [Fact] @@ -348,7 +349,8 @@ public async Task When_poll_interval_is_negative_it_should_fail() // Assert await act.Should().ThrowAsync() - .WithMessage("* value of pollInterval must be non-negative*"); + .WithParameterName("pollInterval") + .WithMessage("*must be non-negative*"); } [Fact] From 91070ab8937328b5abc4c817246b496ddcecb84f Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sun, 30 Oct 2022 13:05:41 +0100 Subject: [PATCH 6/6] ThrowIfArgumentIsNullOrEmpty should throw ArgumentNullException for nulls --- Src/FluentAssertions/Common/Guard.cs | 6 ++- .../Matching/MappedPathMatchingRule.cs | 6 ++- .../Specialized/AssemblyAssertions.cs | 3 +- Src/FluentAssertions/Types/TypeAssertions.cs | 39 ++++++++++++------- .../Xml/XElementAssertions.cs | 6 ++- .../Specialized/AssemblyAssertionSpecs.cs | 2 +- .../TypeAssertionSpecs.HaveExplicitMethod.cs | 8 ++-- ...TypeAssertionSpecs.HaveExplicitProperty.cs | 8 ++-- .../Types/TypeAssertionSpecs.HaveMethod.cs | 4 +- .../Types/TypeAssertionSpecs.HaveProperty.cs | 6 +-- .../Xml/XElementAssertionSpecs.cs | 4 +- docs/_pages/releases.md | 1 + 12 files changed, 57 insertions(+), 36 deletions(-) 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 AndConstraint NotBeStatic(string because = "", params obj /// /// 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 AndConstraint NotBeStatic(string because = "", params obj /// /// 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 NotHaveProperty(string name, string because /// /// 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 NotHaveProperty(string name, string because /// 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 NotHaveProperty(string name, string because /// /// 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 NotHaveProperty(string name, string because /// 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 NotHaveProperty(string name, string because /// /// 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 NotHaveProperty(string name, string because /// 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 NotHaveProperty(string name, string because /// /// 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 NotHaveProperty(string name, string because /// /// 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 AndConstraint NotHaveProperty(string name, string because /// /// 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 HaveValue(string expected, string becau /// /// 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