From 07974b44e2477ef000eda764abb99cafd57dabc7 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 10 Mar 2014 10:48:22 -0700 Subject: [PATCH] Adding unit tests for TypeExtensions.IsCompatibleObject --- .../Internal/TypeExtensionTests.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/Microsoft.AspNet.Mvc.ModelBinding.Test/Internal/TypeExtensionTests.cs diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Internal/TypeExtensionTests.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Internal/TypeExtensionTests.cs new file mode 100644 index 0000000000..d083e0d39e --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Internal/TypeExtensionTests.cs @@ -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 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 + { + } + } +}