diff --git a/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs b/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs index 247607a66b..15d3882f73 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs @@ -1277,7 +1277,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, #pragma warning restore IDE0060 // Remove unused parameter CultureInfo culture, [InterpolatedStringHandlerArgument(nameof(expected), nameof(actual), nameof(ignoreCase), nameof(culture))] ref AssertNonGenericAreEqualInterpolatedStringHandler message, [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "") { - CheckParameterNotNull(culture, "Assert.AreEqual", nameof(culture), string.Empty); + CheckParameterNotNull(culture, "Assert.AreEqual", nameof(culture)); message.ComputeAssertion(expectedExpression, actualExpression); } @@ -1316,7 +1316,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, /// public static void AreEqual(string? expected, string? actual, bool ignoreCase, CultureInfo culture, string? message = "", [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "") { - CheckParameterNotNull(culture, "Assert.AreEqual", "culture", string.Empty); + CheckParameterNotNull(culture, "Assert.AreEqual", "culture"); if (!AreEqualFailing(expected, actual, ignoreCase, culture)) { return; @@ -1372,7 +1372,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC #pragma warning restore IDE0060 // Remove unused parameter CultureInfo culture, [InterpolatedStringHandlerArgument(nameof(notExpected), nameof(actual), nameof(ignoreCase), nameof(culture))] ref AssertNonGenericAreNotEqualInterpolatedStringHandler message, [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "") { - CheckParameterNotNull(culture, "Assert.AreNotEqual", nameof(culture), string.Empty); + CheckParameterNotNull(culture, "Assert.AreNotEqual", nameof(culture)); message.ComputeAssertion(notExpectedExpression, actualExpression); } @@ -1412,7 +1412,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, CultureInfo culture, string? message = "", [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(actual))] string actualExpression = "") { - CheckParameterNotNull(culture, "Assert.AreNotEqual", "culture", string.Empty); + CheckParameterNotNull(culture, "Assert.AreNotEqual", "culture"); if (!AreNotEqualFailing(notExpected, actual, ignoreCase, culture)) { return; diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 0a70e0747f..05f36ec2a4 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -133,7 +133,7 @@ public static T ContainsSingle(IEnumerable collection, string? message = " /// Users shouldn't pass a value for this parameter. /// /// The item. - public static object ContainsSingle(IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") + public static object? ContainsSingle(IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") => ContainsSingle(static _ => true, collection, message, predicateExpression: string.Empty, collectionExpression); /// @@ -192,33 +192,35 @@ public static T ContainsSingle(Func predicate, IEnumerable collec /// Users shouldn't pass a value for this parameter. /// /// The item that matches the predicate. - public static object ContainsSingle(Func predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") + public static object? ContainsSingle(Func predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { object? firstMatch = null; int matchCount = 0; foreach (object? item in collection) { - if (predicate(item)) + if (!predicate(item)) { - if (matchCount == 0) - { - firstMatch = item; - } - - matchCount++; - - // Early exit optimization - no need to continue if we already have more than one match - if (matchCount > 1) - { - break; - } + continue; + } + + if (matchCount == 0) + { + firstMatch = item; + } + + matchCount++; + + // Early exit optimization - no need to continue if we already have more than one match + if (matchCount > 1) + { + break; } } if (matchCount == 1) { - return firstMatch!; + return firstMatch; } if (string.IsNullOrEmpty(predicateExpression)) @@ -232,7 +234,7 @@ public static object ContainsSingle(Func predicate, IEnumerable co ThrowAssertSingleMatchFailed(matchCount, userMessage); } - return default!; + return default; } #endregion // ContainsSingle @@ -279,6 +281,8 @@ public static void Contains(T expected, IEnumerable collection, string? me /// public static void Contains(object? expected, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.Contains", "collection"); + foreach (object? item in collection) { if (object.Equals(item, expected)) @@ -333,6 +337,9 @@ public static void Contains(T expected, IEnumerable collection, IEqualityC /// public static void Contains(object? expected, IEnumerable collection, IEqualityComparer comparer, string? message = "", [CallerArgumentExpression(nameof(expected))] string expectedExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.Contains", "collection"); + CheckParameterNotNull(comparer, "Assert.Contains", "comparer"); + foreach (object? item in collection) { if (comparer.Equals(item, expected)) @@ -385,6 +392,9 @@ public static void Contains(Func predicate, IEnumerable collectio /// public static void Contains(Func predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.Contains", "collection"); + CheckParameterNotNull(predicate, "Assert.Contains", "predicate"); + foreach (object? item in collection) { if (predicate(item)) @@ -461,6 +471,9 @@ public static void Contains(string substring, string value, string? message = "" /// public static void Contains(string substring, string value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(substring))] string substringExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { + CheckParameterNotNull(value, "Assert.Contains", "value"); + CheckParameterNotNull(substring, "Assert.Contains", "substring"); + if (!value.Contains(substring, comparisonType)) { string userMessage = BuildUserMessageForSubstringExpressionAndValueExpression(message, substringExpression, valueExpression); @@ -513,6 +526,8 @@ public static void DoesNotContain(T notExpected, IEnumerable collection, s /// public static void DoesNotContain(object? notExpected, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.DoesNotContain", "collection"); + foreach (object? item in collection) { if (object.Equals(notExpected, item)) @@ -565,6 +580,9 @@ public static void DoesNotContain(T notExpected, IEnumerable collection, I /// public static void DoesNotContain(object? notExpected, IEnumerable collection, IEqualityComparer comparer, string? message = "", [CallerArgumentExpression(nameof(notExpected))] string notExpectedExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.DoesNotContain", "collection"); + CheckParameterNotNull(comparer, "Assert.DoesNotContain", "comparer"); + foreach (object? item in collection) { if (comparer.Equals(item, notExpected)) @@ -615,6 +633,9 @@ public static void DoesNotContain(Func predicate, IEnumerable col /// public static void DoesNotContain(Func predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "") { + CheckParameterNotNull(collection, "Assert.DoesNotContain", "collection"); + CheckParameterNotNull(predicate, "Assert.DoesNotContain", "predicate"); + foreach (object? item in collection) { if (predicate(item)) @@ -689,6 +710,9 @@ public static void DoesNotContain(string substring, string value, string? messag /// public static void DoesNotContain(string substring, string value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(substring))] string substringExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { + CheckParameterNotNull(value, "Assert.DoesNotContain", "value"); + CheckParameterNotNull(substring, "Assert.DoesNotContain", "substring"); + if (value.Contains(substring, comparisonType)) { string userMessage = BuildUserMessageForSubstringExpressionAndValueExpression(message, substringExpression, valueExpression); diff --git a/src/TestFramework/TestFramework/Assertions/Assert.EndsWith.cs b/src/TestFramework/TestFramework/Assertions/Assert.EndsWith.cs index 8463f8e44a..772c1d1e48 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.EndsWith.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.EndsWith.cs @@ -72,8 +72,8 @@ public static void EndsWith([NotNull] string? expectedSuffix, [NotNull] string? /// public static void EndsWith([NotNull] string? expectedSuffix, [NotNull] string? value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(expectedSuffix))] string expectedSuffixExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.EndsWith", "value", string.Empty); - CheckParameterNotNull(expectedSuffix, "Assert.EndsWith", "expectedSuffix", string.Empty); + CheckParameterNotNull(value, "Assert.EndsWith", "value"); + CheckParameterNotNull(expectedSuffix, "Assert.EndsWith", "expectedSuffix"); if (!value.EndsWith(expectedSuffix, comparisonType)) { string userMessage = BuildUserMessageForExpectedSuffixExpressionAndValueExpression(message, expectedSuffixExpression, valueExpression); @@ -146,8 +146,8 @@ public static void DoesNotEndWith([NotNull] string? notExpectedSuffix, [NotNull] /// public static void DoesNotEndWith([NotNull] string? notExpectedSuffix, [NotNull] string? value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(notExpectedSuffix))] string notExpectedSuffixExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.DoesNotEndWith", "value", string.Empty); - CheckParameterNotNull(notExpectedSuffix, "Assert.DoesNotEndWith", "notExpectedSuffix", string.Empty); + CheckParameterNotNull(value, "Assert.DoesNotEndWith", "value"); + CheckParameterNotNull(notExpectedSuffix, "Assert.DoesNotEndWith", "notExpectedSuffix"); if (value.EndsWith(notExpectedSuffix, comparisonType)) { string userMessage = BuildUserMessageForNotExpectedSuffixExpressionAndValueExpression(message, notExpectedSuffixExpression, valueExpression); diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs b/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs index fe834bb6ae..b204970b6d 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Matches.cs @@ -39,8 +39,8 @@ public sealed partial class Assert /// public static void MatchesRegex([NotNull] Regex? pattern, [NotNull] string? value, string? message = "", [CallerArgumentExpression(nameof(pattern))] string patternExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.MatchesRegex", "value", string.Empty); - CheckParameterNotNull(pattern, "Assert.MatchesRegex", "pattern", string.Empty); + CheckParameterNotNull(value, "Assert.MatchesRegex", "value"); + CheckParameterNotNull(pattern, "Assert.MatchesRegex", "pattern"); if (!pattern.IsMatch(value)) { @@ -115,8 +115,8 @@ public static void MatchesRegex([NotNull] string? pattern, [NotNull] string? val /// public static void DoesNotMatchRegex([NotNull] Regex? pattern, [NotNull] string? value, string? message = "", [CallerArgumentExpression(nameof(pattern))] string patternExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.DoesNotMatchRegex", "value", string.Empty); - CheckParameterNotNull(pattern, "Assert.DoesNotMatchRegex", "pattern", string.Empty); + CheckParameterNotNull(value, "Assert.DoesNotMatchRegex", "value"); + CheckParameterNotNull(pattern, "Assert.DoesNotMatchRegex", "pattern"); if (pattern.IsMatch(value)) { @@ -161,7 +161,7 @@ public static void DoesNotMatchRegex([NotNull] string? pattern, [NotNull] string private static Regex? ToRegex([NotNull] string? pattern) { - CheckParameterNotNull(pattern, "Assert.MatchesRegex", "pattern", string.Empty); + CheckParameterNotNull(pattern, "Assert.MatchesRegex", "pattern"); return new Regex(pattern); } } diff --git a/src/TestFramework/TestFramework/Assertions/Assert.StartsWith.cs b/src/TestFramework/TestFramework/Assertions/Assert.StartsWith.cs index 7a6fe16fad..6f5c728535 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.StartsWith.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.StartsWith.cs @@ -72,8 +72,8 @@ public static void StartsWith([NotNull] string? expectedPrefix, [NotNull] string /// public static void StartsWith([NotNull] string? expectedPrefix, [NotNull] string? value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(expectedPrefix))] string expectedPrefixExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.StartsWith", "value", string.Empty); - CheckParameterNotNull(expectedPrefix, "Assert.StartsWith", "expectedPrefix", string.Empty); + CheckParameterNotNull(value, "Assert.StartsWith", "value"); + CheckParameterNotNull(expectedPrefix, "Assert.StartsWith", "expectedPrefix"); if (!value.StartsWith(expectedPrefix, comparisonType)) { string userMessage = BuildUserMessageForExpectedPrefixExpressionAndValueExpression(message, expectedPrefixExpression, valueExpression); @@ -144,8 +144,8 @@ public static void DoesNotStartWith([NotNull] string? notExpectedPrefix, [NotNul /// public static void DoesNotStartWith([NotNull] string? notExpectedPrefix, [NotNull] string? value, StringComparison comparisonType, string? message = "", [CallerArgumentExpression(nameof(notExpectedPrefix))] string notExpectedPrefixExpression = "", [CallerArgumentExpression(nameof(value))] string valueExpression = "") { - CheckParameterNotNull(value, "Assert.DoesNotStartWith", "value", string.Empty); - CheckParameterNotNull(notExpectedPrefix, "Assert.DoesNotStartWith", "notExpectedPrefix", string.Empty); + CheckParameterNotNull(value, "Assert.DoesNotStartWith", "value"); + CheckParameterNotNull(notExpectedPrefix, "Assert.DoesNotStartWith", "notExpectedPrefix"); if (value.StartsWith(notExpectedPrefix, comparisonType)) { string userMessage = BuildUserMessageForNotExpectedPrefixExpressionAndValueExpression(message, notExpectedPrefixExpression, valueExpression); diff --git a/src/TestFramework/TestFramework/Assertions/Assert.cs b/src/TestFramework/TestFramework/Assertions/Assert.cs index 2035fbf618..61a2e56800 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.cs @@ -160,15 +160,11 @@ private static string BuildUserMessageForMinValueExpressionAndMaxValueExpression /// /// parameter name. /// - /// - /// message for the invalid parameter exception. - /// - internal static void CheckParameterNotNull([NotNull] object? param, string assertionName, string parameterName, string? message) + internal static void CheckParameterNotNull([NotNull] object? param, string assertionName, string parameterName) { if (param == null) { - string userMessage = BuildUserMessage(message); - string finalMessage = string.Format(CultureInfo.CurrentCulture, FrameworkMessages.NullParameterToAssert, parameterName, userMessage); + string finalMessage = string.Format(CultureInfo.CurrentCulture, FrameworkMessages.NullParameterToAssert, parameterName); ThrowAssertFailed(assertionName, finalMessage); } } diff --git a/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs b/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs index de8c4c13c5..6be196fa94 100644 --- a/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs +++ b/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs @@ -69,7 +69,7 @@ public static void Contains([NotNull] ICollection? collection, object? element) /// public static void Contains([NotNull] ICollection? collection, object? element, string? message) { - Assert.CheckParameterNotNull(collection, "CollectionAssert.Contains", "collection", string.Empty); + Assert.CheckParameterNotNull(collection, "CollectionAssert.Contains", "collection"); foreach (object? current in collection) { @@ -120,7 +120,7 @@ public static void DoesNotContain([NotNull] ICollection? collection, object? ele /// public static void DoesNotContain([NotNull] ICollection? collection, object? element, string? message) { - Assert.CheckParameterNotNull(collection, "CollectionAssert.DoesNotContain", "collection", string.Empty); + Assert.CheckParameterNotNull(collection, "CollectionAssert.DoesNotContain", "collection"); foreach (object? current in collection) { @@ -160,7 +160,7 @@ public static void AllItemsAreNotNull([NotNull] ICollection? collection) /// public static void AllItemsAreNotNull([NotNull] ICollection? collection, string? message) { - Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreNotNull", "collection", string.Empty); + Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreNotNull", "collection"); foreach (object? current in collection) { if (current == null) @@ -202,7 +202,7 @@ public static void AllItemsAreUnique([NotNull] ICollection? collection) /// public static void AllItemsAreUnique([NotNull] ICollection? collection, string? message) { - Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreUnique", "collection", string.Empty); + Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreUnique", "collection"); message = Assert.ReplaceNulls(message); @@ -292,8 +292,8 @@ public static void IsSubsetOf([NotNull] ICollection? subset, [NotNull] ICollecti /// public static void IsSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message) { - Assert.CheckParameterNotNull(subset, "CollectionAssert.IsSubsetOf", "subset", string.Empty); - Assert.CheckParameterNotNull(superset, "CollectionAssert.IsSubsetOf", "superset", string.Empty); + Assert.CheckParameterNotNull(subset, "CollectionAssert.IsSubsetOf", "subset"); + Assert.CheckParameterNotNull(superset, "CollectionAssert.IsSubsetOf", "superset"); Tuple> isSubsetValue = IsSubsetOfHelper(subset, superset); if (!isSubsetValue.Item1) { @@ -352,8 +352,8 @@ public static void IsNotSubsetOf([NotNull] ICollection? subset, [NotNull] IColle /// public static void IsNotSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message) { - Assert.CheckParameterNotNull(subset, "CollectionAssert.IsNotSubsetOf", "subset", string.Empty); - Assert.CheckParameterNotNull(superset, "CollectionAssert.IsNotSubsetOf", "superset", string.Empty); + Assert.CheckParameterNotNull(subset, "CollectionAssert.IsNotSubsetOf", "subset"); + Assert.CheckParameterNotNull(superset, "CollectionAssert.IsNotSubsetOf", "superset"); Tuple> isSubsetValue = IsSubsetOfHelper(subset, superset); if (isSubsetValue.Item1) { @@ -471,7 +471,7 @@ public static void AreEquivalent( [NotNullIfNotNull(nameof(actual))] IEnumerable? expected, [NotNullIfNotNull(nameof(expected))] IEnumerable? actual, [NotNull] IEqualityComparer? comparer, string? message) { - Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer", string.Empty); + Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer"); // Check whether one is null while the other is not. if (expected == null != (actual == null)) @@ -637,7 +637,7 @@ public static void AreNotEquivalent( [NotNullIfNotNull(nameof(actual))] IEnumerable? notExpected, [NotNullIfNotNull(nameof(notExpected))] IEnumerable? actual, [NotNull] IEqualityComparer? comparer, string? message) { - Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer", string.Empty); + Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer"); // Check whether one is null while the other is not. if (notExpected == null != (actual == null)) @@ -738,8 +738,8 @@ public static void AllItemsAreInstancesOfType([NotNull] ICollection? collection, public static void AllItemsAreInstancesOfType( [NotNull] ICollection? collection, [NotNull] Type? expectedType, string? message) { - Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreInstancesOfType", "collection", string.Empty); - Assert.CheckParameterNotNull(expectedType, "CollectionAssert.AllItemsAreInstancesOfType", "expectedType", string.Empty); + Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreInstancesOfType", "collection"); + Assert.CheckParameterNotNull(expectedType, "CollectionAssert.AllItemsAreInstancesOfType", "expectedType"); int i = 0; foreach (object? element in collection) { @@ -1177,7 +1177,7 @@ private static bool FindMismatchedElement(IEnumerable expected, IEnumerab private static bool AreCollectionsEqual(ICollection? expected, ICollection? actual, [NotNull] IComparer? comparer, ref string reason) { - Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer", string.Empty); + Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer"); if (object.ReferenceEquals(expected, actual)) { reason = string.Format(CultureInfo.CurrentCulture, FrameworkMessages.BothCollectionsSameReference, string.Empty); diff --git a/src/TestFramework/TestFramework/Assertions/StringAssert.cs b/src/TestFramework/TestFramework/Assertions/StringAssert.cs index c54b83bf60..b2e36625ec 100644 --- a/src/TestFramework/TestFramework/Assertions/StringAssert.cs +++ b/src/TestFramework/TestFramework/Assertions/StringAssert.cs @@ -116,8 +116,8 @@ public static void Contains([NotNull] string? value, [NotNull] string? substring /// public static void Contains([NotNull] string? value, [NotNull] string? substring, StringComparison comparisonType, string? message) { - Assert.CheckParameterNotNull(value, "StringAssert.Contains", "value", string.Empty); - Assert.CheckParameterNotNull(substring, "StringAssert.Contains", "substring", string.Empty); + Assert.CheckParameterNotNull(value, "StringAssert.Contains", "value"); + Assert.CheckParameterNotNull(substring, "StringAssert.Contains", "substring"); if (value.IndexOf(substring, comparisonType) < 0) { string userMessage = Assert.BuildUserMessage(message); @@ -213,8 +213,8 @@ public static void StartsWith([NotNull] string? value, [NotNull] string? substri /// public static void StartsWith([NotNull] string? value, [NotNull] string? substring, StringComparison comparisonType, string? message) { - Assert.CheckParameterNotNull(value, "StringAssert.StartsWith", "value", string.Empty); - Assert.CheckParameterNotNull(substring, "StringAssert.StartsWith", "substring", string.Empty); + Assert.CheckParameterNotNull(value, "StringAssert.StartsWith", "value"); + Assert.CheckParameterNotNull(substring, "StringAssert.StartsWith", "substring"); if (!value.StartsWith(substring, comparisonType)) { string userMessage = Assert.BuildUserMessage(message); @@ -310,8 +310,8 @@ public static void EndsWith([NotNull] string? value, [NotNull] string? substring /// public static void EndsWith([NotNull] string? value, [NotNull] string? substring, StringComparison comparisonType, string? message) { - Assert.CheckParameterNotNull(value, "StringAssert.EndsWith", "value", string.Empty); - Assert.CheckParameterNotNull(substring, "StringAssert.EndsWith", "substring", string.Empty); + Assert.CheckParameterNotNull(value, "StringAssert.EndsWith", "value"); + Assert.CheckParameterNotNull(substring, "StringAssert.EndsWith", "substring"); if (!value.EndsWith(substring, comparisonType)) { string userMessage = Assert.BuildUserMessage(message); @@ -364,8 +364,8 @@ public static void Matches([NotNull] string? value, [NotNull] Regex? pattern) /// public static void Matches([NotNull] string? value, [NotNull] Regex? pattern, string? message) { - Assert.CheckParameterNotNull(value, "StringAssert.Matches", "value", string.Empty); - Assert.CheckParameterNotNull(pattern, "StringAssert.Matches", "pattern", string.Empty); + Assert.CheckParameterNotNull(value, "StringAssert.Matches", "value"); + Assert.CheckParameterNotNull(pattern, "StringAssert.Matches", "pattern"); if (!pattern.IsMatch(value)) { @@ -415,8 +415,8 @@ public static void DoesNotMatch([NotNull] string? value, [NotNull] Regex? patter /// public static void DoesNotMatch([NotNull] string? value, [NotNull] Regex? pattern, string? message) { - Assert.CheckParameterNotNull(value, "StringAssert.DoesNotMatch", "value", string.Empty); - Assert.CheckParameterNotNull(pattern, "StringAssert.DoesNotMatch", "pattern", string.Empty); + Assert.CheckParameterNotNull(value, "StringAssert.DoesNotMatch", "value"); + Assert.CheckParameterNotNull(pattern, "StringAssert.DoesNotMatch", "pattern"); if (pattern.IsMatch(value)) { diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index 0a398540b2..1e42a88f47 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1,3 +1,3 @@ #nullable enable -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsSingle(System.Collections.IEnumerable! collection, string? message = "", string! collectionExpression = "") -> object! -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsSingle(System.Func! predicate, System.Collections.IEnumerable! collection, string? message = "", string! predicateExpression = "", string! collectionExpression = "") -> object! +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsSingle(System.Collections.IEnumerable! collection, string? message = "", string! collectionExpression = "") -> object? +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsSingle(System.Func! predicate, System.Collections.IEnumerable! collection, string? message = "", string! predicateExpression = "", string! collectionExpression = "") -> object? diff --git a/src/TestFramework/TestFramework/Resources/FrameworkMessages.resx b/src/TestFramework/TestFramework/Resources/FrameworkMessages.resx index 6c020b112d..b31ee2ee76 100644 --- a/src/TestFramework/TestFramework/Resources/FrameworkMessages.resx +++ b/src/TestFramework/TestFramework/Resources/FrameworkMessages.resx @@ -230,7 +230,7 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. + The parameter '{0}' is invalid. The value cannot be null. Different number of elements. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf index eab65aa221..a25c467f23 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf @@ -314,8 +314,8 @@ Skutečnost: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Parametr '{0}' je neplatný. Hodnota nemůže být null. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Parametr '{0}' je neplatný. Hodnota nemůže být null. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf index f4afaa9934..e8804d2f3e 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf @@ -314,8 +314,8 @@ Tatsächlich: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Der {0}-Parameter ist ungültig. Der Wert kann nicht NULL sein. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Der {0}-Parameter ist ungültig. Der Wert kann nicht NULL sein. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf index c1dbf79e36..2ea479ed93 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf @@ -314,8 +314,8 @@ Real: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - El parámetro '{0}' no es válido. El valor no puede ser null. {1}. + The parameter '{0}' is invalid. The value cannot be null. + El parámetro '{0}' no es válido. El valor no puede ser null. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf index 1a61f44795..23bbfb22af 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf @@ -314,8 +314,8 @@ Réel : {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Le paramètre '{0}' n'est pas valide. La valeur ne peut pas être null. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Le paramètre '{0}' n'est pas valide. La valeur ne peut pas être null. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf index 0506e80a37..ebeb2c95e6 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf @@ -314,8 +314,8 @@ Effettivo: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Il parametro '{0}' non è valido. Il valore non può essere Null. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Il parametro '{0}' non è valido. Il valore non può essere Null. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf index 7f9a7f8ed8..e7afd5d6c9 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf @@ -314,8 +314,8 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - パラメーター '{0}' は無効です。値を null にすることはできません。{1}。 + The parameter '{0}' is invalid. The value cannot be null. + パラメーター '{0}' は無効です。値を null にすることはできません。 diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf index a650895266..45b953dab4 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf @@ -314,8 +314,8 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - '{0}' 매개 변수가 잘못되었습니다. 이 값은 null일 수 없습니다. {1}. + The parameter '{0}' is invalid. The value cannot be null. + '{0}' 매개 변수가 잘못되었습니다. 이 값은 null일 수 없습니다. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf index 1e73855a86..5d3191394e 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf @@ -314,8 +314,8 @@ Rzeczywiste: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Parametr „{0}” jest nieprawidłowy. Wartość nie może być równa null. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Parametr „{0}” jest nieprawidłowy. Wartość nie może być równa null. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pt-BR.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pt-BR.xlf index ae0177763d..ce04ba5c04 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pt-BR.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pt-BR.xlf @@ -314,8 +314,8 @@ Real: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - O parâmetro '{0}' é inválido. O valor não pode ser nulo. {1}. + The parameter '{0}' is invalid. The value cannot be null. + O parâmetro '{0}' é inválido. O valor não pode ser nulo. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ru.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ru.xlf index 23d579aea8..af62e296b8 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ru.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ru.xlf @@ -314,8 +314,8 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - Параметр "{0}" является недопустимым. Значение не может быть NULL. {1}. + The parameter '{0}' is invalid. The value cannot be null. + Параметр "{0}" является недопустимым. Значение не может быть NULL. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.tr.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.tr.xlf index 8621c19d21..569beb77c5 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.tr.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.tr.xlf @@ -314,8 +314,8 @@ Gerçekte olan: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - '{0}' parametresi geçersiz. Değer null olamaz. {1}. + The parameter '{0}' is invalid. The value cannot be null. + '{0}' parametresi geçersiz. Değer null olamaz. diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hans.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hans.xlf index 8587ad71a8..7161aa6780 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hans.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hans.xlf @@ -314,8 +314,8 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - 参数“{0}”无效。值不能为 NULL。{1}。 + The parameter '{0}' is invalid. The value cannot be null. + >参数“{0}”无效。值不能为 NULL。 diff --git a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hant.xlf b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hant.xlf index 18d8fe9c41..77b31c0f61 100644 --- a/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hant.xlf +++ b/src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hant.xlf @@ -314,8 +314,8 @@ Actual: {2} - The parameter '{0}' is invalid. The value cannot be null. {1}. - 參數 '{0}' 無效。此值不可為 null。{1}。 + The parameter '{0}' is invalid. The value cannot be null. + 參數 '{0}' 無效。此值不可為 null。 diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs index 8d35a540a8..374b04b766 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -417,7 +417,7 @@ public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException() Action action = () => Assert.Contains(20, collection, "Item 20 not found"); // Assert - action.Should().Throw().WithMessage("*20*"); + action.Should().Throw().WithMessage("Assert.Contains failed. Expected collection to contain the specified item. 'expected' expression: '20', 'collection' expression: 'collection'. Item 20 not found"); } /// @@ -528,7 +528,7 @@ public void Contains_WithComparer_ItemDoesNotExist_ThrowsException() Action action = () => Assert.Contains("cherry", collection, comparer, "Missing cherry"); // Assert - action.Should().Throw().WithMessage("*cherry*"); + action.Should().Throw().WithMessage("Assert.Contains failed. Expected collection to contain the specified item. 'expected' expression: '\"cherry\"', 'collection' expression: 'collection'. Missing cherry"); } /// @@ -574,7 +574,7 @@ public void Contains_Predicate_NoItemMatches_ThrowsException() Action action = () => Assert.Contains(IsEven, collection, "No even number found"); // Assert - action.Should().Throw().WithMessage("*even*"); + action.Should().Throw().WithMessage("Assert.Contains failed. Expected at least one item to match the predicate. 'predicate' expression: 'IsEven', 'collection' expression: 'collection'. No even number found"); } /// @@ -590,7 +590,7 @@ public void Contains_InNonGenericCollection_Predicate_NoItemMatches_ThrowsExcept Action action = () => Assert.Contains(IsEven, collection, "No even number found"); // Assert - action.Should().Throw().WithMessage("*even*"); + action.Should().Throw().WithMessage("Assert.Contains failed. Expected at least one item to match the predicate. 'predicate' expression: 'IsEven', 'collection' expression: 'collection'. No even number found"); } /// @@ -623,7 +623,7 @@ public void Contains_StringVersion_SubstringNotPresent_ThrowsException() Action action = () => Assert.Contains(substring, value, StringComparison.Ordinal, "Missing substring"); // Assert - action.Should().Throw().WithMessage("*lazy*"); + action.Should().Throw().WithMessage("Assert.Contains failed. String 'The quick brown fox' does not contain string 'lazy'. 'substring' expression: 'substring', 'value' expression: 'value'. Missing substring."); } public void Contains_HashSetWithCustomComparer_ItemExists_DoesNotThrow() @@ -636,6 +636,275 @@ public void Contains_HashSetWithCustomComparer_ItemExists_DoesNotThrow() action(); } + /// + /// Tests the Contains method (non-generic) when collection is null. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + + // Act + Action action = () => Assert.Contains(1, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'collection' is invalid. The value cannot be null."); + } + + /// + /// Tests the Contains method (non-generic) with comparer when collection is null. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_WithComparer_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + EqualityComparer comparer = EqualityComparer.Default; + + // Act + Action action = () => Assert.Contains(1, collection, comparer); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'collection' is invalid. The value cannot be null."); + } + + /// + /// Tests the Contains method (non-generic) with comparer when item exists. + /// + public void Contains_InNonGenericCollection_WithComparer_ItemExists_DoesNotThrow() + { + // Arrange + var collection = new ArrayList { "apple", "BANANA", "cherry" }; + StringComparer comparer = StringComparer.OrdinalIgnoreCase; + + // Act + Action action = () => Assert.Contains("banana", collection, comparer); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the Contains method (non-generic) with comparer when item does not exist. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_WithComparer_ItemDoesNotExist_ThrowsException() + { + // Arrange + var collection = new ArrayList { "apple", "cherry" }; + StringComparer comparer = StringComparer.OrdinalIgnoreCase; + + // Act + Action action = () => Assert.Contains("banana", collection, comparer); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. Expected collection to contain the specified item. 'expected' expression: '\"banana\"', 'collection' expression: 'collection'."); + } + + /// + /// Tests the Contains method (non-generic) with predicate when collection is null. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_Predicate_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + + // Act + Action action = () => Assert.Contains(Predicate, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'collection' is invalid. The value cannot be null."); + + // Local functions + static bool Predicate(object? x) => x is int i && i > 5; + } + + /// + /// Tests the Contains method (non-generic) with null predicate. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_NullPredicate_ThrowsException() + { + // Arrange + var collection = new ArrayList { 1, 2, 3 }; + Func predicate = null!; + + // Act + Action action = () => Assert.Contains(predicate, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'predicate' is invalid. The value cannot be null."); + } + + /// + /// Tests the Contains method (non-generic) with collection containing multiple nulls when searching for null. + /// + public void Contains_InNonGenericCollection_MultipleNulls_SearchingForNull_DoesNotThrow() + { + // Arrange + var collection = new ArrayList { null, 1, null, 2 }; + + // Act + Action action = () => Assert.Contains(x => x is null, collection); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the string Contains method when substring is null. + /// Expects an AssertFailedException. + /// + public void Contains_String_NullSubstring_ThrowsException() + { + // Arrange + string substring = null!; + string value = "test value"; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'substring' is invalid. The value cannot be null."); + } + + /// + /// Tests the string Contains method when value is null. + /// Expects an AssertFailedException. + /// + public void Contains_String_NullValue_ThrowsException() + { + // Arrange + string substring = "test"; + string value = null!; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'value' is invalid. The value cannot be null."); + } + + /// + /// Tests the string Contains method when both substring and value are null. + /// Expects an AssertFailedException. + /// + public void Contains_String_BothNull_ThrowsException() + { + // Arrange + string substring = null!; + string value = null!; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'value' is invalid. The value cannot be null."); + } + + /// + /// Tests the string Contains method with empty substring. + /// + public void Contains_String_EmptySubstring_DoesNotThrow() + { + // Arrange + string substring = string.Empty; + string value = "test value"; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + // Empty string is contained in any string + action.Should().NotThrow(); + } + + /// + /// Tests the string Contains method with empty value. + /// + public void Contains_String_EmptyValue_WithNonEmptySubstring_ThrowsException() + { + // Arrange + string substring = "test"; + string value = string.Empty; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. String '' does not contain string 'test'. 'substring' expression: 'substring', 'value' expression: 'value'.."); + } + + /// + /// Tests the string Contains method with both empty strings. + /// + public void Contains_String_BothEmpty_DoesNotThrow() + { + // Arrange + string substring = string.Empty; + string value = string.Empty; + + // Act + Action action = () => Assert.Contains(substring, value); + + // Assert + // Empty string contains empty string + action.Should().NotThrow(); + } + + /// + /// Tests the string Contains method with StringComparison when substring is null. + /// Expects an AssertFailedException. + /// + public void Contains_String_WithComparison_NullSubstring_ThrowsException() + { + // Arrange + string substring = null!; + string value = "test value"; + + // Act + Action action = () => Assert.Contains(substring, value, StringComparison.OrdinalIgnoreCase); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'substring' is invalid. The value cannot be null."); + } + + /// + /// Tests the string Contains method with StringComparison when value is null. + /// Expects an AssertFailedException. + /// + public void Contains_String_WithComparison_NullValue_ThrowsException() + { + // Arrange + string substring = "test"; + string value = null!; + + // Act + Action action = () => Assert.Contains(substring, value, StringComparison.OrdinalIgnoreCase); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'value' is invalid. The value cannot be null."); + } + + /// + /// Tests the Contains method (non-generic) with null comparer. + /// Expects an AssertFailedException. + /// + public void Contains_InNonGenericCollection_WithComparer_NullComparer_ThrowsException() + { + // Arrange + var collection = new ArrayList { "apple", "banana" }; + IEqualityComparer comparer = null!; + + // Act + Action action = () => Assert.Contains("apple", collection, comparer); + + // Assert + action.Should().Throw().WithMessage("Assert.Contains failed. The parameter 'comparer' is invalid. The value cannot be null."); + } + #endregion #region DoesNotContain Tests @@ -668,7 +937,7 @@ public void DoesNotContain_ValueExpected_ItemPresent_ThrowsException() Action action = () => Assert.DoesNotContain(10, collection, "Item 10 should not be found"); // Assert - action.Should().Throw().WithMessage("*10*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected collection to not contain the specified item. 'notExpected' expression: '10', 'collection' expression: 'collection'. Item 10 should not be found"); } /// @@ -699,7 +968,7 @@ public void DoesNotContain_InNonGenericCollection_ValueExpected_ItemPresent_Thro Action action = () => Assert.DoesNotContain(10, collection, "Assert.DoesNotContain failed. Expected collection to not contain the specified item. Item {0} should not be found"); // Assert - action.Should().Throw().WithMessage("*Assert.DoesNotContain failed. Expected collection to not contain the specified item. Item {0} should not be found*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected collection to not contain the specified item. 'notExpected' expression: '10', 'collection' expression: 'collection'. Assert.DoesNotContain failed. Expected collection to not contain the specified item. Item {0} should not be found"); } /// @@ -748,7 +1017,7 @@ public void DoesNotContain_WithComparer_ItemPresent_ThrowsException() Action action = () => Assert.DoesNotContain("APPLE", collection, comparer, "Unexpected \"APPLE\""); // Assert - action.Should().Throw().WithMessage("*APPLE*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected collection to not contain the specified item. 'notExpected' expression: '\"APPLE\"', 'collection' expression: 'collection'. Unexpected \"APPLE\""); } /// @@ -765,7 +1034,7 @@ public void DoesNotContain_InNonGenericCollection_WithComparer_ItemPresent_Throw Action action = () => Assert.DoesNotContain("APPLE", collection, comparer, "APPLE"); // Assert - action.Should().Throw().WithMessage("*APPLE*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected collection to not contain the specified item. 'notExpected' expression: '\"APPLE\"', 'collection' expression: 'collection'. APPLE"); } /// @@ -811,7 +1080,7 @@ public void DoesNotContain_Predicate_AtLeastOneItemMatches_ThrowsException() Action action = () => Assert.DoesNotContain(IsEven, collection, "An even number exists"); // Assert - action.Should().Throw().WithMessage("*even*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected no items to match the predicate. 'predicate' expression: 'IsEven', 'collection' expression: 'collection'. An even number exists"); } /// @@ -827,7 +1096,7 @@ public void DoesNotContain_InNonGenericCollection_Predicate_AtLeastOneItemMatche Action action = () => Assert.DoesNotContain(IsEven, collection, "An even number exists"); // Assert - action.Should().Throw().WithMessage("*even*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. Expected no items to match the predicate. 'predicate' expression: 'IsEven', 'collection' expression: 'collection'. An even number exists"); } /// @@ -860,7 +1129,7 @@ public void DoesNotContain_StringVersion_SubstringPresent_ThrowsException() Action action = () => Assert.DoesNotContain(substring, value, StringComparison.Ordinal, "Unexpected substring"); // Assert - action.Should().Throw().WithMessage("*brown*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. String 'The quick brown fox' does contain string 'brown'. 'substring' expression: 'substring', 'value' expression: 'value'. Unexpected substring."); } /// @@ -895,7 +1164,7 @@ public void DoesNotContain_StringWithComparisonAndMessage_SubstringPresent_Throw Action action = () => Assert.DoesNotContain(substring, value, StringComparison.OrdinalIgnoreCase, "Found unexpected substring"); // Assert - action.Should().Throw().WithMessage("*Found unexpected substring*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. String 'The quick brown fox' does contain string 'BROWN'. 'substring' expression: 'substring', 'value' expression: 'value'. Found unexpected substring."); } /// @@ -928,7 +1197,7 @@ public void DoesNotContain_StringSimpleOverload_SubstringPresent_ThrowsException Action action = () => Assert.DoesNotContain(substring, value); // Assert - action.Should().Throw().WithMessage("*brown*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. String 'The quick brown fox' does contain string 'brown'. 'substring' expression: 'substring', 'value' expression: 'value'.."); } /// @@ -961,7 +1230,161 @@ public void DoesNotContain_StringWithMessageOnly_SubstringPresent_ThrowsExceptio Action action = () => Assert.DoesNotContain(substring, value, "Found unexpected substring"); // Assert - action.Should().Throw().WithMessage("*Found unexpected substring*"); + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. String 'The quick brown fox' does contain string 'brown'. 'substring' expression: 'substring', 'value' expression: 'value'. Found unexpected substring."); + } + + /// + /// Tests the DoesNotContain method (non-generic) when collection is null. + /// Expects an AssertFailedException. + /// + public void DoesNotContain_InNonGenericCollection_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + + // Act + Action action = () => Assert.DoesNotContain(1, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'collection' is invalid. The value cannot be null."); + } + + /// + /// Tests the DoesNotContain method (non-generic) with comparer when collection is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_InNonGenericCollection_WithComparer_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + EqualityComparer comparer = EqualityComparer.Default; + + // Act + Action action = () => Assert.DoesNotContain(1, collection, comparer); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'collection' is invalid. The value cannot be null."); + } + + /// + /// Tests the DoesNotContain method (non-generic) with null comparer. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_InNonGenericCollection_WithComparer_NullComparer_ThrowsException() + { + // Arrange + var collection = new ArrayList { "apple", "banana" }; + IEqualityComparer comparer = null!; + + // Act + Action action = () => Assert.DoesNotContain("cherry", collection, comparer); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'comparer' is invalid. The value cannot be null."); + } + + /// + /// Tests the DoesNotContain method (non-generic) with predicate when collection is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_InNonGenericCollection_Predicate_NullCollection_ThrowsException() + { + // Arrange + IEnumerable collection = null!; + + // Act + Action action = () => Assert.DoesNotContain(Predicate, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'collection' is invalid. The value cannot be null."); + + // Local functions + static bool Predicate(object? x) => x is int i && i > 5; + } + + /// + /// Tests the DoesNotContain method (non-generic) with null predicate. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_InNonGenericCollection_NullPredicate_ThrowsException() + { + // Arrange + var collection = new ArrayList { 1, 2, 3 }; + Func predicate = null!; + + // Act + Action action = () => Assert.DoesNotContain(predicate, collection); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'predicate' is invalid. The value cannot be null."); + } + + /// + /// Tests the string DoesNotContain method when substring is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_String_NullSubstring_ThrowsException() + { + // Arrange + string substring = null!; + string value = "test value"; + + // Act + Action action = () => Assert.DoesNotContain(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'substring' is invalid. The value cannot be null."); + } + + /// + /// Tests the string DoesNotContain method when value is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_String_NullValue_ThrowsException() + { + // Arrange + string substring = "test"; + string value = null!; + + // Act + Action action = () => Assert.DoesNotContain(substring, value); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'value' is invalid. The value cannot be null."); + } + + /// + /// Tests the string DoesNotContain method with StringComparison when substring is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_String_WithComparison_NullSubstring_ThrowsException() + { + // Arrange + string substring = null!; + string value = "test value"; + + // Act + Action action = () => Assert.DoesNotContain(substring, value, StringComparison.OrdinalIgnoreCase); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'substring' is invalid. The value cannot be null."); + } + + /// + /// Tests the string DoesNotContain method with StringComparison when value is null. + /// Expects an ArgumentNullException. + /// + public void DoesNotContain_String_WithComparison_NullValue_ThrowsException() + { + // Arrange + string substring = "test"; + string value = null!; + + // Act + Action action = () => Assert.DoesNotContain(substring, value, StringComparison.OrdinalIgnoreCase); + + // Assert + action.Should().Throw().WithMessage("Assert.DoesNotContain failed. The parameter 'value' is invalid. The value cannot be null."); } private static bool IsEven(int x) => x % 2 == 0; @@ -996,7 +1419,7 @@ public void ContainsSinglePredicate_InNonGenericCollection_NoMessage_OneItemMatc var collection = new ArrayList { 1, 2, 3, 4, 5, "a" }; // Act - object result = Assert.ContainsSingle(x => x.Equals(3), collection); + object? result = Assert.ContainsSingle(x => x.Equals(3), collection); // Assert result.Should().Be(3); @@ -1029,7 +1452,7 @@ public void ContainsSinglePredicate_InNonGenericCollection_WithMessage_OneItemMa // Act #pragma warning disable CA1865 // Use char overload - not netfx - object result = Assert.ContainsSingle(x => x is string s && s.StartsWith("b", StringComparison.Ordinal), collection, "Expected one item starting with 'b'"); + object? result = Assert.ContainsSingle(x => x is string s && s.StartsWith("b", StringComparison.Ordinal), collection, "Expected one item starting with 'b'"); #pragma warning restore CA1865 // Use char overload // Assert @@ -1049,7 +1472,8 @@ public void ContainsSinglePredicate_NoItemMatches_ThrowsException() Action action = () => Assert.ContainsSingle(x => x % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected exactly one item to match the predicate but found 0 item(s)*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 0 item(s). 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'."); } /// @@ -1065,7 +1489,8 @@ public void ContainsSinglePredicate_InNonGenericCollection_NoItemMatches_ThrowsE Action action = () => Assert.ContainsSingle(x => x is int i && i % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected exactly one item to match the predicate but found 0 item(s)*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 0 item(s). 'predicate' expression: 'x => x is int i && i % 2 == 0', 'collection' expression: 'collection'."); } /// @@ -1081,7 +1506,8 @@ public void ContainsSinglePredicate_MultipleItemsMatch_ThrowsException() Action action = () => Assert.ContainsSingle(x => x % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected exactly one item to match the predicate but found 4 item(s)*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 4 item(s). 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'."); } /// @@ -1091,13 +1517,14 @@ public void ContainsSinglePredicate_MultipleItemsMatch_ThrowsException() public void ContainsSinglePredicate_InNonGenericCollection_MultipleItemsMatch_ThrowsException() { // Arrange - var collection = new ArrayList { 2, 4, 6, 8, "a" }; + var collection = new ArrayList { 2, 4, "a" }; // Act Action action = () => Assert.ContainsSingle(x => x is int i && i % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected exactly one item to match the predicate but found * item(s)*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 2 item(s). 'predicate' expression: 'x => x is int i && i % 2 == 0', 'collection' expression: 'collection'."); } /// @@ -1113,7 +1540,8 @@ public void ContainsSinglePredicate_WithMessage_NoItemMatches_ThrowsException() Action action = () => Assert.ContainsSingle(x => x % 2 == 0, collection, $"No even numbers found in collection with {collection.Count} items"); // Assert - action.Should().Throw().WithMessage("*No even numbers found in collection with 3 items*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 0 item(s). 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'. No even numbers found in collection with 3 items"); } /// @@ -1129,7 +1557,8 @@ public void ContainsSinglePredicate_InNonGenericCollection_WithMessage_NoItemMat Action action = () => Assert.ContainsSingle(x => x is int i && i % 2 == 0, collection, $"No even numbers found in collection with {collection.Count} items"); // Assert - action.Should().Throw().WithMessage("*No even numbers found in collection with 3 items*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 0 item(s). 'predicate' expression: 'x => x is int i && i % 2 == 0', 'collection' expression: 'collection'. No even numbers found in collection with 3 items"); } /// @@ -1145,7 +1574,8 @@ public void ContainsSinglePredicate_WithMessage_MultipleItemsMatch_ThrowsExcepti Action action = () => Assert.ContainsSingle(x => x % 2 == 0, collection, $"Too many even numbers found: {collection.Count}"); // Assert - action.Should().Throw().WithMessage("*Too many even numbers found: 3*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 3 item(s). 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'. Too many even numbers found: 3"); } /// @@ -1155,13 +1585,14 @@ public void ContainsSinglePredicate_WithMessage_MultipleItemsMatch_ThrowsExcepti public void ContainsSinglePredicate_InNonGenericCollection_WithMessage_MultipleItemsMatch_ThrowsException() { // Arrange - var collection = new ArrayList { 2, 4, 6 }; + var collection = new ArrayList { 2, 4, "a" }; // Act - Action action = () => Assert.ContainsSingle(x => x is int i && i % 2 == 0, collection, $"Too many even numbers found: {collection.Count}"); + Action action = () => Assert.ContainsSingle(x => x is int i && i % 2 == 0, collection, "Too many even numbers found: 2"); // Assert - action.Should().Throw().WithMessage("*Too many even numbers found: 3*"); + action.Should().Throw() + .WithMessage("Assert.ContainsSingle failed. Expected exactly one item to match the predicate but found 2 item(s). 'predicate' expression: 'x => x is int i && i % 2 == 0', 'collection' expression: 'collection'. Too many even numbers found: 2"); } /// @@ -1199,9 +1630,10 @@ public void ContainsSinglePredicate_InNonGenericCollection_ComplexObjects_OneIte }; // Act - object result = Assert.ContainsSingle(p => p is Person person && person.Age == 30, items); + object? result = Assert.ContainsSingle(p => p is Person person && person.Age == 30, items); // Assert + result.Should().BeOfType(); var resultPerson = (Person)result; resultPerson.Name.Should().Be("Bob"); resultPerson.Age.Should().Be(30); @@ -1231,7 +1663,7 @@ public void ContainsSinglePredicate_InNonGenericCollection_WithNullValues_OneIte var collection = new ArrayList { "apple", null, "banana" }; // Act - object result = Assert.ContainsSingle(x => x == null, collection); + object? result = Assert.ContainsSingle(x => x == null, collection); // Assert result.Should().BeNull(); @@ -1251,7 +1683,8 @@ public void Contains_ItemNotFound_ShowsSpecificErrorMessage() Action action = () => Assert.Contains(5, collection); // Assert - action.Should().Throw().WithMessage("*Expected collection to contain the specified item*"); + action.Should().Throw() + .WithMessage("Assert.Contains failed. Expected collection to contain the specified item. 'expected' expression: '5', 'collection' expression: 'collection'."); } /// @@ -1266,7 +1699,8 @@ public void Contains_InNonGenericCollection_ItemNotFound_ShowsSpecificErrorMessa Action action = () => Assert.Contains(5, collection); // Assert - action.Should().Throw().WithMessage("*Expected collection to contain the specified item*"); + action.Should().Throw() + .WithMessage("Assert.Contains failed. Expected collection to contain the specified item. 'expected' expression: '5', 'collection' expression: 'collection'."); } /// @@ -1281,7 +1715,8 @@ public void Contains_PredicateNotMatched_ShowsSpecificErrorMessage() Action action = () => Assert.Contains(x => x % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected at least one item to match the predicate*"); + action.Should().Throw() + .WithMessage("Assert.Contains failed. Expected at least one item to match the predicate. 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'."); } /// @@ -1296,7 +1731,8 @@ public void DoesNotContain_ItemFound_ShowsSpecificErrorMessage() Action action = () => Assert.DoesNotContain(2, collection); // Assert - action.Should().Throw().WithMessage("*Expected collection to not contain the specified item*"); + action.Should().Throw() + .WithMessage("Assert.DoesNotContain failed. Expected collection to not contain the specified item. 'notExpected' expression: '2', 'collection' expression: 'collection'."); } /// @@ -1311,7 +1747,8 @@ public void DoesNotContain_PredicateMatched_ShowsSpecificErrorMessage() Action action = () => Assert.DoesNotContain(x => x % 2 == 0, collection); // Assert - action.Should().Throw().WithMessage("*Expected no items to match the predicate*"); + action.Should().Throw() + .WithMessage("Assert.DoesNotContain failed. Expected no items to match the predicate. 'predicate' expression: 'x => x % 2 == 0', 'collection' expression: 'collection'."); } public void DoesNotContains_HashSetWithCustomComparer_ItemDoesNotExist_DoesNotThrow()