public void Scan_SampleAssembly_DoesNotRegisterSystemTypes()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new[] { typeof(CompositionRootMock) });
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(string).GetTypeInfo().Assembly);
            Assert.Equal(0, container.AvailableServices.Count());
        }
        public void Scan_SampleAssembly_DoesNotRegisterNestedPrivateTypes()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new[] { typeof(CompositionRootMock) });
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(Foo).GetTypeInfo().Assembly);
            Assert.False(container.AvailableServices.Any(si => si.ImplementingType.Name == "NestedPrivateBar"));
        }
        public void Scan_SampleAssembly_DoesNotRegisterCompilerGeneratedTypes()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new[] { typeof(CompositionRootMock) });
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(Foo).GetTypeInfo().Assembly);
            Assert.False(container.AvailableServices.Any(si => si.ImplementingType != null && si.ImplementingType.GetTypeInfo().IsDefined(typeof(CompilerGeneratedAttribute), false)));
        }
        public void Scan_SampleAssembly_ConfiguresAccordingToPredicate()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(AssemblyScannerTests).GetTypeInfo().Assembly, (s, i) => s == typeof(IFoo));

            Assert.True(container.AvailableServices.Any(sr => sr.ServiceType == typeof(IFoo)));
            Assert.False(container.AvailableServices.Any(sr => sr.ServiceType == typeof(IBar)));
        }
        public void Register_SearchPattern_CallsAssemblyScanner()
        {
            var scannerMock                  = new AssemblyScannerMock();
            var serviceContainer             = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);
            serviceContainer.CompositionRootTypeExtractor = compositionRootExtractorMock;
            serviceContainer.AssemblyScanner = scannerMock;
            serviceContainer.RegisterAssembly("LightInject.Tests.dll");
            scannerMock.Assert(a => a.Scan(typeof(IFoo).Assembly, The <IServiceRegistry> .IsAnyValue, The <Func <ILifetime> > .IsAnyValue, The <Func <Type, Type, bool> > .IsAnyValue), Invoked.Once);
        }
        public void Scan_HostAssembly_DoesNotConfigureInternalServices()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new[] { typeof(CompositionRootMock) });
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(ServiceContainer).GetTypeInfo().Assembly);
            var result = container.AvailableServices.Where(si => si.ImplementingType.Namespace == "LightInject");

            Assert.False(container.AvailableServices.Any(si => si.ImplementingType != null && si.ImplementingType.Namespace == "LightInject"));
        }
Exemplo n.º 7
0
        public void RegisterAssembly_can_register_multiple_inner_types_with_Same_name()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(Issue373).GetTypeInfo().Assembly, (s, i) => s == typeof(BaseInnerFoo));

            Assert.Contains(container.AvailableServices, sr => sr.ImplementingType == typeof(FooWithInnerFoo.InnerFoo));
            Assert.Contains(container.AvailableServices, sr => sr.ImplementingType == typeof(BarWithInnerFoo.InnerFoo));
        }
Exemplo n.º 8
0
        public void Scan_SampleAssembly_ConfiguresAllServicesByDefault()
        {
            var container = new ServiceContainer();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);
            container.CompositionRootTypeExtractor = compositionRootExtractorMock;
            container.RegisterAssembly(typeof(AssemblyScannerTests).GetTypeInfo().Assembly);

            Assert.Contains(container.AvailableServices, sr => sr.ServiceType == typeof(IFoo));
            Assert.Contains(container.AvailableServices, sr => sr.ServiceType == typeof(IBar));
        }
        public void Scan_AssemblyFileWithCompositionRoot_CallsScanMethodMethod()
        {
            var container = new ServiceContainer();
            var compositionRootTypeExtractorMock = new TypeExtractorMock();

            compositionRootTypeExtractorMock.Arrange(t => t.Execute(The <Assembly> .IsAnyValue)).Returns(new [] { typeof(CompositionRootMock) });
            container.CompositionRootTypeExtractor = compositionRootTypeExtractorMock;
            var assemblyScannerMock = new AssemblyScannerMock();

            container.AssemblyScanner = assemblyScannerMock;

            container.RegisterAssembly("LightInject.Tests.dll");
            assemblyScannerMock.Assert(a => a.Scan(The <Assembly> .IsAnyValue, container), Invoked.Once);
        }
        private MockContext <IServiceContainer> GetContainerMock(Func <ILifetime> lifetimeFactory, Func <Type, Type, bool> shouldRegister)
        {
            var containerMock       = new ContainerMock();
            var compositionRootMock = new CompositionRootMock();

            var compositionRootTypeExtractorMock = new TypeExtractorMock();

            compositionRootTypeExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);

            var assemblyScanner = new AssemblyScanner(new ConcreteTypeExtractor(), compositionRootTypeExtractorMock, new CompositionRootExecutor(containerMock, t => compositionRootMock), new GenericArgumentMapper());

            assemblyScanner.Scan(typeof(IFoo).GetTypeInfo().Assembly, containerMock, lifetimeFactory, shouldRegister);
            return(containerMock);
        }
        private MockContext <IServiceContainer> GetContainerMock(Type type)
        {
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(Type.EmptyTypes);
            var concreteTypeExtractorMock = new TypeExtractorMock();

            concreteTypeExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new Type[] { type });
            var scanner = new AssemblyScanner(concreteTypeExtractorMock, compositionRootExtractorMock, null,
                                              new GenericArgumentMapper());
            var containerMock = new ContainerMock();

            scanner.Scan(typeof(IFoo).GetTypeInfo().Assembly, containerMock, () => null, (st, ip) => true);
            return(containerMock);
        }
        public void Scan_SampleAssemblyWithCompositionRoot_CallsComposeMethodOnce()
        {
            var compositionRootMock          = new CompositionRootMock();
            var containerMock                = new ContainerMock();
            var compositionRootExtractorMock = new TypeExtractorMock();

            compositionRootExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new [] { typeof(CompositionRootMock) });
            var assemblyScanner = new AssemblyScanner(new ConcreteTypeExtractor(),
                                                      compositionRootExtractorMock,
                                                      new CompositionRootExecutor(containerMock, t => compositionRootMock), new GenericArgumentMapper());

            assemblyScanner.Scan(typeof(AssemblyScannerTests).GetTypeInfo().Assembly, containerMock);

            compositionRootMock.Assert(c => c.Compose(containerMock), Invoked.Once);
        }
        public void GetInstance_UnknownService_ExecutesCompositionRootInSourceAssembly()
        {
            var container           = new ServiceContainer();
            var compositionRootMock = new CompositionRootMock();

            compositionRootMock.Arrange(c => c.Compose(container)).Callback <IServiceContainer>(c => c.Register <IFoo, Foo>());

            var compositionRootTypeExtractorMock = new TypeExtractorMock();

            compositionRootTypeExtractorMock.Arrange(c => c.Execute(The <Assembly> .IsAnyValue)).Returns(new[] { typeof(CompositionRootMock) });

            var assemblyScanner = new AssemblyScanner(new ConcreteTypeExtractor(), compositionRootTypeExtractorMock,
                                                      new CompositionRootExecutor(container, t => compositionRootMock));

            container.AssemblyScanner = assemblyScanner;

            container.GetInstance <IFoo>();

            compositionRootMock.Assert(c => c.Compose(container), Invoked.Once);
        }