public TypeAdapterFactory(
     bool singletonAdapters = false,
     TypeAdapterRegistry <IAdapter> registry = null,
     Func <Type, IAdapter> creator           = null
     )
 {
     _typeAdapterRegistry = registry ?? new TypeAdapterRegistry <IAdapter>();
     _adapterCreator      = creator ?? DefaultCreator;
     _singletonCache      = singletonAdapters ? new Dictionary <Type, IAdapter>() : null;
 }
        public void FindReturnsEveryTypeOnlyOnce2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguiseSpecialPearAndPearAsApple>();
            var pearAdapters = registry.FindAdapterTypesFor <SpecialPear>();

            // assert
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), pearAdapters.Single());
        }
        public void SimpleRegisterAndFind()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearAsApple>();
            var adapter = registry.FindAdapterTypesFor <Pear>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearAsApple), adapter);
        }
        public void RegisterAndFindMultiPurposeAdapter()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearOrOrangeAsApple>();
            var pearAdapter   = registry.FindAdapterTypesFor <Pear>().Single();
            var orangeAdapter = registry.FindAdapterTypesFor <Orange>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearOrOrangeAsApple), pearAdapter);
            Assert.AreEqual(typeof(DisguisePearOrOrangeAsApple), orangeAdapter);
        }
        public void RegisterAndFindSpecializedAdaptee2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguisePearAsApple>();
            var pearAdapter        = registry.FindAdapterTypesFor <Pear>().Single();
            var specialPearAdapter = registry.FindAdapterTypesFor <SpecialPear>().Single();

            // assert
            Assert.AreEqual(typeof(DisguisePearAsApple), pearAdapter);
            Assert.AreEqual(typeof(DisguisePearAsApple), specialPearAdapter);
        }
        public void ExpectArgumentExceptionIfAdapterWrongType()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IPear>();

            try
            {
                // act
                registry.Register(typeof(DisguisePearAsApple));

                // assert
                Assert.Fail("Expecting ArgumentException");
            }
            catch (ArgumentException)
            {
                // success :-)
            }
        }
        public void ExpectArgumentExceptionIfAdapteeCouldNotBeResolved()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            try
            {
                // act
                registry.Register <DisguisePearAsBadApple>();

                // assert
                Assert.Fail("Expecting ArgumentException");
            }
            catch (ArgumentException)
            {
                // success :-)
            }
        }
        public void FindReturnsInReverseRegistrationOrder2()
        {
            // arrange
            var registry = new TypeAdapterRegistry <IApple>();

            // act
            registry.Register <DisguiseSpecialPearAndPearAsApple>();
            registry.Register <DisguiseSpecialPearAsApple>();
            registry.Register <DisguisePearAsApple>();

            var pearAdapters        = registry.FindAdapterTypesFor <Pear>().ToArray();
            var specialPearAdapters = registry.FindAdapterTypesFor <SpecialPear>().ToArray();

            // assert
            Assert.AreEqual(2, pearAdapters.Length);
            Assert.AreEqual(typeof(DisguisePearAsApple), pearAdapters[0]);
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), pearAdapters[1]);

            Assert.AreEqual(3, specialPearAdapters.Length);
            Assert.AreEqual(typeof(DisguiseSpecialPearAsApple), specialPearAdapters[0]);
            Assert.AreEqual(typeof(DisguiseSpecialPearAndPearAsApple), specialPearAdapters[1]);
            Assert.AreEqual(typeof(DisguisePearAsApple), specialPearAdapters[2]);
        }