public void WhenAdapterImplementsMultipleInterfaces_ThenItIsCalledForAll() { var mock = new Mock <IAdapter <IFrom, string> >(); mock.Setup(x => x.Adapt(It.IsAny <IFrom>())).Returns("foo"); mock.As <IAdapter <IFrom, ITo> >(); var service = new AdapterService(mock.Object); var from = Mock.Of <IFrom>(); service.Adapt(from).As <string>(); service.Adapt(from).As <ITo>(); mock.Verify(x => x.Adapt(from)); mock.As <IAdapter <IFrom, ITo> >().Verify(x => x.Adapt(from)); }
public void WhenAdapterExistsForBaseInterface_ThenReturnsAdaptedObject() { var service = new AdapterService(new StringAdapter()); var foo = Mock.Of <IFrom3>(); var adapted = service.Adapt(foo).As <string>(); Assert.Equal("foo", adapted); }
public void WhenNoAdapterExists_ThenReturnsDefaultValue() { var service = new AdapterService(); var from = Mock.Of <IFrom>(); var adapted = service.Adapt(from).As <string>(); Assert.Null(adapted); }
public void WhenAdapterExistsToDerivedInterface_ThenReturnsAdaptedObject() { var service = new AdapterService( Mock.Of <IAdapter <IFrom, ITo2> >(x => x.Adapt(It.IsAny <IFrom>()) == Mock.Of <ITo2>())); var adapted = service.Adapt(Mock.Of <IFrom>()).As <ITo>(); Assert.NotNull(adapted); }
public void WhenAdapterImplementsInterface_ThenItIsUsed() { var service = new AdapterService(new StringAdapter()); var from = Mock.Of <IFrom>(); var adapted = service.Adapt(from).As <string>(); Assert.NotNull(adapted); Assert.Equal("foo", adapted); }
public void WhenAdapterExistsToMultipleDerivedInterfaces_ThenReturnsAdaptedObjectFromMostSpecific() { var service = new AdapterService( Mock.Of <IAdapter <IFrom, ITo2> >(x => x.Adapt(It.IsAny <IFrom>()) == Mock.Of <ITo2>()), Mock.Of <IAdapter <IFrom, ITo3> >(x => x.Adapt(It.IsAny <IFrom>()) == Mock.Of <ITo3>())); var adapted = service.Adapt(Mock.Of <IFrom>()).As <ITo>(); Assert.NotNull(adapted); Assert.IsAssignableFrom <ITo3>(adapted); }
public void WhenMultipleAdaptersExist_ThenReturnsAdaptedObjectFromClosestInheritance() { var service = new AdapterService(new IAdapter[] { Mock.Of <IAdapter <IFrom, ITo> >(x => x.Adapt(It.IsAny <IFrom>()) == Mock.Of <ITo>(_ => _.Name == "IFrom, ITo")), Mock.Of <IAdapter <IFrom3, ITo3> >(x => x.Adapt(It.IsAny <IFrom3>()) == Mock.Of <ITo3>(_ => _.Name == "IFrom3, ITo3")), }); Assert.Equal("IFrom, ITo", service.Adapt(Mock.Of <IFrom>()).As <ITo>().Name); Assert.Equal("IFrom, ITo", service.Adapt(Mock.Of <IFrom2>()).As <ITo>().Name); Assert.Equal("IFrom3, ITo3", service.Adapt(Mock.Of <IFrom3>()).As <ITo>().Name); // No registered adapter to ITo2 from IFrom or IFrom2 Assert.Null(service.Adapt(Mock.Of <IFrom>()).As <ITo2>()); Assert.Null(service.Adapt(Mock.Of <IFrom2>()).As <ITo2>()); // But ITo3 is assignable to ITo2, so from IFrom3 we can adapt Assert.Equal("IFrom3, ITo3", service.Adapt(Mock.Of <IFrom3>()).As <ITo2>().Name); }
public void WhenAdapterDoesNotImplementInterface_ThenNoOps() { var service = new AdapterService(new BadAdapter()); Assert.Null(service.Adapt(new object()).As <IFormattable>()); }
/// <summary> /// Returns an adaptable object for the given <paramref name="source"/>. /// </summary> /// <returns>The adaptable object for the given source type.</returns> public static IAdaptable <TSource> Adapt <TSource>(this TSource source) where TSource : class { return(AdapterService.Adapt <TSource>(source)); }