Skip to content

Commit 4adcb83

Browse files
authored
Allow most APIs to accept nullable values or arguments (#1467)
Fixes #1451
1 parent 713032a commit 4adcb83

File tree

16 files changed

+1012
-650
lines changed

16 files changed

+1012
-650
lines changed

src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs

Lines changed: 68 additions & 102 deletions
Large diffs are not rendered by default.

src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
67

78
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -31,9 +32,7 @@ public sealed partial class Assert
3132
/// as <paramref name="actual"/>.
3233
/// </exception>
3334
public static void AreSame<T>(T? expected, T? actual)
34-
{
35-
AreSame(expected, actual, string.Empty, null);
36-
}
35+
=> AreSame(expected, actual, string.Empty, null);
3736

3837
/// <summary>
3938
/// Tests whether the specified objects both refer to the same object and
@@ -58,9 +57,7 @@ public static void AreSame<T>(T? expected, T? actual)
5857
/// as <paramref name="actual"/>.
5958
/// </exception>
6059
public static void AreSame<T>(T? expected, T? actual, string? message)
61-
{
62-
AreSame(expected, actual, message, null);
63-
}
60+
=> AreSame(expected, actual, message, null);
6461

6562
/// <summary>
6663
/// Tests whether the specified objects both refer to the same object and
@@ -130,9 +127,7 @@ public static void AreSame<T>(T? expected, T? actual, string? message, params ob
130127
/// as <paramref name="actual"/>.
131128
/// </exception>
132129
public static void AreNotSame<T>(T? notExpected, T? actual)
133-
{
134-
AreNotSame(notExpected, actual, string.Empty, null);
135-
}
130+
=> AreNotSame(notExpected, actual, string.Empty, null);
136131

137132
/// <summary>
138133
/// Tests whether the specified objects refer to different objects and
@@ -158,9 +153,7 @@ public static void AreNotSame<T>(T? notExpected, T? actual)
158153
/// as <paramref name="actual"/>.
159154
/// </exception>
160155
public static void AreNotSame<T>(T? notExpected, T? actual, string? message)
161-
{
162-
AreNotSame(notExpected, actual, message, null);
163-
}
156+
=> AreNotSame(notExpected, actual, message, null);
164157

165158
/// <summary>
166159
/// Tests whether the specified objects refer to different objects and

src/TestFramework/TestFramework/Assertions/Assert.Fail.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public sealed partial class Assert
2020
/// </exception>
2121
[DoesNotReturn]
2222
public static void Fail()
23-
{
24-
Fail(string.Empty, null);
25-
}
23+
=> Fail(string.Empty, null);
2624

2725
/// <summary>
2826
/// Throws an AssertFailedException.
@@ -36,9 +34,7 @@ public static void Fail()
3634
/// </exception>
3735
[DoesNotReturn]
3836
public static void Fail(string? message)
39-
{
40-
Fail(message, null);
41-
}
37+
=> Fail(message, null);
4238

4339
/// <summary>
4440
/// Throws an AssertFailedException.
@@ -55,7 +51,5 @@ public static void Fail(string? message)
5551
/// </exception>
5652
[DoesNotReturn]
5753
public static void Fail(string? message, params object?[]? parameters)
58-
{
59-
ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
60-
}
54+
=> ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
6155
}

src/TestFramework/TestFramework/Assertions/Assert.Inconclusive.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public sealed partial class Assert
2121
/// </exception>
2222
[DoesNotReturn]
2323
public static void Inconclusive()
24-
{
25-
Inconclusive(string.Empty, null);
26-
}
24+
=> Inconclusive(string.Empty, null);
2725

2826
/// <summary>
2927
/// Throws an AssertInconclusiveException.
@@ -37,9 +35,7 @@ public static void Inconclusive()
3735
/// </exception>
3836
[DoesNotReturn]
3937
public static void Inconclusive(string? message)
40-
{
41-
Inconclusive(message, null);
42-
}
38+
=> Inconclusive(message, null);
4339

4440
/// <summary>
4541
/// Throws an AssertInconclusiveException.
@@ -58,6 +54,7 @@ public static void Inconclusive(string? message)
5854
public static void Inconclusive(string? message, params object?[]? parameters)
5955
{
6056
string userMessage = BuildUserMessage(message, parameters);
61-
throw new AssertInconclusiveException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, "Assert.Inconclusive", userMessage));
57+
throw new AssertInconclusiveException(
58+
string.Format(CultureInfo.CurrentCulture, FrameworkMessages.AssertionFailed, "Assert.Inconclusive", userMessage));
6259
}
6360
}

src/TestFramework/TestFramework/Assertions/Assert.IsInstanceOfType.cs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ public sealed partial class Assert
3232
/// of <paramref name="value"/>.
3333
/// </exception>
3434
public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType)
35-
{
36-
IsInstanceOfType(value, expectedType, string.Empty, null);
37-
}
35+
=> IsInstanceOfType(value, expectedType, string.Empty, null);
3836

3937
/// <summary>
4038
/// Tests whether the specified object is an instance of the generic
41-
/// type and throws an exception if the generictype is not in the
39+
/// type and throws an exception if the generic type is not in the
4240
/// inheritance hierarchy of the object.
4341
/// </summary>
4442
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
4543
public static void IsInstanceOfType<T>([NotNull] object? value)
46-
{
47-
IsInstanceOfType(value, typeof(T), string.Empty, null);
48-
}
44+
=> IsInstanceOfType(value, typeof(T), string.Empty, null);
4945

5046
/// <summary>
5147
/// Tests whether the specified object is an instance of the expected
@@ -69,20 +65,16 @@ public static void IsInstanceOfType<T>([NotNull] object? value)
6965
/// of <paramref name="value"/>.
7066
/// </exception>
7167
public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message)
72-
{
73-
IsInstanceOfType(value, expectedType, message, null);
74-
}
68+
=> IsInstanceOfType(value, expectedType, message, null);
7569

7670
/// <summary>
7771
/// Tests whether the specified object is an instance of the generic
78-
/// type and throws an exception if the generictype is not in the
72+
/// type and throws an exception if the generic type is not in the
7973
/// inheritance hierarchy of the object.
8074
/// </summary>
8175
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
8276
public static void IsInstanceOfType<T>([NotNull] object? value, string? message)
83-
{
84-
IsInstanceOfType(value, typeof(T), message, null);
85-
}
77+
=> IsInstanceOfType(value, typeof(T), message, null);
8678

8779
/// <summary>
8880
/// Tests whether the specified object is an instance of the expected
@@ -108,7 +100,8 @@ public static void IsInstanceOfType<T>([NotNull] object? value, string? message)
108100
/// <paramref name="expectedType"/> is not in the inheritance hierarchy
109101
/// of <paramref name="value"/>.
110102
/// </exception>
111-
public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message, params object?[]? parameters)
103+
public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType, string? message,
104+
params object?[]? parameters)
112105
{
113106
if (expectedType == null || value == null)
114107
{
@@ -132,14 +125,12 @@ public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? exp
132125

133126
/// <summary>
134127
/// Tests whether the specified object is an instance of the generic
135-
/// type and throws an exception if the generictype is not in the
128+
/// type and throws an exception if the generic type is not in the
136129
/// inheritance hierarchy of the object.
137130
/// </summary>
138131
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
139132
public static void IsInstanceOfType<T>([NotNull] object? value, string? message, params object?[]? parameters)
140-
{
141-
IsInstanceOfType(value, typeof(T), message, parameters);
142-
}
133+
=> IsInstanceOfType(value, typeof(T), message, parameters);
143134

144135
/// <summary>
145136
/// Tests whether the specified object is not an instance of the wrong
@@ -158,9 +149,7 @@ public static void IsInstanceOfType<T>([NotNull] object? value, string? message,
158149
/// of <paramref name="value"/>.
159150
/// </exception>
160151
public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType)
161-
{
162-
IsNotInstanceOfType(value, wrongType, string.Empty, null);
163-
}
152+
=> IsNotInstanceOfType(value, wrongType, string.Empty, null);
164153

165154
/// <summary>
166155
/// 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)
169158
/// </summary>
170159
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
171160
public static void IsNotInstanceOfType<T>(object? value)
172-
{
173-
IsNotInstanceOfType(value, typeof(T), string.Empty, null);
174-
}
161+
=> IsNotInstanceOfType(value, typeof(T), string.Empty, null);
175162

176163
/// <summary>
177164
/// Tests whether the specified object is not an instance of the wrong
@@ -195,9 +182,7 @@ public static void IsNotInstanceOfType<T>(object? value)
195182
/// of <paramref name="value"/>.
196183
/// </exception>
197184
public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message)
198-
{
199-
IsNotInstanceOfType(value, wrongType, message, null);
200-
}
185+
=> IsNotInstanceOfType(value, wrongType, message, null);
201186

202187
/// <summary>
203188
/// 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,
206191
/// </summary>
207192
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
208193
public static void IsNotInstanceOfType<T>(object? value, string? message)
209-
{
210-
IsNotInstanceOfType(value, typeof(T), message, null);
211-
}
194+
=> IsNotInstanceOfType(value, typeof(T), message, null);
212195

213196
/// <summary>
214197
/// Tests whether the specified object is not an instance of the wrong
@@ -234,7 +217,8 @@ public static void IsNotInstanceOfType<T>(object? value, string? message)
234217
/// <paramref name="wrongType"/> is in the inheritance hierarchy
235218
/// of <paramref name="value"/>.
236219
/// </exception>
237-
public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message, params object?[]? parameters)
220+
public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType, string? message,
221+
params object?[]? parameters)
238222
{
239223
if (wrongType == null)
240224
{
@@ -269,7 +253,5 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType,
269253
/// </summary>
270254
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
271255
public static void IsNotInstanceOfType<T>(object? value, string? message, params object?[]? parameters)
272-
{
273-
IsNotInstanceOfType(value, typeof(T), message, parameters);
274-
}
256+
=> IsNotInstanceOfType(value, typeof(T), message, parameters);
275257
}

src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public sealed partial class Assert
2323
/// Thrown if <paramref name="value"/> is not null.
2424
/// </exception>
2525
public static void IsNull(object? value)
26-
{
27-
IsNull(value, string.Empty, null);
28-
}
26+
=> IsNull(value, string.Empty, null);
2927

3028
/// <summary>
3129
/// Tests whether the specified object is null and throws an exception
@@ -42,9 +40,7 @@ public static void IsNull(object? value)
4240
/// Thrown if <paramref name="value"/> is not null.
4341
/// </exception>
4442
public static void IsNull(object? value, string? message)
45-
{
46-
IsNull(value, message, null);
47-
}
43+
=> IsNull(value, message, null);
4844

4945
/// <summary>
5046
/// 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
8278
/// Thrown if <paramref name="value"/> is null.
8379
/// </exception>
8480
public static void IsNotNull([NotNull] object? value)
85-
{
86-
IsNotNull(value, string.Empty, null);
87-
}
81+
=> IsNotNull(value, string.Empty, null);
8882

8983
/// <summary>
9084
/// Tests whether the specified object is non-null and throws an exception
@@ -101,9 +95,7 @@ public static void IsNotNull([NotNull] object? value)
10195
/// Thrown if <paramref name="value"/> is null.
10296
/// </exception>
10397
public static void IsNotNull([NotNull] object? value, string? message)
104-
{
105-
IsNotNull(value, message, null);
106-
}
98+
=> IsNotNull(value, message, null);
10799

108100
/// <summary>
109101
/// Tests whether the specified object is non-null and throws an exception

0 commit comments

Comments
 (0)