From 4adcb838dbc4dbeeaab9d7d5f0cae42580c26996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 16 Dec 2022 13:34:34 +0100 Subject: [PATCH] Allow most APIs to accept nullable values or arguments (#1467) Fixes https://github.com/microsoft/testfx/issues/1451 --- .../Assertions/Assert.AreEqual.cs | 170 ++++------ .../Assertions/Assert.AreSame.cs | 17 +- .../TestFramework/Assertions/Assert.Fail.cs | 12 +- .../Assertions/Assert.Inconclusive.cs | 11 +- .../Assertions/Assert.IsInstanceOfType.cs | 52 +-- .../TestFramework/Assertions/Assert.IsNull.cs | 16 +- .../TestFramework/Assertions/Assert.IsTrue.cs | 44 +-- .../Assertions/Assert.ThrowsException.cs | 75 ++-- .../TestFramework/Assertions/Assert.cs | 17 +- .../Assertions/CollectionAssert.cs | 320 ++++++++---------- .../TestFramework/Assertions/StringAssert.cs | 229 ++++++------- .../PublicAPI/PublicAPI.Shipped.txt | 69 ---- .../PublicAPI/PublicAPI.Unshipped.txt | 69 ++++ .../Assertions/AssertTests.AreEqualTests.cs | 61 +++- .../Assertions/CollectionAssertTests.cs | 252 ++++++++++++++ .../Assertions/StringAssertTests.cs | 248 +++++++++++++- 16 files changed, 1012 insertions(+), 650 deletions(-) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs b/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs index b472d1f6b2..87d2c0ba75 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace Microsoft.VisualStudio.TestTools.UnitTesting; @@ -173,7 +174,8 @@ public static void AreEqual(T? expected, T? actual, string? message, params o /// Thrown if is not equal to /// . /// - public static void AreEqual(T? expected, T? actual, IEqualityComparer? comparer, string? message, params object?[]? parameters) + public static void AreEqual(T? expected, T? actual, IEqualityComparer? comparer, + string? message, params object?[]? parameters) { var localComparer = comparer ?? EqualityComparer.Default; if (localComparer.Equals(expected!, actual!)) @@ -362,7 +364,8 @@ public static void AreNotEqual(T? notExpected, T? actual, string? message, pa /// /// Thrown if is equal to . /// - public static void AreNotEqual(T? notExpected, T? actual, IEqualityComparer? comparer, string? message, params object?[]? parameters) + public static void AreNotEqual(T? notExpected, T? actual, IEqualityComparer? comparer, + string? message, params object?[]? parameters) { var localComparer = comparer ?? EqualityComparer.Default; if (!localComparer.Equals(notExpected!, actual!)) @@ -400,9 +403,7 @@ public static void AreNotEqual(T? notExpected, T? actual, IEqualityComparer. /// public static void AreEqual(float expected, float actual, float delta) - { - AreEqual(expected, actual, delta, string.Empty, null); - } + => AreEqual(expected, actual, delta, string.Empty, null); /// /// Tests whether the specified floats are equal and throws an exception @@ -429,9 +430,7 @@ public static void AreEqual(float expected, float actual, float delta) /// . /// public static void AreEqual(float expected, float actual, float delta, string? message) - { - AreEqual(expected, actual, delta, message, null); - } + => AreEqual(expected, actual, delta, message, null); /// /// Tests whether the specified floats are equal and throws an exception @@ -460,7 +459,8 @@ public static void AreEqual(float expected, float actual, float delta, string? m /// Thrown if is not equal to /// . /// - public static void AreEqual(float expected, float actual, float delta, string? message, params object?[]? parameters) + public static void AreEqual(float expected, float actual, float delta, string? message, + params object?[]? parameters) { if (float.IsNaN(expected) || float.IsNaN(actual) || float.IsNaN(delta)) { @@ -509,9 +509,7 @@ public static void AreEqual(float expected, float actual, float delta, string? m /// Thrown if is equal to . /// public static void AreNotEqual(float notExpected, float actual, float delta) - { - AreNotEqual(notExpected, actual, delta, string.Empty, null); - } + => AreNotEqual(notExpected, actual, delta, string.Empty, null); /// /// Tests whether the specified floats are unequal and throws an exception @@ -538,9 +536,7 @@ public static void AreNotEqual(float notExpected, float actual, float delta) /// Thrown if is equal to . /// public static void AreNotEqual(float notExpected, float actual, float delta, string? message) - { - AreNotEqual(notExpected, actual, delta, message, null); - } + => AreNotEqual(notExpected, actual, delta, message, null); /// /// Tests whether the specified floats are unequal and throws an exception @@ -569,7 +565,8 @@ public static void AreNotEqual(float notExpected, float actual, float delta, str /// /// Thrown if is equal to . /// - public static void AreNotEqual(float notExpected, float actual, float delta, string? message, params object?[]? parameters) + public static void AreNotEqual(float notExpected, float actual, float delta, string? message, + params object?[]? parameters) { if (Math.Abs(notExpected - actual) <= delta) { @@ -605,9 +602,7 @@ public static void AreNotEqual(float notExpected, float actual, float delta, str /// . /// public static void AreEqual(decimal expected, decimal actual, decimal delta) - { - AreEqual(expected, actual, delta, string.Empty, null); - } + => AreEqual(expected, actual, delta, string.Empty, null); /// /// Tests whether the specified decimals are equal and throws an exception @@ -634,9 +629,7 @@ public static void AreEqual(decimal expected, decimal actual, decimal delta) /// . /// public static void AreEqual(decimal expected, decimal actual, decimal delta, string? message) - { - AreEqual(expected, actual, delta, message, null); - } + => AreEqual(expected, actual, delta, message, null); /// /// Tests whether the specified decimals are equal and throws an exception @@ -665,7 +658,8 @@ public static void AreEqual(decimal expected, decimal actual, decimal delta, str /// Thrown if is not equal to /// . /// - public static void AreEqual(decimal expected, decimal actual, decimal delta, string? message, params object?[]? parameters) + public static void AreEqual(decimal expected, decimal actual, decimal delta, string? message, + params object?[]? parameters) { if (Math.Abs(expected - actual) > delta) { @@ -701,9 +695,7 @@ public static void AreEqual(decimal expected, decimal actual, decimal delta, str /// Thrown if is equal to . /// public static void AreNotEqual(decimal notExpected, decimal actual, decimal delta) - { - AreNotEqual(notExpected, actual, delta, string.Empty, null); - } + => AreNotEqual(notExpected, actual, delta, string.Empty, null); /// /// Tests whether the specified decimals are unequal and throws an exception @@ -730,9 +722,7 @@ public static void AreNotEqual(decimal notExpected, decimal actual, decimal delt /// Thrown if is equal to . /// public static void AreNotEqual(decimal notExpected, decimal actual, decimal delta, string? message) - { - AreNotEqual(notExpected, actual, delta, message, null); - } + => AreNotEqual(notExpected, actual, delta, message, null); /// /// Tests whether the specified decimals are unequal and throws an exception @@ -761,7 +751,8 @@ public static void AreNotEqual(decimal notExpected, decimal actual, decimal delt /// /// Thrown if is equal to . /// - public static void AreNotEqual(decimal notExpected, decimal actual, decimal delta, string? message, params object?[]? parameters) + public static void AreNotEqual(decimal notExpected, decimal actual, decimal delta, string? message, + params object?[]? parameters) { if (Math.Abs(notExpected - actual) <= delta) { @@ -797,9 +788,7 @@ public static void AreNotEqual(decimal notExpected, decimal actual, decimal delt /// . /// public static void AreEqual(long expected, long actual, long delta) - { - AreEqual(expected, actual, delta, string.Empty, null); - } + => AreEqual(expected, actual, delta, string.Empty, null); /// /// Tests whether the specified longs are equal and throws an exception @@ -826,9 +815,7 @@ public static void AreEqual(long expected, long actual, long delta) /// . /// public static void AreEqual(long expected, long actual, long delta, string? message) - { - AreEqual(expected, actual, delta, message, null); - } + => AreEqual(expected, actual, delta, message, null); /// /// Tests whether the specified longs are equal and throws an exception @@ -857,7 +844,8 @@ public static void AreEqual(long expected, long actual, long delta, string? mess /// Thrown if is not equal to /// . /// - public static void AreEqual(long expected, long actual, long delta, string? message, params object?[]? parameters) + public static void AreEqual(long expected, long actual, long delta, string? message, + params object?[]? parameters) { if (Math.Abs(expected - actual) > delta) { @@ -893,9 +881,7 @@ public static void AreEqual(long expected, long actual, long delta, string? mess /// Thrown if is equal to . /// public static void AreNotEqual(long notExpected, long actual, long delta) - { - AreNotEqual(notExpected, actual, delta, string.Empty, null); - } + => AreNotEqual(notExpected, actual, delta, string.Empty, null); /// /// Tests whether the specified longs are unequal and throws an exception @@ -922,9 +908,7 @@ public static void AreNotEqual(long notExpected, long actual, long delta) /// Thrown if is equal to . /// public static void AreNotEqual(long notExpected, long actual, long delta, string? message) - { - AreNotEqual(notExpected, actual, delta, message, null); - } + => AreNotEqual(notExpected, actual, delta, message, null); /// /// Tests whether the specified longs are unequal and throws an exception @@ -953,7 +937,8 @@ public static void AreNotEqual(long notExpected, long actual, long delta, string /// /// Thrown if is equal to . /// - public static void AreNotEqual(long notExpected, long actual, long delta, string? message, params object?[]? parameters) + public static void AreNotEqual(long notExpected, long actual, long delta, string? message, + params object?[]? parameters) { if (Math.Abs(notExpected - actual) <= delta) { @@ -989,9 +974,7 @@ public static void AreNotEqual(long notExpected, long actual, long delta, string /// . /// public static void AreEqual(double expected, double actual, double delta) - { - AreEqual(expected, actual, delta, string.Empty, null); - } + => AreEqual(expected, actual, delta, string.Empty, null); /// /// Tests whether the specified doubles are equal and throws an exception @@ -1017,9 +1000,7 @@ public static void AreEqual(double expected, double actual, double delta) /// Thrown if is not equal to . /// public static void AreEqual(double expected, double actual, double delta, string? message) - { - AreEqual(expected, actual, delta, message, null); - } + => AreEqual(expected, actual, delta, message, null); /// /// Tests whether the specified doubles are equal and throws an exception @@ -1047,7 +1028,8 @@ public static void AreEqual(double expected, double actual, double delta, string /// /// Thrown if is not equal to . /// - public static void AreEqual(double expected, double actual, double delta, string? message, params object?[]? parameters) + public static void AreEqual(double expected, double actual, double delta, string? message, + params object?[]? parameters) { if (double.IsNaN(expected) || double.IsNaN(actual) || double.IsNaN(delta)) { @@ -1096,9 +1078,7 @@ public static void AreEqual(double expected, double actual, double delta, string /// Thrown if is equal to . /// public static void AreNotEqual(double notExpected, double actual, double delta) - { - AreNotEqual(notExpected, actual, delta, string.Empty, null); - } + => AreNotEqual(notExpected, actual, delta, string.Empty, null); /// /// Tests whether the specified doubles are unequal and throws an exception @@ -1125,9 +1105,7 @@ public static void AreNotEqual(double notExpected, double actual, double delta) /// Thrown if is equal to . /// public static void AreNotEqual(double notExpected, double actual, double delta, string? message) - { - AreNotEqual(notExpected, actual, delta, message, null); - } + => AreNotEqual(notExpected, actual, delta, message, null); /// /// Tests whether the specified doubles are unequal and throws an exception @@ -1156,7 +1134,8 @@ public static void AreNotEqual(double notExpected, double actual, double delta, /// /// Thrown if is equal to . /// - public static void AreNotEqual(double notExpected, double actual, double delta, string? message, params object?[]? parameters) + public static void AreNotEqual(double notExpected, double actual, double delta, string? message, + params object?[]? parameters) { if (Math.Abs(notExpected - actual) <= delta) { @@ -1190,9 +1169,7 @@ public static void AreNotEqual(double notExpected, double actual, double delta, /// Thrown if is not equal to . /// public static void AreEqual(string? expected, string? actual, bool ignoreCase) - { - AreEqual(expected, actual, ignoreCase, string.Empty, null); - } + => AreEqual(expected, actual, ignoreCase, string.Empty, null); /// /// Tests whether the specified strings are equal and throws an exception @@ -1217,9 +1194,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase) /// Thrown if is not equal to . /// public static void AreEqual(string? expected, string? actual, bool ignoreCase, string? message) - { - AreEqual(expected, actual, ignoreCase, message, null); - } + => AreEqual(expected, actual, ignoreCase, message, null); /// /// Tests whether the specified strings are equal and throws an exception @@ -1246,10 +1221,9 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, s /// /// Thrown if is not equal to . /// - public static void AreEqual(string? expected, string? actual, bool ignoreCase, string? message, params object?[]? parameters) - { - AreEqual(expected, actual, ignoreCase, CultureInfo.InvariantCulture, message, parameters); - } + public static void AreEqual(string? expected, string? actual, bool ignoreCase, string? message, + params object?[]? parameters) + => AreEqual(expected, actual, ignoreCase, CultureInfo.InvariantCulture, message, parameters); /// /// Tests whether the specified strings are equal and throws an exception @@ -1266,15 +1240,14 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, s /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// Thrown if is not equal to . /// - public static void AreEqual(string? expected, string? actual, bool ignoreCase, CultureInfo culture) - { - AreEqual(expected, actual, ignoreCase, culture, string.Empty, null); - } + public static void AreEqual(string? expected, string? actual, bool ignoreCase, + [NotNull] CultureInfo? culture) + => AreEqual(expected, actual, ignoreCase, culture, string.Empty, null); /// /// Tests whether the specified strings are equal and throws an exception @@ -1291,7 +1264,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, C /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// The message to include in the exception when @@ -1301,10 +1274,9 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, C /// /// Thrown if is not equal to . /// - public static void AreEqual(string? expected, string? actual, bool ignoreCase, CultureInfo culture, string? message) - { - AreEqual(expected, actual, ignoreCase, culture, message, null); - } + public static void AreEqual(string? expected, string? actual, bool ignoreCase, + [NotNull] CultureInfo? culture, string? message) + => AreEqual(expected, actual, ignoreCase, culture, message, null); /// /// Tests whether the specified strings are equal and throws an exception @@ -1321,7 +1293,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, C /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// The message to include in the exception when @@ -1334,7 +1306,8 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, C /// /// Thrown if is not equal to . /// - public static void AreEqual(string? expected, string? actual, bool ignoreCase, CultureInfo culture, string? message, params object?[]? parameters) + public static void AreEqual(string? expected, string? actual, bool ignoreCase, + [NotNull] CultureInfo? culture, string? message, params object?[]? parameters) { CheckParameterNotNull(culture, "Assert.AreEqual", "culture", string.Empty); if (CompareInternal(expected, actual, ignoreCase, culture) == 0) @@ -1388,9 +1361,7 @@ public static void AreEqual(string? expected, string? actual, bool ignoreCase, C /// Thrown if is equal to . /// public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase) - { - AreNotEqual(notExpected, actual, ignoreCase, string.Empty, null); - } + => AreNotEqual(notExpected, actual, ignoreCase, string.Empty, null); /// /// Tests whether the specified strings are unequal and throws an exception @@ -1416,9 +1387,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// Thrown if is equal to . /// public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, string? message) - { - AreNotEqual(notExpected, actual, ignoreCase, message, null); - } + => AreNotEqual(notExpected, actual, ignoreCase, message, null); /// /// Tests whether the specified strings are unequal and throws an exception @@ -1446,10 +1415,9 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// /// Thrown if is equal to . /// - public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, string? message, params object?[]? parameters) - { - AreNotEqual(notExpected, actual, ignoreCase, CultureInfo.InvariantCulture, message, parameters); - } + public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, + string? message, params object?[]? parameters) + => AreNotEqual(notExpected, actual, ignoreCase, CultureInfo.InvariantCulture, message, parameters); /// /// Tests whether the specified strings are unequal and throws an exception @@ -1467,15 +1435,13 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// Thrown if is equal to . /// - public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, CultureInfo culture) - { - AreNotEqual(notExpected, actual, ignoreCase, culture, string.Empty, null); - } + public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, CultureInfo? culture) + => AreNotEqual(notExpected, actual, ignoreCase, culture, string.Empty, null); /// /// Tests whether the specified strings are unequal and throws an exception @@ -1493,7 +1459,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// The message to include in the exception when @@ -1503,10 +1469,9 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// /// Thrown if is equal to . /// - public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, CultureInfo culture, string? message) - { - AreNotEqual(notExpected, actual, ignoreCase, culture, message, null); - } + public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, + CultureInfo? culture, string? message) + => AreNotEqual(notExpected, actual, ignoreCase, culture, message, null); /// /// Tests whether the specified strings are unequal and throws an exception @@ -1524,7 +1489,7 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// indicates a case-insensitive comparison.) /// /// - /// A CultureInfo object that supplies culture-specific comparison information. + /// A CultureInfo object that supplies culture-specific comparison information. If culture is null, the current culture is used. /// /// /// The message to include in the exception when @@ -1537,7 +1502,8 @@ public static void AreNotEqual(string? notExpected, string? actual, bool ignoreC /// /// Thrown if is equal to . /// - public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, CultureInfo culture, string? message, params object?[]? parameters) + public static void AreNotEqual(string? notExpected, string? actual, bool ignoreCase, + CultureInfo? culture, string? message, params object?[]? parameters) { CheckParameterNotNull(culture, "Assert.AreNotEqual", "culture", string.Empty); if (CompareInternal(notExpected, actual, ignoreCase, culture) != 0) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs b/src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs index 414763511f..fbb98299aa 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace Microsoft.VisualStudio.TestTools.UnitTesting; @@ -31,9 +32,7 @@ public sealed partial class Assert /// as . /// public static void AreSame(T? expected, T? actual) - { - AreSame(expected, actual, string.Empty, null); - } + => AreSame(expected, actual, string.Empty, null); /// /// Tests whether the specified objects both refer to the same object and @@ -58,9 +57,7 @@ public static void AreSame(T? expected, T? actual) /// as . /// public static void AreSame(T? expected, T? actual, string? message) - { - AreSame(expected, actual, message, null); - } + => AreSame(expected, actual, message, null); /// /// Tests whether the specified objects both refer to the same object and @@ -130,9 +127,7 @@ public static void AreSame(T? expected, T? actual, string? message, params ob /// as . /// public static void AreNotSame(T? notExpected, T? actual) - { - AreNotSame(notExpected, actual, string.Empty, null); - } + => AreNotSame(notExpected, actual, string.Empty, null); /// /// Tests whether the specified objects refer to different objects and @@ -158,9 +153,7 @@ public static void AreNotSame(T? notExpected, T? actual) /// as . /// public static void AreNotSame(T? notExpected, T? actual, string? message) - { - AreNotSame(notExpected, actual, message, null); - } + => AreNotSame(notExpected, actual, message, null); /// /// Tests whether the specified objects refer to different objects and diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Fail.cs b/src/TestFramework/TestFramework/Assertions/Assert.Fail.cs index 60b26c0c92..fe55ee3a15 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Fail.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Fail.cs @@ -20,9 +20,7 @@ public sealed partial class Assert /// [DoesNotReturn] public static void Fail() - { - Fail(string.Empty, null); - } + => Fail(string.Empty, null); /// /// Throws an AssertFailedException. @@ -36,9 +34,7 @@ public static void Fail() /// [DoesNotReturn] public static void Fail(string? message) - { - Fail(message, null); - } + => Fail(message, null); /// /// Throws an AssertFailedException. @@ -55,7 +51,5 @@ public static void Fail(string? message) /// [DoesNotReturn] public static void Fail(string? message, params object?[]? parameters) - { - ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters)); - } + => ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters)); } diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Inconclusive.cs b/src/TestFramework/TestFramework/Assertions/Assert.Inconclusive.cs index 5e7551672b..d5d1456c44 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Inconclusive.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Inconclusive.cs @@ -21,9 +21,7 @@ public sealed partial class Assert /// [DoesNotReturn] public static void Inconclusive() - { - Inconclusive(string.Empty, null); - } + => Inconclusive(string.Empty, null); /// /// Throws an AssertInconclusiveException. @@ -37,9 +35,7 @@ public static void Inconclusive() /// [DoesNotReturn] public static void Inconclusive(string? message) - { - Inconclusive(message, null); - } + => Inconclusive(message, null); /// /// Throws an AssertInconclusiveException. @@ -58,6 +54,7 @@ public static void Inconclusive(string? message) public static void Inconclusive(string? message, params object?[]? parameters) { string userMessage = BuildUserMessage(message, parameters); - throw new AssertInconclusiveException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, "Assert.Inconclusive", userMessage)); + throw new AssertInconclusiveException( + string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, "Assert.Inconclusive", userMessage)); } } diff --git a/src/TestFramework/TestFramework/Assertions/Assert.IsInstanceOfType.cs b/src/TestFramework/TestFramework/Assertions/Assert.IsInstanceOfType.cs index 5c3baffdd3..e65af284a3 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.IsInstanceOfType.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.IsInstanceOfType.cs @@ -32,20 +32,16 @@ public sealed partial class Assert /// of . /// public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType) - { - IsInstanceOfType(value, expectedType, string.Empty, null); - } + => IsInstanceOfType(value, expectedType, string.Empty, null); /// /// Tests whether the specified object is an instance of the generic - /// type and throws an exception if the generictype is not in the + /// type and throws an exception if the generic type is not in the /// inheritance hierarchy of the object. /// /// The expected type of . public static void IsInstanceOfType([NotNull] object? value) - { - IsInstanceOfType(value, typeof(T), string.Empty, null); - } + => IsInstanceOfType(value, typeof(T), string.Empty, null); /// /// Tests whether the specified object is an instance of the expected @@ -69,20 +65,16 @@ public static void IsInstanceOfType([NotNull] object? value) /// of . /// public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message) - { - IsInstanceOfType(value, expectedType, message, null); - } + => IsInstanceOfType(value, expectedType, message, null); /// /// Tests whether the specified object is an instance of the generic - /// type and throws an exception if the generictype is not in the + /// type and throws an exception if the generic type is not in the /// inheritance hierarchy of the object. /// /// The expected type of . public static void IsInstanceOfType([NotNull] object? value, string? message) - { - IsInstanceOfType(value, typeof(T), message, null); - } + => IsInstanceOfType(value, typeof(T), message, null); /// /// Tests whether the specified object is an instance of the expected @@ -108,7 +100,8 @@ public static void IsInstanceOfType([NotNull] object? value, string? message) /// is not in the inheritance hierarchy /// of . /// - public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message, params object?[]? parameters) + public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message, + params object?[]? parameters) { if (expectedType == null || value == null) { @@ -132,14 +125,12 @@ public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? exp /// /// Tests whether the specified object is an instance of the generic - /// type and throws an exception if the generictype is not in the + /// type and throws an exception if the generic type is not in the /// inheritance hierarchy of the object. /// /// The expected type of . public static void IsInstanceOfType([NotNull] object? value, string? message, params object?[]? parameters) - { - IsInstanceOfType(value, typeof(T), message, parameters); - } + => IsInstanceOfType(value, typeof(T), message, parameters); /// /// Tests whether the specified object is not an instance of the wrong @@ -158,9 +149,7 @@ public static void IsInstanceOfType([NotNull] object? value, string? message, /// of . /// public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType) - { - IsNotInstanceOfType(value, wrongType, string.Empty, null); - } + => IsNotInstanceOfType(value, wrongType, string.Empty, null); /// /// Tests whether the specified object is not an instance of the wrong generic @@ -169,9 +158,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType) /// /// The type that should not be. public static void IsNotInstanceOfType(object? value) - { - IsNotInstanceOfType(value, typeof(T), string.Empty, null); - } + => IsNotInstanceOfType(value, typeof(T), string.Empty, null); /// /// Tests whether the specified object is not an instance of the wrong @@ -195,9 +182,7 @@ public static void IsNotInstanceOfType(object? value) /// of . /// public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message) - { - IsNotInstanceOfType(value, wrongType, message, null); - } + => IsNotInstanceOfType(value, wrongType, message, null); /// /// Tests whether the specified object is not an instance of the wrong generic @@ -206,9 +191,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, /// /// The type that should not be. public static void IsNotInstanceOfType(object? value, string? message) - { - IsNotInstanceOfType(value, typeof(T), message, null); - } + => IsNotInstanceOfType(value, typeof(T), message, null); /// /// Tests whether the specified object is not an instance of the wrong @@ -234,7 +217,8 @@ public static void IsNotInstanceOfType(object? value, string? message) /// is in the inheritance hierarchy /// of . /// - public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message, params object?[]? parameters) + public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message, + params object?[]? parameters) { if (wrongType == null) { @@ -269,7 +253,5 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, /// /// The type that should not be. public static void IsNotInstanceOfType(object? value, string? message, params object?[]? parameters) - { - IsNotInstanceOfType(value, typeof(T), message, parameters); - } + => IsNotInstanceOfType(value, typeof(T), message, parameters); } diff --git a/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs b/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs index 0b092f8580..235f4b8ca7 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs @@ -23,9 +23,7 @@ public sealed partial class Assert /// Thrown if is not null. /// public static void IsNull(object? value) - { - IsNull(value, string.Empty, null); - } + => IsNull(value, string.Empty, null); /// /// Tests whether the specified object is null and throws an exception @@ -42,9 +40,7 @@ public static void IsNull(object? value) /// Thrown if is not null. /// public static void IsNull(object? value, string? message) - { - IsNull(value, message, null); - } + => IsNull(value, message, null); /// /// Tests whether the specified object is null and throws an exception @@ -82,9 +78,7 @@ public static void IsNull(object? value, string? message, params object?[]? para /// Thrown if is null. /// public static void IsNotNull([NotNull] object? value) - { - IsNotNull(value, string.Empty, null); - } + => IsNotNull(value, string.Empty, null); /// /// Tests whether the specified object is non-null and throws an exception @@ -101,9 +95,7 @@ public static void IsNotNull([NotNull] object? value) /// Thrown if is null. /// public static void IsNotNull([NotNull] object? value, string? message) - { - IsNotNull(value, message, null); - } + => IsNotNull(value, message, null); /// /// Tests whether the specified object is non-null and throws an exception diff --git a/src/TestFramework/TestFramework/Assertions/Assert.IsTrue.cs b/src/TestFramework/TestFramework/Assertions/Assert.IsTrue.cs index 417e9398be..8305f42c97 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.IsTrue.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.IsTrue.cs @@ -23,9 +23,7 @@ public sealed partial class Assert /// Thrown if is false. /// public static void IsTrue([DoesNotReturnIf(false)] bool condition) - { - IsTrue(condition, string.Empty, null); - } + => IsTrue(condition, string.Empty, null); /// /// Tests whether the specified condition is true and throws an exception @@ -38,9 +36,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool condition) /// Thrown if is false. /// public static void IsTrue([DoesNotReturnIf(false)] bool? condition) - { - IsTrue(condition, string.Empty, null); - } + => IsTrue(condition, string.Empty, null); /// /// Tests whether the specified condition is true and throws an exception @@ -57,9 +53,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool? condition) /// Thrown if is false. /// public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? message) - { - IsTrue(condition, message, null); - } + => IsTrue(condition, message, null); /// /// Tests whether the specified condition is true and throws an exception @@ -76,9 +70,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? messa /// Thrown if is false. /// public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? message) - { - IsTrue(condition, message, null); - } + => IsTrue(condition, message, null); /// /// Tests whether the specified condition is true and throws an exception @@ -97,7 +89,8 @@ public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? mess /// /// Thrown if is false. /// - public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? message, params object?[]? parameters) + public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? message, + params object?[]? parameters) { if (!condition) { @@ -122,7 +115,8 @@ public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? messa /// /// Thrown if is false. /// - public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? message, params object?[]? parameters) + public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? message, + params object?[]? parameters) { if (condition is false or null) { @@ -141,9 +135,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? mess /// Thrown if is true. /// public static void IsFalse([DoesNotReturnIf(true)] bool condition) - { - IsFalse(condition, string.Empty, null); - } + => IsFalse(condition, string.Empty, null); /// /// Tests whether the specified condition is false and throws an exception @@ -156,9 +148,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool condition) /// Thrown if is true. /// public static void IsFalse([DoesNotReturnIf(true)] bool? condition) - { - IsFalse(condition, string.Empty, null); - } + => IsFalse(condition, string.Empty, null); /// /// Tests whether the specified condition is false and throws an exception @@ -175,9 +165,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool? condition) /// Thrown if is true. /// public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? message) - { - IsFalse(condition, message, null); - } + => IsFalse(condition, message, null); /// /// Tests whether the specified condition is false and throws an exception @@ -194,9 +182,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? messa /// Thrown if is true. /// public static void IsFalse([DoesNotReturnIf(true)] bool? condition, string? message) - { - IsFalse(condition, message, null); - } + => IsFalse(condition, message, null); /// /// Tests whether the specified condition is false and throws an exception @@ -215,7 +201,8 @@ public static void IsFalse([DoesNotReturnIf(true)] bool? condition, string? mess /// /// Thrown if is true. /// - public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? message, params object?[]? parameters) + public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? message, + params object?[]? parameters) { if (condition) { @@ -240,7 +227,8 @@ public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? messa /// /// Thrown if is true. /// - public static void IsFalse([DoesNotReturnIf(true)] bool? condition, string? message, params object?[]? parameters) + public static void IsFalse([DoesNotReturnIf(true)] bool? condition, string? message, + params object?[]? parameters) { if (condition is true or null) { diff --git a/src/TestFramework/TestFramework/Assertions/Assert.ThrowsException.cs b/src/TestFramework/TestFramework/Assertions/Assert.ThrowsException.cs index 092988d917..0520b724ad 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.ThrowsException.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.ThrowsException.cs @@ -16,8 +16,9 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; public sealed partial class Assert { /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -33,13 +34,12 @@ public sealed partial class Assert /// public static T ThrowsException(Action action) where T : Exception - { - return ThrowsException(action, string.Empty, null); - } + => ThrowsException(action, string.Empty, null); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -59,13 +59,12 @@ public static T ThrowsException(Action action) /// public static T ThrowsException(Action action, string message) where T : Exception - { - return ThrowsException(action, message, null); - } + => ThrowsException(action, message, null); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -81,13 +80,12 @@ public static T ThrowsException(Action action, string message) /// public static T ThrowsException(Func action) where T : Exception - { - return ThrowsException(action, string.Empty, null); - } + => ThrowsException(action, string.Empty, null); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -107,13 +105,12 @@ public static T ThrowsException(Func action) /// public static T ThrowsException(Func action, string message) where T : Exception - { - return ThrowsException(action, message, null); - } + => ThrowsException(action, message, null); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -136,13 +133,12 @@ public static T ThrowsException(Func action, string message) /// public static T ThrowsException(Func action, string message, params object?[]? parameters) where T : Exception - { - return ThrowsException(() => { _ = action(); }, message, parameters); - } + => ThrowsException(() => { _ = action(); }, message, parameters); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -214,8 +210,9 @@ public static T ThrowsException(Action action, string message, params object? } /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// /// Delegate to code to be tested and which is expected to throw exception. @@ -231,13 +228,13 @@ public static T ThrowsException(Action action, string message, params object? /// public static async Task ThrowsExceptionAsync(Func action) where T : Exception - { - return await ThrowsExceptionAsync(action, string.Empty, null).ConfigureAwait(false); - } + => await ThrowsExceptionAsync(action, string.Empty, null) + .ConfigureAwait(false); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// Delegate to code to be tested and which is expected to throw exception. /// @@ -253,13 +250,13 @@ public static async Task ThrowsExceptionAsync(Func action) /// public static async Task ThrowsExceptionAsync(Func action, string message) where T : Exception - { - return await ThrowsExceptionAsync(action, message, null).ConfigureAwait(false); - } + => await ThrowsExceptionAsync(action, message, null) + .ConfigureAwait(false); /// - /// Tests whether the code specified by delegate throws exact given exception of type (and not of derived type) - /// and throws AssertFailedException if code does not throws exception or throws exception of type other than . + /// Tests whether the code specified by delegate throws exact given exception + /// of type (and not of derived type) and throws AssertFailedException + /// if code does not throws exception or throws exception of type other than . /// /// Delegate to code to be tested and which is expected to throw exception. /// diff --git a/src/TestFramework/TestFramework/Assertions/Assert.cs b/src/TestFramework/TestFramework/Assertions/Assert.cs index 9147544d1d..ebb137dbe9 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.cs @@ -42,11 +42,9 @@ private Assert() /// [return: NotNullIfNotNull(nameof(input))] public static string? ReplaceNullChars(string? input) - { - return StringEx.IsNullOrEmpty(input) + => StringEx.IsNullOrEmpty(input) ? input : input.Replace("\0", "\\0"); - } /// /// Helper function that creates and throws an AssertionFailedException. @@ -59,9 +57,8 @@ private Assert() /// [DoesNotReturn] internal static void ThrowAssertFailed(string assertionName, string? message) - { - throw new AssertFailedException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, assertionName, ReplaceNulls(message))); - } + => throw new AssertFailedException( + string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, assertionName, ReplaceNulls(message))); /// /// Builds the formatted message using the given user format message and parameters. @@ -110,7 +107,8 @@ internal static string BuildUserMessage(string? format, params object?[]? parame /// /// The parameters. /// - internal static void CheckParameterNotNull(object? param, string assertionName, string parameterName, string? message, params object?[]? parameters) + internal static void CheckParameterNotNull([NotNull] object? param, string assertionName, string parameterName, + string? message, params object?[]? parameters) { if (param == null) { @@ -131,6 +129,7 @@ internal static void CheckParameterNotNull(object? param, string assertionName, /// The converted string. /// [SuppressMessage("ReSharper", "RedundantToStringCall", Justification = "We are ensuring ToString() isn't overloaded in a way to misbehave")] + [return: NotNull] internal static string ReplaceNulls(object? input) { // Use the localized "(null)" string for null values. @@ -152,9 +151,7 @@ internal static string ReplaceNulls(object? input) } private static int CompareInternal(string? expected, string? actual, bool ignoreCase, CultureInfo? culture) - { - return string.Compare(expected, actual, ignoreCase, culture); - } + => string.Compare(expected, actual, ignoreCase, culture); #region EqualsAssertion diff --git a/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs b/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs index 0039455600..9dcd1cbc23 100644 --- a/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs +++ b/src/TestFramework/TestFramework/Assertions/CollectionAssert.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; @@ -49,13 +50,11 @@ private CollectionAssert() /// The element that is expected to be in the collection. /// /// - /// Thrown if is not found in - /// . + /// is null, or does not contain + /// element . /// - public static void Contains(ICollection collection, object? element) - { - Contains(collection, element, string.Empty, null); - } + public static void Contains([NotNull] ICollection? collection, object? element) + => Contains(collection, element, string.Empty, null); /// /// Tests whether the specified collection contains the specified element @@ -73,13 +72,11 @@ public static void Contains(ICollection collection, object? element) /// test results. /// /// - /// Thrown if is not found in - /// . + /// is null, or does not contain + /// element . /// - public static void Contains(ICollection collection, object? element, string? message) - { - Contains(collection, element, message, null); - } + public static void Contains([NotNull] ICollection? collection, object? element, string? message) + => Contains(collection, element, message, null); /// /// Tests whether the specified collection contains the specified element @@ -100,10 +97,11 @@ public static void Contains(ICollection collection, object? element, string? mes /// An array of parameters to use when formatting . /// /// - /// Thrown if is not found in - /// . + /// is null, or does not contain + /// element . /// - public static void Contains(ICollection collection, object? element, string? message, params object?[]? parameters) + public static void Contains([NotNull] ICollection? collection, object? element, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(collection, "CollectionAssert.Contains", "collection", string.Empty); @@ -129,13 +127,11 @@ public static void Contains(ICollection collection, object? element, string? mes /// The element that is expected not to be in the collection. /// /// - /// Thrown if is found in - /// . + /// is null, or contains + /// element . /// - public static void DoesNotContain(ICollection collection, object? element) - { - DoesNotContain(collection, element, string.Empty, null); - } + public static void DoesNotContain([NotNull] ICollection? collection, object? element) + => DoesNotContain(collection, element, string.Empty, null); /// /// Tests whether the specified collection does not contain the specified @@ -153,13 +149,11 @@ public static void DoesNotContain(ICollection collection, object? element) /// results. /// /// - /// Thrown if is found in - /// . + /// is null, or contains + /// element . /// - public static void DoesNotContain(ICollection collection, object? element, string? message) - { - DoesNotContain(collection, element, message, null); - } + public static void DoesNotContain([NotNull] ICollection? collection, object? element, string? message) + => DoesNotContain(collection, element, message, null); /// /// Tests whether the specified collection does not contain the specified @@ -180,10 +174,11 @@ public static void DoesNotContain(ICollection collection, object? element, strin /// An array of parameters to use when formatting . /// /// - /// Thrown if is found in - /// . + /// is null, or contains + /// element . /// - public static void DoesNotContain(ICollection collection, object? element, string? message, params object?[]? parameters) + public static void DoesNotContain([NotNull] ICollection? collection, object? element, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(collection, "CollectionAssert.DoesNotContain", "collection", string.Empty); @@ -204,12 +199,10 @@ public static void DoesNotContain(ICollection collection, object? element, strin /// The collection in which to search for null elements. /// /// - /// Thrown if a null element is found in . + /// is null, or contains a null element. /// - public static void AllItemsAreNotNull(ICollection collection) - { - AllItemsAreNotNull(collection, string.Empty, null); - } + public static void AllItemsAreNotNull([NotNull] ICollection? collection) + => AllItemsAreNotNull(collection, string.Empty, null); /// /// Tests whether all items in the specified collection are non-null and throws @@ -223,12 +216,10 @@ public static void AllItemsAreNotNull(ICollection collection) /// contains a null element. The message is shown in test results. /// /// - /// Thrown if a null element is found in . + /// is null, or contains a null element. /// - public static void AllItemsAreNotNull(ICollection collection, string? message) - { - AllItemsAreNotNull(collection, message, null); - } + public static void AllItemsAreNotNull([NotNull] ICollection? collection, string? message) + => AllItemsAreNotNull(collection, message, null); /// /// Tests whether all items in the specified collection are non-null and throws @@ -245,9 +236,10 @@ public static void AllItemsAreNotNull(ICollection collection, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if a null element is found in . + /// is null, or contains a null element. /// - public static void AllItemsAreNotNull(ICollection collection, string? message, params object?[]? parameters) + public static void AllItemsAreNotNull([NotNull] ICollection? collection, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreNotNull", "collection", string.Empty); foreach (object? current in collection) @@ -267,13 +259,11 @@ public static void AllItemsAreNotNull(ICollection collection, string? message, p /// The collection in which to search for duplicate elements. /// /// - /// Thrown if a two or more equal elements are found in - /// . + /// is null, or contains at least one duplicate + /// element. /// - public static void AllItemsAreUnique(ICollection collection) - { - AllItemsAreUnique(collection, string.Empty, null); - } + public static void AllItemsAreUnique([NotNull] ICollection? collection) + => AllItemsAreUnique(collection, string.Empty, null); /// /// Tests whether all items in the specified collection are unique or not and @@ -288,13 +278,11 @@ public static void AllItemsAreUnique(ICollection collection) /// test results. /// /// - /// Thrown if a two or more equal elements are found in - /// . + /// is null, or contains at least one duplicate + /// element. /// - public static void AllItemsAreUnique(ICollection collection, string? message) - { - AllItemsAreUnique(collection, message, null); - } + public static void AllItemsAreUnique([NotNull] ICollection? collection, string? message) + => AllItemsAreUnique(collection, message, null); /// /// Tests whether all items in the specified collection are unique or not and @@ -312,10 +300,11 @@ public static void AllItemsAreUnique(ICollection collection, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if a two or more equal elements are found in - /// . + /// is null, or contains at least one duplicate + /// element. /// - public static void AllItemsAreUnique(ICollection collection, string? message, params object?[]? parameters) + public static void AllItemsAreUnique([NotNull] ICollection? collection, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreUnique", "collection", string.Empty); @@ -381,13 +370,12 @@ public static void AllItemsAreUnique(ICollection collection, string? message, pa /// The collection expected to be a superset of . /// /// - /// Thrown if an element in is not found in + /// is null, or is null, + /// or contains at least one element not contained in /// . /// - public static void IsSubsetOf(ICollection subset, ICollection superset) - { - IsSubsetOf(subset, superset, string.Empty, null); - } + public static void IsSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset) + => IsSubsetOf(subset, superset, string.Empty, null); /// /// Tests whether one collection is a subset of another collection and @@ -406,13 +394,12 @@ public static void IsSubsetOf(ICollection subset, ICollection superset) /// The message is shown in test results. /// /// - /// Thrown if an element in is not found in + /// is null, or is null, + /// or contains at least one element not contained in /// . /// - public static void IsSubsetOf(ICollection subset, ICollection superset, string? message) - { - IsSubsetOf(subset, superset, message, null); - } + public static void IsSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message) + => IsSubsetOf(subset, superset, message, null); /// /// Tests whether one collection is a subset of another collection and @@ -434,10 +421,12 @@ public static void IsSubsetOf(ICollection subset, ICollection superset, string? /// An array of parameters to use when formatting . /// /// - /// Thrown if an element in is not found in + /// is null, or is null, + /// or contains at least one element not contained in /// . /// - public static void IsSubsetOf(ICollection subset, ICollection superset, string? message, params object?[]? parameters) + public static void IsSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(subset, "CollectionAssert.IsSubsetOf", "subset", string.Empty); Assert.CheckParameterNotNull(superset, "CollectionAssert.IsSubsetOf", "superset", string.Empty); @@ -459,13 +448,11 @@ public static void IsSubsetOf(ICollection subset, ICollection superset, string? /// The collection expected not to be a superset of . /// /// - /// Thrown if every element in is also found in - /// . + /// is null, or is null, + /// or all elements of are contained in . /// - public static void IsNotSubsetOf(ICollection subset, ICollection superset) - { - IsNotSubsetOf(subset, superset, string.Empty, null); - } + public static void IsNotSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset) + => IsNotSubsetOf(subset, superset, string.Empty, null); /// /// Tests whether one collection is not a subset of another collection and @@ -484,13 +471,11 @@ public static void IsNotSubsetOf(ICollection subset, ICollection superset) /// The message is shown in test results. /// /// - /// Thrown if every element in is also found in - /// . + /// is null, or is null, + /// or all elements of are contained in . /// - public static void IsNotSubsetOf(ICollection subset, ICollection superset, string? message) - { - IsNotSubsetOf(subset, superset, message, null); - } + public static void IsNotSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message) + => IsNotSubsetOf(subset, superset, message, null); /// /// Tests whether one collection is not a subset of another collection and @@ -512,10 +497,11 @@ public static void IsNotSubsetOf(ICollection subset, ICollection superset, strin /// An array of parameters to use when formatting . /// /// - /// Thrown if every element in is also found in - /// . + /// is null, or is null, + /// or all elements of are contained in . /// - public static void IsNotSubsetOf(ICollection subset, ICollection superset, string? message, params object?[]? parameters) + public static void IsNotSubsetOf([NotNull] ICollection? subset, [NotNull] ICollection? superset, string? message, + params object?[]? parameters) { Assert.CheckParameterNotNull(subset, "CollectionAssert.IsNotSubsetOf", "subset", string.Empty); Assert.CheckParameterNotNull(superset, "CollectionAssert.IsNotSubsetOf", "superset", string.Empty); @@ -543,13 +529,12 @@ public static void IsNotSubsetOf(ICollection subset, ICollection superset, strin /// the code under test. /// /// - /// Thrown if an element was found in one of the collections but not - /// the other. + /// and nullabilities don't match, + /// or if any element was found in one of the collections but not the other. /// - public static void AreEquivalent(ICollection? expected, ICollection? actual) - { - AreEquivalent(expected, actual, string.Empty, null); - } + public static void AreEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual) + => AreEquivalent(expected, actual, string.Empty, null); /// /// Tests whether two collections contain the same elements and throws an @@ -570,13 +555,13 @@ public static void AreEquivalent(ICollection? expected, ICollection? actual) /// in test results. /// /// - /// Thrown if an element was found in one of the collections but not - /// the other. + /// and nullabilities don't match, + /// or if any element was found in one of the collections but not the other. /// - public static void AreEquivalent(ICollection? expected, ICollection? actual, string? message) - { - AreEquivalent(expected, actual, message, null); - } + public static void AreEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual, + string? message) + => AreEquivalent(expected, actual, message, null); /// /// Tests whether two collections contain the same elements and throws an @@ -600,10 +585,12 @@ public static void AreEquivalent(ICollection? expected, ICollection? actual, str /// An array of parameters to use when formatting . /// /// - /// Thrown if an element was found in one of the collections but not - /// the other. + /// and nullabilities don't match, + /// or if any element was found in one of the collections but not the other. /// - public static void AreEquivalent(ICollection? expected, ICollection? actual, string? message, params object?[]? parameters) + public static void AreEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual, + string? message, params object?[]? parameters) { // Check whether one is null while the other is not. if ((expected == null) != (actual == null)) @@ -611,8 +598,7 @@ public static void AreEquivalent(ICollection? expected, ICollection? actual, str Assert.ThrowAssertFailed("CollectionAssert.AreEquivalent", Assert.BuildUserMessage(message, parameters)); } - // If the references are the same or both collections are null, they - // are equivalent. + // If the references are the same or both collections are null, they are equivalent. if (ReferenceEquals(expected, actual) || expected == null) { return; @@ -668,13 +654,13 @@ public static void AreEquivalent(ICollection? expected, ICollection? actual, str /// the code under test. /// /// - /// Thrown if the two collections contained the same elements, including - /// the same number of duplicate occurrences of each element. + /// and nullabilities don't match, + /// or if collections contain the same elements, including the same number of duplicate + /// occurrences of each element. /// - public static void AreNotEquivalent(ICollection? expected, ICollection? actual) - { - AreNotEquivalent(expected, actual, string.Empty, null); - } + public static void AreNotEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual) + => AreNotEquivalent(expected, actual, string.Empty, null); /// /// Tests whether two collections contain the different elements and throws an @@ -695,13 +681,14 @@ public static void AreNotEquivalent(ICollection? expected, ICollection? actual) /// is shown in test results. /// /// - /// Thrown if the two collections contained the same elements, including - /// the same number of duplicate occurrences of each element. + /// and nullabilities don't match, + /// or if collections contain the same elements, including the same number of duplicate + /// occurrences of each element. /// - public static void AreNotEquivalent(ICollection? expected, ICollection? actual, string? message) - { - AreNotEquivalent(expected, actual, message, null); - } + public static void AreNotEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual, + string? message) + => AreNotEquivalent(expected, actual, message, null); /// /// Tests whether two collections contain the different elements and throws an @@ -725,10 +712,13 @@ public static void AreNotEquivalent(ICollection? expected, ICollection? actual, /// An array of parameters to use when formatting . /// /// - /// Thrown if the two collections contained the same elements, including - /// the same number of duplicate occurrences of each element. + /// and nullabilities don't match, + /// or if collections contain the same elements, including the same number of duplicate + /// occurrences of each element. /// - public static void AreNotEquivalent(ICollection? expected, ICollection? actual, string? message, params object?[]? parameters) + public static void AreNotEquivalent( + [NotNullIfNotNull(nameof(actual))] ICollection? expected, [NotNullIfNotNull(nameof(expected))] ICollection? actual, + string? message, params object?[]? parameters) { // Check whether one is null while the other is not. if ((expected == null) != (actual == null)) @@ -766,7 +756,7 @@ public static void AreNotEquivalent(ICollection? expected, ICollection? actual, } // Search for a mismatched element. - if (!FindMismatchedElement(expected, actual, out var expectedCount, out var actualCount, out var mismatchedElement)) + if (!FindMismatchedElement(expected, actual, out _, out _, out _)) { string userMessage = Assert.BuildUserMessage(message, parameters); var finalMessage = string.Format( @@ -794,14 +784,12 @@ public static void AreNotEquivalent(ICollection? expected, ICollection? actual, /// The expected type of each element of . /// /// - /// Thrown if an element in is null or - /// is not in the inheritance hierarchy - /// of an element in . + /// is null or, is null, + /// or some elements of do not inherit/implement + /// . /// - public static void AllItemsAreInstancesOfType(ICollection collection, Type expectedType) - { - AllItemsAreInstancesOfType(collection, expectedType, string.Empty, null); - } + public static void AllItemsAreInstancesOfType([NotNull] ICollection? collection, [NotNull] Type? expectedType) + => AllItemsAreInstancesOfType(collection, expectedType, string.Empty, null); /// /// Tests whether all elements in the specified collection are instances @@ -821,14 +809,13 @@ public static void AllItemsAreInstancesOfType(ICollection collection, Type expec /// . The message is shown in test results. /// /// - /// Thrown if an element in is null or - /// is not in the inheritance hierarchy - /// of an element in . + /// is null or, is null, + /// or some elements of do not inherit/implement + /// . /// - public static void AllItemsAreInstancesOfType(ICollection collection, Type expectedType, string? message) - { - AllItemsAreInstancesOfType(collection, expectedType, message, null); - } + public static void AllItemsAreInstancesOfType([NotNull] ICollection? collection, [NotNull] Type? expectedType, + string? message) + => AllItemsAreInstancesOfType(collection, expectedType, message, null); /// /// Tests whether all elements in the specified collection are instances @@ -851,18 +838,19 @@ public static void AllItemsAreInstancesOfType(ICollection collection, Type expec /// An array of parameters to use when formatting . /// /// - /// Thrown if an element in is null or - /// is not in the inheritance hierarchy - /// of an element in . + /// is null or, is null, + /// or some elements of do not inherit/implement + /// . /// - public static void AllItemsAreInstancesOfType(ICollection collection, Type expectedType, string? message, params object?[]? parameters) + public static void AllItemsAreInstancesOfType( + [NotNull] ICollection? collection, [NotNull] Type? expectedType, string? message, params object?[]? parameters) { Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreInstancesOfType", "collection", string.Empty); Assert.CheckParameterNotNull(expectedType, "CollectionAssert.AllItemsAreInstancesOfType", "expectedType", string.Empty); int i = 0; foreach (object? element in collection) { - if (expectedType?.GetTypeInfo() is { } expectedTypeInfo + if (expectedType.GetTypeInfo() is { } expectedTypeInfo && element?.GetType().GetTypeInfo() is { } elementTypeInfo && !expectedTypeInfo.IsAssignableFrom(elementTypeInfo)) { @@ -904,9 +892,7 @@ public static void AllItemsAreInstancesOfType(ICollection collection, Type expec /// . /// public static void AreEqual(ICollection? expected, ICollection? actual) - { - AreEqual(expected, actual, string.Empty, null); - } + => AreEqual(expected, actual, string.Empty, null); /// /// Tests whether the specified collections are equal and throws an exception @@ -932,9 +918,7 @@ public static void AreEqual(ICollection? expected, ICollection? actual) /// . /// public static void AreEqual(ICollection? expected, ICollection? actual, string? message) - { - AreEqual(expected, actual, message, null); - } + => AreEqual(expected, actual, message, null); /// /// Tests whether the specified collections are equal and throws an exception @@ -962,7 +946,8 @@ public static void AreEqual(ICollection? expected, ICollection? actual, string? /// Thrown if is not equal to /// . /// - public static void AreEqual(ICollection? expected, ICollection? actual, string? message, params object?[]? parameters) + public static void AreEqual(ICollection? expected, ICollection? actual, string? message, + params object?[]? parameters) { string reason = string.Empty; if (!AreCollectionsEqual(expected, actual, new ObjectComparer(), ref reason)) @@ -992,9 +977,7 @@ public static void AreEqual(ICollection? expected, ICollection? actual, string? /// Thrown if is equal to . /// public static void AreNotEqual(ICollection? notExpected, ICollection? actual) - { - AreNotEqual(notExpected, actual, string.Empty, null); - } + => AreNotEqual(notExpected, actual, string.Empty, null); /// /// Tests whether the specified collections are unequal and throws an exception @@ -1020,9 +1003,7 @@ public static void AreNotEqual(ICollection? notExpected, ICollection? actual) /// Thrown if is equal to . /// public static void AreNotEqual(ICollection? notExpected, ICollection? actual, string? message) - { - AreNotEqual(notExpected, actual, message, null); - } + => AreNotEqual(notExpected, actual, message, null); /// /// Tests whether the specified collections are unequal and throws an exception @@ -1050,7 +1031,8 @@ public static void AreNotEqual(ICollection? notExpected, ICollection? actual, st /// /// Thrown if is equal to . /// - public static void AreNotEqual(ICollection? notExpected, ICollection? actual, string? message, params object?[]? parameters) + public static void AreNotEqual(ICollection? notExpected, ICollection? actual, string? message, + params object?[]? parameters) { string reason = string.Empty; if (AreCollectionsEqual(notExpected, actual, new ObjectComparer(), ref reason)) @@ -1081,10 +1063,8 @@ public static void AreNotEqual(ICollection? notExpected, ICollection? actual, st /// Thrown if is not equal to /// . /// - public static void AreEqual(ICollection? expected, ICollection? actual, IComparer comparer) - { - AreEqual(expected, actual, comparer, string.Empty, null); - } + public static void AreEqual(ICollection? expected, ICollection? actual, [NotNull] IComparer? comparer) + => AreEqual(expected, actual, comparer, string.Empty, null); /// /// Tests whether the specified collections are equal and throws an exception @@ -1111,10 +1091,9 @@ public static void AreEqual(ICollection? expected, ICollection? actual, ICompare /// Thrown if is not equal to /// . /// - public static void AreEqual(ICollection? expected, ICollection? actual, IComparer comparer, string? message) - { - AreEqual(expected, actual, comparer, message, null); - } + public static void AreEqual(ICollection? expected, ICollection? actual, [NotNull] IComparer? comparer, + string? message) + => AreEqual(expected, actual, comparer, message, null); /// /// Tests whether the specified collections are equal and throws an exception @@ -1144,7 +1123,8 @@ public static void AreEqual(ICollection? expected, ICollection? actual, ICompare /// Thrown if is not equal to /// . /// - public static void AreEqual(ICollection? expected, ICollection? actual, IComparer comparer, string? message, params object?[]? parameters) + public static void AreEqual(ICollection? expected, ICollection? actual, [NotNull] IComparer? comparer, + string? message, params object?[]? parameters) { string reason = string.Empty; if (!AreCollectionsEqual(expected, actual, comparer, ref reason)) @@ -1175,10 +1155,8 @@ public static void AreEqual(ICollection? expected, ICollection? actual, ICompare /// /// Thrown if is equal to . /// - public static void AreNotEqual(ICollection? notExpected, ICollection? actual, IComparer comparer) - { - AreNotEqual(notExpected, actual, comparer, string.Empty, null); - } + public static void AreNotEqual(ICollection? notExpected, ICollection? actual, [NotNull] IComparer? comparer) + => AreNotEqual(notExpected, actual, comparer, string.Empty, null); /// /// Tests whether the specified collections are unequal and throws an exception @@ -1205,10 +1183,9 @@ public static void AreNotEqual(ICollection? notExpected, ICollection? actual, IC /// /// Thrown if is equal to . /// - public static void AreNotEqual(ICollection? notExpected, ICollection? actual, IComparer comparer, string? message) - { - AreNotEqual(notExpected, actual, comparer, message, null); - } + public static void AreNotEqual(ICollection? notExpected, ICollection? actual, [NotNull] IComparer? comparer, + string? message) + => AreNotEqual(notExpected, actual, comparer, message, null); /// /// Tests whether the specified collections are unequal and throws an exception @@ -1238,7 +1215,8 @@ public static void AreNotEqual(ICollection? notExpected, ICollection? actual, IC /// /// Thrown if is equal to . /// - public static void AreNotEqual(ICollection? notExpected, ICollection? actual, IComparer comparer, string? message, params object?[]? parameters) + public static void AreNotEqual(ICollection? notExpected, ICollection? actual, [NotNull] IComparer? comparer, + string? message, params object?[]? parameters) { string reason = string.Empty; if (AreCollectionsEqual(notExpected, actual, comparer, ref reason)) @@ -1372,7 +1350,8 @@ internal static bool IsSubsetOfHelper(ICollection subset, ICollection superset) /// /// true if a mismatched element was found; false otherwise. /// - private static bool FindMismatchedElement(ICollection expected, ICollection actual, out int expectedCount, out int actualCount, out object? mismatchedElement) + private static bool FindMismatchedElement(ICollection expected, ICollection actual, out int expectedCount, + out int actualCount, out object? mismatchedElement) { // $ CONSIDER: The current algorithm counts the number of occurrences of each // $ CONSIDER: element in each collection and then compares the count, resulting @@ -1414,7 +1393,8 @@ private static bool FindMismatchedElement(ICollection expected, ICollection actu return false; } - private static bool AreCollectionsEqual(ICollection? expected, ICollection? actual, IComparer comparer, ref string reason) + private static bool AreCollectionsEqual(ICollection? expected, ICollection? actual, [NotNull] IComparer? comparer, + ref string reason) { Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer", string.Empty); if (!ReferenceEquals(expected, actual)) diff --git a/src/TestFramework/TestFramework/Assertions/StringAssert.cs b/src/TestFramework/TestFramework/Assertions/StringAssert.cs index c6095e146a..ca7c70d2ad 100644 --- a/src/TestFramework/TestFramework/Assertions/StringAssert.cs +++ b/src/TestFramework/TestFramework/Assertions/StringAssert.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text.RegularExpressions; @@ -47,13 +48,11 @@ private StringAssert() /// The string expected to occur within . /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring) - { - Contains(value, substring, string.Empty, StringComparison.Ordinal); - } + public static void Contains([NotNull] string? value, [NotNull] string? substring) + => Contains(value, substring, string.Empty, StringComparison.Ordinal); /// /// Tests whether the specified string contains the specified substring @@ -70,13 +69,11 @@ public static void Contains(string value, string substring) /// The comparison method to compare strings . /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring, StringComparison comparisonType) - { - Contains(value, substring, string.Empty, comparisonType); - } + public static void Contains([NotNull] string? value, [NotNull] string? substring, StringComparison comparisonType) + => Contains(value, substring, string.Empty, comparisonType); /// /// Tests whether the specified string contains the specified substring @@ -95,13 +92,11 @@ public static void Contains(string value, string substring, StringComparison com /// test results. /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring, string? message) - { - Contains(value, substring, message, StringComparison.Ordinal); - } + public static void Contains([NotNull] string? value, [NotNull] string? substring, string? message) + => Contains(value, substring, message, StringComparison.Ordinal); /// /// Tests whether the specified string contains the specified substring @@ -123,13 +118,12 @@ public static void Contains(string value, string substring, string? message) /// The comparison method to compare strings . /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring, string? message, StringComparison comparisonType) - { - Contains(value, substring, message, comparisonType, Empty); - } + public static void Contains([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType) + => Contains(value, substring, message, comparisonType, Empty); /// /// Tests whether the specified string contains the specified substring @@ -151,13 +145,12 @@ public static void Contains(string value, string substring, string? message, Str /// An array of parameters to use when formatting . /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring, string? message, params object?[]? parameters) - { - Contains(value, substring, message, StringComparison.Ordinal, parameters); - } + public static void Contains([NotNull] string? value, [NotNull] string? substring, string? message, + params object?[]? parameters) + => Contains(value, substring, message, StringComparison.Ordinal, parameters); /// /// Tests whether the specified string contains the specified substring @@ -182,10 +175,11 @@ public static void Contains(string value, string substring, string? message, par /// An array of parameters to use when formatting . /// /// - /// Thrown if is not found in - /// . + /// is null, or is null, + /// or does not contain . /// - public static void Contains(string value, string substring, string? message, StringComparison comparisonType, params object?[]? parameters) + public static void Contains([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType, params object?[]? parameters) { Assert.CheckParameterNotNull(value, "StringAssert.Contains", "value", string.Empty); Assert.CheckParameterNotNull(substring, "StringAssert.Contains", "substring", string.Empty); @@ -209,13 +203,11 @@ public static void Contains(string value, string substring, string? message, Str /// The string expected to be a prefix of . /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring) - { - StartsWith(value, substring, string.Empty, StringComparison.Ordinal); - } + public static void StartsWith([NotNull] string? value, [NotNull] string? substring) + => StartsWith(value, substring, string.Empty, StringComparison.Ordinal); /// /// Tests whether the specified string begins with the specified substring @@ -232,13 +224,12 @@ public static void StartsWith(string value, string substring) /// The comparison method to compare strings . /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring, StringComparison comparisonType) - { - StartsWith(value, substring, string.Empty, comparisonType, Empty); - } + public static void StartsWith([NotNull] string? value, [NotNull] string? substring, + StringComparison comparisonType) + => StartsWith(value, substring, string.Empty, comparisonType, Empty); /// /// Tests whether the specified string begins with the specified substring @@ -257,13 +248,11 @@ public static void StartsWith(string value, string substring, StringComparison c /// shown in test results. /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring, string? message) - { - StartsWith(value, substring, message, StringComparison.Ordinal); - } + public static void StartsWith([NotNull] string? value, [NotNull] string? substring, string? message) + => StartsWith(value, substring, message, StringComparison.Ordinal); /// /// Tests whether the specified string begins with the specified substring @@ -285,13 +274,12 @@ public static void StartsWith(string value, string substring, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring, string? message, params object?[]? parameters) - { - StartsWith(value, substring, message, StringComparison.Ordinal, parameters); - } + public static void StartsWith([NotNull] string? value, [NotNull] string? substring, string? message, + params object?[]? parameters) + => StartsWith(value, substring, message, StringComparison.Ordinal, parameters); /// /// Tests whether the specified string begins with the specified substring @@ -313,13 +301,12 @@ public static void StartsWith(string value, string substring, string? message, p /// The comparison method to compare strings . /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring, string? message, StringComparison comparisonType) - { - StartsWith(value, substring, message, comparisonType, Empty); - } + public static void StartsWith([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType) + => StartsWith(value, substring, message, comparisonType, Empty); /// /// Tests whether the specified string begins with the specified substring @@ -344,10 +331,11 @@ public static void StartsWith(string value, string substring, string? message, S /// An array of parameters to use when formatting . /// /// - /// Thrown if does not begin with - /// . + /// is null, or is null, + /// or does not start with . /// - public static void StartsWith(string value, string substring, string? message, StringComparison comparisonType, params object?[]? parameters) + public static void StartsWith([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType, params object?[]? parameters) { Assert.CheckParameterNotNull(value, "StringAssert.StartsWith", "value", string.Empty); Assert.CheckParameterNotNull(substring, "StringAssert.StartsWith", "substring", string.Empty); @@ -371,13 +359,11 @@ public static void StartsWith(string value, string substring, string? message, S /// The string expected to be a suffix of . /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring) - { - EndsWith(value, substring, string.Empty, StringComparison.Ordinal); - } + public static void EndsWith([NotNull] string? value, [NotNull] string? substring) + => EndsWith(value, substring, string.Empty, StringComparison.Ordinal); /// /// Tests whether the specified string ends with the specified substring @@ -394,13 +380,12 @@ public static void EndsWith(string value, string substring) /// The comparison method to compare strings . /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring, StringComparison comparisonType) - { - EndsWith(value, substring, string.Empty, comparisonType, Empty); - } + public static void EndsWith([NotNull] string? value, [NotNull] string? substring, + StringComparison comparisonType) + => EndsWith(value, substring, string.Empty, comparisonType, Empty); /// /// Tests whether the specified string ends with the specified substring @@ -419,13 +404,11 @@ public static void EndsWith(string value, string substring, StringComparison com /// shown in test results. /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring, string? message) - { - EndsWith(value, substring, message, StringComparison.Ordinal); - } + public static void EndsWith([NotNull] string? value, [NotNull] string? substring, string? message) + => EndsWith(value, substring, message, StringComparison.Ordinal); /// /// Tests whether the specified string ends with the specified substring @@ -447,13 +430,12 @@ public static void EndsWith(string value, string substring, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring, string? message, params object?[]? parameters) - { - EndsWith(value, substring, message, StringComparison.Ordinal, parameters); - } + public static void EndsWith([NotNull] string? value, [NotNull] string? substring, string? message, + params object?[]? parameters) + => EndsWith(value, substring, message, StringComparison.Ordinal, parameters); /// /// Tests whether the specified string ends with the specified substring @@ -475,13 +457,12 @@ public static void EndsWith(string value, string substring, string? message, par /// The comparison method to compare strings . /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring, string? message, StringComparison comparisonType) - { - EndsWith(value, substring, message, comparisonType, Empty); - } + public static void EndsWith([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType) + => EndsWith(value, substring, message, comparisonType, Empty); /// /// Tests whether the specified string ends with the specified substring @@ -506,10 +487,11 @@ public static void EndsWith(string value, string substring, string? message, Str /// An array of parameters to use when formatting . /// /// - /// Thrown if does not end with - /// . + /// is null, or is null, + /// or does not end with . /// - public static void EndsWith(string value, string substring, string? message, StringComparison comparisonType, params object?[]? parameters) + public static void EndsWith([NotNull] string? value, [NotNull] string? substring, string? message, + StringComparison comparisonType, params object?[]? parameters) { Assert.CheckParameterNotNull(value, "StringAssert.EndsWith", "value", string.Empty); Assert.CheckParameterNotNull(substring, "StringAssert.EndsWith", "substring", string.Empty); @@ -537,13 +519,11 @@ public static void EndsWith(string value, string substring, string? message, Str /// expected to match. /// /// - /// Thrown if does not match - /// . + /// is null, or is null, + /// or does not match . /// - public static void Matches(string value, Regex pattern) - { - Matches(value, pattern, string.Empty); - } + public static void Matches([NotNull] string? value, [NotNull] Regex? pattern) + => Matches(value, pattern, string.Empty); /// /// Tests whether the specified string matches a regular expression and @@ -562,13 +542,11 @@ public static void Matches(string value, Regex pattern) /// test results. /// /// - /// Thrown if does not match - /// . + /// is null, or is null, + /// or does not match . /// - public static void Matches(string value, Regex pattern, string? message) - { - Matches(value, pattern, message, null); - } + public static void Matches([NotNull] string? value, [NotNull] Regex? pattern, string? message) + => Matches(value, pattern, message, null); /// /// Tests whether the specified string matches a regular expression and @@ -590,10 +568,10 @@ public static void Matches(string value, Regex pattern, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if does not match - /// . + /// is null, or is null, + /// or does not match . /// - public static void Matches(string value, Regex pattern, string? message, params object?[]? parameters) + public static void Matches([NotNull] string? value, [NotNull] Regex? pattern, string? message, params object?[]? parameters) { Assert.CheckParameterNotNull(value, "StringAssert.Matches", "value", string.Empty); Assert.CheckParameterNotNull(pattern, "StringAssert.Matches", "pattern", string.Empty); @@ -618,12 +596,11 @@ public static void Matches(string value, Regex pattern, string? message, params /// expected to not match. /// /// - /// Thrown if matches . + /// is null, or is null, + /// or matches . /// - public static void DoesNotMatch(string value, Regex pattern) - { - DoesNotMatch(value, pattern, string.Empty); - } + public static void DoesNotMatch([NotNull] string? value, [NotNull] Regex? pattern) + => DoesNotMatch(value, pattern, string.Empty); /// /// Tests whether the specified string does not match a regular expression @@ -642,12 +619,11 @@ public static void DoesNotMatch(string value, Regex pattern) /// results. /// /// - /// Thrown if matches . + /// is null, or is null, + /// or matches . /// - public static void DoesNotMatch(string value, Regex pattern, string? message) - { - DoesNotMatch(value, pattern, message, null); - } + public static void DoesNotMatch([NotNull] string? value, [NotNull] Regex? pattern, string? message) + => DoesNotMatch(value, pattern, message, null); /// /// Tests whether the specified string does not match a regular expression @@ -669,9 +645,10 @@ public static void DoesNotMatch(string value, Regex pattern, string? message) /// An array of parameters to use when formatting . /// /// - /// Thrown if matches . + /// is null, or is null, + /// or matches . /// - public static void DoesNotMatch(string value, Regex pattern, string? message, params object?[]? parameters) + public static void DoesNotMatch([NotNull] string? value, [NotNull] Regex? pattern, string? message, params object?[]? parameters) { Assert.CheckParameterNotNull(value, "StringAssert.DoesNotMatch", "value", string.Empty); Assert.CheckParameterNotNull(pattern, "StringAssert.DoesNotMatch", "pattern", string.Empty); diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt index e62f773991..2d2feb3767 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt @@ -241,9 +241,6 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(long expecte static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(T? expected, T? actual) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(T? expected, T? actual, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(T? expected, T? actual, string? message, params object?[]? parameters) -> void @@ -265,9 +262,6 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(long notE static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo! culture, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(T? notExpected, T? actual) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(T? notExpected, T? actual, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(T? notExpected, T? actual, string? message, params object?[]? parameters) -> void @@ -328,72 +322,9 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException(Sy static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func! action) -> System.Threading.Tasks.Task! static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func! action, string! message) -> System.Threading.Tasks.Task! static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func! action, string! message, params object?[]? parameters) -> System.Threading.Tasks.Task! -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection! collection, System.Type! expectedType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection! collection, System.Type! expectedType, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection! collection, System.Type! expectedType, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection! collection) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection! collection, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection! collection, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection! collection) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection! collection, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection! collection, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer! comparer, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection! collection, object? element) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection! collection, object? element, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection! collection, object? element, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection! collection, object? element) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection! collection, object? element, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection! collection, object? element, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection! subset, System.Collections.ICollection! superset, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.That.get -> Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert! static Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessage(string! format, params object?[]! args) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.OnLogMessage -> Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessageHandler? -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring, string? message, System.StringComparison comparisonType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string! value, string! substring, System.StringComparison comparisonType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string! value, System.Text.RegularExpressions.Regex! pattern) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string! value, System.Text.RegularExpressions.Regex! pattern, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string! value, System.Text.RegularExpressions.Regex! pattern, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring, string? message, System.StringComparison comparisonType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string! value, string! substring, System.StringComparison comparisonType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string! value, System.Text.RegularExpressions.Regex! pattern) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string! value, System.Text.RegularExpressions.Regex! pattern, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string! value, System.Text.RegularExpressions.Regex! pattern, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring, string? message) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring, string? message, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring, string? message, System.StringComparison comparisonType) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string! value, string! substring, System.StringComparison comparisonType) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.That.get -> Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert! static readonly Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupExecutionAttribute.DefaultClassCleanupLifecycle -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior static readonly Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute.DefaultDataAccessMethod -> Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index c3a403b069..646944d390 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -16,3 +16,72 @@ Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.DataRowAttribute(o Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.DataRowAttribute(object? arg1, object? arg2, object? arg3, object? arg4, object? arg5, object? arg6, object? arg7, object? arg8, object? arg9, object? arg10, object? arg11, object? arg12, object? arg13, object? arg14) -> void Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.DataRowAttribute(object? arg1, object? arg2, object? arg3, object? arg4, object? arg5, object? arg6, object? arg7, object? arg8, object? arg9, object? arg10, object? arg11, object? arg12, object? arg13, object? arg14, object? arg15) -> void Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.DataRowAttribute(object? arg1, object? arg2, object? arg3, object? arg4, object? arg5, object? arg6, object? arg7, object? arg8, object? arg9, object? arg10, object? arg11, object? arg12, object? arg13, object? arg14, object? arg15, object? arg16) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreNotEqual(string? notExpected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection? collection, System.Type? expectedType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection? collection, System.Type? expectedType, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreInstancesOfType(System.Collections.ICollection? collection, System.Type? expectedType, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection? collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection? collection, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreNotNull(System.Collections.ICollection? collection, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection? collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection? collection, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AllItemsAreUnique(System.Collections.ICollection? collection, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(System.Collections.ICollection? expected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEqual(System.Collections.ICollection? notExpected, System.Collections.ICollection? actual, System.Collections.IComparer? comparer, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreNotEquivalent(System.Collections.ICollection? expected, System.Collections.ICollection? actual, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection? collection, object? element) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection? collection, object? element, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Contains(System.Collections.ICollection? collection, object? element, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection? collection, object? element) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection? collection, object? element, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.DoesNotContain(System.Collections.ICollection? collection, object? element, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsNotSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.IsSubsetOf(System.Collections.ICollection? subset, System.Collections.ICollection? superset, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring, string? message, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Contains(string? value, string? substring, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string? value, System.Text.RegularExpressions.Regex? pattern) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string? value, System.Text.RegularExpressions.Regex? pattern, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.DoesNotMatch(string? value, System.Text.RegularExpressions.Regex? pattern, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring, string? message, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.EndsWith(string? value, string? substring, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string? value, System.Text.RegularExpressions.Regex? pattern) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string? value, System.Text.RegularExpressions.Regex? pattern, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Matches(string? value, System.Text.RegularExpressions.Regex? pattern, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring, string? message, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring, string? message, System.StringComparison comparisonType, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.StartsWith(string? value, string? substring, System.StringComparison comparisonType) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(string? expected, string? actual, bool ignoreCase, System.Globalization.CultureInfo? culture, string? message, params object?[]? parameters) -> void diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs index 941dfb0b51..368c18b0b0 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.AreEqualTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +#nullable enable + using System; using System.Collections.Generic; using System.Globalization; @@ -23,7 +25,7 @@ public void AreNotEqualShouldFailWhenNotEqualTypeWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(1, 1, "A Message")); Verify(ex != null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualString() @@ -37,7 +39,7 @@ public void AreNotEqualShouldFailWhenNotEqualStringWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual("A", "A", "A Message")); Verify(ex != null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualStringAndCaseIgnored() @@ -58,7 +60,7 @@ public void AreNotEqualShouldFailWhenNotEqualIntWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(1, 1, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualLong() @@ -72,7 +74,7 @@ public void AreNotEqualShouldFailWhenNotEqualLongWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(1L, 1L, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualLongWithDelta() @@ -93,7 +95,7 @@ public void AreNotEqualShouldFailWhenNotEqualDecimalWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(0.1M, 0.1M, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualDecimalWithDelta() @@ -114,7 +116,7 @@ public void AreNotEqualShouldFailWhenNotEqualDoubleWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(0.1, 0.1, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualDoubleWithDelta() @@ -135,7 +137,7 @@ public void AreNotEqualShouldFailWhenFloatDoubleWithMessage() { var ex = VerifyThrows(() => Assert.AreNotEqual(100E-2, 100E-2, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreNotEqualShouldFailWhenNotEqualFloatWithDelta() @@ -156,7 +158,7 @@ public void AreEqualShouldFailWhenNotEqualTypeWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(null, "string", "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqual_WithTurkishCultureAndIgnoreCase_Throws() @@ -206,7 +208,7 @@ public void AreEqualShouldFailWhenNotEqualStringWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual("A", "a", "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualStringAndCaseIgnored() @@ -227,7 +229,7 @@ public void AreEqualShouldFailWhenNotEqualIntWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(1, 2, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualLong() @@ -241,7 +243,7 @@ public void AreEqualShouldFailWhenNotEqualLongWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(1L, 2L, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualLongWithDelta() @@ -262,7 +264,7 @@ public void AreEqualShouldFailWhenNotEqualDoubleWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(0.1, 0.2, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualDoubleWithDelta() @@ -283,7 +285,7 @@ public void AreEqualShouldFailWhenNotEqualDecimalWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(0.1M, 0.2M, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualDecimalWithDelta() @@ -304,7 +306,7 @@ public void AreEqualShouldFailWhenFloatDoubleWithMessage() { var ex = VerifyThrows(() => Assert.AreEqual(100E-2, 200E-2, "A Message")); Verify(ex is not null); - Verify(ex.Message.Contains("A Message")); + Verify(ex!.Message.Contains("A Message")); } public void AreEqualShouldFailWhenNotEqualFloatWithDelta() @@ -326,7 +328,7 @@ public void AreEqualTwoObjectsDifferentTypeShouldFail() static void Action() => Assert.AreEqual(new object(), 1); var ex = VerifyThrows(Action); Verify(ex is AssertFailedException); - Verify(ex.Message.Contains("Assert.AreEqual failed. Expected:. Actual:<1 (System.Int32)>.")); + Verify(ex!.Message.Contains("Assert.AreEqual failed. Expected:. Actual:<1 (System.Int32)>.")); } public void AreEqualWithTypeOverridingEqualsShouldWork() @@ -356,9 +358,32 @@ static void Action() Verify(ex is AssertFailedException); } + public void AreEqualStringIgnoreCaseCultureInfoNullabilityPostConditions() + { + CultureInfo? cultureInfo = GetCultureInfo(); + Assert.AreEqual("a", "a", false, cultureInfo); + _ = cultureInfo.Calendar; // no warning + } + + public void AreEqualStringIgnoreCaseCultureInfoMessageNullabilityPostConditions() + { + CultureInfo? cultureInfo = GetCultureInfo(); + Assert.AreEqual("a", "a", false, cultureInfo, "message"); + _ = cultureInfo.Calendar; // no warning + } + + public void AreEqualStringIgnoreCaseCultureInfoMessageParametersNullabilityPostConditions() + { + CultureInfo? cultureInfo = GetCultureInfo(); + Assert.AreEqual("a", "a", false, cultureInfo, "message format {0} {1}", 1, 2); + _ = cultureInfo.Calendar; // no warning + } + + private CultureInfo? GetCultureInfo() => CultureInfo.CurrentCulture; + private class TypeOverridesEquals { - public override bool Equals(object obj) + public override bool Equals(object? obj) { return true; } @@ -371,7 +396,7 @@ public override int GetHashCode() private class EquatableType : IEquatable { - public bool Equals(EquatableType other) + public bool Equals(EquatableType? other) { return true; } @@ -379,7 +404,7 @@ public bool Equals(EquatableType other) private class TypeOverridesEqualsEqualityComparer : EqualityComparer { - public override bool Equals(TypeOverridesEquals x, TypeOverridesEquals y) + public override bool Equals(TypeOverridesEquals? x, TypeOverridesEquals? y) { return false; } diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/CollectionAssertTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/CollectionAssertTests.cs index 31031e9a9b..9adf0634eb 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/CollectionAssertTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/CollectionAssertTests.cs @@ -1,8 +1,16 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +#nullable enable + +using System; +using System.Collections; +using System.Collections.Immutable; + using Microsoft.VisualStudio.TestTools.UnitTesting; +using NuGet.Frameworks; + using TestFramework.ForTestingMSTest; namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Assertions; @@ -17,4 +25,248 @@ public void ThatShouldCacheCollectionAssertInstance() { Verify(CollectionAssert.That == CollectionAssert.That); } + + public void CollectionAssertContainsNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetMatchingElement(); + CollectionAssert.Contains(collection, element); + _ = collection.Count; // no warning + } + + public void CollectionAssertContainsMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetMatchingElement(); + CollectionAssert.Contains(collection, element, "message"); + _ = collection.Count; // no warning + } + + public void CollectionAssertContainsMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetMatchingElement(); + CollectionAssert.Contains(collection, element, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + } + + public void CollectionAssertDoesNotContainNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetNotMatchingElement(); + CollectionAssert.DoesNotContain(collection, element); + _ = collection.Count; // no warning + } + + public void CollectionAssertDoesNotContainMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetNotMatchingElement(); + CollectionAssert.DoesNotContain(collection, element, "message"); + _ = collection.Count; // no warning + } + + public void CollectionAssertDoesNotContainMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + object? element = GetNotMatchingElement(); + CollectionAssert.DoesNotContain(collection, element, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreNotNullNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreNotNull(collection); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreNotNullMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreNotNull(collection, "message"); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreNotNullMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreNotNull(collection, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreUniqueNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreUnique(collection); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreUniqueMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreUnique(collection, "message"); + _ = collection.Count; // no warning + } + + public void CollectionAssertAllItemsAreUniqueMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + CollectionAssert.AllItemsAreUnique(collection, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + } + + public void CollectionAssertIsSubsetOfNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetMatchingSuperSet(); + CollectionAssert.IsSubsetOf(collection, superset); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertIsSubsetOfMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetMatchingSuperSet(); + CollectionAssert.IsSubsetOf(collection, superset, "message"); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertIsSubsetOfMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetMatchingSuperSet(); + CollectionAssert.IsSubsetOf(collection, superset, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertIsNotSubsetOfNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetNotMatchingSuperSet(); + CollectionAssert.IsNotSubsetOf(collection, superset); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertIsNotSubsetOfMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetNotMatchingSuperSet(); + CollectionAssert.IsNotSubsetOf(collection, superset, "message"); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertIsNotSubsetOfMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + ICollection? superset = GetNotMatchingSuperSet(); + CollectionAssert.IsNotSubsetOf(collection, superset, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + _ = superset.Count; // no warning + } + + public void CollectionAssertAllItemsAreInstancesOfTypeNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + Type? type = GetStringType(); + CollectionAssert.AllItemsAreInstancesOfType(collection, type); + _ = collection.Count; // no warning + type.ToString(); // no warning + } + + public void CollectionAssertAllItemsAreInstancesOfTypeMessageNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + Type? type = GetStringType(); + CollectionAssert.AllItemsAreInstancesOfType(collection, type, "message"); + _ = collection.Count; // no warning + type.ToString(); // no warning + } + + public void CollectionAssertAllItemsAreInstancesOfTypeMessageParametersNullabilityPostConditions() + { + ICollection? collection = GetCollection(); + Type? type = GetStringType(); + CollectionAssert.AllItemsAreInstancesOfType(collection, type, "message format {0} {1}", 1, 2); + _ = collection.Count; // no warning + type.ToString(); // no warning + } + + public void CollectionAssertAreEqualComparerNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetCollection(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreEqual(collection1, collection2, comparer); + comparer.ToString(); // no warning + } + + public void CollectionAssertAreEqualComparerMessageNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetCollection(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreEqual(collection1, collection2, comparer, "message"); + comparer.ToString(); // no warning + } + + public void CollectionAssertAreEqualComparerMessageParametersNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetCollection(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreEqual(collection1, collection2, comparer, "message format {0} {1}", 1, 2); + comparer.ToString(); // no warning + } + + public void CollectionAssertAreNotEqualComparerNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetMatchingSuperSet(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreNotEqual(collection1, collection2, comparer); + comparer.ToString(); // no warning + } + + public void CollectionAssertAreNotEqualComparerMessageNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetMatchingSuperSet(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreNotEqual(collection1, collection2, comparer, "message"); + comparer.ToString(); // no warning + } + + public void CollectionAssertAreNotEqualComparerMessageParametersNullabilityPostConditions() + { + ICollection? collection1 = GetCollection(); + ICollection? collection2 = GetMatchingSuperSet(); + IComparer? comparer = GetComparer(); + CollectionAssert.AreNotEqual(collection1, collection2, comparer, "message format {0} {1}", 1, 2); + comparer.ToString(); // no warning + } + + private ICollection? GetCollection() => new[] { "item" }; + + private object? GetMatchingElement() => "item"; + + private object? GetNotMatchingElement() => "not found"; + + private ICollection? GetMatchingSuperSet() => new[] { "item", "item2" }; + + private ICollection? GetNotMatchingSuperSet() => new[] { "item3" }; + + private Type? GetStringType() => typeof(string); + + private IComparer? GetComparer() => new ObjectComparer(); + + private class ObjectComparer : IComparer + { + int IComparer.Compare(object? x, object? y) => Equals(x, y) ? 0 : -1; + } } diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/StringAssertTests.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/StringAssertTests.cs index f382b881e4..ebcf436dd0 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/StringAssertTests.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/StringAssertTests.cs @@ -1,10 +1,13 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +#nullable enable + using System; using System.Text.RegularExpressions; using Microsoft.VisualStudio.TestTools.UnitTesting; + using TestFramework.ForTestingMSTest; namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Assertions; @@ -25,8 +28,7 @@ public void StringAssertContains() string actual = "The quick brown fox jumps over the lazy dog."; string notInString = "I'm not in the string above"; var ex = VerifyThrows(() => StringAssert.Contains(actual, notInString)); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.Contains failed")); + Verify(ex!.Message.Contains("StringAssert.Contains failed")); } public void StringAssertStartsWith() @@ -34,8 +36,7 @@ public void StringAssertStartsWith() string actual = "The quick brown fox jumps over the lazy dog."; string notInString = "I'm not in the string above"; var ex = VerifyThrows(() => StringAssert.StartsWith(actual, notInString)); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.StartsWith failed")); + Verify(ex!.Message.Contains("StringAssert.StartsWith failed")); } public void StringAssertEndsWith() @@ -43,8 +44,7 @@ public void StringAssertEndsWith() string actual = "The quick brown fox jumps over the lazy dog."; string notInString = "I'm not in the string above"; var ex = VerifyThrows(() => StringAssert.EndsWith(actual, notInString)); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.EndsWith failed")); + Verify(ex!.Message.Contains("StringAssert.EndsWith failed")); } public void StringAssertDoesNotMatch() @@ -52,8 +52,7 @@ public void StringAssertDoesNotMatch() string actual = "The quick brown fox jumps over the lazy dog."; Regex doesMatch = new("quick brown fox"); var ex = VerifyThrows(() => StringAssert.DoesNotMatch(actual, doesMatch)); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.DoesNotMatch failed")); + Verify(ex!.Message.Contains("StringAssert.DoesNotMatch failed")); } public void StringAssertContainsIgnoreCase_DoesNotThrow() @@ -81,23 +80,246 @@ public void StringAssertEndsWithIgnoreCase_DoesNotThrow() public void StringAssertContainsDoesNotThrowFormatException() { var ex = VerifyThrows(() => StringAssert.Contains(":-{", "x")); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.Contains failed")); + Verify(ex!.Message.Contains("StringAssert.Contains failed")); } // See https://github.com/dotnet/sdk/issues/25373 public void StringAssertContainsDoesNotThrowFormatExceptionWithArguments() { var ex = VerifyThrows(() => StringAssert.Contains("{", "x", "message {0}", "arg")); - Verify(ex is not null); - Verify(ex.Message.Contains("StringAssert.Contains failed")); + Verify(ex!.Message.Contains("StringAssert.Contains failed")); } // See https://github.com/dotnet/sdk/issues/25373 public void StringAssertContainsFailsIfMessageIsInvalidStringFormatComposite() { var ex = VerifyThrows(() => StringAssert.Contains("a", "b", "message {{0}", "arg")); - Verify(ex is not null); Verify(ex is FormatException); } + + public void StringAssertContainsNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertContainsStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring, StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertContainsMessageNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring, "message"); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertContainsMessageStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring, "message", StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertContainsMessageParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring, "message format {0} {1}", 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertContainsMessageStringComparisonParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.Contains(value, substring, "message format {0} {1}", StringComparison.OrdinalIgnoreCase, 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring, StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithMessageNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring, "message"); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithMessageStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring, "message", StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithMessageParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring, "message format {0} {1}", 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertStartsWithMessageStringComparisonParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingStartsWithString(); + StringAssert.StartsWith(value, substring, "message format {0} {1}", StringComparison.OrdinalIgnoreCase, 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring, StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithMessageNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring, "message"); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithMessageStringComparisonNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring, "message", StringComparison.OrdinalIgnoreCase); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithMessageParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring, "message format {0} {1}", 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertEndsWithMessageStringComparisonParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + string? substring = GetMatchingEndsWithString(); + StringAssert.EndsWith(value, substring, "message format {0} {1}", StringComparison.OrdinalIgnoreCase, 1, 2); + value.ToString(); // no warning + substring.ToString(); // no warning + } + + public void StringAssertMatchesNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetMatchingPattern(); + StringAssert.Matches(value, pattern); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + public void StringAssertMatchesMessageNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetMatchingPattern(); + StringAssert.Matches(value, pattern, "message"); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + public void StringAssertMatchesMessageParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetMatchingPattern(); + StringAssert.Matches(value, pattern, "message format {0} {1}", 1, 2); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + public void StringAssertDoesNotMatchNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetNonMatchingPattern(); + StringAssert.DoesNotMatch(value, pattern); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + public void StringAssertDoesNotMatchMessageNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetNonMatchingPattern(); + StringAssert.DoesNotMatch(value, pattern, "message"); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + public void StringAssertDoesNotMatchMessageParametersNullabilitiesPostConditions() + { + string? value = GetValue(); + Regex? pattern = GetNonMatchingPattern(); + StringAssert.DoesNotMatch(value, pattern, "message format {0} {1}", 1, 2); + value.ToString(); // no warning + pattern.ToString(); // no warning + } + + private string? GetValue() => "some value"; + + private string? GetMatchingStartsWithString() => "some"; + + private string? GetMatchingEndsWithString() => "value"; + + private Regex? GetMatchingPattern() => new("some*"); + + private Regex? GetNonMatchingPattern() => new("something"); }