Пример #1
0
 public void TearDown()
 {
     this.handler      = null;
     this.buildContext = null;
     this.testObject   = null;
     this.mocks        = null;
 }
Пример #2
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);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            var typeMember     = context.TypeMember;
            var typeMemberName = typeMember.Name;
            var propertyName   = typeMemberName;

            // var propertyName = typeMemberName.Replace("get_", string.Empty).Replace("set_", string.Empty);
            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "XX_Norm_XX");
            var storageCategory = string.Format(BuilderConstants.PropertyStorageCategory, context.TestClassDeclaration.Name);
            var propertyData    = context.GetBuilderData(storageCategory);

            if (context.TestClassDeclaration.Name == "VertexDataTest")
            {
                if (propertyName.Contains("PropertyItemNormalBehavior"))
                {
                }
            }

            // var testName = DetermineTestName(context);
            // hmm Generate to generate new and compute to process existing !?!
            // var testObjectName = context.TestObjectName;
            const string TestObjectName = "testObject";

            this.ComputeCodeMemberProperty(typeMember as CodeMemberMethod, propertyData, TestObjectName, propertyName);

            return(true);
        }
Пример #4
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            return true;

            // return !context.IsConstructor && !context.IsProperty && !context.IsEvent && context.MemberInfo != null &&
            // !context.MemberInfo.IsStatic;
        }
Пример #5
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            // context.
            return context.IsProperty;

            // return context.TypeMember.Name.StartsWith("get_") || context.TypeMember.Name.StartsWith("set_");
        }
Пример #6
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            return(context.IsEvent);


            // return context.TypeMember.Name.StartsWith("add_") || context.TypeMember.Name.StartsWith("remove_");
        }
Пример #7
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            return(true);

            // return !context.IsConstructor && !context.IsProperty && !context.IsEvent && context.MemberInfo != null &&
            // !context.MemberInfo.IsStatic;
        }
Пример #8
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            // context.
            return(context.IsProperty);

            // return context.TypeMember.Name.StartsWith("get_") || context.TypeMember.Name.StartsWith("set_");
        }
Пример #9
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            var typeMember = context.TypeMember;
            var eventName  = typeMember.Name;

            this.ComputeCodeMemberEvent(typeMember as CodeMemberMethod, eventName);
            return(true);
        }
Пример #10
0
        /// <summary>
        /// Builds the test method member with the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            var typeMember = context.TypeMember as CodeMemberMethod;
            typeMember.AddComment("From RenamingBuilder {" + Parameters.Id + "}");
            typeMember.AddBlankLine();

            // context.GetBuilderData
            return true;
        }
Пример #11
0
        /// <summary>
        /// Builds the test method member with the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            var typeMember = context.TypeMember as CodeMemberMethod;

            typeMember.AddComment("From RenamingBuilder {" + Parameters.Id + "}");
            typeMember.AddBlankLine();

            // context.GetBuilderData
            return(true);
        }
Пример #12
0
        public void SetUp()
        {
            this.mocks = new MockRepository();

            this.buildContext  = this.mocks.StrictMock <IMemberBuildContext>();
            this.builder       = this.mocks.StrictMock <IMemberBuilder>();
            this.type          = this.builder.GetType();
            this.handlerReturn = false;
            handler            = (e) => { return(this.handlerReturn); };
            this.testObject    = new BuildHandler(this.type, handler, typeof(EmptyBuildParameters));
        }
Пример #13
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException"><c>context</c> is out of range.</exception>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            /*
             * // Todo: just placeholders. Use this in the TestConstructor builder.
             * var typeMember = context.TypeMember as CodeMemberMethod;
             * var testClass = context.TestClassDeclaration.Name;
             * var test = context.TestKey;
             * var suMethod = context.SetUpTearDownContext.SetUpMethod;
             * var setupData = context.BuildData["Setup"];
             * if (setupData != null)
             * {
             *  IBuilderData bdata;
             *  var found = setupData.TryGetValue("bla", out bdata);
             *  foreach (var item in setupData)
             *  {
             *      var testField = item.Key;
             *      var testDaa = item.Value.GetData() as CodeMemberField;
             *      typeMember.Statements.Add(new CodeSnippetStatement(""));
             *      typeMember.Statements.Add(new CodeCommentStatement("... have a this." + testField + " link."));
             *  }
             * }
             * else
             * {
             *
             * }*/
            Guard.NotNull(() => context, context);
            var typeMember = context.TypeMember;

            if (!(typeMember is CodeMemberMethod))
            {
                throw new ArgumentOutOfRangeException(
                          "context", "The supplied build context is not for a CodeMemberMethod type.");
            }

            var typeMemberName = typeMember.Name;
            var propertyName   = typeMemberName;

            // var propertyName = typeMemberName.Replace("get_", string.Empty).Replace("set_", string.Empty);
            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "XX_Norm_XX");
            var storageCategory = string.Format(BuilderConstants.PropertyStorageCategory, context.TestClassDeclaration.Name);
            var propertyData    = context.GetBuilderData(storageCategory);

            //var userData = context.BuildData.General[this.GetType().FullName] as PropertyBuilderUserParameters;
            var userData = context.GetBuilderData <BuildParametersOfPropertyBuilder>(this);

            if (userData != null)
            {
            }
            // var testName = DetermineTestName(context);
            // hmm Generate to generate new and compute to process existing !?!
            this.ComputeCodeMemberProperty(typeMember as CodeMemberMethod, propertyData, propertyName);
            return(true);
        }
Пример #14
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException"><c>context</c> is out of range.</exception>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            /*
            // Todo: just placeholders. Use this in the TestConstructor builder.
            var typeMember = context.TypeMember as CodeMemberMethod;
            var testClass = context.TestClassDeclaration.Name;
            var test = context.TestKey;
            var suMethod = context.SetUpTearDownContext.SetUpMethod;
            var setupData = context.BuildData["Setup"];
            if (setupData != null)
            {
                IBuilderData bdata;
                var found = setupData.TryGetValue("bla", out bdata);
                foreach (var item in setupData)
                {
                    var testField = item.Key;
                    var testDaa = item.Value.GetData() as CodeMemberField;
                    typeMember.Statements.Add(new CodeSnippetStatement(""));
                    typeMember.Statements.Add(new CodeCommentStatement("... have a this." + testField + " link."));
                }
            }
            else
            {

            }*/
            Guard.NotNull(() => context, context);
            var typeMember = context.TypeMember;

            if (!(typeMember is CodeMemberMethod))
            {
                throw new ArgumentOutOfRangeException(
                    "context", "The supplied build context is not for a CodeMemberMethod type.");
            }

            var typeMemberName = typeMember.Name;
            var propertyName = typeMemberName;

            // var propertyName = typeMemberName.Replace("get_", string.Empty).Replace("set_", string.Empty);
            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "XX_Norm_XX");
            var storageCategory = string.Format(BuilderConstants.PropertyStorageCategory, context.TestClassDeclaration.Name);
            var propertyData = context.GetBuilderData(storageCategory);

            //var userData = context.BuildData.General[this.GetType().FullName] as PropertyBuilderUserParameters;
            var userData = context.GetBuilderData<BuildParametersOfPropertyBuilder>(this);
            if (userData != null)
            {
            }
            // var testName = DetermineTestName(context);
            // hmm Generate to generate new and compute to process existing !?!
            this.ComputeCodeMemberProperty(typeMember as CodeMemberMethod, propertyData, propertyName);
            return true;
        }
Пример #15
0
        /// <summary>
        /// Creates a new instance of the <see cref="IMemberBuilder"/> specified in the <see cref="Type"/> property with
        /// the specified context data.
        /// </summary>
        /// <param name="context">The context of the current test object.</param>
        /// <returns>
        /// A test member builder that can handle the request specified in the <see cref="CanHandle"/> method.
        /// </returns>
        public IMemberBuilder CreateInstance(IMemberBuildContext context)
        {
            var            parameters    = new object[] { context };
            IMemberBuilder memberBuilder = null;

            //try
            {
                var inst = Activator.CreateInstance(this.Type, parameters);
                memberBuilder = (IMemberBuilder)inst;
            }
            //catch (Exception ex)
            //{
            //}
            return(memberBuilder);
        }
Пример #16
0
        /// <summary>
        /// Creates a constructor test method.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="testObjectName">Name of the test object.</param>
        /// <param name="createExpr">A <see cref="CodeObjectCreateExpression"/> building the test object.</param>
        /// <returns>A new test method, forming the constructor test.</returns>
        private CodeMemberMethod CreateConstructorTest(
            IMemberBuildContext context,
            string methodName,
            string testObjectName,
            out CodeObjectCreateExpression createExpr)
        {
            var cm = new CustomConstructorCodeMemberMethod
            {
                Name       = methodName,
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            CodeMethodComposer.CreateTestStubForMethod(cm);
            createExpr = this.BuildTestObject(context.TestObjectType.Name, testObjectName, cm);
            context.TestClassDeclaration.Members.Add(cm);
            context.BuildResult.ClassMethodsToAdd.Add(cm);
            return(cm);
        }
Пример #17
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            var typeMember = context.TypeMember;
            var typeMemberName = typeMember.Name;
            var propertyName = typeMemberName;

            // var propertyName = typeMemberName.Replace("get_", string.Empty).Replace("set_", string.Empty);
            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "XX_Norm_XX");
            var storageCategory = string.Format(BuilderConstants.PropertyStorageCategory, context.TestClassDeclaration.Name);
            var propertyData = context.GetBuilderData(storageCategory);

            // var testName = DetermineTestName(context);
            // hmm Generate to generate new and compute to process existing !?!
            const string TestObjectName = "testObject";

            this.ComputeCodeMemberProperty(
                context, typeMember as CodeMemberMethod, propertyData, TestObjectName, propertyName);
            return true;
        }
Пример #18
0
        /// <summary>
        /// Builds the test method member with the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            Guard.NotNull(() => context, context);

            // var bla = context.TypeMember.Name;
            // var to = context.TestObjectType.Name;

            // var methodName = "ConstructWithParameters";
            var ct = context.SetUpTearDownContext as ISetupAndTearDownCreationContext;

            Guard.NotNull(() => ct, ct);
            var objcreator  = ct.TestObjectCreator; // as TestObjectBuilder;
            var assignMents = objcreator.Assignments;

            if (assignMents != null && assignMents.Count() > 1)
            {
                foreach (var assignmentInfos in assignMents)
                {
                    var usedCtor = assignmentInfos.UsedConstructor;
                    if (usedCtor == null)
                    {
                        throw new InvalidOperationException("ConstructorBuilder was called with an AssignmentInfoCollection.UsedConstructor that was null.");
                    }

                    var methodName = BuildNameFromCtorParameters(usedCtor);

                    // Create a new instance of the used constructor.
                    CodeObjectCreateExpression createExpr;
                    var codeMemberMethod = this.CreateConstructorTest(context, methodName, "testObject", out createExpr);
                    objcreator.AssignExtra(context.TestClassDeclaration, codeMemberMethod, createExpr, assignmentInfos);

                    // create the null parameter checks for the current constructor.
                    CreateNullParameterAssertions(context, objcreator, assignmentInfos, createExpr, codeMemberMethod);
                }
            }

            return(true);
        }
Пример #19
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);
        }
Пример #20
0
        /// <summary>
        /// Determines whether this instance can handle a specified build context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanHandleContext(IMemberBuildContext context)
        {
            if (context.MemberInfo != null)
            {
            }

            if (context.TestKey != null)
            {
                if (context.TestObjectType.Name.Contains("AllIwantToTest"))
                {
                    if (context.TestKey.Contains("Static"))
                    {
                        // var abc = context.TestKey;
                        // var older = context.TestObjectType.Name;
                        // var mstatic = context.MemberInfo;
                    }
                }
            }

            return(!context.IsConstructor && !context.IsProperty && !context.IsEvent && context.MemberInfo != null &&
                   context.MemberInfo.IsStatic);

            // return context.TypeMember.Name.StartsWith("get_") || context.TypeMember.Name.StartsWith("set_");
        }
Пример #21
0
        /// <summary>
        /// Tries to get the builder for the specified context dependent on user property activation.
        /// </summary>
        /// <param name="context">The context of the current test object.</param>
        /// <param name="useUserActivation">if set to <c>true</c> use user activation in context.BuildData stored values.
        /// Means: Enable/Disable by setting the corresponding property store value.</param>
        /// <returns>
        /// A list of member builders that can handle the request or an <c>empty</c> list if no one can be found.
        /// </returns>
        public IEnumerable <IMemberBuilder> GetBuilder(IMemberBuildContext context, bool useUserActivation)
        {
            // Todo: maybe cache em.
            foreach (var buildHandler in this.buildHandlers.Values.Where(e => !e.IsMultiBuilder))
            {
                var canHandleContext = buildHandler.CanHandle(context);
                if (!canHandleContext)
                {
                    continue;
                }

                if (useUserActivation)
                {
                    Guard.NotNull(() => context.BuildData, context.BuildData);

                    // if (buildHandler.IsMultiBuilder)
                    // {
                    // continue;
                    // }
                    // else
                    // {
                    var parameter = this.GetParameters(buildHandler.Type, context.BuildData);
                    if (!parameter.Enabled)
                    {
                        continue;
                    }

                    // }
                }

                var memberBuilder = buildHandler.CreateInstance(context);
                if (memberBuilder != null)
                {
                    yield return(memberBuilder);
                }
            }

            var multis = this.MultiParameters(context.BuildData);

            foreach (var multi in multis)
            {
                if (multi.Parameters.Enabled)
                {
                    var multiHandler     = this.buildHandlers[multi.BuilderType];
                    var canHandleContext = multiHandler.CanHandle(context);
                    if (!canHandleContext)
                    {
                        continue;
                    }

                    var multiBuilder = (IMultiBuilder)multiHandler.CreateInstance(context);
                    if (multiBuilder is MultiBuilder)
                    {
                        ((MultiBuilder)multiBuilder).Parameters = multi.Parameters;
                    }

                    yield return(multiBuilder);
                }

                // var mbpara = GetMultiParameter(Guid.Empty, seltype, properties);
            }
        }
Пример #22
0
 /// <summary>
 /// Tries to get the builder for the specified context.
 /// </summary>
 /// <param name="context">The context of the current test object.</param>
 /// <returns>
 /// A list of member builders that can handle the request or an <c>empty</c> list if no one can be found.
 /// </returns>
 public IEnumerable <IMemberBuilder> GetBuilder(IMemberBuildContext context)
 {
     // So we have property Enabled/Disabled checks as default behavior.
     return(this.GetBuilder(context, true));
 }
Пример #23
0
 /// <summary>
 /// Builds the test method member with the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected abstract override bool BuildMember(IMemberBuildContext context);
Пример #24
0
 /// <summary>
 /// Builds the test method member with the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected abstract bool BuildMember(IMemberBuildContext context);
Пример #25
0
 /// <summary>
 /// Creates a new instance of the <see cref="IMemberBuilder"/> specified in the <see cref="Type"/> property with 
 /// the specified context data.
 /// </summary>
 /// <param name="context">The context of the current test object.</param>
 /// <returns>
 /// A test member builder that can handle the request specified in the <see cref="CanHandle"/> method.
 /// </returns>
 public IMemberBuilder CreateInstance(IMemberBuildContext context)
 {
     var parameters = new object[] { context };
     IMemberBuilder memberBuilder = null;
     //try
     {
         var inst = Activator.CreateInstance(this.Type, parameters);
         memberBuilder = (IMemberBuilder)inst;
     }
     //catch (Exception ex)
     //{
     //}
     return memberBuilder;
 }
Пример #26
0
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        protected override bool BuildMember(IMemberBuildContext context)
        {
            /*
             * // Todo: just placeholders. Use this in the TestConstructor builder.
             * var typeMember = context.TypeMember as CodeMemberMethod;
             * var testClass = context.TestClassDeclaration.Name;
             * var test = context.TestKey;
             * var suMethod = context.SetUpTearDownContext.SetUpMethod;
             * var setupData = context.BuildData["Setup"];
             * if (setupData != null)
             * {
             *  IBuilderData bdata;
             *  var found = setupData.TryGetValue("bla", out bdata);
             *  foreach (var item in setupData)
             *  {
             *      var testField = item.Key;
             *      var testDaa = item.Value.GetData() as CodeMemberField;
             *      typeMember.Statements.Add(new CodeSnippetStatement(""));
             *      typeMember.Statements.Add(new CodeCommentStatement("... have a this." + testField + " link."));
             *  }
             * }
             * else
             * {
             *
             * }*/
            Guard.NotNull(() => context, context);
            var typeMember = context.TypeMember as CodeMemberMethod;

            // var abc = context.TestKey;
            // var older = context.TestObjectType.Name;
            // var mstatic = context.MemberInfo;

            /*if (!(typeMember is CodeMemberMethod))
             * {
             *  throw new ArgumentOutOfRangeException("context", "The supplied build context is not for a CodeMemberMethod type.");
             * }*/

            // var typeMemberName = typeMember.Name;
            // var propertyName = typeMemberName;

            // var propertyName = typeMemberName.Replace("get_", string.Empty).Replace("set_", string.Empty);
            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "XX_Norm_XX");
            // var propertyData = context.GetBuilderData("Property");

            // var testName = DetermineTestName(context);
            // hmm Generate to generate new and compute to process existing !?!

            /*var propertyData = builderData as PropertyBuilderData;
             *
             * var variableDeclaration = new CodeVariableDeclarationStatement(
             *  "var",
             *  "expected",
             *  new CodePrimitiveExpression("Testing"));
             *
             * typeMember.Statements.Add(variableDeclaration);
             *
             * variableDeclaration = new ImplicitVariableDeclarationStatement(
             *  "actual", new CodePrimitiveExpression("Testing"));
             * typeMember.Statements.Add(variableDeclaration);
             *
             * // Creates a code expression for a CodeExpressionStatement to contain.
             * var invokeExpression = new CodeMethodInvokeExpression(
             *  new CodeTypeReferenceExpression("Assert"),
             *  "AreEqual",
             *  new CodeVariableReferenceExpression("expected"),
             *  new CodeVariableReferenceExpression("actual"));
             *
             * // Creates a statement using a code expression.
             * // var expressionStatement = new CodeExpressionStatement(invokeExpression);
             *
             * // A C# code generator produces the following source code for the preceeding example code:
             *
             * // Console.Write( "Example string" );
             * typeMember.Statements.Add(invokeExpression);*/

            // BaseCSharpCodeGenerator.ReplaceTestInTestName(typeMember, "NormalBehavior");
            // typeMember.AddBlankLine();
            // CodeExpression ctorAssignmentRight = new CodePrimitiveExpression("This is from Static Method Builder");
            // var expectedAsign = new CodeVariableDeclarationStatement("var", "expected",
            // ctorAssignmentRight);

            // typeMember.StaticClass(context.TestObjectType.Name).Invoke(context.TestKey).WithReference("expected").AssignLocal("actual", true);
            // .Invoke(context.TestKey).WithReference("expected").AssignLocal("actual", true);
            typeMember.AddBlankLine();
            var returnType = context.MemberInfo.ReturnType.FullName;

            typeMember.AddComment("Type is " + returnType);
            typeMember.AddBlankLine();

            var parameterNames = new List <string>();

            foreach (var item in context.MemberInfo.GetParameters())
            {
                bool activatorAdded = false;
                var  parameterType  = item.ParameterType;
                typeMember.AddComment("Parameter '" + item.Name + "' is of type " + parameterType.Name);

                // var def = default(parameterType);
                try
                {
                    // var cct = Activator.CreateInstance(parameterType);
                    var cct = "Use a better implementation of Object selector";
                    if (parameterType.FullName != null)
                    {
                        if (parameterType.FullName.StartsWith("System.") &&
                            parameterType.FullName.ToCharArray().Count(e => e == '.') < 2)
                        {
                            typeMember.Var(item.Name, true).Assign().With(cct).Commit();
                            activatorAdded = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Todo: bad bad, show or handle exceptions ;) Or better: get default instances for the requested types .... later, later...
                    Log(ex.Message);
                }

                if (!activatorAdded)
                {
                    typeMember.Var(item.Name, true).Assign().With(
                        "Insert initialization of parameter ´" + item.Name + "´ here").Commit();
                }

                // Todo: impl var assignValue = context.SetUpTearDownContext.TryGetAcceptableParameterValue(item.Name);
                parameterNames.Add(item.Name);
            }

            // typeMember.Var("expected", true).StaticClass(context.TestObjectType.Name).Invoke(context.TestKey).With("12:03:05").Commit();
            // Todo: if the above(constructor parameter fetching) was successfull generate the missing Parameters from methodinfo and assign it correct.
            typeMember.AddBlankLine();
            typeMember.Var("expected", true).Assign().With("Dreck").Commit();

            // typeMember.Var("expected", false).StaticClass("DateTime").Invoke("Now").Commit();

            // typeMember.Statements.Add(expectedAsign);
            // Todo: check for void return type.
            typeMember.StaticClass(context.TestObjectType.Name).Invoke(context.MemberInfo.Name)
            .WithReference(parameterNames.ToArray()).AssignLocal("actual", true);
            typeMember.StaticClass("Assert").Invoke("AreEqual").WithReference("expected").WithReference("actual").Commit();
            return(true);
        }
Пример #27
0
        /*/// <summary>
        /// Gets or sets the <see cref="T:System.Object"/> at the specified index.
        /// </summary>
        /// <param name="propertyName">The index of the element to get or set.</param>
        /// <value>The <see cref="T:System.Object"/> at the specified index.</value>
        public IBuilderData this[string propertyName]
        {
            get
            {
                // TODO: return the specified index here
                var propData = this.Context.BuildData[this.Context.TestKey];
                var result = propData[propertyName];
                return result;
            }

            //set
            //{
                // TODO: set the specified index to value here
            //}
        }*/
        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        public bool Build(IMemberBuildContext context)
        {
            return this.BuildMember(context);
        }
Пример #28
0
 /// <summary>
 /// Determines the name of the test method.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <param name="originalName">The initial name of the test method member.</param>
 /// <returns>
 /// The name of the test method.
 /// </returns>
 public string GetTestName(IMemberBuildContext context, string originalName)
 {
     return this.DetermineTestName(context, originalName);
 }
Пример #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:BuildHandler"/> class.
 /// </summary>
 public MyMemberBuilder(IMemberBuildContext context)
 {
     this.Context = context;
 }
Пример #30
0
 /// <summary>
 /// Builds the test method member with the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected abstract override bool BuildMember(IMemberBuildContext context);
Пример #31
0
 /// <summary>
 /// Determines whether this instance can handle a specified build context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
 /// </returns>
 public static bool CanHandleContext(IMemberBuildContext context)
 {
     return(context.IsProperty);
 }
Пример #32
0
 /// <summary>
 /// Builds the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected override bool BuildMember(IMemberBuildContext context)
 {
     return true;
 }
Пример #33
0
 /// <summary>
 /// Determines the name of the test method.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <param name="originalName">The initial name of the test method member.</param>
 /// <returns>
 /// The name of the test method.
 /// </returns>
 public string GetTestName(IMemberBuildContext context, string originalName)
 {
     return(this.DetermineTestName(context, originalName));
 }
Пример #34
0
 /// <summary>
 /// Builds the test method member with the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected abstract bool BuildMember(IMemberBuildContext context);
Пример #35
0
 /// <summary>
 /// Determines whether this instance can handle a specified build context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
 /// </returns>
 public static bool CanHandleContext(IMemberBuildContext context)
 {
     return context.IsProperty;
 }
Пример #36
0
        /*/// <summary>
         * /// Gets or sets the <see cref="T:System.Object"/> at the specified index.
         * /// </summary>
         * /// <param name="propertyName">The index of the element to get or set.</param>
         * /// <value>The <see cref="T:System.Object"/> at the specified index.</value>
         * public IBuilderData this[string propertyName]
         * {
         *  get
         *  {
         *      // TODO: return the specified index here
         *      var propData = this.Context.BuildData[this.Context.TestKey];
         *      var result = propData[propertyName];
         *      return result;
         *  }
         *
         *  //set
         *  //{
         *      // TODO: set the specified index to value here
         *  //}
         * }*/

        /// <summary>
        /// Builds the specified context.
        /// </summary>
        /// <param name="context">The build context of the test method member.</param>
        /// <returns>
        /// <c>true</c> on success.
        /// </returns>
        public bool Build(IMemberBuildContext context)
        {
            return(this.BuildMember(context));
        }
Пример #37
0
        /// <summary>
        /// Handle property related stuff before type generation.
        /// </summary>
        /// <param name="context">The context. Todo: remove it with specialized parameters after development.</param>
        /// <param name="typeMember">The type member.</param>
        /// <param name="builderData">The builder data.</param>
        /// <param name="testObjectName">Name of the test object member field.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <exception cref="NotImplementedException">The context of the builder does not supply a valid 
        /// <see cref="ISetupAndTearDownCreationContext"/>.</exception>
        /// <exception cref="NotImplementedException">The context of the builder does not supply a valid 
        /// <see cref="TestObjectComposer"/>.</exception>
        protected virtual void ComputeCodeMemberProperty(
            IMemberBuildContext context,
            CodeMemberMethod typeMember,
            IBuilderData builderData,
            string testObjectName,
            string propertyName)
        {
            var propertyData = builderData as PropertyBuilderData;

            if (propertyData == null)
            {
                return;
            }

            var setAccessor = propertyData.SetAccessor;
            var getAccessor = propertyData.GetAccessor;

            if (getAccessor == null)
            {
                return;
            }

            if (setAccessor == null)
            {
                // create the actual and expected var's here.
                // actualRef
                // expectedRef
            }

            var propName = propertyData.PropertyName;

            // devel: how to create a string initializer from possible constructor setups of the 'SetUp' method.
            var co = context.SetUpTearDownContext as ISetupAndTearDownCreationContext;
            if (co == null)
            {
                throw new NotImplementedException("The context of the builder does not supply a valid ISetupAndTearDownCreationContext.");
            }

            // Todo: put these used methods into the interfaces of TestObjectComposer and maybe modify
            // ISetupAndTearDownContext that it provides the co.TestObjectCreator. ... or fix all this in the
            // pre-build?!!
            var creator = co.TestObjectCreator as TestObjectComposer;
            if (creator == null)
            {
                throw new NotImplementedException("The context of the builder does not supply a valid TestObjectBuilder.");
            }

            ConstructorAssignment ctorAssignment;
            var found = creator.TryFindConstructorAssignment(propName, out ctorAssignment, false);

            CodeExpression ctorAssignmentRight = CodeMethodComposer.CreateExpressionByType(getAccessor.ReturnType, propertyData.PropertyName);
            // CodeExpression ctorAssignmentRight = new CodePrimitiveExpression("Insert expected object here");
            var assertionKind = "AreEqual";
            if (found)
            {
                // use the value of the ctor initializer.
                //ctorAssignmentRight = ctorAssignment.AssignStatement.Right;
                // use the field reference itself.
                ctorAssignmentRight = ctorAssignment.AssignStatement.Left;
                assertionKind = "AreSame";
            }

            typeMember.Statements.Add(new CodeSnippetStatement(string.Empty));
            typeMember.Statements.Add(new CodeCommentStatement("Test read access of '" + propName + "' Property."));

            var expectedAsign = new CodeVariableDeclarationStatement(
                "var",
                "expected",
                ctorAssignmentRight);
            typeMember.Statements.Add(expectedAsign);

            var testObjRef = new CodeTypeReferenceExpression(testObjectName);
            var testPropRef = new CodePropertyReferenceExpression(testObjRef, propName);
            var actualAsign = new CodeVariableDeclarationStatement("var", "actual", testPropRef);
            typeMember.Statements.Add(actualAsign);

            var assertExpr = new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression("Assert"),
                assertionKind,
                new CodeVariableReferenceExpression("expected"),
                new CodeVariableReferenceExpression("actual"));
            typeMember.Statements.Add(assertExpr);
        }
Пример #38
0
 /// <summary>
 /// Builds the specified context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> on success.
 /// </returns>
 protected override bool BuildMember(IMemberBuildContext context)
 {
     return(true);
 }
Пример #39
0
        /// <summary>
        /// Handle property related stuff before type generation.
        /// </summary>
        /// <param name="context">The context. Todo: remove it with specialized parameters after development.</param>
        /// <param name="typeMember">The type member.</param>
        /// <param name="builderData">The builder data.</param>
        /// <param name="testObjectName">Name of the test object member field.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <exception cref="NotImplementedException">The context of the builder does not supply a valid
        /// <see cref="ISetupAndTearDownCreationContext"/>.</exception>
        /// <exception cref="NotImplementedException">The context of the builder does not supply a valid
        /// <see cref="TestObjectComposer"/>.</exception>
        protected virtual void ComputeCodeMemberProperty(
            IMemberBuildContext context,
            CodeMemberMethod typeMember,
            IBuilderData builderData,
            string testObjectName,
            string propertyName)
        {
            var propertyData = builderData as PropertyBuilderData;

            if (propertyData == null)
            {
                return;
            }

            var setAccessor = propertyData.SetAccessor;
            var getAccessor = propertyData.GetAccessor;

            if (getAccessor == null)
            {
                return;
            }

            if (setAccessor == null)
            {
                // create the actual and expected var's here.
                // actualRef
                // expectedRef
            }

            var propName = propertyData.PropertyName;

            // devel: how to create a string initializer from possible constructor setups of the 'SetUp' method.
            var co = context.SetUpTearDownContext as ISetupAndTearDownCreationContext;

            if (co == null)
            {
                throw new NotImplementedException("The context of the builder does not supply a valid ISetupAndTearDownCreationContext.");
            }

            // Todo: put these used methods into the interfaces of TestObjectComposer and maybe modify
            // ISetupAndTearDownContext that it provides the co.TestObjectCreator. ... or fix all this in the
            // pre-build?!!
            var creator = co.TestObjectCreator as TestObjectComposer;

            if (creator == null)
            {
                throw new NotImplementedException("The context of the builder does not supply a valid TestObjectBuilder.");
            }

            ConstructorAssignment ctorAssignment;
            var found = creator.TryFindConstructorAssignment(propName, out ctorAssignment, false);


            CodeExpression ctorAssignmentRight = CodeMethodComposer.CreateExpressionByType(getAccessor.ReturnType, propertyData.PropertyName);
            // CodeExpression ctorAssignmentRight = new CodePrimitiveExpression("Insert expected object here");
            var assertionKind = "AreEqual";

            if (found)
            {
                // use the value of the ctor initializer.
                //ctorAssignmentRight = ctorAssignment.AssignStatement.Right;
                // use the field reference itself.
                ctorAssignmentRight = ctorAssignment.AssignStatement.Left;
                assertionKind       = "AreSame";
            }

            typeMember.Statements.Add(new CodeSnippetStatement(string.Empty));
            typeMember.Statements.Add(new CodeCommentStatement("Test read access of '" + propName + "' Property."));

            var expectedAsign = new CodeVariableDeclarationStatement(
                "var",
                "expected",
                ctorAssignmentRight);

            typeMember.Statements.Add(expectedAsign);

            var testObjRef  = new CodeTypeReferenceExpression(testObjectName);
            var testPropRef = new CodePropertyReferenceExpression(testObjRef, propName);
            var actualAsign = new CodeVariableDeclarationStatement("var", "actual", testPropRef);

            typeMember.Statements.Add(actualAsign);

            var assertExpr = new CodeMethodInvokeExpression(
                new CodeTypeReferenceExpression("Assert"),
                assertionKind,
                new CodeVariableReferenceExpression("expected"),
                new CodeVariableReferenceExpression("actual"));

            typeMember.Statements.Add(assertExpr);
        }
Пример #40
0
 /// <summary>
 /// Determines whether this instance can handle a specified build context.
 /// </summary>
 /// <param name="context">The build context of the test method member.</param>
 /// <returns>
 /// <c>true</c> if this instance can handle the specified context; otherwise, <c>false</c>.
 /// </returns>
 public static bool CanHandleContext(IMemberBuildContext context)
 {
     // return context.TypeMember.Name.StartsWith("set_");
     return(context.IsConstructor);
 }