Skip to content

Commit

Permalink
Allow most APIs to accept nullable values or arguments (#1467)
Browse files Browse the repository at this point in the history
Fixes #1451
  • Loading branch information
Evangelink committed Dec 16, 2022
1 parent 713032a commit 4adcb83
Show file tree
Hide file tree
Showing 16 changed files with 1,012 additions and 650 deletions.
170 changes: 68 additions & 102 deletions src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs

Large diffs are not rendered by default.

17 changes: 5 additions & 12 deletions src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -31,9 +32,7 @@ public sealed partial class Assert
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual)
{
AreSame(expected, actual, string.Empty, null);
}
=> AreSame(expected, actual, string.Empty, null);

/// <summary>
/// Tests whether the specified objects both refer to the same object and
Expand All @@ -58,9 +57,7 @@ public static void AreSame<T>(T? expected, T? actual)
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual, string? message)
{
AreSame(expected, actual, message, null);
}
=> AreSame(expected, actual, message, null);

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

/// <summary>
/// Tests whether the specified objects refer to different objects and
Expand All @@ -158,9 +153,7 @@ public static void AreNotSame<T>(T? notExpected, T? actual)
/// as <paramref name="actual"/>.
/// </exception>
public static void AreNotSame<T>(T? notExpected, T? actual, string? message)
{
AreNotSame(notExpected, actual, message, null);
}
=> AreNotSame(notExpected, actual, message, null);

/// <summary>
/// Tests whether the specified objects refer to different objects and
Expand Down
12 changes: 3 additions & 9 deletions src/TestFramework/TestFramework/Assertions/Assert.Fail.cs
Expand Up @@ -20,9 +20,7 @@ public sealed partial class Assert
/// </exception>
[DoesNotReturn]
public static void Fail()
{
Fail(string.Empty, null);
}
=> Fail(string.Empty, null);

/// <summary>
/// Throws an AssertFailedException.
Expand All @@ -36,9 +34,7 @@ public static void Fail()
/// </exception>
[DoesNotReturn]
public static void Fail(string? message)
{
Fail(message, null);
}
=> Fail(message, null);

/// <summary>
/// Throws an AssertFailedException.
Expand All @@ -55,7 +51,5 @@ public static void Fail(string? message)
/// </exception>
[DoesNotReturn]
public static void Fail(string? message, params object?[]? parameters)
{
ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
}
=> ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
}
Expand Up @@ -21,9 +21,7 @@ public sealed partial class Assert
/// </exception>
[DoesNotReturn]
public static void Inconclusive()
{
Inconclusive(string.Empty, null);
}
=> Inconclusive(string.Empty, null);

/// <summary>
/// Throws an AssertInconclusiveException.
Expand All @@ -37,9 +35,7 @@ public static void Inconclusive()
/// </exception>
[DoesNotReturn]
public static void Inconclusive(string? message)
{
Inconclusive(message, null);
}
=> Inconclusive(message, null);

/// <summary>
/// Throws an AssertInconclusiveException.
Expand All @@ -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));
}
}
Expand Up @@ -32,20 +32,16 @@ public sealed partial class Assert
/// of <paramref name="value"/>.
/// </exception>
public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? expectedType)
{
IsInstanceOfType(value, expectedType, string.Empty, null);
}
=> IsInstanceOfType(value, expectedType, string.Empty, null);

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
public static void IsInstanceOfType<T>([NotNull] object? value)
{
IsInstanceOfType(value, typeof(T), string.Empty, null);
}
=> IsInstanceOfType(value, typeof(T), string.Empty, null);

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

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
public static void IsInstanceOfType<T>([NotNull] object? value, string? message)
{
IsInstanceOfType(value, typeof(T), message, null);
}
=> IsInstanceOfType(value, typeof(T), message, null);

/// <summary>
/// Tests whether the specified object is an instance of the expected
Expand All @@ -108,7 +100,8 @@ public static void IsInstanceOfType<T>([NotNull] object? value, string? message)
/// <paramref name="expectedType"/> is not in the inheritance hierarchy
/// of <paramref name="value"/>.
/// </exception>
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)
{
Expand All @@ -132,14 +125,12 @@ public static void IsInstanceOfType([NotNull] object? value, [NotNull] Type? exp

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The expected type of <paramref name="value"/>.</typeparam>
public static void IsInstanceOfType<T>([NotNull] object? value, string? message, params object?[]? parameters)
{
IsInstanceOfType(value, typeof(T), message, parameters);
}
=> IsInstanceOfType(value, typeof(T), message, parameters);

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

/// <summary>
/// Tests whether the specified object is not an instance of the wrong generic
Expand All @@ -169,9 +158,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType)
/// </summary>
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
public static void IsNotInstanceOfType<T>(object? value)
{
IsNotInstanceOfType(value, typeof(T), string.Empty, null);
}
=> IsNotInstanceOfType(value, typeof(T), string.Empty, null);

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

/// <summary>
/// Tests whether the specified object is not an instance of the wrong generic
Expand All @@ -206,9 +191,7 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType,
/// </summary>
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
public static void IsNotInstanceOfType<T>(object? value, string? message)
{
IsNotInstanceOfType(value, typeof(T), message, null);
}
=> IsNotInstanceOfType(value, typeof(T), message, null);

/// <summary>
/// Tests whether the specified object is not an instance of the wrong
Expand All @@ -234,7 +217,8 @@ public static void IsNotInstanceOfType<T>(object? value, string? message)
/// <paramref name="wrongType"/> is in the inheritance hierarchy
/// of <paramref name="value"/>.
/// </exception>
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)
{
Expand Down Expand Up @@ -269,7 +253,5 @@ public static void IsNotInstanceOfType(object? value, [NotNull] Type? wrongType,
/// </summary>
/// <typeparam name="T">The type that <paramref name="value"/> should not be.</typeparam>
public static void IsNotInstanceOfType<T>(object? value, string? message, params object?[]? parameters)
{
IsNotInstanceOfType(value, typeof(T), message, parameters);
}
=> IsNotInstanceOfType(value, typeof(T), message, parameters);
}
16 changes: 4 additions & 12 deletions src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs
Expand Up @@ -23,9 +23,7 @@ public sealed partial class Assert
/// Thrown if <paramref name="value"/> is not null.
/// </exception>
public static void IsNull(object? value)
{
IsNull(value, string.Empty, null);
}
=> IsNull(value, string.Empty, null);

/// <summary>
/// Tests whether the specified object is null and throws an exception
Expand All @@ -42,9 +40,7 @@ public static void IsNull(object? value)
/// Thrown if <paramref name="value"/> is not null.
/// </exception>
public static void IsNull(object? value, string? message)
{
IsNull(value, message, null);
}
=> IsNull(value, message, null);

/// <summary>
/// Tests whether the specified object is null and throws an exception
Expand Down Expand Up @@ -82,9 +78,7 @@ public static void IsNull(object? value, string? message, params object?[]? para
/// Thrown if <paramref name="value"/> is null.
/// </exception>
public static void IsNotNull([NotNull] object? value)
{
IsNotNull(value, string.Empty, null);
}
=> IsNotNull(value, string.Empty, null);

/// <summary>
/// Tests whether the specified object is non-null and throws an exception
Expand All @@ -101,9 +95,7 @@ public static void IsNotNull([NotNull] object? value)
/// Thrown if <paramref name="value"/> is null.
/// </exception>
public static void IsNotNull([NotNull] object? value, string? message)
{
IsNotNull(value, message, null);
}
=> IsNotNull(value, message, null);

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

0 comments on commit 4adcb83

Please sign in to comment.