Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -1316,7 +1316,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase,
/// </exception>
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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1412,7 +1412,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC
/// </exception>
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;
Expand Down
58 changes: 41 additions & 17 deletions src/TestFramework/TestFramework/Assertions/Assert.Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <returns>The item.</returns>
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);

/// <summary>
Expand Down Expand Up @@ -192,33 +192,35 @@
/// Users shouldn't pass a value for this parameter.
/// </param>
/// <returns>The item that matches the predicate.</returns>
public static object ContainsSingle(Func<object, bool> predicate, IEnumerable collection, string? message = "", [CallerArgumentExpression(nameof(predicate))] string predicateExpression = "", [CallerArgumentExpression(nameof(collection))] string collectionExpression = "")
public static object? ContainsSingle(Func<object, bool> 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))
Expand All @@ -232,7 +234,7 @@
ThrowAssertSingleMatchFailed(matchCount, userMessage);
}

return default!;
return default;
}

#endregion // ContainsSingle
Expand Down Expand Up @@ -279,6 +281,8 @@
/// </param>
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))
Expand Down Expand Up @@ -333,6 +337,9 @@
/// </param>
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))
Expand Down Expand Up @@ -385,6 +392,9 @@
/// </param>
public static void Contains(Func<object?, bool> 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))
Expand All @@ -392,7 +402,7 @@
return;
}
}

Check failure on line 405 in src/TestFramework/TestFramework/Assertions/Assert.Contains.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/TestFramework/TestFramework/Assertions/Assert.Contains.cs#L405

src/TestFramework/TestFramework/Assertions/Assert.Contains.cs(405,1): error : [Timeout_WhenTimeoutValueSmallerThanTestDuration_OutputContainsCancelingMessage ("net10.0")] Assert.Contains failed. String 'MSTest v4.1.0-ci (UTC 12/9/2025) [osx-x64 - .NET 10.0.0] Test run summary: Aborted - /Users/runner/work/1/s/artifacts/tmp/Release/testsuite/OQMGu/TimeoutTest/bin/Release/net10.0/TimeoutTest.dll (net10.0|x64) total: 0 failed: 0 succeeded: 0 skipped: 0 duration: 389ms' does not contain string 'Canceling the test session'. 'substring' expression: 'value', 'value' expression: 'testHostResult.StandardOutput'. Expression 'AssertOutputContains' failed for member 'Timeout_WhenTimeoutValueSmallerThanTestDuration_OutputContainsCancelingMessage' at line 466 of file '/_/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TimeoutTests.cs'. Output of the test host is: Command: /Users/runner/work/1/s/artifacts/tmp/Release/testsuite/OQMGu/TimeoutTest/bin/Release/net10.0/TimeoutTest --timeout 1s ==================== ExitCode: 3 ==================== StandardOutput: MSTest v4.1.0-ci (UTC 12/9/2025) [osx-x64 - .NET 10.0.0] Test run summary: Aborted - /Users/runner/work/1/s/artifacts/tmp/Release/testsuite/OQMGu/TimeoutTest/bin/Release/net10.0/TimeoutTest.dll (net10.0|x64) total: 0 failed: 0 succeeded: 0 skipped: 0 duration: 389ms ==================== StandardError: .
string userMessage = BuildUserMessageForPredicateExpressionAndCollectionExpression(message, predicateExpression, collectionExpression);
ThrowAssertContainsPredicateFailed(userMessage);
}
Expand Down Expand Up @@ -461,6 +471,9 @@
/// </exception>
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);
Expand Down Expand Up @@ -513,6 +526,8 @@
/// </param>
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))
Expand Down Expand Up @@ -565,6 +580,9 @@
/// </param>
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))
Expand Down Expand Up @@ -615,6 +633,9 @@
/// </param>
public static void DoesNotContain(Func<object?, bool> 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))
Expand Down Expand Up @@ -689,6 +710,9 @@
/// </exception>
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);
Expand Down
8 changes: 4 additions & 4 deletions src/TestFramework/TestFramework/Assertions/Assert.EndsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public static void EndsWith([NotNull] string? expectedSuffix, [NotNull] string?
/// </exception>
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);
Expand Down Expand Up @@ -146,8 +146,8 @@ public static void DoesNotEndWith([NotNull] string? notExpectedSuffix, [NotNull]
/// </exception>
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);
Expand Down
10 changes: 5 additions & 5 deletions src/TestFramework/TestFramework/Assertions/Assert.Matches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public sealed partial class Assert
/// </exception>
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))
{
Expand Down Expand Up @@ -115,8 +115,8 @@ public static void MatchesRegex([NotNull] string? pattern, [NotNull] string? val
/// </exception>
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))
{
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public static void StartsWith([NotNull] string? expectedPrefix, [NotNull] string
/// </exception>
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);
Expand Down Expand Up @@ -144,8 +144,8 @@ public static void DoesNotStartWith([NotNull] string? notExpectedPrefix, [NotNul
/// </exception>
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);
Expand Down
8 changes: 2 additions & 6 deletions src/TestFramework/TestFramework/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,11 @@ private static string BuildUserMessageForMinValueExpressionAndMaxValueExpression
/// <param name="parameterName">
/// parameter name.
/// </param>
/// <param name="message">
/// message for the invalid parameter exception.
/// </param>
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);
}
}
Expand Down
Loading
Loading