PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/MethodHelper.cs

#
C# | 191 lines | 173 code | 13 blank | 5 comment | 69 complexity | ba6d6ce7fba2793df4536d4802348366 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace NUnit.Core
  10. {
  11. public class MethodHelper
  12. {
  13. public static string GetDisplayName(MethodInfo method, object[] arglist)
  14. {
  15. StringBuilder sb = new StringBuilder(method.Name);
  16. #if NET_2_0
  17. if (method.IsGenericMethod)
  18. {
  19. sb.Append("<");
  20. int cnt = 0;
  21. foreach (Type t in method.GetGenericArguments())
  22. {
  23. if (cnt++ > 0) sb.Append(",");
  24. sb.Append(t.Name);
  25. }
  26. sb.Append(">");
  27. }
  28. #endif
  29. if (arglist != null)
  30. {
  31. sb.Append("(");
  32. for (int i = 0; i < arglist.Length; i++)
  33. {
  34. if (i > 0) sb.Append(",");
  35. sb.Append(GetDisplayString(arglist[i]));
  36. }
  37. sb.Append(")");
  38. }
  39. return sb.ToString();
  40. }
  41. private static string GetDisplayString(object arg)
  42. {
  43. string display = arg == null
  44. ? "null"
  45. : Convert.ToString( arg, System.Globalization.CultureInfo.InvariantCulture);
  46. if (arg is double)
  47. {
  48. double d = (double)arg;
  49. if (double.IsNaN(d))
  50. display = "double.NaN";
  51. else if (double.IsPositiveInfinity(d))
  52. display = "double.PositiveInfinity";
  53. else if (double.IsNegativeInfinity(d))
  54. display = "double.NegativeInfinity";
  55. else if (d == double.MaxValue)
  56. display = "double.MaxValue";
  57. else if (d == double.MinValue)
  58. display = "double.MinValue";
  59. else
  60. {
  61. if (display.IndexOf('.') == -1)
  62. display += ".0";
  63. display += "d";
  64. }
  65. }
  66. else if (arg is float)
  67. {
  68. float f = (float)arg;
  69. if (float.IsNaN(f))
  70. display = "float.NaN";
  71. else if (float.IsPositiveInfinity(f))
  72. display = "float.PositiveInfinity";
  73. else if (float.IsNegativeInfinity(f))
  74. display = "float.NegativeInfinity";
  75. else if (f == float.MaxValue)
  76. display = "float.MaxValue";
  77. else if (f == float.MinValue)
  78. display = "float.MinValue";
  79. else
  80. {
  81. if (display.IndexOf('.') == -1)
  82. display += ".0";
  83. display += "f";
  84. }
  85. }
  86. else if (arg is decimal)
  87. {
  88. decimal d = (decimal)arg;
  89. if (d == decimal.MinValue)
  90. display = "decimal.MinValue";
  91. else if (d == decimal.MaxValue)
  92. display = "decimal.MaxValue";
  93. else
  94. display += "m";
  95. }
  96. else if (arg is long)
  97. {
  98. long l = (long)arg;
  99. if (l == long.MinValue)
  100. display = "long.MinValue";
  101. else if (l == long.MinValue)
  102. display = "long.MaxValue";
  103. else
  104. display += "L";
  105. }
  106. else if (arg is ulong)
  107. {
  108. ulong ul = (ulong)arg;
  109. if (ul == ulong.MinValue)
  110. display = "ulong.MinValue";
  111. else if (ul == ulong.MinValue)
  112. display = "ulong.MaxValue";
  113. else
  114. display += "UL";
  115. }
  116. else if (arg is string)
  117. {
  118. StringBuilder sb = new StringBuilder();
  119. sb.Append("\"");
  120. foreach (char c in (string)arg)
  121. sb.Append(EscapeControlChar(c));
  122. sb.Append("\"");
  123. display = sb.ToString();
  124. }
  125. else if (arg is char)
  126. {
  127. display = "\'" + EscapeControlChar((char)arg) + "\'";
  128. }
  129. else if (arg is int)
  130. {
  131. int ival = (int)arg;
  132. if (ival == int.MaxValue)
  133. display = "int.MaxValue";
  134. else if (ival == int.MinValue)
  135. display = "int.MinValue";
  136. }
  137. return display;
  138. }
  139. private static string EscapeControlChar(char c)
  140. {
  141. switch (c)
  142. {
  143. case '\'':
  144. return "\\\'";
  145. case '\"':
  146. return "\\\"";
  147. case '\\':
  148. return "\\\\";
  149. case '\0':
  150. return "\\0";
  151. case '\a':
  152. return "\\a";
  153. case '\b':
  154. return "\\b";
  155. case '\f':
  156. return "\\f";
  157. case '\n':
  158. return "\\n";
  159. case '\r':
  160. return "\\r";
  161. case '\t':
  162. return "\\t";
  163. case '\v':
  164. return "\\v";
  165. case '\x0085':
  166. case '\x2028':
  167. case '\x2029':
  168. return string.Format("\\x{0:X4}", (int)c);
  169. default:
  170. return char.IsControl(c) || (int)c > 128
  171. ? string.Format("\\x{0:X4}", (int)c)
  172. : c.ToString();
  173. }
  174. }
  175. }
  176. }