Ejemplo n.º 1
0
        public static string GetNextAvailableCopyName(CodeClass codeClass, ref string codeFunctionName, Project project)
        {
            codeFunctionName = "CopyOf" + codeFunctionName;

            if (!CodeSelectionHandler.CanGenerateHandleCodeFunction(
                    codeClass,
                    codeFunctionName))
            {
                return(GetNextAvailableCopyName(codeClass, ref codeFunctionName, project));
            }

            return(codeFunctionName);
        }
Ejemplo n.º 2
0
        public override CodeFunction GenerateTest(CodeClass unitTestCodeClass, CodeProperty originalClassCodeProperty)
        {
            vsCMFunction functionKind = vsCMFunction.vsCMFunctionFunction;
            object       functionType = null;

            functionKind = vsCMFunction.vsCMFunctionSub;
            functionType = vsCMTypeRef.vsCMTypeRefVoid;

            string nextAvailableName = originalClassCodeProperty.Name;

            if (!CodeSelectionHandler.CanGenerateHandleCodeFunction(unitTestCodeClass,
                                                                    nextAvailableName))
            {
                nextAvailableName = GetNextAvailableCopyName(unitTestCodeClass, ref nextAvailableName, unitTestCodeClass.ProjectItem.ContainingProject);
            }

            CodeFunction unitTestCodeFunction = unitTestCodeClass.AddFunction(
                nextAvailableName,
                functionKind,
                functionType,
                -1,
                originalClassCodeProperty.Access,
                -1);

            unitTestCodeFunction.AddAttribute("NUnit.Framework.Test", "", -1);

            try
            {
                unitTestCodeFunction.Comment    = originalClassCodeProperty.Comment;
                unitTestCodeFunction.DocComment = originalClassCodeProperty.DocComment;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                //ignore
            }

            TextPoint bodyStartingPoint =
                unitTestCodeFunction.GetStartPoint(vsCMPart.vsCMPartBody);

            EditPoint boydEditPoint = bodyStartingPoint.CreateEditPoint();

            //Stop here if not read-write type property now...

            if (originalClassCodeProperty.Setter == null)
            {
                boydEditPoint = bodyStartingPoint.CreateEditPoint();

                boydEditPoint.Insert(StringHelper.GetTabString() + "' Property is not read-write please add your own code here." + Environment.NewLine);

                return(unitTestCodeFunction);
            }

            string tvFunctionCallTemplate = string.Empty; // "iv{0}Type.{1} = default({2});";

            tvFunctionCallTemplate = "iv{0}Type.{1} = New {2}()" + Environment.NewLine;

            string tvFunctionCall = tvFunctionCallTemplate;

            CodeTypeRef tvPropertyType = originalClassCodeProperty.Type;

            string tvPropertyTypeAsString = tvPropertyType.AsString;

            tvFunctionCall = string.Format(tvFunctionCall, ((CodeClass)originalClassCodeProperty.Parent).Name, originalClassCodeProperty.Name, tvPropertyTypeAsString);

            bodyStartingPoint = unitTestCodeFunction.GetStartPoint(vsCMPart.vsCMPartBody);

            boydEditPoint = bodyStartingPoint.CreateEditPoint();

            //FIX ME (tabing)
            boydEditPoint.Insert(StringHelper.GetTabString() + tvFunctionCall + Environment.NewLine);

            //FIX ME (tabbing)
            string tvTempString = string.Empty; // "iv{0}Type.{1}, default({2})";

            tvTempString = "iv{0}Type.{1}, New {2}()";

            tvTempString = string.Format(tvTempString, ((CodeClass)originalClassCodeProperty.Parent).Name, originalClassCodeProperty.Name, tvPropertyTypeAsString);

            boydEditPoint.Insert(Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'TODO: Update Assert to meet test needs" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'Assert.AreEqual(" + tvTempString + ")" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\tThrow New Exception 'Not Implemented'" + Environment.NewLine);

            return(unitTestCodeFunction);
        }
Ejemplo n.º 3
0
        public override CodeFunction GenerateTest(CodeClass unitTestCodeClass, CodeFunction originalClassCodeFuntion)
        {
            vsCMFunction functionKind = vsCMFunction.vsCMFunctionFunction;
            object       functionType = null;

            functionKind = vsCMFunction.vsCMFunctionSub;
            functionType = vsCMTypeRef.vsCMTypeRefVoid;

            string nextAvailableName = originalClassCodeFuntion.Name;

            if (!CodeSelectionHandler.CanGenerateHandleCodeFunction(unitTestCodeClass,
                                                                    nextAvailableName))
            {
                nextAvailableName = GetNextAvailableCopyName(unitTestCodeClass, ref nextAvailableName, unitTestCodeClass.ProjectItem.ContainingProject);
            }

            CodeFunction unitTestCodeFunction = unitTestCodeClass.AddFunction(
                nextAvailableName,
                functionKind,
                functionType,
                -1,
                originalClassCodeFuntion.Access,
                -1);

            bool tvIsStatic = originalClassCodeFuntion.IsShared;

            //add the NUnit attribute to the function
            unitTestCodeFunction.AddAttribute("NUnit.Framework.Test", "", -1);

            try
            {
                unitTestCodeFunction.Comment    = originalClassCodeFuntion.Comment;
                unitTestCodeFunction.DocComment = originalClassCodeFuntion.DocComment;
            }
            catch (Exception ex)
            {
                //ignore, for some reason the doc throws in vb
                Logger.LogException(ex);
            }

            string tvFunctionCallTemplate   = string.Empty; //"iv{0}Type.{1}(";
            string tvFunctionReturnTemplate = string.Empty; //"{0} iv{1}Return = ";

            tvFunctionCallTemplate = "iv{0}Type.{1}(";

            if (tvIsStatic)
            {
                tvFunctionCallTemplate = "{0}.{1}(";
            }

            tvFunctionReturnTemplate = "Dim iv{1}Return As {0} = ";

            string tvTempParameterList = string.Empty;
            string tvFunctionCall      = tvFunctionCallTemplate;
            string tvFunctionReturn    = tvFunctionReturnTemplate;


            if (!originalClassCodeFuntion.FunctionKind.ToString().Equals("vsCMFunctionConstructor"))
            {
                CodeElements tvParameters = originalClassCodeFuntion.Parameters;

                foreach (CodeElement tvCodeElement in tvParameters)
                {
                    if (!tvFunctionCall.Equals(tvFunctionCallTemplate))
                    {
                        tvFunctionCall += ", ";
                    }

                    CodeParameter2 tvCodeParameter = (CodeParameter2)tvCodeElement;

                    string parameterName = tvCodeParameter.Name;

                    CodeTypeRef tvParameterType = tvCodeParameter.Type;

                    vsCMParameterKind tvParameterKind = tvCodeParameter.ParameterKind;

                    string parameterTypeAsString = tvParameterType.AsString;

                    tvTempParameterList += "Dim " + parameterName + " As " + parameterTypeAsString + " = New " + parameterTypeAsString + "()" + Environment.NewLine + StringHelper.GetTabString();

                    if (tvParameterKind == vsCMParameterKind.vsCMParameterKindRef)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else if (tvParameterKind == vsCMParameterKind.vsCMParameterKindOut)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else if (tvParameterKind == vsCMParameterKind.vsCMParameterKindIn)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else
                    {
                        tvFunctionCall += parameterName;
                    }
                }

                tvFunctionCall = string.Format(tvFunctionCall + ")" + Environment.NewLine, ((CodeClass)originalClassCodeFuntion.Parent).Name, originalClassCodeFuntion.Name);
            }

            if (originalClassCodeFuntion.Type.TypeKind != vsCMTypeRef.vsCMTypeRefVoid)
            {
                tvFunctionReturn = string.Format(tvFunctionReturn, originalClassCodeFuntion.Type.AsString, originalClassCodeFuntion.Name);
                tvFunctionCall   = tvFunctionReturn + tvFunctionCall;
            }

            TextPoint bodyStartingPoint =
                unitTestCodeFunction.GetStartPoint(vsCMPart.vsCMPartBody);

            EditPoint boydEditPoint = bodyStartingPoint.CreateEditPoint();

            if (!originalClassCodeFuntion.FunctionKind.ToString().Equals("vsCMFunctionConstructor"))
            {
                boydEditPoint.Insert("\t\t\t' TODO: Update variable/s' defaults to meet test needs" + Environment.NewLine);

                boydEditPoint.Insert(StringHelper.GetTabString() + tvTempParameterList + Environment.NewLine);
                boydEditPoint.Insert(StringHelper.GetTabString() + tvFunctionCall + Environment.NewLine);
            }

            if (originalClassCodeFuntion.Type.TypeKind != vsCMTypeRef.vsCMTypeRefVoid)
            {
                string stringHolder = "iv{0}Return";
                stringHolder = string.Format(stringHolder, originalClassCodeFuntion.Name);
                //FIX ME (tabing)
                //boydEditPoint.Insert(string.Format("\t\t\tAssert.AreEqual({0}, default({1}));\r\n", stringHolder, originalClassCodeFuntion.Type.AsString));
            }


            boydEditPoint.Insert(Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'TODO: Update Assert to meet test needs" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'Assert.AreEqual( , )" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\tThrow New Exception 'Not Implemented'" + Environment.NewLine);

            return(unitTestCodeFunction);
        }