Ejemplo n.º 1
0
        private static ConstructorInfo GetSelectedConstructor(ConventionBuilder builder, Type type)
        {
            ConstructorInfo reply = null;

            foreach (var ci in type.GetTypeInfo().DeclaredConstructors)
            {
                var li = builder.GetDeclaredAttributes(type, ci);
                if (li.Length > 0)
                {
                    Assert.True(reply == null);                   // Fail if we got more than one constructor
                    reply = ci;
                }
            }

            return(reply);
        }
Ejemplo n.º 2
0
        public void NotifyImportsSatisfiedTwice_ShouldSucceed()
        {
            var builder = new ConventionBuilder();

            builder.ForType <OnImportsSatisfiedMultipleClass>().NotifyImportsSatisfied(p => p.OnImportsSatisfied1());
            builder.ForType <OnImportsSatisfiedMultipleClass>().NotifyImportsSatisfied(p => p.OnImportsSatisfied2());
            var container = new ContainerConfiguration()
                            .WithPart <OnImportsSatisfiedMultipleClass>(builder)
                            .WithPart <ExportValues>(builder)
                            .CreateContainer();
            var test = container.GetExport <OnImportsSatisfiedMultipleClass>();

            Assert.IsNotNull(test.P1);
            Assert.IsNotNull(test.P2);
            Assert.AreEqual(6, test.OnImportsSatisfiedInvoked);
        }
Ejemplo n.º 3
0
        public void NotifyImportsSatisfiedAttributeAppliedToDerivedClassExportBase_ShouldSucceed()
        {
            var builder = new ConventionBuilder();

            builder.ForType <OnImportsSatisfiedDerivedClass>().NotifyImportsSatisfied(p => p.OnImportsSatisfied());
            var container = new ContainerConfiguration()
                            .WithPart <OnImportsSatisfiedTestClass>(builder)
                            .WithPart <OnImportsSatisfiedDerivedClass>(builder)
                            .WithPart <ExportValues>(builder)
                            .CreateContainer();
            var test = container.GetExport <OnImportsSatisfiedTestClass>();

            Assert.IsNotNull(test.P1);
            Assert.IsNotNull(test.P2);
            Assert.AreEqual(0, test.OnImportsSatisfiedInvoked);
        }
Ejemplo n.º 4
0
        public void ConventionSelectsConstructor_SelectsTheOneWithMostParameters()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImplWithConstructors>();

            var selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));

            Assert.IsNotNull(selectedConstructor);
            Assert.AreEqual(2, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(int id, string name) { }


            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.AreEqual(0, attributes.Count());
        }
Ejemplo n.º 5
0
        public void InsideTheLambdaCallGetCustomAttributesShouldSucceed()
        {
            var builder = new ConventionBuilder();

            builder.ForTypesMatching((t) => !t.GetTypeInfo().IsDefined(typeof(MyDoNotIncludeAttribute), false)).Export();
            var container = new ContainerConfiguration()
                            .WithPart <MyNotToBeIncludedClass>(builder)
                            .WithPart <MyToBeIncludedClass>(builder)
                            .CreateContainer();

            var importer = new ImporterOfMyNotTobeIncludedClass();

            container.SatisfyImports(importer);

            Assert.Null(importer.MyNotToBeIncludedClass);
            Assert.NotNull(importer.MyToBeIncludedClass);
        }
Ejemplo n.º 6
0
        public void NoOperations_ShouldGenerateNoAttributes()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl));

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.Equal(0, attributes.Count());
        }
Ejemplo n.º 7
0
        public void ExportSelf_ShouldGenerateSingleExportAttribute()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).Export();

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.Equal(1, attributes.Count());
            Assert.NotNull(attributes[0] as ExportAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.Equal(0, attributes.Count());
        }
Ejemplo n.º 8
0
        public void ManuallySelectingConstructor_SelectsTheExplicitOne()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImplWithConstructors>().SelectConstructor(param => new FooImplWithConstructors(param.Import <int>()));

            var selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));

            Assert.IsNotNull(selectedConstructor);
            Assert.AreEqual(1, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(IEnumerable<IFoo>) { }

            var pi = selectedConstructor.GetParameters()[0];

            Assert.AreEqual(typeof(int), pi.ParameterType);

            var attributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), pi);

            Assert.AreEqual(1, attributes.Count());
            Assert.IsNotNull(attributes[0] as ImportAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImplWithConstructors), null);
            Assert.AreEqual(0, attributes.Count());
        }
Ejemplo n.º 9
0
        public void ImportProperty_ShouldGenerateImportForPropertySelected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty(p => p.P1);

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.AreEqual(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.AreEqual(1, attributes.Count());

            var importAttribute = attributes.First((t) => t.GetType() == typeof(ImportAttribute)) as ImportAttribute;

            Assert.IsNull(importAttribute.ContractName);

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.AreEqual(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.AreEqual(0, attributes.Count());
        }
Ejemplo n.º 10
0
        public void ImportProperties_ShouldGenerateImportForPropertySelected_And_ApplyImportMany()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).ImportProperties(p => p.Name == "P3");

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.Equal(1, attributes.Count());

            var importAttribute = attributes.First((t) => t.GetType() == typeof(ImportManyAttribute)) as ImportManyAttribute;

            Assert.Null(importAttribute.ContractName);
        }
Ejemplo n.º 11
0
        public void ManuallySelectingConstructor_SelectsTheExplicitOne_IEnumerableParameterBecomesImportMany()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImplWithConstructors)).SelectConstructor(cis => cis.ElementAt(2));

            var selectedConstructor = GetSelectedConstructor(builder, typeof(FooImplWithConstructors));

            Assert.NotNull(selectedConstructor);
            Assert.Equal(1, selectedConstructor.GetParameters().Length);     // Should select public FooImplWithConstructors(IEnumerable<IFoo>) { }

            var pi = selectedConstructor.GetParameters()[0];

            Assert.Equal(typeof(IEnumerable <IFoo>), pi.ParameterType);

            var attributes = builder.GetDeclaredAttributes(typeof(FooImplWithConstructors), pi);

            Assert.Equal(1, attributes.Count());
            Assert.NotNull(attributes[0] as ImportManyAttribute);

            attributes = GetAttributesFromMember(builder, typeof(FooImplWithConstructors), null);
            Assert.Equal(0, attributes.Count());
        }
Ejemplo n.º 12
0
        public void StandardExportInterfacesInterfaceFilterConfiguredContractShouldWork()
        {
            //Same test as above only using default export builder
            var builder = new ConventionBuilder();

            builder.ForTypesMatching((t) => true).ExportInterfaces((iface) => iface != typeof(System.IDisposable), (iface, bldr) => bldr.AsContractType((Type)iface));
            builder.ForTypesMatching((t) => t.GetTypeInfo().ImplementedInterfaces.Where((iface) => iface != typeof(System.IDisposable)).Count() == 0).Export();

            var container = new ContainerConfiguration()
                            .WithPart <Standard>(builder)
                            .WithPart <Dippy>(builder)
                            .WithPart <Derived>(builder)
                            .WithPart <BareClass>(builder)
                            .CreateContainer();

            var importer = new Importer();

            container.SatisfyImports(importer);

            Assert.NotNull(importer.First);
            Assert.True(importer.First.Count() == 3);
            Assert.NotNull(importer.Second);
            Assert.True(importer.Second.Count() == 3);
            Assert.NotNull(importer.Third);
            Assert.True(importer.Third.Count() == 3);
            Assert.NotNull(importer.Fourth);
            Assert.True(importer.Fourth.Count() == 3);
            Assert.NotNull(importer.Fifth);
            Assert.True(importer.Fifth.Count() == 3);

            Assert.Null(importer.Base);
            Assert.Null(importer.Derived);
            Assert.Null(importer.Dippy);
            Assert.Null(importer.Standard);
            Assert.Null(importer.Disposable);
            Assert.NotNull(importer.BareClass);
        }
Ejemplo n.º 13
0
        public void StandardExportInterfacesShouldWork()
        {
            // Export all interfaces except IDisposable, Export contracts on types without interfaces. except for disposable types
            var builder = new ConventionBuilder();

            builder.ForTypesMatching((t) => true).ExportInterfaces();
            builder.ForTypesMatching((t) => t.GetTypeInfo().ImplementedInterfaces.Where((iface) => iface != typeof(System.IDisposable)).Count() == 0).Export();

            var container = new ContainerConfiguration()
                            .WithPart <Standard>(builder)
                            .WithPart <Dippy>(builder)
                            .WithPart <Derived>(builder)
                            .WithPart <BareClass>(builder)
                            .CreateContainer();

            var importer = new Importer();

            container.SatisfyImports(importer);

            Assert.NotNull(importer.First);
            Assert.True(importer.First.Count() == 3);
            Assert.NotNull(importer.Second);
            Assert.True(importer.Second.Count() == 3);
            Assert.NotNull(importer.Third);
            Assert.True(importer.Third.Count() == 3);
            Assert.NotNull(importer.Fourth);
            Assert.True(importer.Fourth.Count() == 3);
            Assert.NotNull(importer.Fifth);
            Assert.True(importer.Fifth.Count() == 3);

            Assert.Null(importer.Base);
            Assert.Null(importer.Derived);
            Assert.Null(importer.Dippy);
            Assert.Null(importer.Standard);
            Assert.Null(importer.Disposable);
            Assert.NotNull(importer.BareClass);
        }
Ejemplo n.º 14
0
        public void ExportPropertyWithConfiguration_ShouldGenerateExportForAllProperties()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ExportProperty(p => p.P1, c => c.AsContractName("hey").AsContractType <IFoo>());

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.Equal(1, attributes.Count());

            var exportAttribute = attributes.First((t) => t.GetType() == typeof(ExportAttribute)) as ExportAttribute;

            Assert.Same("hey", exportAttribute.ContractName);
            Assert.Same(typeof(IFoo), exportAttribute.ContractType);

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.Equal(0, attributes.Count());
        }
Ejemplo n.º 15
0
        public void ExportPropertyOfT_ShouldGenerateExportForPropertySelectedWithTAsContractType()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).ExportProperties(p => p.Name == "P1", (p, c) => c.AsContractType <IFoo>());

            var attributes = GetAttributesFromMember(builder, typeof(FooImpl), null);

            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P1");
            Assert.Equal(1, attributes.Count());

            var exportAttribute = attributes.First((t) => t.GetType() == typeof(ExportAttribute)) as ExportAttribute;

            Assert.Null(exportAttribute.ContractName);
            Assert.Same(typeof(IFoo), exportAttribute.ContractType);

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P2");
            Assert.Equal(0, attributes.Count());

            attributes = GetAttributesFromMember(builder, typeof(FooImpl), "P3");
            Assert.Equal(0, attributes.Count());
        }
Ejemplo n.º 16
0
        private static IEnumerable <ExportAttribute> GetExportAttributes(ConventionBuilder builder, Type type)
        {
            var list = builder.GetDeclaredAttributes(type, type.GetTypeInfo());

            return(list.Cast <ExportAttribute>());
        }
Ejemplo n.º 17
0
        private static ImportMetadataConstraintAttribute GetImportMetadataConstraintAttribute(ConventionBuilder builder)
        {
            var list = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetRuntimeProperties().Where((m) => m.Name == "IFooProperty").First());

            Assert.Equal(2, list.Length);
            return(list.OfType <ImportMetadataConstraintAttribute>().FirstOrDefault());
        }