-
Notifications
You must be signed in to change notification settings - Fork 178
/
PrettyPrintingExtensions.cs
234 lines (203 loc) · 6.79 KB
/
PrettyPrintingExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Machine.Specifications.Utility.Internal
{
public static class PrettyPrintingExtensions
{
private const string CurlyBraceSurround = "{{{0}}}";
private static readonly Regex EscapeNonFormatBraces = new Regex(@"{([^\d].*?)}", RegexOptions.Compiled | RegexOptions.Singleline);
public static string FormatErrorMessage(object actualObject, object expectedObject)
{
if (actualObject == null || !(actualObject is string) ||
expectedObject == null || !(expectedObject is string))
{
// format objects
var actual = actualObject.ToUsefulString();
var expected = expectedObject.ToUsefulString();
return string.Format(" Expected: {1}{0} But was: {2}", Environment.NewLine, expected, actual);
}
else
{
// format strings
var actual = actualObject as string;
var expected = expectedObject as string;
var expectedStringLengthMessage = GetExpectedStringLengthMessage(expected.Length, actual.Length);
var firstIndexOfFirstDifference = GetFirstIndexOfFirstDifference(actual, expected);
string actualReported;
string expectedReported;
GetStringsAroundFirstDifference(expected, actual, firstIndexOfFirstDifference, out actualReported, out expectedReported);
var count = GetFirstIndexOfFirstDifference(actualReported, expectedReported);
return string.Format(
" {1} Strings differ at index {2}.{0}" +
" Expected: \"{3}\"{0}" +
" But was: \"{4}\"{0}" +
" -----------{5}^",
Environment.NewLine,
expectedStringLengthMessage,
firstIndexOfFirstDifference,
expectedReported,
actualReported,
new string('-', count));
}
}
static void GetStringsAroundFirstDifference(string expected, string actual, int firstIndexOfFirstDifference, out string actualReported, out string expectedReported)
{
var left = firstIndexOfFirstDifference;
var actualRight = firstIndexOfFirstDifference;
var expectedRight = firstIndexOfFirstDifference;
var keepAugmenting = true;
while (keepAugmenting &&
IsInCopyFrameLength(left, actualRight, actual.Length) &&
IsInCopyFrameLength(left, expectedRight, expected.Length))
{
keepAugmenting = false;
if (left > 0)
{
left--;
keepAugmenting = true;
}
if (IsInCopyFrameLength(left, actualRight, actual.Length) &&
IsInCopyFrameLength(left, expectedRight, expected.Length))
{
if (actual.Length > actualRight)
{
actualRight++;
keepAugmenting = true;
}
if (expected.Length > expectedRight)
{
expectedRight++;
keepAugmenting = true;
}
}
}
actualReported = actual.Substring(left, actualRight - left);
expectedReported = expected.Substring(left, expectedRight - left);
if (left != 0)
{
actualReported = "..." + actualReported;
expectedReported = "..." + expectedReported;
}
if (actualRight != actual.Length || expectedRight != expected.Length)
{
actualReported = actualReported + "...";
expectedReported = expectedReported + "...";
}
}
static bool IsInCopyFrameLength(int start, int end, int max)
{
int length = end - start;
if (start > 0)
length += 3;
if (end < max)
length += 3;
return length < 64;
}
static int GetFirstIndexOfFirstDifference(string actual, string expected)
{
for (var i = 0; i < actual.Length; i++)
{
if (expected.Length <= i || expected[i] != actual[i])
{
return i;
}
}
return actual.Length;
}
static string GetExpectedStringLengthMessage(int actual, int expected)
{
if (actual == expected)
{
return string.Format("String lengths are both {0}.", actual);
}
return string.Format("Expected string length {0} but was {1}.", actual, expected);
}
static string Tab(this string str)
{
if (string.IsNullOrEmpty(str))
{
return "";
}
var split = str.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
var sb = new StringBuilder();
sb.Append(" " + split[0]);
foreach (var part in split.Skip(1))
{
sb.AppendLine();
sb.Append(" " + part);
}
return sb.ToString();
}
public static string EachToUsefulString<T>(this IEnumerable<T> enumerable)
{
var sb = new StringBuilder();
sb.AppendLine("{");
sb.Append(String.Join(",\r\n", enumerable.Select(x => x.ToUsefulString().Tab()).Take(10).ToArray()));
if (enumerable.Count() > 10)
{
if (enumerable.Count() > 11)
{
sb.AppendLine(String.Format(",\r\n ...({0} more elements)", enumerable.Count() - 10));
}
else
{
sb.AppendLine(",\r\n" + enumerable.Last().ToUsefulString().Tab());
}
}
else
{
sb.AppendLine();
}
sb.AppendLine("}");
return sb.ToString();
}
internal static string ToUsefulString(this object obj)
{
string str;
if (obj == null)
{
return "[null]";
}
if (obj.GetType() == typeof(string))
{
str = (string) obj;
return "\"" + str.Replace("\n", "\\n") + "\"";
}
if (obj.GetType().IsValueType)
{
return "[" + obj + "]";
}
if (obj is IEnumerable)
{
var enumerable = ((IEnumerable) obj).Cast<object>();
return obj.GetType() + ":\r\n" + enumerable.EachToUsefulString();
}
str = obj.ToString();
if (str == null || str.Trim() == "")
{
return String.Format("{0}:[]", obj.GetType());
}
str = str.Trim();
if (str.Contains("\n"))
{
return string.Format(@"{1}:
[
{0}
]", str.Tab(), obj.GetType());
}
if (obj.GetType().ToString() == str)
{
return obj.GetType().ToString();
}
return string.Format("{0}:[{1}]", obj.GetType(), str);
}
internal static string EnsureSafeFormat(this string message)
{
return EscapeNonFormatBraces.Replace(message, match => string.Format(CurlyBraceSurround, match.Groups[0]));
}
}
}