public void Visit_GenericTypeDefinitionsAndArgumentsIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); var type2 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS2", MemberVisibility.Public, TypeMemberKind.Class, "ClassTwo"); var type3 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS3", MemberVisibility.Public, TypeMemberKind.Class, "ClassThree"); type1.Members.Add(new PropertyMember( type1, MemberVisibility.Public, MemberModifier.None, ((TypeMember)typeof(IList <>)).MakeGenericType(type2), "Twos")); type1.Members.Add(new PropertyMember( type1, MemberVisibility.Public, MemberModifier.Static, ((TypeMember)typeof(IDictionary <,>)).MakeGenericType(type2, type3), "ThreeByTwo")); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(IList <>), typeof(IDictionary <,>), type2, type3 }); }
public void Visit_MethodSignatureTypesIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); type1.Members.Add(new MethodMember(MemberVisibility.Public, MemberModifier.None, "M1", new MethodSignature( new[] { new MethodParameter("x", 1, typeof(int)), new MethodParameter("y", 2, typeof(string)) }, returnValue: null, isAsync: false ))); type1.Members.Add(new MethodMember(MemberVisibility.Public, MemberModifier.Static, "M2", new MethodSignature( new MethodParameter[0], returnValue: new MethodParameter(null, -1, typeof(TimeSpan)), isAsync: false ))); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(int), typeof(string), typeof(TimeSpan) }); }
public void Visit_ConstructorBodyStatementsIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); var constructor1 = new ConstructorMember(MemberVisibility.Public, MemberModifier.None, "ClassOne", new MethodSignature()); var variable1 = new LocalVariable { Name = "x", Type = typeof(TimeSpan) }; constructor1.Body = new BlockStatement( new VariableDeclarationStatement { Variable = variable1 } ); type1.Members.Add(constructor1); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(TimeSpan) }); }
public void Visit_EventDelegateTypesInluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); var type2 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS2", MemberVisibility.Public, TypeMemberKind.Class, "ClassTwo"); type1.Members.Add(new EventMember( MemberVisibility.Public, MemberModifier.None, typeof(Action <DateTime>), "E1")); type1.Members.Add(new EventMember( MemberVisibility.Public, MemberModifier.Static, ((TypeMember)typeof(Action <>)).MakeGenericType(type2), "E2")); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(Action <>), typeof(DateTime), type2 }); }
public void Visit_ConstructorDelegationCallIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); var type2 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS2", MemberVisibility.Public, TypeMemberKind.Class, "ClassTwo"); MethodCallExpression baseConstructorCall; var constructor1 = new ConstructorMember(MemberVisibility.Public, MemberModifier.None, "ClassOne", new MethodSignature()) { CallBaseConstructor = baseConstructorCall = new MethodCallExpression() }; baseConstructorCall.Arguments.Add(new Argument { Expression = new NewObjectExpression { Type = type2 } }); MethodCallExpression thisConstructorCall; var constructor2 = new ConstructorMember(MemberVisibility.Public, MemberModifier.None, "ClassOne", new MethodSignature()) { CallThisConstructor = thisConstructorCall = new MethodCallExpression() }; thisConstructorCall.Arguments.Add(new Argument { Expression = new NewObjectExpression { Type = typeof(System.IO.MemoryStream) } }); type1.Members.Add(constructor1); type1.Members.Add(constructor2); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, type2, typeof(System.IO.MemoryStream) }); }
public void Visit_PropertyTypesIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); type1.Members.Add(new PropertyMember(type1, MemberVisibility.Public, MemberModifier.None, typeof(TimeSpan), "Duration")); type1.Members.Add(new PropertyMember(type1, MemberVisibility.Public, MemberModifier.Static, typeof(DateTime), "Timestamp")); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(TimeSpan), typeof(DateTime) }); }
public void Visit_FieldTypesIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); type1.Members.Add(new FieldMember(type1, MemberVisibility.Private, MemberModifier.None, typeof(int), "_number")); type1.Members.Add(new FieldMember(type1, MemberVisibility.Private, MemberModifier.Static, typeof(string), "_text")); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(int), typeof(string) }); }
public void Visit_BaseTypesIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); var type2 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS2", MemberVisibility.Public, TypeMemberKind.Class, "ClassTwo*"); type1.BaseType = typeof(IDisposable); type1.Interfaces.Add(((TypeMember)typeof(IList <>)).MakeGenericType(type2)); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(IDisposable), typeof(IList <>), type2 }); }
public void Visit_AttributeTypesIncluded() { //-- arrange var type1 = new TypeMember(new TypeGeneratorInfo(this.GetType()), "NS1", MemberVisibility.Public, TypeMemberKind.Class, "ClassOne"); type1.Attributes.Add(new AttributeDescription() { AttributeType = typeof(System.ComponentModel.DefaultPropertyAttribute) }); var foundTypes = new HashSet <TypeMember>(); var visitorUnderTest = new TypeReferenceMemberVisitor(foundTypes); //-- act type1.AcceptVisitor(visitorUnderTest); //-- assert foundTypes.Should().BeEquivalentTo(new TypeMember[] { type1, typeof(System.ComponentModel.DefaultPropertyAttribute) }); }
public void CanVisitTypeMembers() { //-- arrange var class1 = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "Class1"); var field1 = new FieldMember(class1, MemberVisibility.Private, MemberModifier.None, typeof(int), "_field1"); var field2 = new FieldMember(class1, MemberVisibility.Private, MemberModifier.None, typeof(int), "_field2"); var constructor1 = new ConstructorMember(MemberVisibility.Public, MemberModifier.None, "Class1", new MethodSignature()); var constructor2 = new ConstructorMember(MemberVisibility.Public, MemberModifier.Static, "Class1", new MethodSignature()); var method1 = new MethodMember(MemberVisibility.Public, MemberModifier.None, "M1", new MethodSignature()); var method2 = new MethodMember(MemberVisibility.Public, MemberModifier.None, "M2", new MethodSignature()); var property1 = new PropertyMember(class1, MemberVisibility.Public, MemberModifier.None, typeof(int), "P1"); var property2 = new PropertyMember(class1, MemberVisibility.Public, MemberModifier.None, typeof(int), "P2"); var event1 = new EventMember(MemberVisibility.Public, MemberModifier.None, typeof(Action), "E1"); var event2 = new EventMember(MemberVisibility.Public, MemberModifier.None, typeof(Action), "E2"); class1.Members.AddRange(new AbstractMember[] { field1, field2, constructor1, constructor2, method1, method2, property1, property2, event1, event2 }); property2.Getter = new MethodMember(property2.Visibility, "get_" + property2.Name); property2.Setter = new MethodMember(property2.Visibility, "set_" + property2.Name); event2.Adder = new MethodMember(property2.Visibility, "add_" + event2.Name); event2.Remover = new MethodMember(property2.Visibility, "remove_" + event2.Name); var visitLog = new List <Visit>(); var visitor = new TestMemberVisitor(visitLog); //-- act class1.AcceptVisitor(visitor); //-- assert visitLog.Should().Equal( new Visit(nameof(MemberVisitor.VisitAbstractMember), class1), new Visit(nameof(MemberVisitor.VisitTypeMember), class1), new Visit(nameof(MemberVisitor.VisitClassType), class1), new Visit(nameof(MemberVisitor.VisitAbstractMember), field1), new Visit(nameof(MemberVisitor.VisitField), field1), new Visit(nameof(MemberVisitor.VisitAbstractMember), field2), new Visit(nameof(MemberVisitor.VisitField), field2), new Visit(nameof(MemberVisitor.VisitAbstractMember), constructor1), new Visit(nameof(MemberVisitor.VisitMethodBase), constructor1), new Visit(nameof(MemberVisitor.VisitConstructor), constructor1), new Visit(nameof(MemberVisitor.VisitAbstractMember), constructor2), new Visit(nameof(MemberVisitor.VisitMethodBase), constructor2), new Visit(nameof(MemberVisitor.VisitConstructor), constructor2), new Visit(nameof(MemberVisitor.VisitAbstractMember), method1), new Visit(nameof(MemberVisitor.VisitMethodBase), method1), new Visit(nameof(MemberVisitor.VisitMethod), method1), new Visit(nameof(MemberVisitor.VisitAbstractMember), method2), new Visit(nameof(MemberVisitor.VisitMethodBase), method2), new Visit(nameof(MemberVisitor.VisitMethod), method2), new Visit(nameof(MemberVisitor.VisitAbstractMember), property1), new Visit(nameof(MemberVisitor.VisitProperty), property1), new Visit(nameof(MemberVisitor.VisitAbstractMember), property2), new Visit(nameof(MemberVisitor.VisitProperty), property2), new Visit(nameof(MemberVisitor.VisitAbstractMember), property2.Getter), new Visit(nameof(MemberVisitor.VisitMethodBase), property2.Getter), new Visit(nameof(MemberVisitor.VisitMethod), property2.Getter), new Visit(nameof(MemberVisitor.VisitAbstractMember), property2.Setter), new Visit(nameof(MemberVisitor.VisitMethodBase), property2.Setter), new Visit(nameof(MemberVisitor.VisitMethod), property2.Setter), new Visit(nameof(MemberVisitor.VisitAbstractMember), event1), new Visit(nameof(MemberVisitor.VisitEvent), event1), new Visit(nameof(MemberVisitor.VisitAbstractMember), event2), new Visit(nameof(MemberVisitor.VisitEvent), event2), new Visit(nameof(MemberVisitor.VisitAbstractMember), event2.Adder), new Visit(nameof(MemberVisitor.VisitMethodBase), event2.Adder), new Visit(nameof(MemberVisitor.VisitMethod), event2.Adder), new Visit(nameof(MemberVisitor.VisitAbstractMember), event2.Remover), new Visit(nameof(MemberVisitor.VisitMethodBase), event2.Remover), new Visit(nameof(MemberVisitor.VisitMethod), event2.Remover) ); }
public void CanVisitAppliedAttributes() { //-- arrange var classAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A1") }; var fieldAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A2") }; var constructorAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A3") }; var constructorParamAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A3B") }; var methodAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A4") }; var methodParamAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A4B") }; var methodRetValAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A4C") }; var propertyAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A5") }; var propertyGetterAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A5") }; var propertySetterAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A5") }; var eventAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A6") }; var eventAdderAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A6B") }; var eventRemoverAttribute1 = new AttributeDescription() { AttributeType = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "A6C") }; var class1 = new TypeMember(MemberVisibility.Public, TypeMemberKind.Class, "Class1"); class1.Attributes.Add(classAttribute1); #region Build class type members var field1 = new FieldMember(class1, MemberVisibility.Private, MemberModifier.None, typeof(int), "_field1"); field1.Attributes.Add(fieldAttribute1); var constructor1 = new ConstructorMember(MemberVisibility.Public, MemberModifier.None, "Class1", new MethodSignature( new[] { new MethodParameter("n", 1, typeof(int), MethodParameterModifier.None, constructorParamAttribute1) }, returnValue: null, isAsync: false )); constructor1.Attributes.Add(constructorAttribute1); var method1 = new MethodMember(MemberVisibility.Public, MemberModifier.None, "M1", new MethodSignature( new[] { new MethodParameter("n", 1, typeof(int), MethodParameterModifier.None, methodParamAttribute1) }, returnValue: new MethodParameter(null, -1, typeof(string), MethodParameterModifier.None, methodRetValAttribute1), isAsync: false )); method1.Attributes.Add(methodAttribute1); var property1 = new PropertyMember(class1, MemberVisibility.Public, MemberModifier.None, typeof(int), "P1"); property1.Getter = new MethodMember(property1.Visibility, "get_" + property1.Name); property1.Setter = new MethodMember(property1.Visibility, "set_" + property1.Name); property1.Attributes.Add(propertyAttribute1); property1.Getter.Attributes.Add(propertyGetterAttribute1); property1.Setter.Attributes.Add(propertySetterAttribute1); var event1 = new EventMember(MemberVisibility.Public, MemberModifier.None, typeof(Action), "E1"); event1.Adder = new MethodMember(event1.Visibility, "add_" + event1.Name); event1.Remover = new MethodMember(event1.Visibility, "remove_" + event1.Name); event1.Attributes.Add(eventAttribute1); event1.Adder.Attributes.Add(eventAdderAttribute1); event1.Remover.Attributes.Add(eventRemoverAttribute1); #endregion class1.Members.AddRange(new AbstractMember[] { field1, constructor1, method1, property1, event1 }); var visitLog = new List <Visit>(); var visitor = new TestMemberVisitor(visitLog); //-- act class1.AcceptVisitor(visitor); //-- assert visitLog.Should().ContainInOrder( new Visit(nameof(MemberVisitor.VisitAttribute), classAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), fieldAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), constructorAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), constructorParamAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), methodAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), methodParamAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), methodRetValAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), propertyAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), propertyGetterAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), propertySetterAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), eventAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), eventAdderAttribute1), new Visit(nameof(MemberVisitor.VisitAttribute), eventRemoverAttribute1) ); }