public void GetInstance_ICollection_InterceptsThatCollection()
        {
            // Arrange
            var expectedInterceptedTypes = new[]
            {
                typeof(IEnumerable <ITimeProvider>),
                typeof(ICollection <ITimeProvider>)
            };

            var container = new Container();

            container.Options.EnableAutoVerification = false;

            var contexts = new List <InitializationContext>();

            container.Options.RegisterResolveInterceptor((c, p) =>
            {
                contexts.Add(c);
                var i = p();
                return(i);
            },
                                                         c => true);

            container.Collection.Append <ITimeProvider, RealTimeProvider>();

            // Act
            container.GetInstance <ICollection <ITimeProvider> >();

            // Assert
            var actualInterceptedTypes =
                from context in contexts
                select context.Registration.ImplementationType;

            AssertThat.SequenceEquals(expectedInterceptedTypes, actualInterceptedTypes);
        }
Ejemplo n.º 2
0
        public void GetAllInstances_AppendingInstancesOfOpenGenericImplementations_ResolvesTheExpectedCollection()
        {
            // Arrange
            Type[] expectedHandlerTypes = new[]
            {
                typeof(NewConstraintEventHandler <StructEvent>),
                typeof(StructConstraintEventHandler <StructEvent>),
            };

            var container = ContainerFactory.New();

            container.Collection.Register(typeof(IEventHandler <>), new[] { typeof(NewConstraintEventHandler <>) });

            container.Collection
            .AppendInstance(typeof(IEventHandler <>), new StructConstraintEventHandler <StructEvent>());

            // AuditableEventEventHandler<AuditableEvent> can be registered, and resolved, but should not
            // be resolved as part of IEnumerable<IEventHandler<StructEvent>>.
            container.Collection
            .AppendInstance(typeof(IEventHandler <>), new AuditableEventEventHandler <AuditableEvent>());

            // Act
            var handlers = container.GetAllInstances(typeof(IEventHandler <StructEvent>));

            Type[] actualHandlerTypes = handlers.Select(h => h.GetType()).ToArray();

            // Assert
            AssertThat.SequenceEquals(expectedHandlerTypes, actualHandlerTypes);
        }
Ejemplo n.º 3
0
        public void GetAllInstances_MultipleAppendedOpenGenericTypesMixedWithClosedGenericRegisterCollection_ResolvesTheExpectedCollection()
        {
            // Arrange
            Type[] expectedHandlerTypes = new[]
            {
                typeof(NewConstraintEventHandler <StructEvent>),
                typeof(AuditableEventEventHandler <StructEvent>),
                typeof(StructConstraintEventHandler <StructEvent>),
            };

            var container = ContainerFactory.New();

            container.Collection.Append(typeof(IEventHandler <>), typeof(NewConstraintEventHandler <>));

            container.Collection.Register(typeof(IEventHandler <StructEvent>), new[]
            {
                typeof(AuditableEventEventHandler <StructEvent>)
            });

            container.Collection.Append(typeof(IEventHandler <>), typeof(StructConstraintEventHandler <>));

            // Act
            Type[] actualHandlerTypes = container.GetAllInstances(typeof(IEventHandler <StructEvent>))
                                        .Select(h => h.GetType()).ToArray();

            // Assert
            AssertThat.SequenceEquals(expectedHandlerTypes, actualHandlerTypes);
        }
Ejemplo n.º 4
0
        private static void _(Type type, Type genericTypeDefinition, params Type[] expected)
        {
            Type[] actual = type.GetClosedTypesOf(genericTypeDefinition);

            AssertThat.SequenceEquals(
                expectedTypes: expected.OrderBy(HashCode),
                actualTypes: actual.OrderBy(HashCode));
        }
        public void Create_WhenReturnedCollectionIterated_ProducesTheExpectedInstances()
        {
            // Arrange
            var expectedTypes = new[] { typeof(NullLogger), typeof(ConsoleLogger) };

            var container = new Container();

            // Act
            var stream = container.Collection.Create <ILogger>(expectedTypes);

            // Assert
            AssertThat.SequenceEquals(expectedTypes, actualTypes: stream.Select(GetType));
        }
        public void Create_CollectionWithAbstraction_CallsBackIntoContainerUponIterationToGetTheDefaultRegistration()
        {
            // Arrange
            var container = new Container();

            container.Register <ILogger, ConsoleLogger>();

            // Act
            var stream = container.Collection.Create <ILogger>(typeof(ILogger));

            // Assert
            AssertThat.SequenceEquals(
                expectedTypes: new[] { typeof(ConsoleLogger) },
                actualTypes: stream.Select(GetType));
        }
        public void GetAllInstances_TwoUncontrolledVariantCollections2_ResolvesInstancesThroughBaseType()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.RegisterCollection <ITypeConverter <DerivedA> >(new[] { new DerivedAConverter() });
            container.RegisterCollection <ITypeConverter <DerivedB> >(new[] { new DerivedBConverter() });

            // Act
            var baseConverters = container.GetAllInstances <ITypeConverter <BaseClass> >();
            var types          = baseConverters.Select(b => b.GetType()).ToArray();

            // Assert
            AssertThat.SequenceEquals(new[] { typeof(DerivedAConverter), typeof(DerivedBConverter) }, types);
        }
        public void Create_CreatingMultipleCollectionsOfTheSameServiceType_ProducesTheExpectedInstancesForBothCollections()
        {
            // Arrange
            var expectedTypes1 = new[] { typeof(NullLogger), typeof(ConsoleLogger) };
            var expectedTypes2 = new[] { typeof(Logger <int>), typeof(Logger <bool>) };

            var container = new Container();

            // Act
            var stream1 = container.Collection.Create <ILogger>(expectedTypes1);
            var stream2 = container.Collection.Create <ILogger>(expectedTypes2);

            // Assert
            AssertThat.SequenceEquals(expectedTypes1, actualTypes: stream1.Select(GetType));
            AssertThat.SequenceEquals(expectedTypes2, actualTypes: stream2.Select(GetType));
        }
Ejemplo n.º 9
0
        private static void Assert_CalledTwiceForSameType_ResolvesExpectedSequence <T>(
            Action <Container> registration,
            params Type[] expectedTypes)
            where T : class
        {
            // Arrange
            var container = new Container();

            // Act
            registration(container);

            var instances = container.GetAllInstances <T>();

            var actualTypes = instances.Select(GetType).ToArray();

            // Assert
            AssertThat.SequenceEquals(expectedTypes, actualTypes);
        }
Ejemplo n.º 10
0
        public void GetAllInstances_AppendingInstancesOfClosedGenericImplementation_ResolvesTheExpectedCollection()
        {
            // Arrange
            Type[] expectedHandlerTypes = new[]
            {
                typeof(NewConstraintEventHandler <StructEvent>),
                typeof(AuditableEventEventHandler <StructEvent>),
            };

            var container = ContainerFactory.New();

            container.Collection.Register(typeof(IEventHandler <>), new[] { typeof(NewConstraintEventHandler <>) });

            container.Collection
            .AppendInstance <IEventHandler <StructEvent> >(new AuditableEventEventHandler <StructEvent>());

            // Act
            var handlers = container.GetAllInstances(typeof(IEventHandler <StructEvent>));

            Type[] actualHandlerTypes = handlers.Select(h => h.GetType()).ToArray();

            // Assert
            AssertThat.SequenceEquals(expectedHandlerTypes, actualHandlerTypes);
        }
Ejemplo n.º 11
0
        public void GetAllInstances_RegistrationPrependedToExistingOpenGenericRegistration_ResolvesTheExtectedCollection()
        {
            // Arrange
            Type[] expectedHandlerTypes = new[]
            {
                typeof(StructEventHandler),
                typeof(NewConstraintEventHandler <StructEvent>),
            };

            var container = ContainerFactory.New();

            var registration = Lifestyle.Transient.CreateRegistration <StructEventHandler>(container);

            container.Collection.Append(typeof(IEventHandler <>), registration);

            container.Collection.Register(typeof(IEventHandler <>), new[] { typeof(NewConstraintEventHandler <>) });

            // Act
            Type[] actualHandlerTypes = container.GetAllInstances(typeof(IEventHandler <StructEvent>))
                                        .Select(h => h.GetType()).ToArray();

            // Assert
            AssertThat.SequenceEquals(expectedHandlerTypes, actualHandlerTypes);
        }