PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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