public void AddConstructorAssignmentTest() { var expected = 1; var actual = testObject.Count; Assert.AreEqual(expected, actual); var asgn = new NStub.CSharp.ObjectGeneration.AssignmentInfoCollection(); testObject.AddConstructorAssignment(asgn); expected = 2; actual = testObject.Count; Assert.AreEqual(expected, actual); // add the same again. testObject.AddConstructorAssignment(asgn); expected = 3; actual = testObject.Count; Assert.AreEqual(expected, actual); // add a range of assignments. testObject.AddConstructorAssignment(new[] { asgn, asgn, asgn, asgn }); expected = 7; actual = testObject.Count; Assert.AreEqual(expected, actual); }
public void ConstructWithParametersPreferredConstructorTest() { this.preferredConstructor = new NStub.CSharp.ObjectGeneration.AssignmentInfoCollection(); this.testObject = new ConstructorAssignmentCollection(this.preferredConstructor); Assert.Throws<ArgumentNullException>(() => new ConstructorAssignmentCollection(null)); }
public void ConstructWithParametersPreferredConstructorTest() { this.preferredConstructor = new NStub.CSharp.ObjectGeneration.AssignmentInfoCollection(); this.testObject = new ConstructorAssignmentCollection(this.preferredConstructor); Assert.Throws <ArgumentNullException>(() => new ConstructorAssignmentCollection(null)); }
/// <summary> /// Assigns the parameters detected with <see cref="BuildTestObject"/> to an explicitly specified constructor /// create expression to a specified method. /// </summary> /// <param name="testClassDeclaration">The test class declaration.</param> /// <param name="testMethod">The test method, to add the assign-statements to.</param> /// <param name="testObjectConstructor">The object constructor to create the parameter initializers for.</param> /// <param name="ctorAssignments">The list of constructor assignments that specify the parameter to add.</param> public override void AssignOnly( CodeTypeDeclaration testClassDeclaration, CodeMemberMethod testMethod, CodeObjectCreateExpression testObjectConstructor, AssignmentInfoCollection ctorAssignments) { Guard.NotNull(() => testClassDeclaration, testClassDeclaration); Guard.NotNull(() => testObjectConstructor, testObjectConstructor); // Guard.NotNull(() => this.assignments, this.assignments); if (testClassDeclaration.Name.StartsWith("Jedzia.SamCat.Model.Tasks.TaskComposer")) { } if (this.HasParameterAssignments) { var testObjectInitializerPosition = testMethod.Statements.Count - 1; var ctorparameters = ctorAssignments.UsedConstructor.GetParameters(); if (ctorparameters.Length > 1) { } foreach (var para in ctorparameters) { var assignment = ctorAssignments[para.Name]; if (assignment == null) { continue; } // Add the member field to the test class. /*if (false) * { * testClassDeclaration.Members.Add(assignment.MemberField); * * // this.BuildData.AddDataItem("Setup", assignment.MemberField.Name, new BuilderData<CodeMemberField>(assignment.MemberField)); * // BuildData.AddDataItem( * // "Assignments.Extra." + testMethod.Name + "." + testClassDeclaration.Name, assignment.MemberField.Name, new BuilderData<CodeMemberField>(assignment.MemberField)); * * // Add a local variable for the constructor parameter. * testMethod.Statements.Add(assignment.AssignStatement); * }*/ // Add the local variable to the constructor initializer in the object create expression // (e.g. SetUp method, test object constructor) of the specified method. testObjectConstructor.Parameters.Add(assignment.AssignStatement.Left); } // reorder the testObject initializer to the bottom of the SetUp method. this.ReorderSetupStatement(testMethod, testObjectInitializerPosition); } }
/// <summary> /// Assigns the parameters detected with <see cref="BuildTestObject"/> to an explicitly specified constructor /// create expression without initializing extra fields like in <see cref="AssignExtra"/>. /// </summary> /// <param name="testClassDeclaration">The test class declaration.</param> /// <param name="testMethod">The test method, to add the assign-statements to.</param> /// <param name="testObjectConstructor">The object constructor to create the parameter initializers for.</param> /// <param name="ctorAssignments">The list of constructor assignments that specify the parameter to add.</param> public abstract void AssignOnly( CodeTypeDeclaration testClassDeclaration, CodeMemberMethod testMethod, CodeObjectCreateExpression testObjectConstructor, AssignmentInfoCollection ctorAssignments);
public void SetUp() { this.preferredConstructor = new NStub.CSharp.ObjectGeneration.AssignmentInfoCollection(); this.testObject = new ConstructorAssignmentCollection(this.preferredConstructor); }
/// <summary> /// Gets a new <see cref="AssignmentInfoCollection"/> that is empty. /// </summary> /// <returns>a new <see cref="AssignmentInfoCollection"/> that is empty.</returns> public static AssignmentInfoCollection Empty() { var returnValue = new AssignmentInfoCollection(); return(returnValue); }
/// <summary> /// Builds a assignment info for the specified constructor. /// </summary> /// <param name="bindingAttr">A bit mask comprised of one or more <see cref="BindingFlags"/> /// that specify how the search is conducted. -or- Zero, to return <c>null</c>.</param> /// <param name="constructor">The constructor to build the parameter assignment info's for.</param> /// <returns> /// An <see cref="AssignmentInfoCollection"/> initialized with the data from the specified /// <paramref name="constructor"/>. /// </returns> private AssignmentInfoCollection BuildAssignmentInfoForConstructor( BindingFlags bindingAttr, ConstructorInfo constructor) { // var ctorParameterTypesInterface = new List<ParameterInfo>(); // var ctorParameterTypesStd = new List<ParameterInfo>(); var ctorParameterTypes = new ParameterInfoCollection(); var ctorParameters = constructor.GetParameters(); foreach (var para in ctorParameters) { { // if (!para.ParameterType.IsGenericType) ctorParameterTypes.AddParameterInfo(para); /*if (para.ParameterType.IsInterface) * { * hasInterfaceInCtorParameters = true; * ctorParameterTypesInterface.Add(para); * } * else * { * ctorParameterTypesStd.Add(para); * }*/ } } // BuildAssignmentInfoForConstructor var assignmentInfoCollection = new AssignmentInfoCollection { UsedConstructor = constructor }; var addTypesFrom = ctorParameterTypes; /*var addTypesFrom = ctorParameterTypes.StandardTypes; * * // Todo: do not use bindingAttr here, use the MemberVisibility enumeration * if ((bindingAttr & BindingFlags.NonPublic) == BindingFlags.NonPublic) * { * addTypesFrom = ctorParameterTypes; * }*/ foreach (var paraInfo in addTypesFrom) { if (!paraInfo.ParameterType.IsGenericType) { var memberField = CreateMemberField(paraInfo.ParameterType.FullName, paraInfo.Name); // var fieldAssignment = CodeMethodComposer.CreateAndInitializeMemberField( // paraInfo.ParameterType, paraInfo.Name); // var assignment = new ConstructorAssignment( // paraInfo.Name, fieldAssignment, memberField, paraInfo.ParameterType); var fieldAssignment = CodeMethodComposer.CreateAndInitializeMemberField( paraInfo.ParameterType, memberField.Name); var assignment = new ConstructorAssignment( memberField.Name, fieldAssignment, memberField, paraInfo.ParameterType); assignmentInfoCollection.AddAssignment(assignment); } else { var genericTypeDefinition = paraInfo.ParameterType.GetGenericTypeDefinition(); if (TestObjectMemberField.Type.BaseType == "DefaultMemberBuilderFactory") { } if (CheckForItemizable(paraInfo, genericTypeDefinition)) { var genArgs = paraInfo.ParameterType.GetGenericArguments(); if (genArgs.Length == 1) { var memberFieldName = paraInfo.Name + "Item"; var memberField = CreateMemberField(genArgs[0].FullName, memberFieldName); var fieldAssignment = CodeMethodComposer.CreateAndInitializeMemberField( genArgs[0], memberField.Name); var assignment = new ConstructorAssignment( memberField.Name, fieldAssignment, memberField, genArgs[0]); // assignmentInfoCollection.AddAssignment(assignment); // AddAssignStatement(fieldAssignment); var collectionFieldName = paraInfo.Name; var collectionField = CreateMemberField(paraInfo.ParameterType.FullName, collectionFieldName); var collectionAssignment = CodeMethodComposer.CreateAndInitializeCollectionField( /*paraInfo.ParameterType,*/ collectionFieldName, memberFieldName); var collection = new ConstructorAssignment( collectionFieldName, collectionAssignment, collectionField, paraInfo.ParameterType); collection.CreateAssignments.Add(assignment); assignmentInfoCollection.AddAssignment(collection); } } if (typeof(IEnumerable).IsAssignableFrom(paraInfo.ParameterType)) { } if (paraInfo.ParameterType.IsAssignableFrom(typeof(IEnumerable <>))) { } } } return(assignmentInfoCollection); }
/// <summary> /// Add the parameter assignments to the specified constructor expression. /// </summary> /// <param name="visibility">A bit mask comprised of one or more <see cref="BindingFlags"/> /// that specify how the search of member types is conducted. -or- Zero, to return <c>null</c>.</param> /// <returns> /// A structure with data about parameter initialization for the type of test object of this instance. /// </returns> /// <exception cref="InvalidOperationException">No preferred constructor found, but there should one.</exception> private ConstructorAssignmentCollection AddParametersToConstructor(MemberVisibility visibility) { var bindingAttr = BindingFlags.Instance; var noPrivate = true; switch (visibility) { case MemberVisibility.Public: bindingAttr |= BindingFlags.Public; break; case MemberVisibility.Internal: bindingAttr |= BindingFlags.Public | BindingFlags.NonPublic; break; case MemberVisibility.Private: bindingAttr |= BindingFlags.Public | BindingFlags.NonPublic; noPrivate = false; break; default: break; } /*if ((visibility & MemberVisibility.Public) == MemberVisibility.Public) * { * bindingAttr |= BindingFlags.Public; * } * if ((visibility & MemberVisibility.Internal) == MemberVisibility.Internal) * { * bindingAttr |= BindingFlags.NonPublic; * } * if ((visibility & MemberVisibility.Private) == MemberVisibility.Private) * { * bindingAttr |= BindingFlags.NonPublic; * noPrivate = false; * }*/ var testObjectConstructors = TestObjectType.GetConstructors(bindingAttr); // var ctorParameterTypes = new List<ParameterInfo>(); if (testObjectConstructors.Length < 1) { return(null); } // for now, use to ctor with the most parameters. // foreach testObjectConstructors // var most = testObjectConstructors.Max(e => e.GetParameters().Length); // var constructor = testObjectConstructors.Where(e => e.GetParameters().Length == most).First(); var tempAssignments = new List <AssignmentInfoCollection>(); int most = -1; AssignmentInfoCollection mostAssignmentInfoCollection = null; foreach (var constructor in testObjectConstructors) { if (noPrivate && constructor.IsPrivate) { continue; } var assignmentInfoCollection = this.BuildAssignmentInfoForConstructor(bindingAttr, constructor); int parameterAmount = constructor.GetParameters().Length; if (parameterAmount > most) { if (mostAssignmentInfoCollection != null) { // here comes a new bigger one. put the old to the temp list. tempAssignments.Add(mostAssignmentInfoCollection); } most = parameterAmount; mostAssignmentInfoCollection = assignmentInfoCollection; // tempAssignments.Add(assignmentInfoCollection); } else { tempAssignments.Add(assignmentInfoCollection); } } if (mostAssignmentInfoCollection == null) { throw new InvalidOperationException("No preferred constructor found, but there should one."); } var result = new ConstructorAssignmentCollection(mostAssignmentInfoCollection); result.AddConstructorAssignment(tempAssignments); return(result); }