forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAsserts.cs
291 lines (260 loc) · 13.6 KB
/
StringAsserts.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Text.RegularExpressions;
using Xunit.Sdk;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Verifies that a string contains a given sub-string, using the current culture.
/// </summary>
/// <param name="expectedSubstring">The sub-string expected to be in the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-string is not present inside the string</exception>
public static void Contains(string expectedSubstring, string actualString)
{
Contains(expectedSubstring, actualString, StringComparison.CurrentCulture);
}
/// <summary>
/// Verifies that a string contains a given sub-string, using the given comparison type.
/// </summary>
/// <param name="expectedSubstring">The sub-string expected to be in the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <param name="comparisonType">The type of string comparison to perform</param>
/// <exception cref="ContainsException">Thrown when the sub-string is not present inside the string</exception>
public static void Contains(string expectedSubstring, string actualString, StringComparison comparisonType)
{
if (actualString == null || actualString.IndexOf(expectedSubstring, comparisonType) < 0)
throw new ContainsException(expectedSubstring, actualString);
}
/// <summary>
/// Verifies that a string does not contain a given sub-string, using the current culture.
/// </summary>
/// <param name="expectedSubstring">The sub-string which is expected not to be in the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-string is present inside the string</exception>
public static void DoesNotContain(string expectedSubstring, string actualString)
{
DoesNotContain(expectedSubstring, actualString, StringComparison.CurrentCulture);
}
/// <summary>
/// Verifies that a string does not contain a given sub-string, using the current culture.
/// </summary>
/// <param name="expectedSubstring">The sub-string which is expected not to be in the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <param name="comparisonType">The type of string comparison to perform</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-string is present inside the given string</exception>
public static void DoesNotContain(string expectedSubstring, string actualString, StringComparison comparisonType)
{
if (actualString != null && actualString.IndexOf(expectedSubstring, comparisonType) >= 0)
throw new DoesNotContainException(expectedSubstring, actualString);
}
/// <summary>
/// Verifies that a string starts with a given string, using the current culture.
/// </summary>
/// <param name="expectedStartString">The string expected to be at the start of the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="ContainsException">Thrown when the string does not start with the expected string</exception>
public static void StartsWith(string expectedStartString, string actualString)
{
StartsWith(expectedStartString, actualString, StringComparison.CurrentCulture);
}
/// <summary>
/// Verifies that a string starts with a given string, using the given comparison type.
/// </summary>
/// <param name="expectedStartString">The string expected to be at the start of the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <param name="comparisonType">The type of string comparison to perform</param>
/// <exception cref="ContainsException">Thrown when the string does not start with the expected string</exception>
public static void StartsWith(string expectedStartString, string actualString, StringComparison comparisonType)
{
if (expectedStartString == null || actualString == null || !actualString.StartsWith(expectedStartString, comparisonType))
throw new StartsWithException(expectedStartString, actualString);
}
/// <summary>
/// Verifies that a string ends with a given string, using the current culture.
/// </summary>
/// <param name="expectedEndString">The string expected to be at the end of the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="ContainsException">Thrown when the string does not end with the expected string</exception>
public static void EndsWith(string expectedEndString, string actualString)
{
EndsWith(expectedEndString, actualString, StringComparison.CurrentCulture);
}
/// <summary>
/// Verifies that a string ends with a given string, using the given comparison type.
/// </summary>
/// <param name="expectedEndString">The string expected to be at the end of the string</param>
/// <param name="actualString">The string to be inspected</param>
/// <param name="comparisonType">The type of string comparison to perform</param>
/// <exception cref="ContainsException">Thrown when the string does not end with the expected string</exception>
public static void EndsWith(string expectedEndString, string actualString, StringComparison comparisonType)
{
if (expectedEndString == null || actualString == null || !actualString.EndsWith(expectedEndString, comparisonType))
throw new EndsWithException(expectedEndString, actualString);
}
/// <summary>
/// Verifies that a string matches a regular expression.
/// </summary>
/// <param name="expectedRegexPattern">The regex pattern expected to match</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="MatchesException">Thrown when the string does not match the regex pattern</exception>
public static void Matches(string expectedRegexPattern, string actualString)
{
Assert.GuardArgumentNotNull("expectedRegexPattern", expectedRegexPattern);
if (actualString == null || !Regex.IsMatch(actualString, expectedRegexPattern))
throw new MatchesException(expectedRegexPattern, actualString);
}
/// <summary>
/// Verifies that a string matches a regular expression.
/// </summary>
/// <param name="expectedRegex">The regex expected to match</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="MatchesException">Thrown when the string does not match the regex</exception>
public static void Matches(Regex expectedRegex, string actualString)
{
Assert.GuardArgumentNotNull("expectedRegex", expectedRegex);
if (actualString == null || !expectedRegex.IsMatch(actualString))
throw new MatchesException(expectedRegex.ToString(), actualString);
}
/// <summary>
/// Verifies that a string does not match a regular expression.
/// </summary>
/// <param name="expectedRegexPattern">The regex pattern expected not to match</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="DoesNotMatchException">Thrown when the string matches the regex pattern</exception>
public static void DoesNotMatch(string expectedRegexPattern, string actualString)
{
Assert.GuardArgumentNotNull("expectedRegexPattern", expectedRegexPattern);
if (actualString != null && Regex.IsMatch(actualString, expectedRegexPattern))
throw new DoesNotMatchException(expectedRegexPattern, actualString);
}
/// <summary>
/// Verifies that a string does not match a regular expression.
/// </summary>
/// <param name="expectedRegex">The regex expected not to match</param>
/// <param name="actualString">The string to be inspected</param>
/// <exception cref="DoesNotMatchException">Thrown when the string matches the regex</exception>
public static void DoesNotMatch(Regex expectedRegex, string actualString)
{
Assert.GuardArgumentNotNull("expectedRegex", expectedRegex);
if (actualString != null && expectedRegex.IsMatch(actualString))
throw new DoesNotMatchException(expectedRegex.ToString(), actualString);
}
/// <summary>
/// Verifies that two strings are equivalent.
/// </summary>
/// <param name="expected">The expected string value.</param>
/// <param name="actual">The actual string value.</param>
/// <exception cref="EqualException">Thrown when the strings are not equivalent.</exception>
public static void Equal(string expected, string actual)
{
Equal(expected, actual, false, false, false);
}
/// <summary>
/// Verifies that two strings are equivalent.
/// </summary>
/// <param name="expected">The expected string value.</param>
/// <param name="actual">The actual string value.</param>
/// <param name="ignoreCase">If set to <c>true</c>, ignores cases differences. The invariant culture is used.</param>
/// <param name="ignoreLineEndingDifferences">If set to <c>true</c>, treats \r\n, \r, and \n as equivalent.</param>
/// <param name="ignoreWhiteSpaceDifferences">If set to <c>true</c>, treats spaces and tabs (in any non-zero quantity) as equivalent.</param>
/// <exception cref="EqualException">Thrown when the strings are not equivalent.</exception>
public static void Equal(string expected, string actual, bool ignoreCase = false, bool ignoreLineEndingDifferences = false, bool ignoreWhiteSpaceDifferences = false)
{
// Start out assuming the one of the values is null
int expectedIndex = -1;
int actualIndex = -1;
int expectedLength = 0;
int actualLength = 0;
if (expected == null)
{
if (actual == null)
return;
}
else if (actual != null)
{
// Walk the string, keeping separate indices since we can skip variable amounts of
// data based on ignoreLineEndingDifferences and ignoreWhiteSpaceDifferences.
expectedIndex = 0;
actualIndex = 0;
expectedLength = expected.Length;
actualLength = actual.Length;
while (expectedIndex < expectedLength && actualIndex < actualLength)
{
char expectedChar = expected[expectedIndex];
char actualChar = actual[actualIndex];
if (ignoreLineEndingDifferences && IsLineEnding(expectedChar) && IsLineEnding(actualChar))
{
expectedIndex = SkipLineEnding(expected, expectedIndex);
actualIndex = SkipLineEnding(actual, actualIndex);
}
else if (ignoreWhiteSpaceDifferences && IsWhiteSpace(expectedChar) && IsWhiteSpace(actualChar))
{
expectedIndex = SkipWhitespace(expected, expectedIndex);
actualIndex = SkipWhitespace(actual, actualIndex);
}
else
{
if (ignoreCase)
{
expectedChar = Char.ToUpperInvariant(expectedChar);
actualChar = Char.ToUpperInvariant(actualChar);
}
if (expectedChar != actualChar)
{
break;
}
expectedIndex++;
actualIndex++;
}
}
}
if (expectedIndex < expectedLength || actualIndex < actualLength)
{
throw new EqualException(expected, actual, expectedIndex, actualIndex);
}
}
static bool IsLineEnding(char c)
{
return c == '\r' || c == '\n';
}
static bool IsWhiteSpace(char c)
{
return c == ' ' || c == '\t';
}
static int SkipLineEnding(string value, int index)
{
if (value[index] == '\r')
{
++index;
}
if (index < value.Length && value[index] == '\n')
{
++index;
}
return index;
}
static int SkipWhitespace(string value, int index)
{
while (index < value.Length)
{
switch (value[index])
{
case ' ':
case '\t':
index++;
break;
default:
return index;
}
}
return index;
}
}
}