public void EmptyTest()
 {
     // unit test for Empty list.
     testObject = AssignmentInfoCollection.Empty();
     Assert.AreEqual(0, testObject.Count);
     Assert.IsEmpty(testObject);
 }
Beispiel #2
0
 public void EmptyTest()
 {
     // unit test for Empty list.
     testObject = AssignmentInfoCollection.Empty();
     Assert.AreEqual(0, testObject.Count);
     Assert.IsEmpty(testObject);
 }
Beispiel #3
0
        /// <summary>
        /// Builds a lambda throw assertion arround a CodeObjectCreateExpression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="objcreator">The constructor of the objcreator.</param>
        /// <param name="assignmentInfos">The assignment information about common known parameters(OuT specific).</param>
        /// <param name="createExpr">The constructor create expression of the object under tst.</param>
        /// <param name="codeMemberMethod">The code member method definition of the current test method.</param>
        private void CreateNullParameterAssertions(
            IMemberBuildContext context,
            ITestObjectComposer objcreator,
            AssignmentInfoCollection assignmentInfos,
            CodeObjectCreateExpression createExpr,
            CodeMemberMethod codeMemberMethod)
        {
            codeMemberMethod.AddBlankLine();

            for (int i = 0; i < createExpr.Parameters.Count; i++)
            {
                var            cref              = (CodeFieldReferenceExpression)createExpr.Parameters[i];
                var            paraName          = cref.FieldName;
                var            inf               = assignmentInfos[paraName];
                var            paraType          = inf.MemberField.Type.BaseType;
                CodeExpression paraAssert        = new CodePrimitiveExpression(null);
                var            expectedException = typeof(ArgumentNullException);
                CreateAssertThrowWithExceptionType(context, objcreator, assignmentInfos, codeMemberMethod, i, paraAssert, expectedException);
                if (paraType == typeof(string).FullName)
                {
                    expectedException = typeof(ArgumentException);
                    //paraAssert = StaticClass<string>.Property("Empty");
                    paraAssert = StaticClass.Property(() => string.Empty);
                    CreateAssertThrowWithExceptionType(context, objcreator, assignmentInfos, codeMemberMethod, i, paraAssert, expectedException);
                }
            }
        }
        private static void CheckCtorAsgn <T>(AssignmentInfoCollection preffered, string name, bool hasCreationAssignments)
        {
            Type memberType = typeof(T);

            Assert.IsNotNull(preffered[name].AssignStatement);
            Assert.IsNotNull(preffered[name].CreateAssignments);
            Assert.AreEqual(hasCreationAssignments, preffered[name].HasCreationAssignments);
            Assert.IsNotNull(preffered[name].MemberField);
            Assert.AreEqual(name, preffered[name].MemberFieldName);
            Assert.AreEqual(memberType, preffered[name].MemberType);
            Assert.AreEqual(name, preffered[name].ParameterName);
        }
 public void ConstructTest()
 {
     this.testObject = new AssignmentInfoCollection();
     Assert.AreEqual(0, this.testObject.Count);
     Assert.IsEmpty(this.testObject);
 }
 public void TearDown()
 {
     this.testObject = null;
 }
 public void SetUp()
 {
     this.testObject = new AssignmentInfoCollection();
 }
Beispiel #8
0
        private void CreateAssertThrowWithExceptionType(IMemberBuildContext context, ITestObjectComposer objcreator, AssignmentInfoCollection assignmentInfos, CodeMemberMethod codeMemberMethod, int i, CodeExpression paraAssert, Type expectedException)
        {
            var subcreator = BuildTestObject(context.TestObjectType.Name, "notneeded", codeMemberMethod);

            // The upper method created a field assignment statement an initialized it with "= new <Object Under Test>()".
            // We only need the CreateExpression of it, so remove it from the statements of the Constructor test method.
            codeMemberMethod.Statements.RemoveAt(codeMemberMethod.Statements.Count - 1);

            // assign well known constructor arguments
            objcreator.AssignOnly(context.TestClassDeclaration, codeMemberMethod, subcreator, assignmentInfos);

            //var cref = (CodeFieldReferenceExpression)subcreator.Parameters[i];
            //var paraName = cref.FieldName;
            //var inf = assignmentInfos[paraName];
            //var paraType = inf.MemberField.Type.BaseType;


            // for each assert statement null a parameter of the constructor.
            if (subcreator.Parameters.Count > i)
            {
                subcreator.Parameters[i] = paraAssert;
            }
            var ancreate = BuildLambdaThrowAssertion(expectedException, codeMemberMethod, subcreator);
        }
Beispiel #9
0
 /// <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);
Beispiel #10
0
 public void ConstructTest()
 {
     this.testObject = new AssignmentInfoCollection();
     Assert.AreEqual(0, this.testObject.Count);
     Assert.IsEmpty(this.testObject);
 }
Beispiel #11
0
 public void TearDown()
 {
     this.testObject = null;
 }
Beispiel #12
0
 public void SetUp()
 {
     this.testObject = new AssignmentInfoCollection();
 }
Beispiel #13
0
        public void BuildWithAssignmentsNoMatchingParameters()
        {
            var method          = new CodeMemberMethod();
            var assCol1         = new AssignmentInfoCollection();
            var assCol2         = new AssignmentInfoCollection();
            var assignments     = new[] { assCol1, assCol2 };
            var memberField     = new CodeMemberField(typeof(int), "myField");
            var assignStatement = new CodeAssignStatement();
            var ctorAssignment  = new ConstructorAssignment("theParameter", assignStatement, memberField, typeof(string));

            assignments[0].AddAssignment(ctorAssignment);
            var helpctors = typeof(ConstructorBuilderHelpType).GetConstructors();

            assignments[0].UsedConstructor = helpctors[0];
            assignments[1].UsedConstructor = helpctors[0];

            var tobuilder = this.mocks.StrictMock <ITestObjectComposer>();

            Expect.Call(tobuilder.Assignments).Return(assignments);

            var stcontext = this.mocks.StrictMock <ISetupAndTearDownCreationContext>();

            Expect.Call(stcontext.TestObjectCreator).Return(tobuilder);

            Expect.Call(this.context.SetUpTearDownContext).Return(stcontext);//.Repeat.Any();
            Expect.Call(this.context.TestObjectType).Return(typeof(int)).Repeat.AtLeastOnce();
            var ctdecl = new CodeTypeDeclaration("TheTestClass");

            Expect.Call(this.context.TestClassDeclaration).Return(ctdecl).Repeat.AtLeastOnce();
            var buildResult = new MemberBuildResult();

            Expect.Call(this.context.BuildResult).Return(buildResult).Repeat.AtLeastOnce();

            Expect.Call(delegate { tobuilder.AssignExtra(ctdecl, method, null, assCol1); })
            //.Constraints(Is.Equal(ctdecl), Is.Equal(method), Is.Anything(), Is.Equal(assCol1)
            .Constraints(Is.Equal(ctdecl), Is.Anything(), Is.Anything(), Is.Equal(assCol1))
            .Do((Action <CodeTypeDeclaration, CodeMemberMethod, CodeObjectCreateExpression, AssignmentInfoCollection>)
                delegate(CodeTypeDeclaration testClassDeclaration,
                         CodeMemberMethod testMethod,
                         CodeObjectCreateExpression testObjectConstructor,
                         AssignmentInfoCollection ctorAssignments)
            {
                var a = testClassDeclaration;
                var b = testMethod;
                Assert.IsInstanceOfType <CustomConstructorCodeMemberMethod>(testMethod);
                Assert.AreEqual("ConstructWithParametersTheIntParameterTest", testMethod.Name);
                var c = testObjectConstructor;
                Assert.IsEmpty(testObjectConstructor.Parameters);
                Assert.AreEqual("Int32", testObjectConstructor.CreateType.BaseType);
                var d = ctorAssignment;

                var e = ctorAssignment;
            });
            Expect.Call(delegate { tobuilder.AssignExtra(ctdecl, method, null, assCol1); })
            //.Constraints(Is.Equal(ctdecl), Is.Equal(method), Is.Anything(), Is.Equal(assCol1)
            .Constraints(Is.Equal(ctdecl), Is.Anything(), Is.Anything(), Is.Equal(assCol2)
                         );
            mocks.ReplayAll();
            var expected = true;
            var actual   = testObject.Build(this.context);

            Assert.AreEqual(expected, actual);

            // there should be two methods (constructor testers) added.
            Assert.Count(2, buildResult.ClassMethodsToAdd);
            var bresList = buildResult.ClassMethodsToAdd.ToList();

            Assert.IsInstanceOfType <CustomConstructorCodeMemberMethod>(bresList[0]);
            Assert.IsInstanceOfType <CustomConstructorCodeMemberMethod>(bresList[1]);

            mocks.VerifyAll();
        }
Beispiel #14
0
        public void BuildWithAssignmentsNoMatchingParameters()
        {
            var method = new CodeMemberMethod();
            var assCol1 = new AssignmentInfoCollection();
            var assCol2 = new AssignmentInfoCollection();
            var assignments = new[] { assCol1, assCol2 };
            var memberField = new CodeMemberField(typeof(int), "myField");
            var assignStatement = new CodeAssignStatement();
            var ctorAssignment = new ConstructorAssignment("theParameter", assignStatement, memberField, typeof(string));
            assignments[0].AddAssignment(ctorAssignment);
            var helpctors = typeof(ConstructorBuilderHelpType).GetConstructors();
            assignments[0].UsedConstructor = helpctors[0];
            assignments[1].UsedConstructor = helpctors[0];

            var tobuilder = this.mocks.StrictMock<ITestObjectComposer>();
            Expect.Call(tobuilder.Assignments).Return(assignments);

            var stcontext = this.mocks.StrictMock<ISetupAndTearDownCreationContext>();
            Expect.Call(stcontext.TestObjectCreator).Return(tobuilder);

            Expect.Call(this.context.SetUpTearDownContext).Return(stcontext);//.Repeat.Any();
            Expect.Call(this.context.TestObjectType).Return(typeof(int)).Repeat.AtLeastOnce();
            var ctdecl = new CodeTypeDeclaration("TheTestClass");
            Expect.Call(this.context.TestClassDeclaration).Return(ctdecl).Repeat.AtLeastOnce();
            var buildResult = new MemberBuildResult();
            Expect.Call(this.context.BuildResult).Return(buildResult).Repeat.AtLeastOnce();

            Expect.Call(delegate { tobuilder.AssignExtra(ctdecl, method, null, assCol1); })
                //.Constraints(Is.Equal(ctdecl), Is.Equal(method), Is.Anything(), Is.Equal(assCol1)
                .Constraints(Is.Equal(ctdecl), Is.Anything(), Is.Anything(), Is.Equal(assCol1))
                .Do((Action<CodeTypeDeclaration,CodeMemberMethod,CodeObjectCreateExpression ,AssignmentInfoCollection>)
                delegate(CodeTypeDeclaration testClassDeclaration,
            CodeMemberMethod testMethod,
            CodeObjectCreateExpression testObjectConstructor,
            AssignmentInfoCollection ctorAssignments)
            {
                var a = testClassDeclaration;
                var b = testMethod;
                Assert.IsInstanceOfType<CustomConstructorCodeMemberMethod>(testMethod);
                Assert.AreEqual("ConstructWithParametersTheIntParameterTest", testMethod.Name);
                var c = testObjectConstructor;
                Assert.IsEmpty(testObjectConstructor.Parameters);
                Assert.AreEqual("Int32", testObjectConstructor.CreateType.BaseType);
                var d = ctorAssignment;

                var e = ctorAssignment;
            });
            Expect.Call(delegate { tobuilder.AssignExtra(ctdecl, method, null, assCol1); })
                //.Constraints(Is.Equal(ctdecl), Is.Equal(method), Is.Anything(), Is.Equal(assCol1)
                .Constraints(Is.Equal(ctdecl), Is.Anything(), Is.Anything(), Is.Equal(assCol2)
                );
            mocks.ReplayAll();
            var expected = true;
            var actual = testObject.Build(this.context);
            Assert.AreEqual(expected, actual);

            // there should be two methods (constructor testers) added.
            Assert.Count(2, buildResult.ClassMethodsToAdd);
            var bresList = buildResult.ClassMethodsToAdd.ToList();
            Assert.IsInstanceOfType<CustomConstructorCodeMemberMethod>(bresList[0]);
            Assert.IsInstanceOfType<CustomConstructorCodeMemberMethod>(bresList[1]);

            mocks.VerifyAll();
        }