PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/StackTraceAnalysers/FunctionParser.cs

#
C# | 92 lines | 50 code | 15 blank | 27 comment | 14 complexity | 162330b6b1b8214ff15fc1be246a88ae MD5 | raw file
 1// ****************************************************************
 2// This is free software licensed under the NUnit license. You may
 3// obtain a copy of the license at http://nunit.org
 4// ****************************************************************
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Text;
 9using NUnit.UiException.StackTraceAnalyzers;
10
11namespace NUnit.UiException.StackTraceAnalysers
12{
13    /// <summary>
14    /// This class is responsible for locating and initializing
15    /// RawError.Function property with the function name as it
16    /// is mentioned in the stack trace line.
17    ///   To work correclty, the class makes some
18    /// assumptions concerning the function format.
19    ///   A function name is made of two parts: [name][args]
20    /// where [name] refers to a string of characters that doesn't
21    /// contain ' ' and [args] refers to a string delimited by
22    /// '(' and ')'.
23    /// </summary>
24    public class FunctionParser :
25        IErrorParser
26    {
27        #region IErrorParser Membres
28
29        /// <summary>
30        /// Try to match a function name by reading RawError.Input.
31        /// If a match is found, the method outputs the result into
32        /// RawError.Function and returns true.
33        /// </summary>
34        /// <param name="parser">An instance of parser, this parameter
35        /// cannot be null.</param>
36        /// <param name="args">An instance of RawError. This parameter
37        /// cannot be null.</param>
38        /// <returns>True if a match occurs, false otherwise.</returns>
39        public bool TryParse(StackTraceParser parser, RawError args)
40        {
41            int posEndingParenthesis;
42            int posOpeningParenthesis;
43            int posName;
44            int endName;
45            string res;
46            int i;
47
48            UiExceptionHelper.CheckNotNull(parser, "parser");
49            UiExceptionHelper.CheckNotNull(args, "args");
50
51            posEndingParenthesis = args.Input.LastIndexOf(")");
52            posOpeningParenthesis = args.Input.LastIndexOf("(");
53
54            if (posEndingParenthesis == -1 || posOpeningParenthesis == -1 ||
55                posOpeningParenthesis > posEndingParenthesis)
56                return (false);
57
58            endName = posOpeningParenthesis;
59            for (i = posOpeningParenthesis - 1; i >= 0; i--)
60            {
61                if (args.Input[i] != ' ')
62                    break;
63
64                endName = i;
65            }
66
67            posName = -1;
68            for (i = endName - 1; i >= 0; i--)
69            {
70                if (args.Input[i] == ' ')
71                    break;
72
73                posName = i;
74            }
75
76            // Added this test to allow for odd case where we would
77            // otherwise include the leading "at" or "à" in name.
78            if (posName == 0)
79                return false;
80
81            if (posName == -1)
82                return (false);
83
84            res = args.Input.Substring(posName, posEndingParenthesis - posName + 1);
85            args.Function = res;
86            
87            return (true);
88        }       
89
90        #endregion
91    }
92}