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

Add Assert.IsInstanceOfType<T> #1241

Merged
merged 15 commits into from
Sep 15, 2022
1,591 changes: 1,591 additions & 0 deletions src/TestFramework/MSTest.Core/Assertions/Assert.AreEqual.cs

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions src/TestFramework/MSTest.Core/Assertions/Assert.AreSame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !NETCOREAPP3_0_OR_GREATER && !NET6_0_OR_GREATER
#define HIDE_MESSAGELESS_IMPLEMENTATION
#endif

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

/// <summary>
/// A collection of helper classes to test various conditions within
/// unit tests. If the condition being tested is not met, an exception
/// is thrown.
/// </summary>
public sealed partial class Assert
{
#if HIDE_MESSAGELESS_IMPLEMENTATION
/// <summary>
/// Tests whether the specified objects both refer to the same object and
/// throws an exception if the two inputs do not refer to the same object.
/// </summary>
/// <param name="expected">
/// The first object to compare. This is the value the test expects.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame(object expected, object actual)
{
AreSame(expected, actual, string.Empty, null);
}
#endif

/// <summary>
/// Tests whether the specified objects both refer to the same object and
/// throws an exception if the two inputs do not refer to the same object.
/// </summary>
/// <param name="expected">
/// The first object to compare. This is the value the test expects.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is not the same as <paramref name="expected"/>. The message is shown
/// in test results.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame(object expected, object actual, [CallerArgumentExpression("actual")] string message = null)
{
AreSame(expected, actual, message, null);
}

/// <summary>
/// Tests whether the specified objects both refer to the same object and
/// throws an exception if the two inputs do not refer to the same object.
/// </summary>
/// <param name="expected">
/// The first object to compare. This is the value the test expects.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is not the same as <paramref name="expected"/>. The message is shown
/// in test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="expected"/> does not refer to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreSame(object expected, object actual, [CallerArgumentExpression("actual")] string message = null, params object[] parameters)
{
if (!ReferenceEquals(expected, actual))
{
string userMessage = BuildUserMessage(message, parameters);
string finalMessage = userMessage;

if (expected is ValueType valExpected)
{
if (actual is ValueType valActual)
{
finalMessage = string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.AreSameGivenValues,
userMessage);
}
}

ThrowAssertFailed("Assert.AreSame", finalMessage);
}
}

#if HIDE_MESSAGELESS_IMPLEMENTATION
/// <summary>
/// Tests whether the specified objects refer to different objects and
/// throws an exception if the two inputs refer to the same object.
/// </summary>
/// <param name="notExpected">
/// The first object to compare. This is the value the test expects not
/// to match <paramref name="actual"/>.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="notExpected"/> refers to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreNotSame(object notExpected, object actual)
{
AreNotSame(notExpected, actual, string.Empty, null);
}
#endif

/// <summary>
/// Tests whether the specified objects refer to different objects and
/// throws an exception if the two inputs refer to the same object.
/// </summary>
/// <param name="notExpected">
/// The first object to compare. This is the value the test expects not
/// to match <paramref name="actual"/>.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is the same as <paramref name="notExpected"/>. The message is shown in
/// test results.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="notExpected"/> refers to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreNotSame(object notExpected, object actual, [CallerArgumentExpression("actual")] string message = null)
{
AreNotSame(notExpected, actual, message, null);
}

/// <summary>
/// Tests whether the specified objects refer to different objects and
/// throws an exception if the two inputs refer to the same object.
/// </summary>
/// <param name="notExpected">
/// The first object to compare. This is the value the test expects not
/// to match <paramref name="actual"/>.
/// </param>
/// <param name="actual">
/// The second object to compare. This is the value produced by the code under test.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="actual"/>
/// is the same as <paramref name="notExpected"/>. The message is shown in
/// test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="notExpected"/> refers to the same object
/// as <paramref name="actual"/>.
/// </exception>
public static void AreNotSame(object notExpected, object actual, [CallerArgumentExpression("actual")] string message = null, params object[] parameters)
{
if (ReferenceEquals(notExpected, actual))
{
ThrowAssertFailed("Assert.AreNotSame", BuildUserMessage(message, parameters));
}
}
}
66 changes: 66 additions & 0 deletions src/TestFramework/MSTest.Core/Assertions/Assert.Fail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

/// <summary>
/// A collection of helper classes to test various conditions within
/// unit tests. If the condition being tested is not met, an exception
/// is thrown.
/// </summary>
public sealed partial class Assert
{
/// <summary>
/// Throws an AssertFailedException.
/// </summary>
/// <exception cref="AssertFailedException">
/// Always thrown.
/// </exception>
[DoesNotReturn]
public static void Fail()
{
Fail(string.Empty, null);
}

/// <summary>
/// Throws an AssertFailedException.
/// </summary>
/// <param name="message">
/// The message to include in the exception. The message is shown in
/// test results.
/// </param>
/// <exception cref="AssertFailedException">
/// Always thrown.
/// </exception>
[DoesNotReturn]
public static void Fail(string message)
{
Fail(message, null);
}

/// <summary>
/// Throws an AssertFailedException.
/// </summary>
/// <param name="message">
/// The message to include in the exception. The message is shown in
/// test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertFailedException">
/// Always thrown.
/// </exception>
[DoesNotReturn]
public static void Fail(string message, params object[] parameters)
{
ThrowAssertFailed("Assert.Fail", BuildUserMessage(message, parameters));
}
}
58 changes: 58 additions & 0 deletions src/TestFramework/MSTest.Core/Assertions/Assert.Inconclusive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

/// <summary>
/// A collection of helper classes to test various conditions within
/// unit tests. If the condition being tested is not met, an exception
/// is thrown.
/// </summary>
public sealed partial class Assert
{
/// <summary>
/// Throws an AssertInconclusiveException.
/// </summary>
/// <exception cref="AssertInconclusiveException">
/// Always thrown.
/// </exception>
public static void Inconclusive()
{
Inconclusive(string.Empty, null);
}

/// <summary>
/// Throws an AssertInconclusiveException.
/// </summary>
/// <param name="message">
/// The message to include in the exception. The message is shown in
/// test results.
/// </param>
/// <exception cref="AssertInconclusiveException">
/// Always thrown.
/// </exception>
public static void Inconclusive(string message)
{
Inconclusive(message, null);
}

/// <summary>
/// Throws an AssertInconclusiveException.
/// </summary>
/// <param name="message">
/// The message to include in the exception. The message is shown in
/// test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertInconclusiveException">
/// Always thrown.
/// </exception>
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));
}
}