Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions src/TestFramework/MSTest.Core/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,14 +1658,14 @@ public static void IsInstanceOfType(object value, Type expectedType, string mess
/// </exception>
public static void IsInstanceOfType(object value, Type expectedType, string message, params object[] parameters)
{
if (expectedType == null)
if (expectedType == null || value == null)
{
HandleFail("Assert.IsInstanceOfType", message, parameters);
}

var elementTypeInfo = value != null ? value.GetType().GetTypeInfo() : null;
var expectedTypeInfo = expectedType != null ? expectedType.GetTypeInfo() : null;
if (expectedTypeInfo != null && elementTypeInfo != null && !expectedTypeInfo.IsAssignableFrom(elementTypeInfo))
var elementTypeInfo = value.GetType().GetTypeInfo();
var expectedTypeInfo = expectedType.GetTypeInfo();
if (!expectedTypeInfo.IsAssignableFrom(elementTypeInfo))
{
string finalMessage = string.Format(
CultureInfo.CurrentCulture,
Expand Down Expand Up @@ -1750,14 +1750,14 @@ public static void IsNotInstanceOfType(object value, Type wrongType, string mess
/// </exception>
public static void IsNotInstanceOfType(object value, Type wrongType, string message, params object[] parameters)
{
if (wrongType == null)
if (wrongType == null || value == null)
{
HandleFail("Assert.IsNotInstanceOfType", message, parameters);
}

var elementTypeInfo = value != null ? value.GetType().GetTypeInfo() : null;
var expectedTypeInfo = wrongType != null ? wrongType.GetTypeInfo() : null;
if (expectedTypeInfo != null && elementTypeInfo != null && expectedTypeInfo.IsAssignableFrom(elementTypeInfo))
var elementTypeInfo = value.GetType().GetTypeInfo();
var expectedTypeInfo = wrongType.GetTypeInfo();
if (expectedTypeInfo.IsAssignableFrom(elementTypeInfo))
{
string finalMessage = string.Format(
CultureInfo.CurrentCulture,
Expand Down
56 changes: 56 additions & 0 deletions test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,61 @@ public void ReplaceNullCharsShouldReplaceNullCharsInAString()
}

#endregion

#region InstanceOfType tests

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

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

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

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

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

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

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

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

#endregion
}
}