Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow most APIs to accept nullable values or arguments #1467

Merged
merged 8 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 64 additions & 98 deletions src/TestFramework/TestFramework/Assertions/Assert.AreEqual.cs

Large diffs are not rendered by default.

27 changes: 12 additions & 15 deletions src/TestFramework/TestFramework/Assertions/Assert.AreSame.cs
Original file line number Diff line number Diff line change
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 @@ -30,10 +31,8 @@ public sealed partial class Assert
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual)
{
AreSame(expected, actual, string.Empty, null);
}
public static void AreSame<T>([NotNullIfNotNull(nameof(actual))] T? expected, [NotNullIfNotNull(nameof(expected))] T? actual)
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
=> AreSame(expected, actual, string.Empty, null);

/// <summary>
/// Tests whether the specified objects both refer to the same object and
Expand All @@ -57,10 +56,10 @@ public static void AreSame<T>(T? expected, T? actual)
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual, string? message)
{
AreSame(expected, actual, message, null);
}
public static void AreSame<T>(
[NotNullIfNotNull(nameof(actual))] T? expected, [NotNullIfNotNull(nameof(expected))] T? actual,
string? message)
=> AreSame(expected, actual, message, null);

/// <summary>
/// Tests whether the specified objects both refer to the same object and
Expand All @@ -87,7 +86,9 @@ public static void AreSame<T>(T? expected, T? actual, string? message)
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame<T>(T? expected, T? actual, string? message, params object?[]? parameters)
public static void AreSame<T>(
[NotNullIfNotNull(nameof(actual))] T? expected, [NotNullIfNotNull(nameof(expected))] T? actual,
string? message, params object?[]? parameters)
{
if (ReferenceEquals(expected, actual))
{
Expand Down Expand Up @@ -130,9 +131,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 +157,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
Original file line number Diff line number Diff line change
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));
}
Original file line number Diff line number Diff line change
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));
}
}
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -132,14 +124,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 +148,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 +157,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 +181,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 +190,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 Down Expand Up @@ -269,7 +251,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
Original file line number Diff line number Diff line change
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
32 changes: 8 additions & 24 deletions src/TestFramework/TestFramework/Assertions/Assert.IsTrue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public sealed partial class Assert
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue([DoesNotReturnIf(false)] bool condition)
{
IsTrue(condition, string.Empty, null);
}
=> IsTrue(condition, string.Empty, null);

/// <summary>
/// Tests whether the specified condition is true and throws an exception
Expand All @@ -38,9 +36,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool condition)
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue([DoesNotReturnIf(false)] bool? condition)
{
IsTrue(condition, string.Empty, null);
}
=> IsTrue(condition, string.Empty, null);

/// <summary>
/// Tests whether the specified condition is true and throws an exception
Expand All @@ -57,9 +53,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool? condition)
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? message)
{
IsTrue(condition, message, null);
}
=> IsTrue(condition, message, null);

/// <summary>
/// Tests whether the specified condition is true and throws an exception
Expand All @@ -76,9 +70,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool condition, string? messa
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? message)
{
IsTrue(condition, message, null);
}
=> IsTrue(condition, message, null);

/// <summary>
/// Tests whether the specified condition is true and throws an exception
Expand Down Expand Up @@ -141,9 +133,7 @@ public static void IsTrue([DoesNotReturnIf(false)] bool? condition, string? mess
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse([DoesNotReturnIf(true)] bool condition)
{
IsFalse(condition, string.Empty, null);
}
=> IsFalse(condition, string.Empty, null);

/// <summary>
/// Tests whether the specified condition is false and throws an exception
Expand All @@ -156,9 +146,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool condition)
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse([DoesNotReturnIf(true)] bool? condition)
{
IsFalse(condition, string.Empty, null);
}
=> IsFalse(condition, string.Empty, null);

/// <summary>
/// Tests whether the specified condition is false and throws an exception
Expand All @@ -175,9 +163,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool? condition)
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? message)
{
IsFalse(condition, message, null);
}
=> IsFalse(condition, message, null);

/// <summary>
/// Tests whether the specified condition is false and throws an exception
Expand All @@ -194,9 +180,7 @@ public static void IsFalse([DoesNotReturnIf(true)] bool condition, string? messa
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse([DoesNotReturnIf(true)] bool? condition, string? message)
{
IsFalse(condition, message, null);
}
=> IsFalse(condition, message, null);

/// <summary>
/// Tests whether the specified condition is false and throws an exception
Expand Down
Loading