public void CannotHaveSecurityAttributeOnMixinImplementation()
        {
            var config = new CiladorConfigType();
            ;
            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(SecurityDeclarationMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Cloning root source type may not be annotated with security attributes: [{0}]",
                    typeof(SecurityDeclarationMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #2
0
        public void CannotUseOpenGenericMixinWithoutProvidingTypeArguments()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(GenericMixinImplementation <>).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Cloning root source type cannot be an open generic type: [{0}]",
                                         typeof(GenericMixinImplementation <>).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void CanMixinEvents()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof (IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin = typeof (EventsMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 28, 12, 0, 14, 0);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(EventsMixin));
        }
Example #4
0
        public void CanContainDynamicMembersAndLocalVariables()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(DynamicMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 7, 3, 1, 1, 1);   // MS compiler is adding a nested type...not sure it should actually be tested for since it's a compiler implementation detail, but leaving it for now
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(DynamicMixin));
        }
Example #5
0
        public void CanMixArraysOfMixedType()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(ArraysOfMixedTypeMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 7, 0, 0, 1);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(ArraysOfMixedTypeMixin));
        }
        public void CanAddInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(EmptyMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 0, 0, 0, 0);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");
            var instance = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instance is IEmptyInterface);
        }
Example #7
0
        public void ConstructorsCannotHaveParameters()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(ConstructorWithParametersMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Cloning root source type cannot have constructors with parameters: [{0}]",
                                         typeof(ConstructorWithParametersMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #8
0
        public void MixinImplementationMustInheritDirectlyFromObject()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(InheritingMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Cloning root source type cannot have a base type other than System.Object: [{0}]",
                                         typeof(InheritingMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #9
0
        public void CanMixinFields()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin     = typeof(FieldsMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };


            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 30, 0, 0, 0);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(FieldsMixin));

            MemberMixingFixture.ValidateNonTargetTypeAndAttributesAreUntouched(assembly);
        }
Example #10
0
        public void CanContainDynamicMembersAndLocalVariables()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(DynamicMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 7, 3, 1, 1, 1);   // MS compiler is adding a nested type...not sure it should actually be tested for since it's a compiler implementation detail, but leaving it for now
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(DynamicMixin));
        }
Example #11
0
        public void CannotImplementOtherInterfaces()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(ExtraInterfaceMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Configured mixin implementation type [{0}] may implement only the mixin definition interface [{1}]",
                                         typeof(ExtraInterfaceMixin).FullName,
                                         typeof(IEmptyInterface).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #12
0
        public void CannotMakeUnmanagedCall()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(UnmanagedCallMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Cloning source may not contain extern methods: [{0}]",
                                         typeof(UnmanagedCallMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #13
0
        public void MixinInterfaceMustBeAnInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(NotAValidMixinInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(IncorrectMixinSpecifyingClassInsteadOfInterface).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Configured mixin definition interface type is not an interface: [{0}]",
                                         typeof(NotAValidMixinInterface).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #14
0
        public void CannotHaveSecurityAttributeOnMethod()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(SecurityDeclarationOnMethodMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Cloning source type may not contain methods annotated with security attributes: [{0}]",
                                         typeof(SecurityDeclarationOnMethodMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #15
0
        public void TargetCannotAlreadyImplmentMixinDefinitionInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IIsAlreadyImplementedTester).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(InterfaceIsAlreadyImplementedMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                                         "Target type [{0}] already implements interface to be mixed [{1}]",
                                         typeof(InterfaceIsAlreadyImplementedTarget).FullName,
                                         typeof(IIsAlreadyImplementedTester).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #16
0
        public void StaticConstructorIsClonedForTargetWithStaticConstructor()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IForTargetWithStaticConstructors).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(StaticInitializationMixinForTargetWithStaticConstructor).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(StaticConstructorsTarget).FullName);

            Assert.That(typeof(IForTargetWithStaticConstructors).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(2, 1, 12, 0, 0, 0);

            Assert.That(targetType.BaseType.GetField("SomeNumberSetTo56561InStaticConstructor", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(56561));
            Assert.That(targetType.BaseType.GetField("SomeNumberInitializedTo8188", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(8188));

            Assert.That(targetType.GetField("OriginalUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(9834897));
            Assert.That(targetType.GetField("OriginalUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QWEhinIOnsonf nui uif"));
            var uriBuilder = targetType.GetField("OriginalUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;

            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("d.e.f"));

            Assert.That(targetType.GetField("OriginalInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(35881));
            Assert.That(targetType.GetField("OriginalInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("Elak Ion fiugrnn"));
            uriBuilder = targetType.GetField("OriginalInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("a.b.c"));

            Assert.That(targetType.GetField("MixedUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(3788448));
            Assert.That(targetType.GetField("MixedUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("APONION ioniosdnfionaiuhg "));
            uriBuilder = targetType.GetField("MixedUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("j.k.l"));

            Assert.That(targetType.GetField("MixedInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(438974789));
            Assert.That(targetType.GetField("MixedInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QKLdsionioj ioenu buif"));
            uriBuilder = targetType.GetField("MixedInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("g.h.i"));
        }
        public void StaticConstructorIsClonedForTargetWithStaticConstructor()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IForTargetWithStaticConstructors).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(StaticInitializationMixinForTargetWithStaticConstructor).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(StaticConstructorsTarget).FullName);

            Assert.That(typeof(IForTargetWithStaticConstructors).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(2, 1, 12, 0, 0, 0);

            Assert.That(targetType.BaseType.GetField("SomeNumberSetTo56561InStaticConstructor", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(56561));
            Assert.That(targetType.BaseType.GetField("SomeNumberInitializedTo8188", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(8188));

            Assert.That(targetType.GetField("OriginalUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(9834897));
            Assert.That(targetType.GetField("OriginalUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QWEhinIOnsonf nui uif"));
            var uriBuilder = targetType.GetField("OriginalUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("d.e.f"));

            Assert.That(targetType.GetField("OriginalInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(35881));
            Assert.That(targetType.GetField("OriginalInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("Elak Ion fiugrnn"));
            uriBuilder = targetType.GetField("OriginalInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("a.b.c"));

            Assert.That(targetType.GetField("MixedUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(3788448));
            Assert.That(targetType.GetField("MixedUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("APONION ioniosdnfionaiuhg "));
            uriBuilder = targetType.GetField("MixedUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("j.k.l"));

            Assert.That(targetType.GetField("MixedInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(438974789));
            Assert.That(targetType.GetField("MixedInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QKLdsionioj ioenu buif"));
            uriBuilder = targetType.GetField("MixedInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("g.h.i"));
        }
Example #18
0
        public void TryCatchFinallyFlowIsCorrect()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin     = typeof(TryCatchFinallyMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };


            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 1, 9, 0, 0, 1);

            var instance = (IEmptyInterface)targetType.GetConstructor(new Type[0]).Invoke(new object[0]);

            Assert.That(targetType.GetField("FieldSetBeforeTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetBeforeThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetAfterThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInApplicationExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInMyExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInFinallyBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetAfterFinallyBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("TypeOfCaughtException").GetValue(instance), Is.Null);

            targetType.GetMethod("CatchExceptionWithFinallyBlock").Invoke(instance, new object[0]);

            Assert.That(targetType.GetField("FieldSetBeforeTryBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetBeforeThrowInTryBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetAfterThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInApplicationExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInMyExceptionCatchBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetInExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInFinallyBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetAfterFinallyBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("TypeOfCaughtException").GetValue(instance), Is.EqualTo(targetType.GetNestedType("MyException")));
        }
Example #19
0
        public static Assembly WeaveAndLoadTestTarget(
            string targetAssemblyFilename,
            CiladorConfigType config,
            params Tuple <string, string>[] fodyWeaverTaskProperties)
        {
            Contract.Requires(config != null);
            Contract.Ensures(Contract.Result <Assembly>() != null);

            var mixedAssembly = AppDomain.CurrentDomain.Load(ModuleWeaverHelper.GetRawWeavedAssembly(
                                                                 targetAssemblyFilename,
                                                                 ModuleWeaverHelper.BuildXElementConfig(config, fodyWeaverTaskProperties)));

            return(mixedAssembly);
        }
        public void TryCatchFinallyFlowIsCorrect()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof (IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin = typeof (TryCatchFinallyMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 1, 9, 0, 0, 1);

            var instance = (IEmptyInterface)targetType.GetConstructor(new Type[0]).Invoke(new object[0]);

            Assert.That(targetType.GetField("FieldSetBeforeTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetBeforeThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetAfterThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInApplicationExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInMyExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInFinallyBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetAfterFinallyBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("TypeOfCaughtException").GetValue(instance), Is.Null);

            targetType.GetMethod("CatchExceptionWithFinallyBlock").Invoke(instance, new object[0]);

            Assert.That(targetType.GetField("FieldSetBeforeTryBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetBeforeThrowInTryBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetAfterThrowInTryBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInApplicationExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInMyExceptionCatchBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetInExceptionCatchBlock").GetValue(instance), Is.False);
            Assert.That(targetType.GetField("FieldSetInFinallyBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("FieldSetAfterFinallyBlock").GetValue(instance), Is.True);
            Assert.That(targetType.GetField("TypeOfCaughtException").GetValue(instance), Is.EqualTo(targetType.GetNestedType("MyException")));
        }
Example #21
0
        public void CanMixinNestedTypes()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin     = typeof(NestedTypesMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };


            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 1, 0, 0, 0, 64);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(NestedTypesMixin));

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);

            Assert.That(instanceObject, Is.Not.Null);

            var getTypesMethod = targetType.GetMethod("GetTypes", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(getTypesMethod, Is.Not.Null);

            var typesObject = getTypesMethod.Invoke(instanceObject, new object[0]);

            Assert.That(getTypesMethod, Is.Not.Null);

            var types = (Type[])typesObject;

            foreach (var type in types)
            {
                Assert.That(type.FullName.StartsWith(typeof(EmptyInterfaceTarget).FullName));
            }
        }
Example #22
0
        public static XElement BuildXElementConfig(CiladorConfigType config, params Tuple<string, string>[] fodyWeaverTaskProperties)
        {
            Contract.Requires(config != null);
            Contract.Ensures(Contract.Result<XElement>() != null);

            var xmlConfig = new XElement("Cilador", config.ToXElement());
            if(fodyWeaverTaskProperties != null && fodyWeaverTaskProperties.Length > 0)
            {
                foreach(var property in fodyWeaverTaskProperties)
                {
                    xmlConfig.Add(new XAttribute(property.Item1, property.Item2));
                }
            }
            return xmlConfig;
        }
Example #23
0
        public static XElement BuildXElementConfig(CiladorConfigType config, params Tuple <string, string>[] fodyWeaverTaskProperties)
        {
            Contract.Requires(config != null);
            Contract.Ensures(Contract.Result <XElement>() != null);

            var xmlConfig = new XElement("Cilador", config.ToXElement());

            if (fodyWeaverTaskProperties != null && fodyWeaverTaskProperties.Length > 0)
            {
                foreach (var property in fodyWeaverTaskProperties)
                {
                    xmlConfig.Add(new XAttribute(property.Item1, property.Item2));
                }
            }
            return(xmlConfig);
        }
        public void CanAddInterfaceWithContent()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(EmptyInterfaceWithContentMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));

            targetType.ValidateMemberCountsAre(1, 3, 1, 1, 0, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var property = targetType.GetProperty("SomeValue");

            Assert.That(property != null, "Couldn't find new property SomeValue");

            var method = targetType.GetMethod("SomeMethod");

            Assert.That(method != null, "Couldn't find new method SomeMethod");

            var instance = Activator.CreateInstance(targetType, new object[0]);

            method.Invoke(instance, new object[0]);   // make sure no error is raised
            Assert.That(instance != null, "Failed to create instance with existing default constructor");
            Assert.That(0.Equals(property.GetValue(instance)));
            property.SetValue(instance, 45);
            Assert.That(45.Equals(property.GetValue(instance)));
        }
Example #25
0
        public void NestedTypeConstructorsAreCloned()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new []
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(NestedTypeConstructorsMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 0, 0, 0, 1);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");
            var nestedType = targetType.GetNestedType("InnerClass", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(nestedType != null);

            var staticProperty = nestedType.GetProperty("StaticSomeValue", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(staticProperty != null);
            Assert.That(488.Equals(staticProperty.GetValue(null)));

            var property = nestedType.GetProperty("SomeValue", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(property != null);
            var instance = Activator.CreateInstance(nestedType, new object[0]);

            Assert.That(42.Equals(property.GetValue(instance)));
            instance = Activator.CreateInstance(nestedType, new object[] { 5 });
            Assert.That(5.Equals(property.GetValue(instance)));
        }
        public void CanAddInterfaceWithContent()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(EmptyInterfaceWithContentMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));

            targetType.ValidateMemberCountsAre(1, 3, 1, 1, 0, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var property = targetType.GetProperty("SomeValue");
            Assert.That(property != null, "Couldn't find new property SomeValue");

            var method = targetType.GetMethod("SomeMethod");
            Assert.That(method != null, "Couldn't find new method SomeMethod");

            var instance = Activator.CreateInstance(targetType, new object[0]);
            method.Invoke(instance, new object[0]);   // make sure no error is raised
            Assert.That(instance != null, "Failed to create instance with existing default constructor");
            Assert.That(0.Equals(property.GetValue(instance)));
            property.SetValue(instance, 45);
            Assert.That(45.Equals(property.GetValue(instance)));
        }
        public void CanHandleThisReference()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(ThisPameterMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType("Cilador.Fody.TestMixinTargets.EmptyInterfaceTarget");

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            Assert.That(targetType.GetConstructors(TestContent.BindingFlagsForWeavedMembers).Length == 1, "Expected 1 constructor");
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instance = Activator.CreateInstance(targetType);

            Assert.That(instance, Is.Not.Null);

            var method = targetType.GetMethod("GetThis", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(method, Is.Not.Null);
            Assert.That(method.ReturnType.FullName, Is.EqualTo(targetType.FullName));
            Assert.That(instance, Is.SameAs(method.Invoke(instance, new object[0])));

            method = targetType.GetMethod("GetThisAsInterface", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ReturnType.FullName, Is.EqualTo(typeof(IEmptyInterface).FullName));
            Assert.That(instance, Is.SameAs(method.Invoke(instance, new object[0])));
        }
        public void UnconfiguredInterfaceIsSkipped()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[0]
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType("Cilador.Fody.TestMixinTargets.EmptyInterfaceTarget");

            Assert.That(!typeof(IEmptyInterface).IsAssignableFrom(targetType));
            Assert.That(targetType.GetConstructors(TestContent.BindingFlagsForWeavedMembers).Length == 1, "Expected 1 constructor");
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");
            var instance = Activator.CreateInstance(targetType, new object[0]);

            Assert.That(!(instance is IEmptyInterface));
        }
Example #29
0
        public void NestedTypeConstructorsAreCloned()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new []
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(NestedTypeConstructorsMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 0, 0, 0, 1);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");
            var nestedType = targetType.GetNestedType("InnerClass", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(nestedType != null);

            var staticProperty = nestedType.GetProperty("StaticSomeValue", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(staticProperty != null);
            Assert.That(488.Equals(staticProperty.GetValue(null)));

            var property = nestedType.GetProperty("SomeValue", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(property != null);
            var instance = Activator.CreateInstance(nestedType, new object[0]);
            Assert.That(42.Equals(property.GetValue(instance)));
            instance = Activator.CreateInstance(nestedType, new object[] { 5 });
            Assert.That(5.Equals(property.GetValue(instance)));
        }
Example #30
0
        public void StaticConstructorIsClonedForTargetWithoutStaticConstructor()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(StaticInitializationMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(2, 0, 6, 0, 0, 0);

            Assert.That(targetType.GetField("MixedUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(3788448));
            Assert.That(targetType.GetField("MixedUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("APONION ioniosdnfionaiuhg "));
            var uriBuilder = targetType.GetField("MixedUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;

            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("j.k.l"));

            Assert.That(targetType.GetField("MixedInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(438974789));
            Assert.That(targetType.GetField("MixedInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QKLdsionioj ioenu buif"));
            uriBuilder = targetType.GetField("MixedInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("g.h.i"));
        }
Example #31
0
        public void CannotUseInterfaceTypeMixin()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(InterfaceTypeMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.AssignableTo <Exception>(),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void StaticConstructorIsClonedForTargetWithoutStaticConstructor()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(StaticInitializationMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(2, 0, 6, 0, 0, 0);

            Assert.That(targetType.GetField("MixedUninitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(3788448));
            Assert.That(targetType.GetField("MixedUninitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("APONION ioniosdnfionaiuhg "));
            var uriBuilder = targetType.GetField("MixedUninitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("j.k.l"));

            Assert.That(targetType.GetField("MixedInitializedInt", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo(438974789));
            Assert.That(targetType.GetField("MixedInitializedString", TestContent.BindingFlagsForWeavedMembers).GetValue(null), Is.EqualTo("QKLdsionioj ioenu buif"));
            uriBuilder = targetType.GetField("MixedInitializedObject", TestContent.BindingFlagsForWeavedMembers).GetValue(null) as UriBuilder;
            Assert.That(uriBuilder, Is.Not.Null);
            Assert.That(uriBuilder.Host, Is.EqualTo("g.h.i"));
        }
        public void DoesNotAddReferenceFromMixinTargetToMixinImplementationAssembly()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(EmptyInterfaceWithContentMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly             = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var referencedAssemblies = assembly.GetReferencedAssemblies();

            Assert.That(!referencedAssemblies.Any(reference => typeof(EmptyInterfaceWithContentMixin).Assembly.Equals(reference)));
        }
        public void CannotUseValueTypeMixin()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(ValueTypeMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.AssignableTo<Exception>(),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void CannotUseOpenGenericMixinWithoutProvidingTypeArguments()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(GenericMixinImplementation<>).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Cloning root source type cannot be an open generic type: [{0}]",
                    typeof(GenericMixinImplementation<>).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void CannotMakeUnmanagedCall()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(UnmanagedCallMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Cloning source may not contain extern methods: [{0}]",
                    typeof(UnmanagedCallMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void CanImplementInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IInterfaceWithOnlyPrimitiveTypes).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(PropertiesAndMethodsWithPrimitiveTypesMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(InterfaceWithOnlyPrimitiveTypesTarget).FullName);

            Assert.That(typeof(IInterfaceWithOnlyPrimitiveTypes).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 42, 14, 14, 0, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instanceObject is IInterfaceWithOnlyPrimitiveTypes);
            var instance = (IInterfaceWithOnlyPrimitiveTypes)instanceObject;

            instance.BooleanProperty = true;
            Assert.That(instance.BooleanProperty);
            instance.BooleanProperty = false;
            Assert.That(!instance.BooleanProperty);

            instance.ByteProperty = Byte.MinValue;
            Assert.That(instance.ByteProperty == Byte.MinValue);
            instance.ByteProperty = 0;
            Assert.That(instance.ByteProperty == 0);
            instance.ByteProperty = Byte.MaxValue;
            Assert.That(instance.ByteProperty == Byte.MaxValue);

            instance.SByteProperty = SByte.MinValue;
            Assert.That(instance.SByteProperty == SByte.MinValue);
            instance.SByteProperty = 0;
            Assert.That(instance.SByteProperty == 0);
            instance.SByteProperty = SByte.MaxValue;
            Assert.That(instance.SByteProperty == SByte.MaxValue);

            instance.Int16Property = Int16.MinValue;
            Assert.That(instance.Int16Property == Int16.MinValue);
            instance.Int16Property = 0;
            Assert.That(instance.Int16Property == 0);
            instance.Int16Property = Int16.MaxValue;
            Assert.That(instance.Int16Property == Int16.MaxValue);

            instance.UInt16Property = UInt16.MinValue;
            Assert.That(instance.UInt16Property == UInt16.MinValue);
            instance.UInt16Property = 0;
            Assert.That(instance.UInt16Property == 0);
            instance.UInt16Property = UInt16.MaxValue;
            Assert.That(instance.UInt16Property == UInt16.MaxValue);

            instance.Int32Property = Int32.MinValue;
            Assert.That(instance.Int32Property == Int32.MinValue);
            instance.Int32Property = 0;
            Assert.That(instance.Int32Property == 0);
            instance.Int32Property = Int32.MaxValue;
            Assert.That(instance.Int32Property == Int32.MaxValue);

            instance.UInt32Property = UInt32.MinValue;
            Assert.That(instance.UInt32Property == UInt32.MinValue);
            instance.UInt32Property = 0;
            Assert.That(instance.UInt32Property == 0);
            instance.UInt32Property = UInt32.MaxValue;
            Assert.That(instance.UInt32Property == UInt32.MaxValue);

            instance.Int64Property = Int64.MinValue;
            Assert.That(instance.Int64Property == Int64.MinValue);
            instance.Int64Property = 0;
            Assert.That(instance.Int64Property == 0);
            instance.Int64Property = Int64.MaxValue;
            Assert.That(instance.Int64Property == Int64.MaxValue);

            instance.UInt64Property = UInt64.MinValue;
            Assert.That(instance.UInt64Property == UInt64.MinValue);
            instance.UInt64Property = 0;
            Assert.That(instance.UInt64Property == 0);
            instance.UInt64Property = UInt64.MaxValue;
            Assert.That(instance.UInt64Property == UInt64.MaxValue);

            instance.IntPtrProperty = IntPtr.Add(IntPtr.Zero, 402);
            Assert.That(instance.IntPtrProperty == IntPtr.Add(IntPtr.Zero, 402));
            instance.IntPtrProperty = IntPtr.Zero;
            Assert.That(instance.IntPtrProperty == IntPtr.Zero);

            instance.UIntPtrProperty = UIntPtr.Add(UIntPtr.Zero, 4987);
            Assert.That(instance.UIntPtrProperty == UIntPtr.Add(UIntPtr.Zero, 4987));
            instance.UIntPtrProperty = UIntPtr.Zero;
            Assert.That(instance.UIntPtrProperty == UIntPtr.Zero);

            instance.CharProperty = Char.MinValue;
            Assert.That(instance.CharProperty == Char.MinValue);
            instance.CharProperty = (Char)0;
            Assert.That(instance.CharProperty == 0);
            instance.CharProperty = Char.MaxValue;
            Assert.That(instance.CharProperty == Char.MaxValue);

            instance.DoubleProperty = Double.MinValue;
            Assert.That(instance.DoubleProperty == Double.MinValue);
            instance.DoubleProperty = 0;
            Assert.That(instance.DoubleProperty == 0);
            instance.DoubleProperty = Double.MaxValue;
            Assert.That(instance.DoubleProperty == Double.MaxValue);

            instance.SingleProperty = Single.MinValue;
            Assert.That(instance.SingleProperty == Single.MinValue);
            instance.SingleProperty = 0;
            Assert.That(instance.SingleProperty == 0);
            instance.SingleProperty = Single.MaxValue;
            Assert.That(instance.SingleProperty == Single.MaxValue);
        }
Example #38
0
        public void CanMixinNestedTypes()
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof (IEmptyInterface).GetShortAssemblyQualifiedName(),
                                Mixin = typeof (NestedTypesMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 1, 0, 0, 0, 64);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            targetType.ValidateMemberSources(typeof(NestedTypesMixin));

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instanceObject, Is.Not.Null);

            var getTypesMethod = targetType.GetMethod("GetTypes", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(getTypesMethod, Is.Not.Null);

            var typesObject = getTypesMethod.Invoke(instanceObject, new object[0]);
            Assert.That(getTypesMethod, Is.Not.Null);

            var types = (Type[])typesObject;
            foreach(var type in types)
            {
                Assert.That(type.FullName.StartsWith(typeof(EmptyInterfaceTarget).FullName));
            }
        }
        public void TargetCannotAlreadyImplmentMixinDefinitionInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IIsAlreadyImplementedTester).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(InterfaceIsAlreadyImplementedMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Target type [{0}] already implements interface to be mixed [{1}]",
                    typeof(InterfaceIsAlreadyImplementedTarget).FullName,
                    typeof(IIsAlreadyImplementedTester).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void MixinInterfaceMustBeAnInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(NotAValidMixinInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(IncorrectMixinSpecifyingClassInsteadOfInterface).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Configured mixin definition interface type is not an interface: [{0}]",
                    typeof(NotAValidMixinInterface).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void UnconfiguredInterfaceIsSkipped()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[0]
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType("Cilador.Fody.TestMixinTargets.EmptyInterfaceTarget");
            Assert.That(!typeof(IEmptyInterface).IsAssignableFrom(targetType));
            Assert.That(targetType.GetConstructors(TestContent.BindingFlagsForWeavedMembers).Length == 1, "Expected 1 constructor");
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");
            var instance = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(!(instance is IEmptyInterface));
        }
        public void DoesNotAddReferenceFromMixinTargetToMixinImplementationAssembly()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(EmptyInterfaceWithContentMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var referencedAssemblies = assembly.GetReferencedAssemblies();
            Assert.That(!referencedAssemblies.Any(reference => typeof(EmptyInterfaceWithContentMixin).Assembly.Equals(reference)));
        }
        public void CanHandleThisReference()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(ThisPameterMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType("Cilador.Fody.TestMixinTargets.EmptyInterfaceTarget");
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            Assert.That(targetType.GetConstructors(TestContent.BindingFlagsForWeavedMembers).Length == 1, "Expected 1 constructor");
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instance = Activator.CreateInstance(targetType);
            Assert.That(instance, Is.Not.Null);

            var method = targetType.GetMethod("GetThis", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ReturnType.FullName, Is.EqualTo(targetType.FullName));
            Assert.That(instance, Is.SameAs(method.Invoke(instance, new object[0])));

            method = targetType.GetMethod("GetThisAsInterface", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ReturnType.FullName, Is.EqualTo(typeof(IEmptyInterface).FullName));
            Assert.That(instance, Is.SameAs(method.Invoke(instance, new object[0])));
        }
        public void CanImplementExplicitly()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IInterfaceForImplicitExplicitTesting).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(ImplicitExplicitTestingExplicitOnlyMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(InterfaceForImplicitExplicitTestingTarget).FullName);

            Assert.That(typeof(IInterfaceForImplicitExplicitTesting).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 12, 3, 3, 3, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instanceObject is IInterfaceForImplicitExplicitTesting);
            var instanceInterface = (IInterfaceForImplicitExplicitTesting)instanceObject;

            Assert.That(targetType.GetMethod("Method1", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetMethod("Method2", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetMethod("Method3", TestContent.BindingFlagsForWeavedMembers) == null);

            Assert.That("Explicit Method 1".Equals(instanceInterface.Method1()));
            Assert.That("Explicit Method 2".Equals(instanceInterface.Method2()));
            Assert.That("Explicit Method 3".Equals(instanceInterface.Method3()));

            Assert.That(targetType.GetProperty("Property1", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetProperty("Property2", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetProperty("Property3", TestContent.BindingFlagsForWeavedMembers) == null);

            Assert.That("Explicit Property 1".Equals(instanceInterface.Property1));
            Assert.That("Explicit Property 2".Equals(instanceInterface.Property2));
            Assert.That("Explicit Property 3".Equals(instanceInterface.Property3));

            EventHandler eventHandler = (sender, eventArgs) => { return; };

            Assert.That(targetType.GetEvent("Event1", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetEvent("Event2", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetEvent("Event3", TestContent.BindingFlagsForWeavedMembers) == null);

            Assert.That(targetType.GetField("Event1", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetField("Event2", TestContent.BindingFlagsForWeavedMembers) == null);
            Assert.That(targetType.GetField("Event3", TestContent.BindingFlagsForWeavedMembers) == null);

            var explicitEventHandler1 = targetType.GetField("explicitEventHandler1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(explicitEventHandler1 != null);
            var explicitEventHandler2 = targetType.GetField("explicitEventHandler2", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(explicitEventHandler2 != null);
            var explicitEventHandler3 = targetType.GetField("explicitEventHandler3", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(explicitEventHandler3 != null);

            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event1 += eventHandler;
            Assert.That(object.Equals(explicitEventHandler1.GetValue(instanceObject), eventHandler));
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);
            instanceInterface.Event1 -= eventHandler;
            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event2 += eventHandler;
            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(object.Equals(explicitEventHandler2.GetValue(instanceObject), eventHandler));
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);
            instanceInterface.Event2 -= eventHandler;
            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event3 += eventHandler;
            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(object.Equals(explicitEventHandler3.GetValue(instanceObject), eventHandler));
            instanceInterface.Event3 -= eventHandler;
            Assert.That(explicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(explicitEventHandler3.GetValue(instanceObject) == null);
        }
        public void CanImplementImplicitly()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IInterfaceForImplicitExplicitTesting).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(ImplicitExplicitTestingImplicitOnlyMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(InterfaceForImplicitExplicitTestingTarget).FullName);

            Assert.That(typeof(IInterfaceForImplicitExplicitTesting).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 12, 3, 3, 3, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);

            Assert.That(instanceObject is IInterfaceForImplicitExplicitTesting);
            var instanceInterface = (IInterfaceForImplicitExplicitTesting)instanceObject;

            Assert.That("Implicit Method 1".Equals(
                            targetType.GetMethod("Method1", TestContent.BindingFlagsForWeavedMembers).Invoke(instanceObject, new object[] { })));
            Assert.That("Implicit Method 2".Equals(
                            targetType.GetMethod("Method2", TestContent.BindingFlagsForWeavedMembers).Invoke(instanceObject, new object[] { })));
            Assert.That("Implicit Method 3".Equals(
                            targetType.GetMethod("Method3", TestContent.BindingFlagsForWeavedMembers).Invoke(instanceObject, new object[] { })));

            Assert.That("Implicit Method 1".Equals(instanceInterface.Method1()));
            Assert.That("Implicit Method 2".Equals(instanceInterface.Method2()));
            Assert.That("Implicit Method 3".Equals(instanceInterface.Method3()));

            Assert.That("Implicit Property 1".Equals(
                            targetType.GetProperty("Property1", TestContent.BindingFlagsForWeavedMembers).GetValue(instanceObject)));
            Assert.That("Implicit Property 2".Equals(
                            targetType.GetProperty("Property2", TestContent.BindingFlagsForWeavedMembers).GetValue(instanceObject)));
            Assert.That("Implicit Property 3".Equals(
                            targetType.GetProperty("Property3", TestContent.BindingFlagsForWeavedMembers).GetValue(instanceObject)));

            Assert.That("Implicit Property 1".Equals(instanceInterface.Property1));
            Assert.That("Implicit Property 2".Equals(instanceInterface.Property2));
            Assert.That("Implicit Property 3".Equals(instanceInterface.Property3));

            EventHandler eventHandler = (sender, eventArgs) => { return; };

            var typeEvent1 = targetType.GetEvent("Event1", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(typeEvent1 != null);
            var typeEvent2 = targetType.GetEvent("Event2", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(typeEvent2 != null);
            var typeEvent3 = targetType.GetEvent("Event3", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(typeEvent2 != null);

            var implicitEventHandler1 = targetType.GetField("Event1", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(implicitEventHandler1 != null);
            var implicitEventHandler2 = targetType.GetField("Event2", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(implicitEventHandler2 != null);
            var implicitEventHandler3 = targetType.GetField("Event3", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(implicitEventHandler3 != null);

            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            typeEvent1.AddEventHandler(instanceObject, eventHandler);
            Assert.That(object.Equals(implicitEventHandler1.GetValue(instanceObject), eventHandler));
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);
            typeEvent1.RemoveEventHandler(instanceObject, eventHandler);
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            typeEvent2.AddEventHandler(instanceObject, eventHandler);
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(object.Equals(implicitEventHandler2.GetValue(instanceObject), eventHandler));
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);
            typeEvent2.RemoveEventHandler(instanceObject, eventHandler);
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            typeEvent3.AddEventHandler(instanceObject, eventHandler);
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(object.Equals(implicitEventHandler3.GetValue(instanceObject), eventHandler));
            typeEvent3.RemoveEventHandler(instanceObject, eventHandler);
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event1 += eventHandler;
            Assert.That(object.Equals(implicitEventHandler1.GetValue(instanceObject), eventHandler));
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);
            instanceInterface.Event1 -= eventHandler;
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event2 += eventHandler;
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(object.Equals(implicitEventHandler2.GetValue(instanceObject), eventHandler));
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);
            instanceInterface.Event2 -= eventHandler;
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);

            instanceInterface.Event3 += eventHandler;
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(object.Equals(implicitEventHandler3.GetValue(instanceObject), eventHandler));
            instanceInterface.Event3 -= eventHandler;
            Assert.That(implicitEventHandler1.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler2.GetValue(instanceObject) == null);
            Assert.That(implicitEventHandler3.GetValue(instanceObject) == null);
        }
Example #46
0
        public void CanMixGenericNestedType()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(GenericNestedTypeMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 0, 0, 0, 6);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            // basic nested generic type
            var type = targetType.GetNestedType("GenericType`1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T")));

            var method = type.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                GenericParameterTypeValidator.Named("T"),
                GenericParameterTypeValidator.Named("T"));

            var genericInstanceType = type.MakeGenericType(typeof(int));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            var instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceMethod = genericInstanceType.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType<int>(),
                NonGenericTypeValidator.ForType<int>());
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { 556 }), Is.EqualTo(556));

            genericInstanceType = type.MakeGenericType(typeof(object));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceMethod = genericInstanceType.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType<object>(),
                NonGenericTypeValidator.ForType<object>());
            var someObject = new object();
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { someObject }), Is.SameAs(someObject));

            // nested generic type with constraints
            type = targetType.GetNestedType("GenericTypeWithConstraints`6", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("TClass"),
                    GenericParameterTypeValidator.Named("TStruct"),
                    GenericParameterTypeValidator.Named("TNew"),
                    GenericParameterTypeValidator.Named("TClassNew"),
                    GenericParameterTypeValidator.Named("TDisposable"),
                    GenericParameterTypeValidator.Named("TTClass")));

            genericInstanceType = type.MakeGenericType(
                typeof(Stream),
                typeof(DictionaryEntry),
                typeof(DictionaryEntry),
                typeof(object),
                typeof(StringWriter),
                typeof(FileStream));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceMethod = genericInstanceType.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple<,,,,,>),
                    NonGenericTypeValidator.ForType<Stream>(),
                    NonGenericTypeValidator.ForType<DictionaryEntry>(),
                    NonGenericTypeValidator.ForType<DictionaryEntry>(),
                    NonGenericTypeValidator.ForType<object>(),
                    NonGenericTypeValidator.ForType<StringWriter>(),
                    NonGenericTypeValidator.ForType<FileStream>()),
                NonGenericTypeValidator.ForType<Stream>(),
                NonGenericTypeValidator.ForType<DictionaryEntry>(),
                NonGenericTypeValidator.ForType<DictionaryEntry>(),
                NonGenericTypeValidator.ForType<object>(),
                NonGenericTypeValidator.ForType<StringWriter>(),
                NonGenericTypeValidator.ForType<FileStream>());

            // nested generic type with open generic method
            type = targetType.GetNestedType("GenericTypeWithGenericMethod`1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("TType")));

            method = type.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(typeof(Tuple<,>),
                    GenericParameterTypeValidator.Named("TType"),
                    GenericParameterTypeValidator.Named("TMethod")),
                GenericParameterTypeValidator.Named("TType"),
                GenericParameterTypeValidator.Named("TMethod"));

            genericInstanceType = type.MakeGenericType(typeof(int));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceGenericMethod = genericInstanceType.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceGenericMethod, Is.Not.Null);
            Assert.That(genericInstanceGenericMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceGenericMethod,
                new GenericTypeValidator(typeof(Tuple<,>),
                    NonGenericTypeValidator.ForType<int>(),
                    GenericParameterTypeValidator.Named("TMethod")),
                NonGenericTypeValidator.ForType<int>(),
                GenericParameterTypeValidator.Named("TMethod"));

            genericInstanceMethod = genericInstanceGenericMethod.MakeGenericMethod(typeof(object));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple<,>),
                    NonGenericTypeValidator.ForType<int>(),
                    NonGenericTypeValidator.ForType<object>()),
                NonGenericTypeValidator.ForType<int>(),
                NonGenericTypeValidator.ForType<object>());
            someObject = new object();
            var things = (Tuple<int, object>)genericInstanceMethod.Invoke(instance, new object[] { 45866, someObject });
            Assert.That(things.Item1, Is.EqualTo(45866));
            Assert.That(things.Item2, Is.SameAs(someObject));

            // generic type with multiple parameters
            type = targetType.GetNestedType("GenericTypeWithMultipleParameters`2", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T1"),
                    GenericParameterTypeValidator.Named("T2")));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing1"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T1"));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing2"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T2"));

            genericInstanceType = type.MakeGenericType(typeof(int), typeof(string));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceProperty = genericInstanceType.GetProperty("Thing1", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<int>());
            genericInstanceProperty.SetValue(instance, 48008);
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(48008));

            genericInstanceProperty = genericInstanceType.GetProperty("Thing2", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<string>());
            genericInstanceProperty.SetValue(instance, "SAdfiohoqiweAsiohnewlroi asoidfh inra6f");
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("SAdfiohoqiweAsiohnewlroi asoidfh inra6f"));

            instance = Activator.CreateInstance(genericInstanceType, new object[] { 34543, "ALKJEWklnrpioewhfioehgiu" });

            genericInstanceProperty = genericInstanceType.GetProperty("Thing1", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<int>());
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(34543));

            genericInstanceProperty = genericInstanceType.GetProperty("Thing2", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<string>());
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("ALKJEWklnrpioewhfioehgiu"));

            // partially closed generic type
            type = targetType.GetNestedType("PartiallyClosedGenericType`1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T3")));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing1"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<int>());

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing2"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T3"));

            genericInstanceType = type.MakeGenericType(typeof(string));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceProperty = genericInstanceType.GetProperty(
                "Thing1",
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<int>());
            genericInstanceProperty.SetValue(instance, 5446);
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(5446));

            genericInstanceProperty = genericInstanceType.BaseType.GetProperty(
                "Thing2",
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType<string>());
            genericInstanceProperty.SetValue(instance, "AFioenAjeiona aesoing ijeng inuia eabruibeg uybvwaytvr  b");
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("AFioenAjeiona aesoing ijeng inuia eabruibeg uybvwaytvr  b"));

            // type with method with partially closed generic type
            type = targetType.GetNestedType("TypeWithPartiallyClosedGenericMethod", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(!type.IsGenericType);

            instance = Activator.CreateInstance(type);

            var returnTypeGenericDefinition = targetType.GetNestedType("GenericTypeWithMultipleParameters`2", TestContent.BindingFlagsForWeavedMembers);

            method = type.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(
                    returnTypeGenericDefinition,
                    NonGenericTypeValidator.ForType<int>(),
                    GenericParameterTypeValidator.Named("T4")),
                GenericParameterTypeValidator.Named("T4"));

            genericInstanceMethod = method.MakeGenericMethod(new Type[] { typeof(string) });
            var returnValue = genericInstanceMethod.Invoke(instance, new object[] { "Eoahio eioua bueagingnpiuw nufib fuiwebyu" });
            var genericInstanceReturnType = returnTypeGenericDefinition.MakeGenericType(new Type[] { typeof(int), typeof(string) });
            Assert.That(returnValue.GetType(), Is.EqualTo(genericInstanceReturnType));
            Assert.That(genericInstanceReturnType.GetProperty("Thing1").GetValue(returnValue), Is.EqualTo(297387));
            Assert.That(genericInstanceReturnType.GetProperty("Thing2").GetValue(returnValue), Is.EqualTo("Eoahio eioua bueagingnpiuw nufib fuiwebyu"));

            method = type.GetMethod("GetOtherThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(
                    returnTypeGenericDefinition,
                    GenericParameterTypeValidator.Named("T5"),
                    NonGenericTypeValidator.ForType<int>()),
                GenericParameterTypeValidator.Named("T5"));

            genericInstanceMethod = method.MakeGenericMethod(new Type[] { typeof(string) });
            returnValue = genericInstanceMethod.Invoke(instance, new object[] { "Eaiosn4oil ifhiu 3ybusabu 3byuvsdf yuvaytithomn gfd" });
            genericInstanceReturnType = returnTypeGenericDefinition.MakeGenericType(new Type[] { typeof(string), typeof(int) });
            Assert.That(returnValue.GetType(), Is.EqualTo(genericInstanceReturnType));
            Assert.That(genericInstanceReturnType.GetProperty("Thing1").GetValue(returnValue), Is.EqualTo("Eaiosn4oil ifhiu 3ybusabu 3byuvsdf yuvaytithomn gfd"));
            Assert.That(genericInstanceReturnType.GetProperty("Thing2").GetValue(returnValue), Is.EqualTo(789437));
        }
Example #47
0
        public void CanMixGenericMethod()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(GenericMethodMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);
            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 2, 0, 0, 0, 0);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            // simple generic method
            var method = targetType.GetMethod("GenericMethod", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                GenericParameterTypeValidator.Named("T"),
                GenericParameterTypeValidator.Named("T"));

            var genericParameters = method.GetGenericArguments();
            Assert.That(genericParameters, Is.Not.Null);
            Assert.That(genericParameters.Length, Is.EqualTo(1));

            var instance = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instance is IEmptyInterface);

            var genericInstanceMethod = method.MakeGenericMethod(typeof(int));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType<int>(),
                NonGenericTypeValidator.ForType<int>());
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { 94387 }), Is.EqualTo(94387));

            genericInstanceMethod = method.MakeGenericMethod(typeof(object));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType<object>(),
                NonGenericTypeValidator.ForType<object>());
            var someObject = new object();
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { someObject }), Is.SameAs(someObject));

            // generic method with constraints
            method = targetType.GetMethod("GenericMethodWithConstraints", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(typeof(Tuple<,,,,,>),
                    GenericParameterTypeValidator.Named("TClass"),
                    GenericParameterTypeValidator.Named("TStruct"),
                    GenericParameterTypeValidator.Named("TNew"),
                    GenericParameterTypeValidator.Named("TClassNew"),
                    GenericParameterTypeValidator.Named("TDisposable"),
                    GenericParameterTypeValidator.Named("TTClass")),
                GenericParameterTypeValidator.Named("TClass"),
                GenericParameterTypeValidator.Named("TStruct"),
                GenericParameterTypeValidator.Named("TNew"),
                GenericParameterTypeValidator.Named("TClassNew"),
                GenericParameterTypeValidator.Named("TDisposable"),
                GenericParameterTypeValidator.Named("TTClass"));

            genericParameters = method.GetGenericArguments();
            Assert.That(genericParameters, Is.Not.Null);
            Assert.That(genericParameters.Length, Is.EqualTo(6));

            instance = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instance is IEmptyInterface);

            genericInstanceMethod = method.MakeGenericMethod(
                typeof(Stream),
                typeof(DictionaryEntry),
                typeof(DictionaryEntry),
                typeof(object),
                typeof(StringWriter),
                typeof(FileStream));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple<,,,,,>),
                    NonGenericTypeValidator.ForType<Stream>(),
                    NonGenericTypeValidator.ForType<DictionaryEntry>(),
                    NonGenericTypeValidator.ForType<DictionaryEntry>(),
                    NonGenericTypeValidator.ForType<object>(),
                    NonGenericTypeValidator.ForType<StringWriter>(),
                    NonGenericTypeValidator.ForType<FileStream>()),
                NonGenericTypeValidator.ForType<Stream>(),
                NonGenericTypeValidator.ForType<DictionaryEntry>(),
                NonGenericTypeValidator.ForType<DictionaryEntry>(),
                NonGenericTypeValidator.ForType<object>(),
                NonGenericTypeValidator.ForType<StringWriter>(),
                NonGenericTypeValidator.ForType<FileStream>());
        }
        public void ConstructorsCannotHaveParameters()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(ConstructorWithParametersMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Cloning root source type cannot have constructors with parameters: [{0}]",
                    typeof(ConstructorWithParametersMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        public void MixinImplementationMustInheritDirectlyFromObject()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(InheritingMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Cloning root source type cannot have a base type other than System.Object: [{0}]",
                    typeof(InheritingMixin).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
        private void TestWith(
            Type[] constructorTypes,
            object[] constructorArgs,
            int intValue,
            string stringValue,
            string uriHostValue,
            string whichBaseConstructor)
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof (IForTargetWithMultipleConstructors).GetShortAssemblyQualifiedName(),
                                Mixin = typeof (InstanceInitializationMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };

            var assembly = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(MultipleConstructorsTarget).FullName);

            Assert.That(typeof(IForTargetWithMultipleConstructors).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(4, 1, 13, 0, 0, 2);

            var constructor = targetType.GetConstructor(constructorTypes);
            Assert.That(constructor, Is.Not.Null);

            var instanceObject = constructor.Invoke(constructorArgs);
            Assert.That(instanceObject, Is.Not.Null);
            Assert.That(instanceObject is IForTargetWithMultipleConstructors);
            var instance = (IForTargetWithMultipleConstructors)instanceObject;

            Assert.That(targetType.GetField("SomeNumberSetTo395493InConstructor").GetValue(instance), Is.EqualTo(395493));

            Assert.That(targetType.GetField("OriginalInitializedInt").GetValue(instance), Is.EqualTo(48685));
            Assert.That(targetType.GetField("OriginalInitializedString").GetValue(instance), Is.EqualTo("Tion3lao ehiuawh iuh buib ld"));
            var innerObject = (UriBuilder)targetType.GetField("OriginalInitializedObject").GetValue(instance);
            Assert.That(innerObject, Is.Not.Null);
            Assert.That(innerObject.Host, Is.EqualTo("a.b.c"));

            Assert.That(targetType.GetField("OriginalUninitializedInt").GetValue(instance), Is.EqualTo(intValue));
            Assert.That(targetType.GetField("OriginalUninitializedString").GetValue(instance), Is.EqualTo(stringValue));
            Assert.That(innerObject, Is.Not.Null);
            innerObject = (UriBuilder)targetType.GetField("OriginalUninitializedObject").GetValue(instance);
            Assert.That(innerObject, Is.Not.Null);
            Assert.That(innerObject.Host, Is.EqualTo(uriHostValue));

            Assert.That(targetType.GetField("WhichConstructor").GetValue(instance), Is.EqualTo(whichBaseConstructor));

            Assert.That(targetType.GetField("SomeNumber").GetValue(instance), Is.EqualTo(684865));
            Assert.That(targetType.GetField("SomeString").GetValue(instance), Is.EqualTo("Tawhlej oisahoeh 8ohf 4ifh8ohe fni dlgj"));
            Assert.That(targetType.GetField("SomeObject").GetValue(instance), Is.Not.Null);

            var innerTypeInstance = targetType.GetField("SomeInnerType").GetValue(instance);
            Assert.That(innerTypeInstance, Is.Not.Null);
            var innerType = innerTypeInstance.GetType();
            Assert.That(innerType.GetProperty("SomeInt").GetValue(innerTypeInstance), Is.EqualTo(4235));
            Assert.That(innerType.GetProperty("SomeString").GetValue(innerTypeInstance), Is.EqualTo("JLKOIN  oin aon oingori d"));
            Assert.That(innerType.GetProperty("SomeObject").GetValue(innerTypeInstance), Is.Not.Null);

            var someFunc = targetType.GetField("SomeFunc").GetValue(instance) as Delegate;
            Assert.That(someFunc, Is.Not.Null);
            var result = someFunc.DynamicInvoke(new[] { 1, "2", new object() }) as Tuple<int, string, object>;
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Item1, Is.EqualTo(1));
            Assert.That(result.Item2, Is.EqualTo("2"));
            Assert.That(result.Item3, Is.Not.Null);

            var someMethodDelegateInstance = targetType.GetField("SomeMethodDelegateInstance").GetValue(instance) as Delegate;
            Assert.That(someMethodDelegateInstance, Is.Not.Null);
            result = someMethodDelegateInstance.DynamicInvoke(new[] { 3, "4", new object() }) as Tuple<int, string, object>;
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Item1, Is.EqualTo(3));
            Assert.That(result.Item2, Is.EqualTo("4"));
            Assert.That(result.Item3, Is.Not.Null);
        }
        public void MustImplementInterfaceForMixin()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin = typeof(InterfacelessMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            Assert.Throws(
                Is.TypeOf((typeof(WeavingException)))
                .And.Message.EqualTo(string.Format(
                    "Configured mixin implementation type [{0}] must implement the interface specified mixin interface definition [{1}]",
                    typeof(InterfacelessMixin).FullName,
                    typeof(IEmptyInterface).FullName)),
                () => ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config));
        }
Example #52
0
        public void CanMixGenericMethod()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(GenericMethodMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 2, 0, 0, 0, 0);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            // simple generic method
            var method = targetType.GetMethod("GenericMethod", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                GenericParameterTypeValidator.Named("T"),
                GenericParameterTypeValidator.Named("T"));

            var genericParameters = method.GetGenericArguments();

            Assert.That(genericParameters, Is.Not.Null);
            Assert.That(genericParameters.Length, Is.EqualTo(1));

            var instance = Activator.CreateInstance(targetType, new object[0]);

            Assert.That(instance is IEmptyInterface);

            var genericInstanceMethod = method.MakeGenericMethod(typeof(int));

            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType <int>(),
                NonGenericTypeValidator.ForType <int>());
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { 94387 }), Is.EqualTo(94387));

            genericInstanceMethod = method.MakeGenericMethod(typeof(object));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType <object>(),
                NonGenericTypeValidator.ForType <object>());
            var someObject = new object();

            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { someObject }), Is.SameAs(someObject));

            // generic method with constraints
            method = targetType.GetMethod("GenericMethodWithConstraints", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(typeof(Tuple <, , , , ,>),
                                         GenericParameterTypeValidator.Named("TClass"),
                                         GenericParameterTypeValidator.Named("TStruct"),
                                         GenericParameterTypeValidator.Named("TNew"),
                                         GenericParameterTypeValidator.Named("TClassNew"),
                                         GenericParameterTypeValidator.Named("TDisposable"),
                                         GenericParameterTypeValidator.Named("TTClass")),
                GenericParameterTypeValidator.Named("TClass"),
                GenericParameterTypeValidator.Named("TStruct"),
                GenericParameterTypeValidator.Named("TNew"),
                GenericParameterTypeValidator.Named("TClassNew"),
                GenericParameterTypeValidator.Named("TDisposable"),
                GenericParameterTypeValidator.Named("TTClass"));

            genericParameters = method.GetGenericArguments();
            Assert.That(genericParameters, Is.Not.Null);
            Assert.That(genericParameters.Length, Is.EqualTo(6));

            instance = Activator.CreateInstance(targetType, new object[0]);
            Assert.That(instance is IEmptyInterface);

            genericInstanceMethod = method.MakeGenericMethod(
                typeof(Stream),
                typeof(DictionaryEntry),
                typeof(DictionaryEntry),
                typeof(object),
                typeof(StringWriter),
                typeof(FileStream));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple <, , , , ,>),
                                         NonGenericTypeValidator.ForType <Stream>(),
                                         NonGenericTypeValidator.ForType <DictionaryEntry>(),
                                         NonGenericTypeValidator.ForType <DictionaryEntry>(),
                                         NonGenericTypeValidator.ForType <object>(),
                                         NonGenericTypeValidator.ForType <StringWriter>(),
                                         NonGenericTypeValidator.ForType <FileStream>()),
                NonGenericTypeValidator.ForType <Stream>(),
                NonGenericTypeValidator.ForType <DictionaryEntry>(),
                NonGenericTypeValidator.ForType <DictionaryEntry>(),
                NonGenericTypeValidator.ForType <object>(),
                NonGenericTypeValidator.ForType <StringWriter>(),
                NonGenericTypeValidator.ForType <FileStream>());
        }
Example #53
0
        public void CanMixGenericNestedType()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IEmptyInterface).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(GenericNestedTypeMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(EmptyInterfaceTarget).FullName);

            Assert.That(typeof(IEmptyInterface).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 0, 0, 0, 0, 6);
            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            // basic nested generic type
            var type = targetType.GetNestedType("GenericType`1", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T")));

            var method = type.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                GenericParameterTypeValidator.Named("T"),
                GenericParameterTypeValidator.Named("T"));

            var genericInstanceType = type.MakeGenericType(typeof(int));

            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            var instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceMethod = genericInstanceType.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType <int>(),
                NonGenericTypeValidator.ForType <int>());
            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { 556 }), Is.EqualTo(556));

            genericInstanceType = type.MakeGenericType(typeof(object));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceMethod = genericInstanceType.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                NonGenericTypeValidator.ForType <object>(),
                NonGenericTypeValidator.ForType <object>());
            var someObject = new object();

            Assert.That(genericInstanceMethod.Invoke(instance, new object[] { someObject }), Is.SameAs(someObject));

            // nested generic type with constraints
            type = targetType.GetNestedType("GenericTypeWithConstraints`6", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("TClass"),
                    GenericParameterTypeValidator.Named("TStruct"),
                    GenericParameterTypeValidator.Named("TNew"),
                    GenericParameterTypeValidator.Named("TClassNew"),
                    GenericParameterTypeValidator.Named("TDisposable"),
                    GenericParameterTypeValidator.Named("TTClass")));

            genericInstanceType = type.MakeGenericType(
                typeof(Stream),
                typeof(DictionaryEntry),
                typeof(DictionaryEntry),
                typeof(object),
                typeof(StringWriter),
                typeof(FileStream));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceMethod = genericInstanceType.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(genericInstanceMethod, Is.Not.Null);
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple <, , , , ,>),
                                         NonGenericTypeValidator.ForType <Stream>(),
                                         NonGenericTypeValidator.ForType <DictionaryEntry>(),
                                         NonGenericTypeValidator.ForType <DictionaryEntry>(),
                                         NonGenericTypeValidator.ForType <object>(),
                                         NonGenericTypeValidator.ForType <StringWriter>(),
                                         NonGenericTypeValidator.ForType <FileStream>()),
                NonGenericTypeValidator.ForType <Stream>(),
                NonGenericTypeValidator.ForType <DictionaryEntry>(),
                NonGenericTypeValidator.ForType <DictionaryEntry>(),
                NonGenericTypeValidator.ForType <object>(),
                NonGenericTypeValidator.ForType <StringWriter>(),
                NonGenericTypeValidator.ForType <FileStream>());

            // nested generic type with open generic method
            type = targetType.GetNestedType("GenericTypeWithGenericMethod`1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(type.ContainsGenericParameters);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("TType")));

            method = type.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            Assert.That(method.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(typeof(Tuple <,>),
                                         GenericParameterTypeValidator.Named("TType"),
                                         GenericParameterTypeValidator.Named("TMethod")),
                GenericParameterTypeValidator.Named("TType"),
                GenericParameterTypeValidator.Named("TMethod"));

            genericInstanceType = type.MakeGenericType(typeof(int));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceGenericMethod = genericInstanceType.GetMethod("GetThings", TestContent.BindingFlagsForWeavedMembers);

            Assert.That(genericInstanceGenericMethod, Is.Not.Null);
            Assert.That(genericInstanceGenericMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceGenericMethod,
                new GenericTypeValidator(typeof(Tuple <,>),
                                         NonGenericTypeValidator.ForType <int>(),
                                         GenericParameterTypeValidator.Named("TMethod")),
                NonGenericTypeValidator.ForType <int>(),
                GenericParameterTypeValidator.Named("TMethod"));

            genericInstanceMethod = genericInstanceGenericMethod.MakeGenericMethod(typeof(object));
            Assert.That(!genericInstanceMethod.ContainsGenericParameters);
            TypeValidatorBase.ValidateParameters(
                genericInstanceMethod,
                new GenericTypeValidator(typeof(Tuple <,>),
                                         NonGenericTypeValidator.ForType <int>(),
                                         NonGenericTypeValidator.ForType <object>()),
                NonGenericTypeValidator.ForType <int>(),
                NonGenericTypeValidator.ForType <object>());
            someObject = new object();
            var things = (Tuple <int, object>)genericInstanceMethod.Invoke(instance, new object[] { 45866, someObject });

            Assert.That(things.Item1, Is.EqualTo(45866));
            Assert.That(things.Item2, Is.SameAs(someObject));

            // generic type with multiple parameters
            type = targetType.GetNestedType("GenericTypeWithMultipleParameters`2", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T1"),
                    GenericParameterTypeValidator.Named("T2")));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing1"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T1"));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing2"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T2"));

            genericInstanceType = type.MakeGenericType(typeof(int), typeof(string));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            var genericInstanceProperty = genericInstanceType.GetProperty("Thing1", TestContent.BindingFlagsForWeavedMembers);

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <int>());
            genericInstanceProperty.SetValue(instance, 48008);
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(48008));

            genericInstanceProperty = genericInstanceType.GetProperty("Thing2", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <string>());
            genericInstanceProperty.SetValue(instance, "SAdfiohoqiweAsiohnewlroi asoidfh inra6f");
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("SAdfiohoqiweAsiohnewlroi asoidfh inra6f"));

            instance = Activator.CreateInstance(genericInstanceType, new object[] { 34543, "ALKJEWklnrpioewhfioehgiu" });

            genericInstanceProperty = genericInstanceType.GetProperty("Thing1", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <int>());
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(34543));

            genericInstanceProperty = genericInstanceType.GetProperty("Thing2", TestContent.BindingFlagsForWeavedMembers);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <string>());
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("ALKJEWklnrpioewhfioehgiu"));

            // partially closed generic type
            type = targetType.GetNestedType("PartiallyClosedGenericType`1", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            TypeValidatorBase.ValidateType(
                type,
                new GenericTypeValidator(
                    type,
                    GenericParameterTypeValidator.Named("T3")));

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing1"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <int>());

            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                type.GetProperty("Thing2"),
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                GenericParameterTypeValidator.Named("T3"));

            genericInstanceType = type.MakeGenericType(typeof(string));
            Assert.That(!genericInstanceType.ContainsGenericParameters);
            Assert.That(genericInstanceType.IsConstructedGenericType);

            instance = Activator.CreateInstance(genericInstanceType, new object[0]);

            genericInstanceProperty = genericInstanceType.GetProperty(
                "Thing1",
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <int>());
            genericInstanceProperty.SetValue(instance, 5446);
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo(5446));

            genericInstanceProperty = genericInstanceType.BaseType.GetProperty(
                "Thing2",
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            TypeValidatorBase.ValidatePropertyTypeAndAccessors(
                genericInstanceProperty,
                TypeValidatorBase.PropertyAccessorExpectations.Both,
                NonGenericTypeValidator.ForType <string>());
            genericInstanceProperty.SetValue(instance, "AFioenAjeiona aesoing ijeng inuia eabruibeg uybvwaytvr  b");
            Assert.That(genericInstanceProperty.GetValue(instance), Is.EqualTo("AFioenAjeiona aesoing ijeng inuia eabruibeg uybvwaytvr  b"));


            // type with method with partially closed generic type
            type = targetType.GetNestedType("TypeWithPartiallyClosedGenericMethod", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(type, Is.Not.Null);
            Assert.That(!type.IsGenericType);

            instance = Activator.CreateInstance(type);

            var returnTypeGenericDefinition = targetType.GetNestedType("GenericTypeWithMultipleParameters`2", TestContent.BindingFlagsForWeavedMembers);

            method = type.GetMethod("GetThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(
                    returnTypeGenericDefinition,
                    NonGenericTypeValidator.ForType <int>(),
                    GenericParameterTypeValidator.Named("T4")),
                GenericParameterTypeValidator.Named("T4"));

            genericInstanceMethod = method.MakeGenericMethod(new Type[] { typeof(string) });
            var returnValue = genericInstanceMethod.Invoke(instance, new object[] { "Eoahio eioua bueagingnpiuw nufib fuiwebyu" });
            var genericInstanceReturnType = returnTypeGenericDefinition.MakeGenericType(new Type[] { typeof(int), typeof(string) });

            Assert.That(returnValue.GetType(), Is.EqualTo(genericInstanceReturnType));
            Assert.That(genericInstanceReturnType.GetProperty("Thing1").GetValue(returnValue), Is.EqualTo(297387));
            Assert.That(genericInstanceReturnType.GetProperty("Thing2").GetValue(returnValue), Is.EqualTo("Eoahio eioua bueagingnpiuw nufib fuiwebyu"));

            method = type.GetMethod("GetOtherThing", TestContent.BindingFlagsForWeavedMembers);
            Assert.That(method, Is.Not.Null);
            TypeValidatorBase.ValidateParameters(
                method,
                new GenericTypeValidator(
                    returnTypeGenericDefinition,
                    GenericParameterTypeValidator.Named("T5"),
                    NonGenericTypeValidator.ForType <int>()),
                GenericParameterTypeValidator.Named("T5"));

            genericInstanceMethod     = method.MakeGenericMethod(new Type[] { typeof(string) });
            returnValue               = genericInstanceMethod.Invoke(instance, new object[] { "Eaiosn4oil ifhiu 3ybusabu 3byuvsdf yuvaytithomn gfd" });
            genericInstanceReturnType = returnTypeGenericDefinition.MakeGenericType(new Type[] { typeof(string), typeof(int) });
            Assert.That(returnValue.GetType(), Is.EqualTo(genericInstanceReturnType));
            Assert.That(genericInstanceReturnType.GetProperty("Thing1").GetValue(returnValue), Is.EqualTo("Eaiosn4oil ifhiu 3ybusabu 3byuvsdf yuvaytithomn gfd"));
            Assert.That(genericInstanceReturnType.GetProperty("Thing2").GetValue(returnValue), Is.EqualTo(789437));
        }
Example #54
0
        private void TestWith(
            Type[] constructorTypes,
            object[] constructorArgs,
            int intValue,
            string stringValue,
            string uriHostValue,
            string whichBaseConstructor)
        {
            var config = new CiladorConfigType
            {
                WeaveConfig = new WeaveConfigTypeBase[]
                {
                    new InterfaceMixinConfigType
                    {
                        InterfaceMixinMap = new[]
                        {
                            new InterfaceMixinMapType
                            {
                                Interface = typeof(IForTargetWithMultipleConstructors).GetShortAssemblyQualifiedName(),
                                Mixin     = typeof(InstanceInitializationMixin).GetShortAssemblyQualifiedName()
                            }
                        }
                    },
                }
            };


            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(MultipleConstructorsTarget).FullName);

            Assert.That(typeof(IForTargetWithMultipleConstructors).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(4, 1, 13, 0, 0, 2);

            var constructor = targetType.GetConstructor(constructorTypes);

            Assert.That(constructor, Is.Not.Null);

            var instanceObject = constructor.Invoke(constructorArgs);

            Assert.That(instanceObject, Is.Not.Null);
            Assert.That(instanceObject is IForTargetWithMultipleConstructors);
            var instance = (IForTargetWithMultipleConstructors)instanceObject;

            Assert.That(targetType.GetField("SomeNumberSetTo395493InConstructor").GetValue(instance), Is.EqualTo(395493));

            Assert.That(targetType.GetField("OriginalInitializedInt").GetValue(instance), Is.EqualTo(48685));
            Assert.That(targetType.GetField("OriginalInitializedString").GetValue(instance), Is.EqualTo("Tion3lao ehiuawh iuh buib ld"));
            var innerObject = (UriBuilder)targetType.GetField("OriginalInitializedObject").GetValue(instance);

            Assert.That(innerObject, Is.Not.Null);
            Assert.That(innerObject.Host, Is.EqualTo("a.b.c"));

            Assert.That(targetType.GetField("OriginalUninitializedInt").GetValue(instance), Is.EqualTo(intValue));
            Assert.That(targetType.GetField("OriginalUninitializedString").GetValue(instance), Is.EqualTo(stringValue));
            Assert.That(innerObject, Is.Not.Null);
            innerObject = (UriBuilder)targetType.GetField("OriginalUninitializedObject").GetValue(instance);
            Assert.That(innerObject, Is.Not.Null);
            Assert.That(innerObject.Host, Is.EqualTo(uriHostValue));

            Assert.That(targetType.GetField("WhichConstructor").GetValue(instance), Is.EqualTo(whichBaseConstructor));

            Assert.That(targetType.GetField("SomeNumber").GetValue(instance), Is.EqualTo(684865));
            Assert.That(targetType.GetField("SomeString").GetValue(instance), Is.EqualTo("Tawhlej oisahoeh 8ohf 4ifh8ohe fni dlgj"));
            Assert.That(targetType.GetField("SomeObject").GetValue(instance), Is.Not.Null);

            var innerTypeInstance = targetType.GetField("SomeInnerType").GetValue(instance);

            Assert.That(innerTypeInstance, Is.Not.Null);
            var innerType = innerTypeInstance.GetType();

            Assert.That(innerType.GetProperty("SomeInt").GetValue(innerTypeInstance), Is.EqualTo(4235));
            Assert.That(innerType.GetProperty("SomeString").GetValue(innerTypeInstance), Is.EqualTo("JLKOIN  oin aon oingori d"));
            Assert.That(innerType.GetProperty("SomeObject").GetValue(innerTypeInstance), Is.Not.Null);

            var someFunc = targetType.GetField("SomeFunc").GetValue(instance) as Delegate;

            Assert.That(someFunc, Is.Not.Null);
            var result = someFunc.DynamicInvoke(new[] { 1, "2", new object() }) as Tuple <int, string, object>;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Item1, Is.EqualTo(1));
            Assert.That(result.Item2, Is.EqualTo("2"));
            Assert.That(result.Item3, Is.Not.Null);

            var someMethodDelegateInstance = targetType.GetField("SomeMethodDelegateInstance").GetValue(instance) as Delegate;

            Assert.That(someMethodDelegateInstance, Is.Not.Null);
            result = someMethodDelegateInstance.DynamicInvoke(new[] { 3, "4", new object() }) as Tuple <int, string, object>;
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Item1, Is.EqualTo(3));
            Assert.That(result.Item2, Is.EqualTo("4"));
            Assert.That(result.Item3, Is.Not.Null);
        }
        public void CanImplementInterface()
        {
            var config = new CiladorConfigType();

            config.WeaveConfig = new WeaveConfigTypeBase[]
            {
                new InterfaceMixinConfigType
                {
                    InterfaceMixinMap = new InterfaceMixinMapType[]
                    {
                        new InterfaceMixinMapType
                        {
                            Interface = typeof(IInterfaceWithOnlyPrimitiveTypes).GetShortAssemblyQualifiedName(),
                            Mixin     = typeof(PropertiesAndMethodsWithPrimitiveTypesMixin).GetShortAssemblyQualifiedName()
                        }
                    }
                },
            };

            var assembly   = ModuleWeaverHelper.WeaveAndLoadTestTarget("Cilador.Fody.TestMixinTargets", config);
            var targetType = assembly.GetType(typeof(InterfaceWithOnlyPrimitiveTypesTarget).FullName);

            Assert.That(typeof(IInterfaceWithOnlyPrimitiveTypes).IsAssignableFrom(targetType));
            targetType.ValidateMemberCountsAre(1, 42, 14, 14, 0, 0);

            Assert.That(targetType.GetConstructor(new Type[0]) != null, "Lost existing default constructor");

            var instanceObject = Activator.CreateInstance(targetType, new object[0]);

            Assert.That(instanceObject is IInterfaceWithOnlyPrimitiveTypes);
            var instance = (IInterfaceWithOnlyPrimitiveTypes)instanceObject;

            instance.BooleanProperty = true;
            Assert.That(instance.BooleanProperty);
            instance.BooleanProperty = false;
            Assert.That(!instance.BooleanProperty);

            instance.ByteProperty = Byte.MinValue;
            Assert.That(instance.ByteProperty == Byte.MinValue);
            instance.ByteProperty = 0;
            Assert.That(instance.ByteProperty == 0);
            instance.ByteProperty = Byte.MaxValue;
            Assert.That(instance.ByteProperty == Byte.MaxValue);

            instance.SByteProperty = SByte.MinValue;
            Assert.That(instance.SByteProperty == SByte.MinValue);
            instance.SByteProperty = 0;
            Assert.That(instance.SByteProperty == 0);
            instance.SByteProperty = SByte.MaxValue;
            Assert.That(instance.SByteProperty == SByte.MaxValue);

            instance.Int16Property = Int16.MinValue;
            Assert.That(instance.Int16Property == Int16.MinValue);
            instance.Int16Property = 0;
            Assert.That(instance.Int16Property == 0);
            instance.Int16Property = Int16.MaxValue;
            Assert.That(instance.Int16Property == Int16.MaxValue);

            instance.UInt16Property = UInt16.MinValue;
            Assert.That(instance.UInt16Property == UInt16.MinValue);
            instance.UInt16Property = 0;
            Assert.That(instance.UInt16Property == 0);
            instance.UInt16Property = UInt16.MaxValue;
            Assert.That(instance.UInt16Property == UInt16.MaxValue);

            instance.Int32Property = Int32.MinValue;
            Assert.That(instance.Int32Property == Int32.MinValue);
            instance.Int32Property = 0;
            Assert.That(instance.Int32Property == 0);
            instance.Int32Property = Int32.MaxValue;
            Assert.That(instance.Int32Property == Int32.MaxValue);

            instance.UInt32Property = UInt32.MinValue;
            Assert.That(instance.UInt32Property == UInt32.MinValue);
            instance.UInt32Property = 0;
            Assert.That(instance.UInt32Property == 0);
            instance.UInt32Property = UInt32.MaxValue;
            Assert.That(instance.UInt32Property == UInt32.MaxValue);

            instance.Int64Property = Int64.MinValue;
            Assert.That(instance.Int64Property == Int64.MinValue);
            instance.Int64Property = 0;
            Assert.That(instance.Int64Property == 0);
            instance.Int64Property = Int64.MaxValue;
            Assert.That(instance.Int64Property == Int64.MaxValue);

            instance.UInt64Property = UInt64.MinValue;
            Assert.That(instance.UInt64Property == UInt64.MinValue);
            instance.UInt64Property = 0;
            Assert.That(instance.UInt64Property == 0);
            instance.UInt64Property = UInt64.MaxValue;
            Assert.That(instance.UInt64Property == UInt64.MaxValue);

            instance.IntPtrProperty = IntPtr.Add(IntPtr.Zero, 402);
            Assert.That(instance.IntPtrProperty == IntPtr.Add(IntPtr.Zero, 402));
            instance.IntPtrProperty = IntPtr.Zero;
            Assert.That(instance.IntPtrProperty == IntPtr.Zero);

            instance.UIntPtrProperty = UIntPtr.Add(UIntPtr.Zero, 4987);
            Assert.That(instance.UIntPtrProperty == UIntPtr.Add(UIntPtr.Zero, 4987));
            instance.UIntPtrProperty = UIntPtr.Zero;
            Assert.That(instance.UIntPtrProperty == UIntPtr.Zero);

            instance.CharProperty = Char.MinValue;
            Assert.That(instance.CharProperty == Char.MinValue);
            instance.CharProperty = (Char)0;
            Assert.That(instance.CharProperty == 0);
            instance.CharProperty = Char.MaxValue;
            Assert.That(instance.CharProperty == Char.MaxValue);

            instance.DoubleProperty = Double.MinValue;
            Assert.That(instance.DoubleProperty == Double.MinValue);
            instance.DoubleProperty = 0;
            Assert.That(instance.DoubleProperty == 0);
            instance.DoubleProperty = Double.MaxValue;
            Assert.That(instance.DoubleProperty == Double.MaxValue);

            instance.SingleProperty = Single.MinValue;
            Assert.That(instance.SingleProperty == Single.MinValue);
            instance.SingleProperty = 0;
            Assert.That(instance.SingleProperty == 0);
            instance.SingleProperty = Single.MaxValue;
            Assert.That(instance.SingleProperty == Single.MaxValue);
        }
Example #56
0
        public static Assembly WeaveAndLoadTestTarget(
            string targetAssemblyFilename,
            CiladorConfigType config,
            params Tuple<string, string>[] fodyWeaverTaskProperties)
        {
            Contract.Requires(config != null);
            Contract.Ensures(Contract.Result<Assembly>() != null);

            var mixedAssembly = AppDomain.CurrentDomain.Load(ModuleWeaverHelper.GetRawWeavedAssembly(
                targetAssemblyFilename,
                ModuleWeaverHelper.BuildXElementConfig(config, fodyWeaverTaskProperties)));
            return mixedAssembly;
        }