Adding unit tests for TypeExtensions.IsCompatibleObject

This commit is contained in:
Pranav K 2014-03-10 10:48:22 -07:00
parent 59eaa8f642
commit 07974b44e2
1 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Extensions;
namespace Microsoft.AspNet.Mvc.ModelBinding.Internal.Test
{
public class TypeExtensionTests
{
[Theory]
[InlineData(typeof(decimal))]
[InlineData(typeof(Guid))]
public void IsCompatibleWithReturnsFalse_IfValueTypeIsNull(Type type)
{
// Act
bool result = TypeExtensions.IsCompatibleObject(type, value: null);
// Assert
Assert.False(result);
}
[Theory]
[InlineData(typeof(short))]
[InlineData(typeof(DateTimeOffset))]
[InlineData(typeof(Foo))]
public void IsCompatibleWithReturnsFalse_IfValueIsMismatched(Type type)
{
// Act
bool result = TypeExtensions.IsCompatibleObject(type, value: "Hello world");
// Assert
Assert.False(result);
}
public static IEnumerable<object[]> TypesWithValues
{
get
{
yield return new object[] { typeof(int?), null };
yield return new object[] { typeof(int), 4 };
yield return new object[] { typeof(int?), 1 };
yield return new object[] { typeof(DateTime?), null };
yield return new object[] { typeof(Guid), Guid.Empty };
yield return new object[] { typeof(DateTimeOffset?), DateTimeOffset.UtcNow };
yield return new object[] { typeof(string), null };
yield return new object[] { typeof(string), "foo string" };
yield return new object[] { typeof(Foo), null };
yield return new object[] { typeof(Foo), new Foo() };
}
}
[Theory]
[PropertyData("TypesWithValues")]
public void IsCompatibleWithReturnsTrue_IfValueIsAssignable(Type type, object value)
{
// Act
bool result = TypeExtensions.IsCompatibleObject(type, value);
// Assert
Assert.True(result);
}
private class Foo
{
}
}
}