예제 #1
0
        /// <summary>
        /// 克隆一个方法
        /// </summary>
        /// <param name="cloneFunction">要克隆的方法</param>
        /// <param name="codeClass">添加到类中</param>
        private void CloneFunction(CodeFunction cloneFunction, CodeClass codeClass)
        {
            CodeFunction codeFunction = codeClass.AddFunction(cloneFunction.Name, cloneFunction.FunctionKind,
                                                              cloneFunction.Type, -1, cloneFunction.Access, null);

            //添加参数
            for (int i = 1; i <= cloneFunction.Parameters.Count; i++)
            {
                CodeParameter2 parameter      = cloneFunction.Parameters.Item(i) as CodeParameter2;
                CodeParameter2 cloneParameter = codeFunction.AddParameter(parameter.FullName, parameter.Type.AsFullName, i) as CodeParameter2;
                cloneParameter.DefaultValue  = parameter.DefaultValue;
                cloneParameter.ParameterKind = parameter.ParameterKind;
            }

            //添加属性
            for (int i = 1; i <= cloneFunction.Attributes.Count; i++)
            {
                CodeAttribute attribute = cloneFunction.Attributes.Item(i) as CodeAttribute;
                codeFunction.AddAttribute(attribute.Name, attribute.Value, i);
            }

            //方法注释
            codeFunction.Comment = cloneFunction.Comment;
            //方法说明
            codeFunction.DocComment = cloneFunction.DocComment;
            //静态修饰
            codeFunction.IsShared = cloneFunction.IsShared;
            //抽象修饰
            codeFunction.MustImplement = cloneFunction.MustImplement;
            //重载修饰
            codeFunction.CanOverride = cloneFunction.CanOverride;
        }
        private static void AddStepAttribute(CodeFunction implementationFunction, string stepValue)
        {
            var codeAttribute = implementationFunction.AddAttribute("Step", stepValue.ToLiteral(), -1);

            if (codeAttribute == null)
            {
                throw new ChangeRejectedException("Step Attribute not created");
            }
        }
예제 #3
0
        /// <summary>
        /// 重构方法
        /// </summary>
        /// <param name="replacedFunction"></param>
        /// <param name="newFunction"></param>
        private void RefactorMethod(CodeFunction replacedFunction, CodeFunction newFunction)
        {
            //清除方法参数
            while (replacedFunction.Parameters.Count > 0)
            {
                replacedFunction.RemoveParameter(replacedFunction.Parameters.Count);
            }

            //清除方法属性
            while (replacedFunction.Attributes.Count > 0)
            {
                ((CodeAttribute)replacedFunction.Attributes.Item(replacedFunction.Attributes.Count)).Delete();
            }

            //添加参数
            for (int i = 1; i <= newFunction.Parameters.Count; i++)
            {
                CodeParameter2 parameter      = newFunction.Parameters.Item(i) as CodeParameter2;
                CodeParameter2 cloneParameter = replacedFunction.AddParameter(parameter.FullName, parameter.Type.AsFullName, i) as CodeParameter2;
                cloneParameter.DefaultValue  = parameter.DefaultValue;
                cloneParameter.ParameterKind = parameter.ParameterKind;
            }

            //添加属性
            for (int i = 1; i <= newFunction.Attributes.Count; i++)
            {
                CodeAttribute2 attribute = newFunction.Attributes.Item(i) as CodeAttribute2;
                replacedFunction.AddAttribute(attribute.Name, attribute.Value, i);
            }

            //方法名
            replacedFunction.Name = newFunction.Name;
            //方法注释
            replacedFunction.Comment = newFunction.Comment;
            //方法说明
            replacedFunction.DocComment = newFunction.DocComment;
            //静态修饰
            replacedFunction.IsShared = newFunction.IsShared;
            //抽象修饰
            replacedFunction.MustImplement = newFunction.MustImplement;
            //重载修饰
            replacedFunction.CanOverride = newFunction.CanOverride;
        }
예제 #4
0
 /// <summary>
 /// Adds the attribute internal.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <returns></returns>
 protected override CodeAttribute2 AddAttributeInternal(string name)
 {
     return(_codeElement.AddAttribute(name, String.Empty, null) as CodeAttribute2);
 }
예제 #5
0
        private bool AddMethodCall(CodeElements codeElements, string className, string targetMethodName, string methodCall)
        {
            CodeClass    featureReceiverClass = GetClassByName(codeElements, className);
            CodeFunction function             = null;
            bool         result = false;

            if (featureReceiverClass != null)
            {
                //try to find the targetMethodName and if found then add the methodCall at the end
                foreach (CodeElement codeElement in featureReceiverClass.Members)
                {
                    if (codeElement.Kind == vsCMElement.vsCMElementFunction)
                    {
                        if (codeElement.Name == targetMethodName)
                        {
                            function = codeElement as CodeFunction;
                        }
                    }
                }

                if (function == null)
                {
                    //method not found (SPFeatureReceiverProperties properties)
                    function = featureReceiverClass.AddFunction(targetMethodName, vsCMFunction.vsCMFunctionFunction, "void", 0, vsCMAccess.vsCMAccessPublic, null);
                    CodeFunction2 function2 = function as CodeFunction2;
                    function2.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
                    function.AddParameter("properties", "SPFeatureReceiverProperties", -1);
                    function.AddAttribute("SharePointPermission", "(SecurityAction.LinkDemand, ObjectModel = true)");
                    Helpers.LogMessage(function.DTE, function.DTE, Helpers.GetFullPathOfProjectItem(function.ProjectItem) + ": Added method '" + methodCall + "'");
                }

                if (function != null)
                {
                    EditPoint2 editPoint = (EditPoint2)function.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

                    //get indent of editpoint (at the end of the function
                    int charsBefore = editPoint.AbsoluteCharOffset;
                    int lineAdded   = editPoint.Line;

                    if (!methodCall.StartsWith("this."))
                    {
                        //add this. to be StyleCop conform
                        methodCall = "this." + methodCall;
                    }

                    editPoint.InsertNewLine(1);
                    editPoint.Insert("// Call to method " + methodCall);
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 2);
                    editPoint.Insert(methodCall);
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 2);

                    Helpers.LogMessage(function.DTE, function.DTE, Helpers.GetFullPathOfProjectItem(function.ProjectItem) + "(" + lineAdded.ToString() + ",0): Added code to method '" + methodCall + "'");

                    result = true;
                }
            }
            else
            {
                throw new Exception("Class " + className + " not found");
            }
            return(result);
        }
예제 #6
0
        /// <summary>
        /// 重构方法
        /// </summary>
        /// <param name="replacedFunction"></param>
        /// <param name="newFunction"></param>
        private void RefactorMethod(CodeFunction replacedFunction, CodeFunction newFunction)
        {
            //清除方法参数
            while (replacedFunction.Parameters.Count > 0)
            {
                replacedFunction.RemoveParameter(replacedFunction.Parameters.Count);
            }

            //清除方法属性
            while (replacedFunction.Attributes.Count > 0)
            {
                ((CodeAttribute)replacedFunction.Attributes.Item(replacedFunction.Attributes.Count)).Delete();
            }

            //添加参数
            for (int i = 1; i <= newFunction.Parameters.Count; i++)
            {
                CodeParameter2 parameter = newFunction.Parameters.Item(i) as CodeParameter2;
                CodeParameter2 cloneParameter = replacedFunction.AddParameter(parameter.FullName, parameter.Type.AsFullName, i) as CodeParameter2;
                cloneParameter.DefaultValue = parameter.DefaultValue;
                cloneParameter.ParameterKind = parameter.ParameterKind;
            }

            //添加属性
            for (int i = 1; i <= newFunction.Attributes.Count; i++)
            {
                CodeAttribute2 attribute = newFunction.Attributes.Item(i) as CodeAttribute2;
                replacedFunction.AddAttribute(attribute.Name, attribute.Value, i);
            }

            //方法名
            replacedFunction.Name = newFunction.Name;
            //方法注释
            replacedFunction.Comment = newFunction.Comment;
            //方法说明
            replacedFunction.DocComment = newFunction.DocComment;
            //静态修饰
            replacedFunction.IsShared = newFunction.IsShared;
            //抽象修饰
            replacedFunction.MustImplement = newFunction.MustImplement;
            //重载修饰
            replacedFunction.CanOverride = newFunction.CanOverride;
        }
예제 #7
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);
        }
예제 #8
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);
        }