Пример #1
0
        public void ChildSpecificAccept()
        {
            var visitorMock = MockRepository.GenerateMock <IDefinitionVisitor>();
            var definition  = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType1), typeof(BT1Mixin1)).Mixins[0];

            var interfaceIntroduction           = DefinitionObjectMother.CreateInterfaceIntroductionDefinition(definition);
            var nonInterfaceIntroduction        = DefinitionObjectMother.CreateNonInterfaceIntroductionDefinition(definition);
            var attributeIntroduction           = DefinitionObjectMother.CreateAttributeIntroductionDefinition(definition);
            var nonAttributeIntroduction        = DefinitionObjectMother.CreateNonAttributeIntroductionDefinition(definition);
            var suppressedAttributeIntroduction = DefinitionObjectMother.CreateSuppressedAttributeIntroductionDefinition(definition);
            var targetCallDependency            = DefinitionObjectMother.CreateTargetCallDependencyDefinition(definition);
            var nextCallDependency = DefinitionObjectMother.CreateNextCallDependencyDefinition(definition);
            var mixinDependency    = DefinitionObjectMother.CreateMixinDependencyDefinition(definition);

            using (visitorMock.GetMockRepository().Ordered())
            {
                visitorMock.Expect(mock => mock.Visit(definition));
                visitorMock.Expect(mock => mock.Visit(interfaceIntroduction));
                visitorMock.Expect(mock => mock.Visit(nonInterfaceIntroduction));
                visitorMock.Expect(mock => mock.Visit(attributeIntroduction));
                visitorMock.Expect(mock => mock.Visit(nonAttributeIntroduction));
                visitorMock.Expect(mock => mock.Visit(suppressedAttributeIntroduction));
                visitorMock.Expect(mock => mock.Visit(targetCallDependency));
                visitorMock.Expect(mock => mock.Visit(nextCallDependency));
                visitorMock.Expect(mock => mock.Visit(mixinDependency));
            }

            visitorMock.Replay();
            PrivateInvoke.InvokeNonPublicMethod(definition, "ChildSpecificAccept", visitorMock);
            visitorMock.VerifyAllExpectations();
        }
        public void GetMixinByConfiguredType_Null()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType3));

            Assert.That(targetClassDefinition.GetMixinByConfiguredType(typeof(BT3Mixin1)), Is.Null);
            Assert.That(targetClassDefinition.GetMixinByConfiguredType(typeof(BT3Mixin3 <,>)), Is.Null);
        }
        public void HasMixinWithConfiguredType_False()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType3));

            Assert.That(targetClassDefinition.HasMixinWithConfiguredType(typeof(BT3Mixin1)), Is.False);
            Assert.That(targetClassDefinition.HasMixinWithConfiguredType(typeof(BT3Mixin3 <,>)), Is.False);
        }
        public void CreateRequiredMethodDefinitions_BaseWithSameMembers()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(ClassImplementingInterfaceWithDuckTypingWithBaseWithSameMembers));

            DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTyping).GetMethod("Method1"));
            DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTyping).GetMethod("Method2"));
            var m1b = DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTypingWithBaseWithSameMembers).GetMethod("Method1"));
            var m2b = DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTypingWithBaseWithSameMembers).GetMethod("Method2"));

            var requirement = DefinitionObjectMother.CreateRequiredTargetCallTypeDefinition(targetClassDefinition, typeof(IInterface));
            var builder     = new DuckTypingRequiredMethodDefinitionCollector(targetClassDefinition);

            var definitions = builder.CreateRequiredMethodDefinitions(requirement).OrderBy(def => def.FullName).ToArray();

            Assert.That(definitions.Length, Is.EqualTo(2));

            Assert.That(definitions[0].DeclaringRequirement, Is.SameAs(requirement));
            Assert.That(definitions[0].InterfaceMethod, Is.EqualTo(typeof(IInterface).GetMethod("Method1")));
            Assert.That(definitions[0].ImplementingMethod, Is.SameAs(m1b));

            Assert.That(definitions[1].DeclaringRequirement, Is.SameAs(requirement));
            Assert.That(definitions[1].InterfaceMethod, Is.EqualTo(typeof(IInterface).GetMethod("Method2")));
            Assert.That(definitions[1].ImplementingMethod, Is.SameAs(m2b));
        }
        public void ChildSpecificAccept()
        {
            var targetClassDefinition                 = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType1), typeof(BT1Mixin1));
            var mixinDefinition                       = targetClassDefinition.Mixins[0];
            var requiredTargetCallTypeDefinition      = DefinitionObjectMother.CreateRequiredTargetCallTypeDefinition(targetClassDefinition, typeof(IBT1Mixin1));
            var requiredNextCallTypeDefinition        = DefinitionObjectMother.CreateRequiredNextCallTypeDefinition(targetClassDefinition, typeof(IBT1Mixin1));
            var requiredMixinTypeDefinition           = DefinitionObjectMother.CreateRequiredMixinTypeDefinition(targetClassDefinition, typeof(BT1Mixin2));
            var composedInterfaceDependencyDefinition = DefinitionObjectMother.CreateComposedInterfaceDependencyDefinition(targetClassDefinition);

            var visitorMock = MockRepository.GenerateMock <IDefinitionVisitor> ();

            using (visitorMock.GetMockRepository().Ordered())
            {
                visitorMock.Expect(mock => mock.Visit(targetClassDefinition));
                visitorMock.Expect(mock => mock.Visit(mixinDefinition));
                visitorMock.Expect(mock => mock.Visit(requiredTargetCallTypeDefinition));
                visitorMock.Expect(mock => mock.Visit(requiredNextCallTypeDefinition));
                visitorMock.Expect(mock => mock.Visit(requiredMixinTypeDefinition));
                visitorMock.Expect(mock => mock.Visit(composedInterfaceDependencyDefinition));
            }

            visitorMock.Replay();

            targetClassDefinition.Accept(visitorMock);

            visitorMock.VerifyAllExpectations();
        }
        public void GetAdjustedInterfaceMap_MethodDeclaredOnBaseType()
        {
            var classDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(DerivedImplementingInterface));
            var mapping         = classDefinition.GetAdjustedInterfaceMap(typeof(InterfaceImplementedByDerived));

            Assert.That(GetTargetMethod(mapping, "Void Foo()"), Is.EqualTo(typeof(BaseWithDerivedImplementingInterface).GetMethod("Foo")));
        }
Пример #7
0
        public void GetAllOverrides()
        {
            var definition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType1), typeof(BT1Mixin1)).Mixins[0];

            var methodOverride   = DefinitionObjectMother.CreateMethodDefinition(definition, definition.Type.GetMethod("ToString"));
            var overriddenMethod = DefinitionObjectMother.CreateMethodDefinition(definition.TargetClass, definition.Type.GetMethod("ToString"));

            DefinitionObjectMother.DeclareOverride(methodOverride, overriddenMethod);

            var propertyOverride   = DefinitionObjectMother.CreatePropertyDefinition(definition, typeof(DateTime).GetProperty("Now"));
            var overriddenProperty = DefinitionObjectMother.CreatePropertyDefinition(definition.TargetClass, typeof(DateTime).GetProperty("Now"));

            DefinitionObjectMother.DeclareOverride(propertyOverride, overriddenProperty);

            var eventOverride   = DefinitionObjectMother.CreateEventDefinition(definition, typeof(AppDomain).GetEvent("ProcessExit"));
            var overriddenEvent = DefinitionObjectMother.CreateEventDefinition(definition.TargetClass, typeof(AppDomain).GetEvent("ProcessExit"));

            DefinitionObjectMother.DeclareOverride(eventOverride, overriddenEvent);

            var nonOverride = DefinitionObjectMother.CreateMethodDefinition(definition, definition.Type.GetMethod("GetHashCode"));

            var overrides = definition.GetAllOverrides().ToArray();

            Assert.That(overrides, Is.EquivalentTo(new MemberDefinitionBase[] { methodOverride, propertyOverride, eventOverride }));
            Assert.That(overrides, Has.No.Member(nonOverride));
        }
        public void GetdjustedInterfaceMap_MethodDeclaredOnThisType_NonGenericOverload()
        {
            var classDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(DerivedImplementingInterface));
            var mapping         = classDefinition.GetAdjustedInterfaceMap(typeof(InterfaceWithGenericOverloadsImplementedByDerived));

            var expected = typeof(DerivedImplementingInterface).GetMethods().Where(m => m.ToString() == "Void GBar()").Single();

            Assert.That(GetTargetMethod(mapping, "Void GBar()"), Is.EqualTo(expected));
        }
        public void CreateRequiredMethodDefinitions_NoMatch_NoRequiringMixin()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));

            var requirement = DefinitionObjectMother.CreateRequiredTargetCallTypeDefinition(targetClassDefinition, typeof(IInterface));
            var builder     = new DuckTypingRequiredMethodDefinitionCollector(targetClassDefinition);

            builder.CreateRequiredMethodDefinitions(requirement).ToArray();
        }
        public void ChildSpecificAccept_CallsVisitForMixins()
        {
            var visitorMock           = MockRepository.GenerateMock <IDefinitionVisitor> ();
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType1), typeof(BT1Mixin1));

            targetClassDefinition.Accept(visitorMock);

            visitorMock.AssertWasCalled(mock => mock.Visit(targetClassDefinition.Mixins[0]));
        }
Пример #11
0
        public void Accept()
        {
            var targetClass = DefinitionObjectMother.CreateTargetClassDefinition(typeof(NullTarget));
            var requiredTargetCallTypeDefinition = DefinitionObjectMother.CreateRequiredTargetCallTypeDefinition(targetClass, typeof(ISimpleInterface));
            var dependency = new ComposedInterfaceDependencyDefinition(requiredTargetCallTypeDefinition, typeof(ISimpleInterface), null);

            var visitorMock = MockRepository.GenerateMock <IDefinitionVisitor> ();

            dependency.Accept(visitorMock);

            visitorMock.AssertWasCalled(mock => mock.Visit(dependency));
        }
        public void GetMixinByConfiguredType_NonNull()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType3));

            var nonGenericMixinType       = typeof(BT3Mixin1);
            var nonGenericMixinDefinition = DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, nonGenericMixinType);

            var closedMixinType       = targetClassDefinition.MixinTypeCloser.GetClosedMixinType(typeof(BT3Mixin3 <,>));
            var closedMixinDefinition = DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, closedMixinType);

            Assert.That(targetClassDefinition.GetMixinByConfiguredType(nonGenericMixinType), Is.SameAs(nonGenericMixinDefinition));
            Assert.That(targetClassDefinition.GetMixinByConfiguredType(closedMixinType), Is.SameAs(closedMixinDefinition));
            Assert.That(targetClassDefinition.GetMixinByConfiguredType(closedMixinType.GetGenericTypeDefinition()), Is.SameAs(closedMixinDefinition));
        }
        public void CreateRequiredMethodDefinitions_NoMatch()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));
            var requiringMixin        = DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, typeof(string));

            var dependency  = DefinitionObjectMother.CreateTargetCallDependencyDefinition(requiringMixin);
            var requirement = dependency.RequiredType;

            DefinitionObjectMother.AddRequiringDependency(requirement, dependency);

            var builder = new DuckTypingRequiredMethodDefinitionCollector(targetClassDefinition);

            builder.CreateRequiredMethodDefinitions(requirement).ToArray();
        }
        public void HasMixinWithConfiguredType_True()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(BaseType3));

            var nonGenericMixinType = typeof(BT3Mixin1);

            DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, nonGenericMixinType);

            var closedMixinType = targetClassDefinition.MixinTypeCloser.GetClosedMixinType(typeof(BT3Mixin3 <,>));

            DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, closedMixinType);

            Assert.That(targetClassDefinition.HasMixinWithConfiguredType(nonGenericMixinType), Is.True);
            Assert.That(targetClassDefinition.HasMixinWithConfiguredType(closedMixinType), Is.True);
            Assert.That(targetClassDefinition.HasMixinWithConfiguredType(closedMixinType.GetGenericTypeDefinition()), Is.True);
        }
        public void Serialization()
        {
            var log  = new DefaultValidationLog();
            var rule = new DelegateValidationRule <TargetClassDefinition> (DummyRule);

            var definition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));

            log.ValidationStartsFor(definition);
            log.Succeed(rule);
            log.ValidationEndsFor(definition);

            var exception = new ValidationException(log.GetData());

            var deserializedException = Serializer.SerializeAndDeserialize(exception);

            Assert.That(deserializedException.Message, Is.EqualTo(exception.Message));
            Assert.That(deserializedException.ValidationLogData, Is.Not.Null);
        }
        public void OverriddenMethods_AreFiltered()
        {
            var classDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(DerivedDerivedWithOverrides));
            var builder         = new MemberDefinitionBuilder(classDefinition, mi => true);

            builder.Apply(typeof(DerivedDerivedWithOverrides));

            const BindingFlags bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;

            Assert.That(classDefinition.Methods.ContainsKey(typeof(DerivedWithNewVirtualMembers).GetMethod("Method", bf)), Is.False);
            Assert.That(classDefinition.Methods.ContainsKey(typeof(DerivedDerivedWithOverrides).GetMethod("Method", bf)), Is.True);

            Assert.That(classDefinition.Properties.ContainsKey(typeof(DerivedWithNewVirtualMembers).GetProperty("Property", bf)), Is.False);
            Assert.That(classDefinition.Properties.ContainsKey(typeof(DerivedDerivedWithOverrides).GetProperty("Property", bf)), Is.True);

            Assert.That(classDefinition.Events.ContainsKey(typeof(DerivedWithNewVirtualMembers).GetEvent("Event", bf)), Is.False);
            Assert.That(classDefinition.Events.ContainsKey(typeof(DerivedDerivedWithOverrides).GetEvent("Event", bf)), Is.True);
        }
        private static TargetClassDefinition CreateTargetClassDefinition(params Tuple <Type, bool, Type[]>[] mixinTypesAndDependencies)
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(NullTarget));

            foreach (var tuple in mixinTypesAndDependencies)
            {
                DefinitionObjectMother.CreateMixinDefinition(targetClassDefinition, tuple.Item1, tuple.Item2);
            }

            foreach (var tuple in mixinTypesAndDependencies)
            {
                foreach (var dependency in tuple.Item3)
                {
                    DefinitionObjectMother.CreateMixinDependencyDefinition(targetClassDefinition.Mixins[tuple.Item1], targetClassDefinition.Mixins[dependency]);
                }
            }

            return(targetClassDefinition);
        }
        public void Ctor_CopiesPropertiesFromValidationLogData()
        {
            var log        = new DefaultValidationLog();
            var rule       = new DelegateValidationRule <TargetClassDefinition> (DummyRule);
            var definition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));

            log.ValidationStartsFor(definition);
            log.Succeed(rule);
            log.ValidationEndsFor(definition);

            var expected = log.GetData();
            var actual   = expected.MakeSerializable();

            Assert.That(actual.NumberOfFailures, Is.EqualTo(expected.GetNumberOfFailures()));
            Assert.That(actual.NumberOfRulesExecuted, Is.EqualTo(expected.GetNumberOfRulesExecuted()));
            Assert.That(actual.NumberOfSuccesses, Is.EqualTo(expected.GetNumberOfSuccesses()));
            Assert.That(actual.NumberOfUnexpectedExceptions, Is.EqualTo(expected.GetNumberOfUnexpectedExceptions()));
            Assert.That(actual.NumberOfWarnings, Is.EqualTo(expected.GetNumberOfWarnings()));
        }
        public void Serializable()
        {
            var log        = new DefaultValidationLog();
            var rule       = new DelegateValidationRule <TargetClassDefinition> (DummyRule);
            var definition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));

            log.ValidationStartsFor(definition);
            log.Succeed(rule);
            log.ValidationEndsFor(definition);

            var serializableValidationLogData = log.GetData().MakeSerializable();

            var deserialized = Serializer.SerializeAndDeserialize(serializableValidationLogData);

            Assert.That(deserialized.NumberOfFailures, Is.EqualTo(serializableValidationLogData.NumberOfFailures));
            Assert.That(deserialized.NumberOfRulesExecuted, Is.EqualTo(serializableValidationLogData.NumberOfRulesExecuted));
            Assert.That(deserialized.NumberOfSuccesses, Is.EqualTo(serializableValidationLogData.NumberOfSuccesses));
            Assert.That(deserialized.NumberOfUnexpectedExceptions, Is.EqualTo(serializableValidationLogData.NumberOfUnexpectedExceptions));
            Assert.That(deserialized.NumberOfWarnings, Is.EqualTo(serializableValidationLogData.NumberOfWarnings));
        }
        public void CreateRequiredMethodDefinitions_WithOverloads()
        {
            var targetClassDefinition = DefinitionObjectMother.CreateTargetClassDefinition(typeof(ClassImplementingInterfaceWithDuckTypingWithOverloads));

            DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTypingWithOverloads).GetMethod("Method1", new[] { typeof(string) }));
            var m1b = DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTypingWithOverloads).GetMethod("Method1", Type.EmptyTypes));

            DefinitionObjectMother.CreateMethodDefinition(
                targetClassDefinition,
                typeof(ClassImplementingInterfaceWithDuckTypingWithOverloads).GetMethod("Method2"));

            var requirement = DefinitionObjectMother.CreateRequiredTargetCallTypeDefinition(targetClassDefinition, typeof(IInterface));
            var builder     = new DuckTypingRequiredMethodDefinitionCollector(targetClassDefinition);

            var definitions = builder.CreateRequiredMethodDefinitions(requirement).OrderBy(def => def.FullName).ToArray();

            Assert.That(definitions[0].DeclaringRequirement, Is.SameAs(requirement));
            Assert.That(definitions[0].InterfaceMethod, Is.EqualTo(typeof(IInterface).GetMethod("Method1")));
            Assert.That(definitions[0].ImplementingMethod, Is.SameAs(m1b));
        }
 public void SetUp()
 {
     _parentDefinition       = DefinitionObjectMother.CreateTargetClassDefinition(typeof(object));
     _nestedDefinition       = DefinitionObjectMother.CreateMixinDefinition(_parentDefinition, typeof(string));
     _nestedNestedDefinition = DefinitionObjectMother.CreateMethodDefinition(_nestedDefinition, ReflectionObjectMother.GetSomeMethod());
 }