Skip to content

Commit 0d6c7ff

Browse files
authored
Deprecates That property and suggest Instance instead (#5811)
1 parent f036f44 commit 0d6c7ff

File tree

9 files changed

+66
-14
lines changed

9 files changed

+66
-14
lines changed

src/TestFramework/TestFramework/Assertions/Assert.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ private Assert()
1414
{
1515
}
1616

17+
/// <summary>
18+
/// Gets the singleton instance of the Assert functionality.
19+
/// </summary>
20+
/// <remarks>
21+
/// Users can use this to plug-in custom assertions through C# extension methods.
22+
/// For instance, the signature of a custom assertion provider could be "public static void IsOfType&lt;T&gt;(this Assert assert, object obj)"
23+
/// Users could then use a syntax similar to the default assertions which in this case is "Assert.Instance.IsOfType&lt;Dog&gt;(animal);"
24+
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
25+
/// </remarks>
26+
public static Assert Instance { get; } = new Assert();
27+
1728
/// <summary>
1829
/// Gets the singleton instance of the Assert functionality.
1930
/// </summary>
@@ -23,7 +34,12 @@ private Assert()
2334
/// Users could then use a syntax similar to the default assertions which in this case is "Assert.That.IsOfType&lt;Dog&gt;(animal);"
2435
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
2536
/// </remarks>
26-
public static Assert That { get; } = new Assert();
37+
#if NET6_0_OR_GREATER
38+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage, DiagnosticId = "MSTESTOBS")]
39+
#else
40+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage)]
41+
#endif
42+
public static Assert That { get; } = Instance;
2743

2844
/// <summary>
2945
/// Replaces null characters ('\0') with "\\0".

src/TestFramework/TestFramework/Assertions/CollectionAssert.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ private CollectionAssert()
1616
{
1717
}
1818

19+
/// <summary>
20+
/// Gets the singleton instance of the CollectionAssert functionality.
21+
/// </summary>
22+
/// <remarks>
23+
/// Users can use this to plug-in custom assertions through C# extension methods.
24+
/// For instance, the signature of a custom assertion provider could be "public static void AreEqualUnordered(this CollectionAssert customAssert, ICollection expected, ICollection actual)"
25+
/// Users could then use a syntax similar to the default assertions which in this case is "CollectionAssert.Instance.AreEqualUnordered(list1, list2);"
26+
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
27+
/// </remarks>
28+
public static CollectionAssert Instance { get; } = new CollectionAssert();
29+
1930
/// <summary>
2031
/// Gets the singleton instance of the CollectionAssert functionality.
2132
/// </summary>
@@ -25,7 +36,12 @@ private CollectionAssert()
2536
/// Users could then use a syntax similar to the default assertions which in this case is "CollectionAssert.That.AreEqualUnordered(list1, list2);"
2637
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
2738
/// </remarks>
28-
public static CollectionAssert That { get; } = new CollectionAssert();
39+
#if NET6_0_OR_GREATER
40+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage, DiagnosticId = "MSTESTOBS")]
41+
#else
42+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage)]
43+
#endif
44+
public static CollectionAssert That { get; } = Instance;
2945

3046
#endregion
3147

src/TestFramework/TestFramework/Assertions/StringAssert.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ private StringAssert()
1616
{
1717
}
1818

19+
/// <summary>
20+
/// Gets the singleton instance of the StringAssert functionality.
21+
/// </summary>
22+
/// <remarks>
23+
/// Users can use this to plug-in custom assertions through C# extension methods.
24+
/// For instance, the signature of a custom assertion provider could be "public static void ContainsWords(this StringAssert customAssert, string value, ICollection substrings)"
25+
/// Users could then use a syntax similar to the default assertions which in this case is "StringAssert.Instance.ContainsWords(value, substrings);"
26+
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
27+
/// </remarks>
28+
public static StringAssert Instance { get; } = new StringAssert();
29+
1930
/// <summary>
2031
/// Gets the singleton instance of the StringAssert functionality.
2132
/// </summary>
@@ -25,7 +36,12 @@ private StringAssert()
2536
/// Users could then use a syntax similar to the default assertions which in this case is "StringAssert.That.ContainsWords(value, substrings);"
2637
/// More documentation is at "https://github.com/Microsoft/testfx/docs/README.md".
2738
/// </remarks>
28-
public static StringAssert That { get; } = new StringAssert();
39+
#if NET6_0_OR_GREATER
40+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage, DiagnosticId = "MSTESTOBS")]
41+
#else
42+
[Obsolete(FrameworkConstants.ThatPropertyObsoleteMessage)]
43+
#endif
44+
public static StringAssert That { get; } = Instance;
2945

3046
#endregion
3147

src/TestFramework/TestFramework/FrameworkConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
99
internal static class FrameworkConstants
1010
{
1111
internal const string PublicTypeObsoleteMessage = "We will remove or hide this type starting with v4. If you are using this type, reach out to our team on https://github.com/microsoft/testfx.";
12+
internal const string ThatPropertyObsoleteMessage = "The 'That' property is obsolete and will be removed in a future version. Use the 'Instance' property instead.";
1213
}

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsGreaterThan<T>(T lo
77
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsGreaterThanOrEqualTo<T>(T lowerBound, T value) -> void
88
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsGreaterThanOrEqualTo<T>(T lowerBound, T value, string? message) -> void
99
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsGreaterThanOrEqualTo<T>(T lowerBound, T value, string? message, params object?[]? parameters) -> void
10+
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.Assert!
1011
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsInRange<T>(T minValue, T maxValue, T value) -> void
1112
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsInRange<T>(T minValue, T maxValue, T value, string? message) -> void
1213
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsInRange<T>(T minValue, T maxValue, T value, string? message, params object?[]? parameters) -> void
@@ -26,3 +27,5 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsPositive<T>(T value
2627
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsPositive<T>(T value, string? message) -> void
2728
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsPositive<T>(T value, string? message, params object?[]? parameters) -> void
2829
static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Throws<TException>(System.Func<object?>! action, ref Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException!> message) -> TException!
30+
static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert!
31+
static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert!

test/IntegrationTests/TestAssets/FxExtensibilityTestProject/AssertExTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ namespace FxExtensibilityTestProject;
1111
public class AssertExTest
1212
{
1313
[TestMethod]
14-
public void BasicAssertExtensionTest() => Assert.That.IsOfType<ArgumentException>(new ArgumentOutOfRangeException());
14+
public void BasicAssertExtensionTest() => Assert.Instance.IsOfType<ArgumentException>(new ArgumentOutOfRangeException());
1515

1616
[TestMethod]
1717
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
18-
public void BasicFailingAssertExtensionTest() => Assert.That.IsOfType<FormatException>(new ArgumentNullException());
18+
public void BasicFailingAssertExtensionTest() => Assert.Instance.IsOfType<FormatException>(new ArgumentNullException());
1919

2020
[TestMethod]
21-
public void ChainedAssertExtensionTest() => Assert.That.Is().Divisor(120, 5);
21+
public void ChainedAssertExtensionTest() => Assert.Instance.Is().Divisor(120, 5);
2222

2323
[TestMethod]
2424
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
25-
public void ChainedFailingAssertExtensionTest() => Assert.That.Is().Positive(-10);
25+
public void ChainedFailingAssertExtensionTest() => Assert.Instance.Is().Positive(-10);
2626
}

test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests;
55

66
public partial class AssertTests
77
{
8-
#region That tests
9-
public void ThatShouldReturnAnInstanceOfAssert() => Verify(Assert.That is not null);
8+
#region Instance tests
9+
public void InstanceShouldReturnAnInstanceOfAssert() => Verify(Assert.Instance is not null);
1010

11-
public void ThatShouldCacheAssertInstance() => Verify(ReferenceEquals(Assert.That, Assert.That));
11+
public void InstanceShouldCacheAssertInstance() => Verify(ReferenceEquals(Assert.Instance, Assert.Instance));
1212
#endregion
1313

1414
#region ReplaceNullChars tests

test/UnitTests/TestFramework.UnitTests/Assertions/CollectionAssertTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Assertions
99

1010
public class CollectionAssertTests : TestContainer
1111
{
12-
public void ThatShouldReturnAnInstanceOfCollectionAssert() => Verify(CollectionAssert.That is not null);
12+
public void InstanceShouldReturnAnInstanceOfCollectionAssert() => Verify(CollectionAssert.Instance is not null);
1313

14-
public void ThatShouldCacheCollectionAssertInstance() => Verify(CollectionAssert.That == CollectionAssert.That);
14+
public void InstanceShouldCacheCollectionAssertInstance() => Verify(CollectionAssert.Instance == CollectionAssert.Instance);
1515

1616
public void CollectionAssertContainsNullabilityPostConditions()
1717
{

test/UnitTests/TestFramework.UnitTests/Assertions/StringAssertTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Assertions
77

88
public class StringAssertTests : TestContainer
99
{
10-
public void ThatShouldReturnAnInstanceOfStringAssert() => Verify(StringAssert.That is not null);
10+
public void InstanceShouldReturnAnInstanceOfStringAssert() => Verify(StringAssert.Instance is not null);
1111

12-
public void ThatShouldCacheStringAssertInstance() => Verify(StringAssert.That == StringAssert.That);
12+
public void InstanceShouldCacheStringAssertInstance() => Verify(StringAssert.Instance == StringAssert.Instance);
1313

1414
public void StringAssertContains()
1515
{

0 commit comments

Comments
 (0)