Пример #1
0
        /// <summary>
        /// add a default constructor.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="moveToCorrectPosition">if set to <c>true</c> [move to correct position].</param>
        /// <returns>The constructor.</returns>
        public static CodeFunction AddDefaultConstructor(
            this CodeClass instance,
            bool moveToCorrectPosition)
        {
            TraceService.WriteLine("CodeClassExtensions::AddDefaultConstructor file=" + instance.Name);

            CodeFunction codeFunction = instance.AddFunction(
                instance.Name,
                vsCMFunction.vsCMFunctionConstructor,
                vsCMTypeRef.vsCMTypeRefVoid,
                0,
                vsCMAccess.vsCMAccessPublic,
                null);

            codeFunction.GetEndPoint().CreateEditPoint().InsertNewLine();
            string comment = "<doc><summary>\r\nInitializes a new instance of the " + instance.Name + " class.\r\n</summary></doc>\r\n";

            codeFunction.DocComment = comment;

            if (moveToCorrectPosition)
            {
                TraceService.WriteLine("Move to correct position");

                IEnumerable <CodeVariable> variables = instance.GetVariables();

                CodeVariable lastVariable = variables.LastOrDefault();

                if (lastVariable != null)
                {
                    EditPoint startPoint = codeFunction.StartPoint.CreateEditPoint();
                    EditPoint endPoint   = codeFunction.EndPoint.CreateEditPoint();
                    string    text       = startPoint.GetText(endPoint);

                    //// remove current code Function text
                    instance.RemoveMember(codeFunction);

                    if (endPoint.GetText(1) == Environment.NewLine)
                    {
                        TraceService.WriteLine("Delete line");
                        endPoint.Delete(1);
                    }

                    EditPoint editPoint = lastVariable.EndPoint.CreateEditPoint();
                    editPoint.Insert(string.Format("{0}{1}{2}", Environment.NewLine, Environment.NewLine, text));

                    //// we need to re find the function as we have deleted this one!
                    codeFunction = instance.GetFunction(instance.Name);

                    codeFunction.DocComment = comment;
                }
            }

            return(codeFunction);
        }
Пример #2
0
        /// <summary>
        /// Implements the mock code.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="codeSnippet">The code snippet.</param>
        public static void ImplementMockCode(
            this CodeClass instance,
            CodeSnippet codeSnippet)
        {
            TraceService.WriteLine("CodeClassExtensions::ImplementMockCode file=" + instance.Name);

            CodeFunction codeFunction = instance.GetFunction(codeSnippet.TestInitMethod);

            if (codeFunction != null)
            {
                if (string.IsNullOrEmpty(codeSnippet.GetMockInitCode()) == false)
                {
                    codeFunction.InsertCode(codeSnippet.GetMockInitCode(), true);
                }

                if (string.IsNullOrEmpty(codeSnippet.GetMockConstructorCode()) == false)
                {
                    string code = codeFunction.GetCode();

                    string testFileName = instance.Name;

                    //// remove Test at the start - need a better way of doing this!
                    string fileName = testFileName.Replace("Test", string.Empty);

                    //// TODO : this wont always work if the function spans multiple lines!
                    int index = code.IndexOf(fileName + "(", StringComparison.Ordinal);

                    if (index != 0)
                    {
                        string seperator = string.Empty;

                        //// here we are looking for the closing bracket
                        //// if we dont have a closing bracket then we already have something
                        //// on the constructor and therefore need a ',' to make it work!
                        if (code.Substring(index + fileName.Length + 1, 1) != ")")
                        {
                            //// TODO : do we want to create a new line too!
                            seperator = ", ";
                        }

                        StringBuilder sb = new StringBuilder();
                        sb.Append(code.Substring(0, index + fileName.Length + 1));
                        sb.Append(codeSnippet.GetMockConstructorCode() + seperator);
                        sb.Append(code.Substring(index + fileName.Length + 1));

                        codeFunction.ReplaceCode(sb.ToString());
                    }
                }
            }
        }