Ejemplo n.º 1
0
        /// <summary>
        /// Adds the given mixin to the given target type with a number of explicit dependencies and suppressed mixins. This is a shortcut
        /// method for calling <see cref="ForClass"/>, <see cref="ClassContextBuilder.AddMixin(System.Type,Remotion.Mixins.Context.MixinContextOrigin)"/>,
        /// <see cref="MixinContextBuilder.WithDependencies"/>, and <see cref="MixinContextBuilder.ReplaceMixins"/> in a row.
        /// </summary>
        /// <param name="mixinKind">The kind of relationship the mixin has with its target class.</param>
        /// <param name="targetType">The target type to add a mixin for.</param>
        /// <param name="mixinType">The mixin type to add.</param>
        /// <param name="introducedMemberVisibility">The default visibility to be used for introduced members.</param>
        /// <param name="explicitDependencies">The explicit dependencies of the mixin in the context of the target type.</param>
        /// <param name="suppressedMixins">The mixins suppressed by this mixin in the context of the target type.</param>
        /// <param name="origin">A <see cref="MixinContextOrigin"/> object describing where the mixin configuration originates from.</param>
        public virtual MixinConfigurationBuilder AddMixinToClass(
            MixinKind mixinKind,
            Type targetType,
            Type mixinType,
            MemberVisibility introducedMemberVisibility,
            IEnumerable <Type> explicitDependencies,
            IEnumerable <Type> suppressedMixins,
            MixinContextOrigin origin)
        {
            ArgumentUtility.CheckNotNull("targetType", targetType);
            ArgumentUtility.CheckNotNull("mixinType", mixinType);
            ArgumentUtility.CheckNotNull("explicitDependencies", explicitDependencies);
            ArgumentUtility.CheckNotNull("suppressedMixins", suppressedMixins);
            ArgumentUtility.CheckNotNull("origin", origin);

            MixinContextBuilder mixinContextBuilder = AddMixinToClass(targetType, mixinType, origin);

            mixinContextBuilder
            .OfKind(mixinKind)
            .WithDependencies(explicitDependencies.ToArray())
            .WithIntroducedMemberVisibility(introducedMemberVisibility)
            .ReplaceMixins(suppressedMixins.ToArray());

            return(this);
        }
        public void SetUp()
        {
            _mockRepository    = new MockRepository();
            _parentBuilderMock = _mockRepository.StrictMock <ClassContextBuilder> (typeof(object));
            _origin            = MixinContextOriginObjectMother.Create();

            _mixinBuilder = new MixinContextBuilder(_parentBuilderMock, typeof(BT2Mixin1), _origin);
        }
 public void SetUp()
 {
     _mockRepository    = new MockRepository();
     _parentBuilderMock = _mockRepository.StrictMock <MixinConfigurationBuilder> ((MixinConfiguration)null);
     _classBuilder      = new ClassContextBuilder(_parentBuilderMock, typeof(BaseType2));
     _classBuilderMock  = _mockRepository.StrictMock <ClassContextBuilder> (_parentBuilderMock, typeof(BaseType2));
     _mixinBuilderMock  = _mockRepository.StrictMock <MixinContextBuilder> (_classBuilderMock, typeof(BT2Mixin1), MixinContextOriginObjectMother.Create());
 }
        public void AddMixin_NonGeneric()
        {
            var origin = MixinContextOriginObjectMother.Create();
            MixinContextBuilder mixinBuilder = _classBuilder.AddMixin(typeof(BT2Mixin1), origin);

            Assert.That(mixinBuilder.MixinType, Is.SameAs(typeof(BT2Mixin1)));
            Assert.That(mixinBuilder.Parent, Is.SameAs(_classBuilder));
            Assert.That(mixinBuilder.Origin, Is.SameAs(origin));
            Assert.That(_classBuilder.MixinContextBuilders, Has.Member(mixinBuilder));
        }
        public void EnsureMixin_Inheritance()
        {
            var contextWithMixin = ClassContextObjectMother.Create(typeof(BaseType3), typeof(NullTarget));

            MixinContextBuilder builder = _classBuilder.EnsureMixin(typeof(DerivedNullTarget));

            Assert.That(builder.MixinType, Is.EqualTo(typeof(DerivedNullTarget)));
            Type[] mixinTypes = GetMixinTypes();
            Assert.That(mixinTypes, Is.EquivalentTo(new object[] { typeof(DerivedNullTarget) }));

            ClassContext builtContext = _classBuilder.BuildClassContext(new[] { contextWithMixin });

            Assert.That(builtContext.Mixins.Count, Is.EqualTo(1));
            Assert.That(builtContext.Mixins.ContainsKey(typeof(DerivedNullTarget)), Is.True);
            Assert.That(builtContext.Mixins.ContainsKey(typeof(NullTarget)), Is.False);
        }
        public void EnsureMixin_NonGeneric()
        {
            var origin = MixinContextOriginObjectMother.Create();

            MixinContextBuilder builder = _classBuilder.EnsureMixin(typeof(BT2Mixin1), origin);

            Assert.That(builder.MixinType, Is.EqualTo(typeof(BT2Mixin1)));
            Assert.That(builder.Origin, Is.EqualTo(origin));
            Type[] mixinTypes = GetMixinTypes();
            Assert.That(mixinTypes, Is.EquivalentTo(new object[] { typeof(BT2Mixin1) }));

            var otherOrigin = MixinContextOriginObjectMother.Create("some other kind");

            Assert.That(_classBuilder.EnsureMixin(typeof(BT2Mixin1), otherOrigin), Is.SameAs(builder));
            Type[] mixinTypesAfterSecondEnsureMixin = GetMixinTypes();
            Assert.That(mixinTypesAfterSecondEnsureMixin, Is.EquivalentTo(new object[] { typeof(BT2Mixin1) }));
            Assert.That(builder.Origin, Is.EqualTo(origin));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Collects the given types as mixins for the <see cref="TargetType"/> and adds dependencies between the mixins to ensure a proper base call
        /// order.
        /// </summary>
        /// <param name="mixinTypes">The mixin types to collect with dependencies.</param>
        /// <param name="origin">A <see cref="MixinContextOrigin"/> object describing where the mixin configuration originates from.</param>
        /// <returns>This object for further configuration of the <see cref="TargetType"/>.</returns>
        public virtual ClassContextBuilder AddOrderedMixins(MixinContextOrigin origin, params Type[] mixinTypes)
        {
            ArgumentUtility.CheckNotNull("origin", origin);
            ArgumentUtility.CheckNotNull("mixinTypes", mixinTypes);

            Type lastMixinType = null;

            foreach (Type mixinType in mixinTypes)
            {
                MixinContextBuilder mixinContextBuilder = AddMixin(mixinType, origin);
                if (lastMixinType != null)
                {
                    mixinContextBuilder.WithDependency(lastMixinType);
                }
                lastMixinType = mixinType;
            }
            return(this);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Collects the given type as a mixin for the <see cref="TargetType"/>.
        /// </summary>
        /// <param name="mixinType">The mixin type to collect.</param>
        /// <param name="origin">A <see cref="MixinContextOrigin"/> object describing where the mixin configuration originates from.</param>
        /// <returns>
        /// A <see cref="MixinContextBuilder"/> object for further configuration of the mixin.
        /// </returns>
        public virtual MixinContextBuilder AddMixin(Type mixinType, MixinContextOrigin origin)
        {
            ArgumentUtility.CheckNotNull("mixinType", mixinType);
            ArgumentUtility.CheckNotNull("origin", origin);

            if (AlreadyAppliedSame(mixinType))
            {
                Type mixinTypeForException = mixinType.IsGenericType ? mixinType.GetGenericTypeDefinition() : mixinType;
                Assertion.IsNotNull(mixinTypeForException);
                throw new ArgumentException(
                          string.Format("{0} is already configured as a mixin for type {1}.", mixinTypeForException.FullName, TargetType.FullName), "mixinType");
            }

            var mixinContextBuilder = new MixinContextBuilder(this, mixinType, origin);

            _mixinContextBuilders.Add(mixinType, mixinContextBuilder);
            return(mixinContextBuilder);
        }
        public void ParentMembers()
        {
            _mockRepository.BackToRecordAll();

            var suppressionRuleStub = MockRepository.GenerateStub <IMixinSuppressionRule> ();
            var r1     = new ClassContextBuilder(new MixinConfigurationBuilder(null), typeof(object));
            var r2     = new MixinConfiguration();
            var r3     = _mockRepository.StrictMock <IDisposable> ();
            var r4     = new MixinContextBuilder(r1, typeof(BT1Mixin1), _origin);
            var r5     = ClassContextObjectMother.Create(typeof(object));
            var origin = MixinContextOriginObjectMother.Create();

            IEnumerable <ClassContext> inheritedContexts = new ClassContext[0];

            var expectedInferredOrigin = MixinContextOrigin.CreateForMethod(MethodBase.GetCurrentMethod());

            using (_mockRepository.Ordered())
            {
                _parentBuilderMock.Expect(mock => mock.Clear()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixin(typeof(object), origin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.AddMixin(typeof(object), expectedInferredOrigin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.AddMixin <string> (origin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.AddMixin <string> (expectedInferredOrigin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.AddMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixins(expectedInferredOrigin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixins <BT1Mixin1, BT1Mixin2> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixins <BT1Mixin1, BT1Mixin2> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins(expectedInferredOrigin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins <BT1Mixin1, BT1Mixin2> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins <BT1Mixin1, BT1Mixin2> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddOrderedMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixin(typeof(object), origin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.EnsureMixin(typeof(object), expectedInferredOrigin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.EnsureMixin <string> (origin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.EnsureMixin <string> (expectedInferredOrigin)).Return(r4);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins(expectedInferredOrigin, typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins <BT1Mixin1, BT1Mixin2> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins <BT1Mixin1, BT1Mixin2> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.EnsureMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (expectedInferredOrigin)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterface(typeof(IBT6Mixin1))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterface <IBT6Mixin1>()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterfaces(typeof(IBT6Mixin1), typeof(IBT6Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2, IBT6Mixin3> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2, IBT6Mixin3> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixin(suppressionRuleStub)).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixin(typeof(object))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixin <string> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixins(typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixins <BT1Mixin1, BT1Mixin2> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.SuppressMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixinDependency(typeof(BT1Mixin1), typeof(BT1Mixin2))).Return(r1);
                _parentBuilderMock.Expect(mock => mock.AddMixinDependency <BT1Mixin1, BT1Mixin2> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.BuildClassContext(inheritedContexts)).Return(r5);
                _parentBuilderMock.Expect(mock => mock.BuildClassContext()).Return(r5);

                _parentBuilderMock.Expect(mock => mock.ForClass <object> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.ForClass <string> ()).Return(r1);
                _parentBuilderMock.Expect(mock => mock.BuildConfiguration()).Return(r2);
                _parentBuilderMock.Expect(mock => mock.EnterScope()).Return(r3);
            }

            _mockRepository.ReplayAll();

            Assert.That(_mixinBuilder.Clear(), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixin(typeof(object), origin), Is.SameAs(r4));
            Assert.That(_mixinBuilder.AddMixin(typeof(object)), Is.SameAs(r4));
            Assert.That(_mixinBuilder.AddMixin <string> (origin), Is.SameAs(r4));
            Assert.That(_mixinBuilder.AddMixin <string> (), Is.SameAs(r4));
            Assert.That(_mixinBuilder.AddMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixins(typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixins <BT1Mixin1, BT1Mixin2> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixins <BT1Mixin1, BT1Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins(typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins <BT1Mixin1, BT1Mixin2> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins <BT1Mixin1, BT1Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddOrderedMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixin(typeof(object), origin), Is.SameAs(r4));
            Assert.That(_mixinBuilder.EnsureMixin(typeof(object)), Is.SameAs(r4));
            Assert.That(_mixinBuilder.EnsureMixin <string> (origin), Is.SameAs(r4));
            Assert.That(_mixinBuilder.EnsureMixin <string> (), Is.SameAs(r4));
            Assert.That(_mixinBuilder.EnsureMixins(origin, typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixins(typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixins <BT1Mixin1, BT1Mixin2> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixins <BT1Mixin1, BT1Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (origin), Is.SameAs(r1));
            Assert.That(_mixinBuilder.EnsureMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterface(typeof(IBT6Mixin1)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterface <IBT6Mixin1> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterfaces(typeof(IBT6Mixin1), typeof(IBT6Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2, IBT6Mixin3> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddComposedInterfaces <IBT6Mixin1, IBT6Mixin2, IBT6Mixin3> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixin(suppressionRuleStub), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixin(typeof(object)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixin <string> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixins(typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixins <BT1Mixin1, BT1Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.SuppressMixins <BT1Mixin1, BT1Mixin2, BT3Mixin1> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixinDependency(typeof(BT1Mixin1), typeof(BT1Mixin2)), Is.SameAs(r1));
            Assert.That(_mixinBuilder.AddMixinDependency <BT1Mixin1, BT1Mixin2> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.BuildClassContext(inheritedContexts), Is.SameAs(r5));
            Assert.That(_mixinBuilder.BuildClassContext(), Is.SameAs(r5));

            Assert.That(_mixinBuilder.ForClass <object> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.ForClass <string> (), Is.SameAs(r1));
            Assert.That(_mixinBuilder.BuildConfiguration(), Is.SameAs(r2));
            Assert.That(_mixinBuilder.EnterScope(), Is.SameAs(r3));

            _mockRepository.VerifyAll();
        }
        public void ReplaceMixin_SelfSuppressor_GenericDefinition()
        {
            var mixinBuilder = new MixinContextBuilder(_parentBuilderMock, typeof(GenericMixin <object>), _origin);

            mixinBuilder.ReplaceMixin(typeof(GenericMixin <>));
        }
        public void WithIntroducedMemberVisibility_ReturnsMixinBuilder()
        {
            MixinContextBuilder result = _mixinBuilder.WithIntroducedMemberVisibility(MemberVisibility.Public);

            Assert.That(result, Is.SameAs(_mixinBuilder));
        }