Example #1
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.NotNull(test.P1);
            Assert.NotNull(test.P2);
            Assert.Equal(6, test.OnImportsSatisfiedInvoked);
        }
        public void ExportOfT_ShouldGenerateSingleExportAttributeWithContractType()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).Export <IFoo>();

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

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

            var exportAttribute = attributes[0] as ExportAttribute;

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


            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());
        }
        public void AddMetadataWithFunc_ShouldGeneratePartMetadataAttribute()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).Export <IFoo>().AddPartMetadata("name", t => t.Name);

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

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

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

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

            var mdAttribute = attributes.First((t) => t.GetType() == typeof(PartMetadataAttribute)) as PartMetadataAttribute;

            Assert.Equal("name", mdAttribute.Name);
            Assert.Equal(typeof(FooImpl).Name, mdAttribute.Value);

            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());
        }
Example #4
0
        public void AsContractName_AndContractType_ComputeContractNameFromType()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, c => c.AsContractName(t => "Contract:" + t.FullName));

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal("Contract:" + typeof(IFoo).FullName, importAtt.ContractName);
        }
Example #5
0
        public void AsContractName_AndContractType_SetsContractNameAndType()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AsContractName("hey"));

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.Equal("hey", importAtt.ContractName);
        }
Example #6
0
        public void ImportPropertyTargetingDerivedClass_ShouldGenerateImportForPropertySelected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ImportProperty(p => p.P4); // P4 is string

            var importAttribute = GetAttributeFromMember(builder, typeof(DerClass), "P4") as ImportAttribute;

            Assert.NotNull(importAttribute);
            Assert.Null(importAttribute.ContractName);
        }
Example #7
0
        public void ImportPropertyTargetingBaseClass_ShouldGenerateImportManyForPropertySelected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ImportProperty(p => p.P3); // P3 is IEnumerable<int>

            var importManyAttribute = GetAttributeFromMember(builder, typeof(DerClass), "P3") as ImportManyAttribute;

            Assert.NotNull(importManyAttribute);
            Assert.Null(importManyAttribute.ContractName);
        }
Example #8
0
        public void AddImportConstraintFuncVal_AddsImportConstraintMetadataAttribute()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AddMetadataConstraint("name", t => t.Name));

            ImportMetadataConstraintAttribute importMetadataConstraint = GetImportMetadataConstraintAttribute(builder);

            Assert.Equal("name", importMetadataConstraint.Name);
            Assert.Equal(typeof(IFoo).Name, importMetadataConstraint.Value);
        }
Example #9
0
        public void AsMany_And_ContractName_ChangesGeneratedAttributeToImportMany()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AsContractName("hey").AsMany());

            ImportManyAttribute importAtt = GetImportManyAttribute(builder);

            Assert.NotNull(importAtt);
            Assert.Equal("hey", importAtt.ContractName);
        }
Example #10
0
        public void WhenExportingInterfaces_NoPredicate_OnlyContractInterfacesAreExported()
        {
            var builder = new ConventionBuilder();

            builder.ForType <ClassWithLifetimeConcerns>().ExportInterfaces();

            var attributes        = GetExportAttributes(builder, typeof(ClassWithLifetimeConcerns));
            var exportedContracts = attributes.Select(e => e.ContractType).ToArray();

            AssertX.Equivalent(s_contractInterfaces, exportedContracts);
        }
Example #11
0
        public void ImportPropertyTargetingBaseClass_ShouldGenerateImportManyForP3Selected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ImportProperties((p) => p.Name == "P3");                // P2 is Enumerable

            var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P3") as ImportManyAttribute;

            Assert.NotNull(pAttr);                   // Ensure P3 has ImportManyAttribute (default configured)
            Assert.Null(pAttr.ContractName);
        }
Example #12
0
        public void ExportInterfaceWithTypeOf1()
        {
            var builder = new ConventionBuilder();

            builder.ForType <CFoo>().Export <IFoo>();

            var exports = builder.GetDeclaredAttributes(typeof(CFoo), typeof(CFoo).GetTypeInfo()).Where <Attribute>(e => e is ExportAttribute).Cast <ExportAttribute>();

            Assert.AreEqual(1, exports.Count());
            Assert.AreEqual(exports.First().ContractType, typeof(IFoo));
        }
Example #13
0
        public void ExportInterfaceWithTypeOf2()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImpl)).Export((c) => c.AsContractType(typeof(IFoo)));

            var exports = builder.GetDeclaredAttributes(typeof(FooImpl), typeof(FooImpl).GetTypeInfo()).Where <Attribute>(e => e is ExportAttribute).Cast <ExportAttribute>();

            Assert.Equal(1, exports.Count());
            Assert.Equal(exports.First().ContractType, typeof(IFoo));
        }
Example #14
0
        public void ImportPropertyTargetingDerivedClass_ShouldGenerateImportAttributeForP4Selected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ImportProperties((p) => p.Name == "P4");                // P1 is string

            var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P4") as ImportAttribute;

            Assert.NotNull(pAttr);                   // Ensure P1 has ImportAttribute (default configured)
            Assert.Null(pAttr.ContractName);
        }
Example #15
0
        public void AllowDefault_SetsAllowDefaultProperty()
        {
            var builder = new ConventionBuilder();

            builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AllowDefault());

            ImportAttribute importAtt = GetImportAttribute(builder);

            Assert.True(importAtt.AllowDefault);
            Assert.Null(importAtt.ContractName);
        }
Example #16
0
        public void ExportPropertyTargetingDerivedClass_ShouldGenerateExportForPropertySelected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ExportProperties((p) => p.Name == "P4");                // P1 is string

            var pAttr = GetAttributeFromMember(builder, typeof(DerClass), "P4") as ExportAttribute;

            Assert.IsNotNull(pAttr);
            Assert.IsNull(pAttr.ContractName);
            Assert.IsNull(pAttr.ContractType);
        }
Example #17
0
        public void WhenExportingInterfaces_PredicateSpecified_OnlyContractInterfacesAreSeenByThePredicate()
        {
            var seenInterfaces = new List <Type>();

            var builder = new ConventionBuilder();

            builder.ForType <ClassWithLifetimeConcerns>().ExportInterfaces(i => { seenInterfaces.Add(i); return(true); });

            var attributes = GetExportAttributes(builder, typeof(ClassWithLifetimeConcerns));

            AssertX.Equivalent(s_contractInterfaces, seenInterfaces);
        }
Example #18
0
        public void ExportPropertyTargetingBaseClass_ShouldGenerateExportForPropertySelected()
        {
            var builder = new ConventionBuilder();

            builder.ForType <DerClass>().ExportProperties((p) => p.Name == "P2");                // P2 is string

            var exportAttribute = GetAttributeFromMember(builder, typeof(DerClass), "P2") as ExportAttribute;

            Assert.NotNull(exportAttribute);
            Assert.Null(exportAttribute.ContractName);
            Assert.Null(exportAttribute.ContractType);
        }
Example #19
0
        public void ConventionSelectsConstructor_SelectsTheOneWithMostParameters()
        {
            var builder = new ConventionBuilder();

            builder.ForType(typeof(FooImplWithConstructors));

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

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

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

            Assert.Equal(0, attributes.Count());
        }
Example #20
0
        public void NotifyImportsSatisfiedAttributeAppliedToBaseClass_ShouldSucceed()
        {
            var builder = new ConventionBuilder();

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

            Assert.NotNull(test.P1);
            Assert.NotNull(test.P2);
            Assert.Equal(1, test.OnImportsSatisfiedInvoked);
        }
Example #21
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());
        }
Example #22
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());
        }
Example #23
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());
        }
Example #24
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);
        }
Example #25
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());
        }
Example #26
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());
        }
Example #27
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());
        }
Example #28
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());
        }