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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public static void IsInstanceOfType(object value, Type expectedType)
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
/// inheritance hierarchy of the object.
/// </summary>
public static void IsInstanceOfType<T>(object value)
{
IsInstanceOfType(value, typeof(T), string.Empty, null);
}

/// <summary>
/// Tests whether the specified object is an instance of the expected
/// type and throws an exception if the expected type is not in the
Expand All @@ -61,6 +71,16 @@ public static void IsInstanceOfType(object value, Type expectedType, string mess
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
/// inheritance hierarchy of the object.
/// </summary>
public static void IsInstanceOfType<T>(object value, string message)
{
IsInstanceOfType(value, typeof(T), message, null);
}

/// <summary>
/// Tests whether the specified object is an instance of the expected
/// type and throws an exception if the expected type is not in the
Expand Down Expand Up @@ -107,6 +127,16 @@ public static void IsInstanceOfType(object value, Type expectedType, string mess
}
}

/// <summary>
/// Tests whether the specified object is an instance of the generic
/// type and throws an exception if the generictype is not in the
/// inheritance hierarchy of the object.
/// </summary>
public static void IsInstanceOfType<T>(object value, string message, params object[] parameters)
{
IsInstanceOfType(value, typeof(T), message, parameters);
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong
/// type and throws an exception if the specified type is in the
Expand All @@ -128,6 +158,16 @@ public static void IsNotInstanceOfType(object value, Type wrongType)
IsNotInstanceOfType(value, wrongType, string.Empty, null);
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong generic
/// type and throws an exception if the specified type is in the
/// inheritance hierarchy of the object.
/// </summary>
public static void IsNotInstanceOfType<T>(object value)
{
IsNotInstanceOfType(value, typeof(T), string.Empty, null);
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong
/// type and throws an exception if the specified type is in the
Expand All @@ -154,6 +194,16 @@ public static void IsNotInstanceOfType(object value, Type wrongType, string mess
IsNotInstanceOfType(value, wrongType, message, null);
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong generic
/// type and throws an exception if the specified type is in the
/// inheritance hierarchy of the object.
/// </summary>
public static void IsNotInstanceOfType<T>(object value, string message)
{
IsNotInstanceOfType(value, typeof(T), message, null);
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong
/// type and throws an exception if the specified type is in the
Expand Down Expand Up @@ -205,4 +255,14 @@ public static void IsNotInstanceOfType(object value, Type wrongType, string mess
ThrowAssertFailed("Assert.IsNotInstanceOfType", finalMessage);
}
}

/// <summary>
/// Tests whether the specified object is not an instance of the wrong generic
/// type and throws an exception if the specified type is in the
/// inheritance hierarchy of the object.
/// </summary>
public static void IsNotInstanceOfType<T>(object value, string message, params object[] parameters)
{
IsNotInstanceOfType(value, typeof(T), message, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,90 @@ namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests;
public partial class AssertTests
{
[TestMethod]
public void InstanceOfTypeShouldFailWhenValueIsNull()
public void IsInstanceOfType_WhenValueIsNull_Fails()
{
static void action() => TestFrameworkV2.Assert.IsInstanceOfType(null, typeof(AssertTests));
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TestFrameworkV2.AssertFailedException));
}

[TestMethod]
public void InstanceOfTypeShouldFailWhenTypeIsNull()
public void IsInstanceOfType_WhenTypeIsNull_Fails()
{
static void action() => TestFrameworkV2.Assert.IsInstanceOfType(5, null);
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TestFrameworkV2.AssertFailedException));
}

[TestMethod]
public void InstanceOfTypeShouldPassOnSameInstance()
public void IsInstanceOfType_OnSameInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsInstanceOfType(5, typeof(int));
}

[TestMethod]
public void InstanceOfTypeShouldPassOnHigherInstance()
public void IsInstanceOfType_OnHigherInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsInstanceOfType(5, typeof(object));
}

[TestMethod]
public void InstanceNotOfTypeShouldFailWhenValueIsNull()
public void IsNotInstanceOfType_WhenValueIsNull_DoesNotThrow()
{
Action action = () => TestFrameworkV2.Assert.IsNotInstanceOfType(null, typeof(AssertTests));
TestFrameworkV2.Assert.IsNotInstanceOfType(null, typeof(object));
}

[TestMethod]
public void InstanceNotOfTypeShouldFailWhenTypeIsNull()
public void IsNotInstanceOfType_WhenTypeIsNull_Fails()
{
static void action() => TestFrameworkV2.Assert.IsNotInstanceOfType(5, null);
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TestFrameworkV2.AssertFailedException));
}

[TestMethod]
public void InstanceNotOfTypeShouldPassOnWrongInstance()
public void IsNotInstanceOfType_OnWrongInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsNotInstanceOfType(5L, typeof(int));
}

[TestMethod]
public void InstanceNotOfTypeShouldPassOnSubInstance()
public void IsNotInstanceOfType_OnSubInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsNotInstanceOfType(new object(), typeof(int));
}

[TestMethod]
public void IsInstanceOfTypeUsingGenericType_WhenValueIsNull_Fails()
{
static void action() => TestFrameworkV2.Assert.IsInstanceOfType<AssertTests>(null);
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TestFrameworkV2.AssertFailedException));
}

[TestMethod]
public void IsInstanceOfTypeUsingGenericType_OnSameInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsInstanceOfType<int>(5);
}

[TestMethod]
public void IsInstanceOfTypeUsingGenericType_OnHigherInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsInstanceOfType<object>(5);
}

[TestMethod]
public void IsNotInstanceOfTypeUsingGenericType_WhenValueIsNull_DoesNotThrow()
{
TestFrameworkV2.Assert.IsNotInstanceOfType<object>(null);
}

[TestMethod]
public void IsNotInstanceOfType_OnWrongInstanceUsingGenericType_DoesNotThrow()
{
TestFrameworkV2.Assert.IsNotInstanceOfType<int>(5L);
}

[TestMethod]
public void IsNotInstanceOfTypeUsingGenericType_OnSubInstance_DoesNotThrow()
{
TestFrameworkV2.Assert.IsNotInstanceOfType<int>(new object());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static void ActionShouldThrowExceptionOfType(Action action, Type type)
}
catch (Exception ex)
{
TestFramework.Assert.AreEqual(type, ex.GetType());
var t = ex.GetType();
TestFramework.Assert.AreEqual(type, t);
}
}

Expand Down