Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public void WhenNoAdapterExists_ThenReturnsDefaultValue()
        {
            var service = new AdapterService();
            var from    = Mock.Of <IFrom>();

            var adapted = service.Adapt(from).As <string>();

            Assert.Null(adapted);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        public void WhenAdapterDoesNotImplementInterface_ThenNoOps()
        {
            var service = new AdapterService(new BadAdapter());

            Assert.Null(service.Adapt(new object()).As <IFormattable>());
        }
Esempio n. 9
0
 /// <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));
 }