private void checkCast <T>(object source, T value) { Assert.IsTrue(Typecasts.CanCastTo(source, typeof(T))); var result = Typecasts.CastTo(source, typeof(T)); Assert.AreEqual(value, result); }
public void CastCollection() { checkCast <object>(collection, collection); checkCast <IEnumerable <ITypedElement> >(collection, collection); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(ITypedElement))); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool))); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool?))); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(string))); }
public void CastFromNull() { checkCast <object>(null, null); checkCast <IEnumerable <ITypedElement> >(null, ElementNode.EmptyList); checkCast <ITypedElement>(null, null); Assert.IsFalse(Typecasts.CanCastTo(null, typeof(bool))); checkCast <bool?>(null, null); checkCast <string>(null, null); }
public void CastFromNull() { checkCast <object>(null, null); checkCast <IEnumerable <IElementNavigator> >(null, FhirValueList.Empty); checkCast <IElementNavigator>(null, null); Assert.False(Typecasts.CanCastTo(null, typeof(bool))); checkCast <bool?>(null, null); checkCast <string>(null, null); }
public void CastComplex() { checkCast <object>(complex, complex); Assert.IsTrue(Typecasts.CanCastTo(complex, typeof(IEnumerable <ITypedElement>))); var result = (IEnumerable <ITypedElement>)Typecasts.CastTo(complex, typeof(IEnumerable <ITypedElement>)); Assert.AreEqual(complex, result.Single()); checkCast <ITypedElement>(complex, complex); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool))); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool?))); Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(string))); }
public void CastNullable() { checkCast <object>("hi", "hi"); Assert.IsTrue(Typecasts.CanCastTo("hi", typeof(IEnumerable <ITypedElement>))); var result = (IEnumerable <ITypedElement>)Typecasts.CastTo("hi", typeof(IEnumerable <ITypedElement>)); Assert.AreEqual("hi", result.Single().Value); Assert.IsTrue(Typecasts.CanCastTo("hi", typeof(ITypedElement))); var result2 = (ITypedElement)Typecasts.CastTo("hi", typeof(ITypedElement)); Assert.AreEqual("hi", result2.Value); checkCast <bool?>(true, true); checkCast <decimal?>(4L, 4m); checkCast <string>("hi", "hi"); Assert.IsFalse(Typecasts.CanCastTo(4, typeof(string))); Assert.IsFalse(Typecasts.CanCastTo(4m, typeof(long?))); }
public void CastValue() { checkCast <object>(4L, 4L); Assert.True(Typecasts.CanCastTo(4, typeof(IEnumerable <IElementNavigator>))); var result = (IEnumerable <IElementNavigator>)Typecasts.CastTo(4L, typeof(IEnumerable <IElementNavigator>)); Assert.Equal(4L, result.Single().Value); Assert.True(Typecasts.CanCastTo(4L, typeof(IElementNavigator))); var result2 = (IElementNavigator)Typecasts.CastTo(4L, typeof(IElementNavigator)); Assert.Equal(4L, result2.Value); checkCast <bool>(true, true); checkCast <decimal>(4L, 4m); checkCast <bool?>(true, true); checkCast <decimal?>(4L, 4m); checkCast <string>("hi", "hi"); Assert.False(Typecasts.CanCastTo(4, typeof(string))); Assert.False(Typecasts.CanCastTo(4m, typeof(long))); }