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