Adding unit tests for TypeExtensions.IsCompatibleObject
This commit is contained in:
parent
59eaa8f642
commit
07974b44e2
|
|
@ -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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue