This repository was archived by the owner on Jun 7, 2024. It is now read-only.
forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAsserts.cs
More file actions
405 lines (369 loc) · 13.7 KB
/
Copy pathStringAsserts.cs
File metadata and controls
405 lines (369 loc) · 13.7 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#if XUNIT_NULLABLE
#nullable enable
#endif
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,
#if XUNIT_NULLABLE
string? actualString) =>
#else
string actualString) =>
#endif
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,
#if XUNIT_NULLABLE
string? actualString,
#else
string actualString,
#endif
StringComparison comparisonType)
{
GuardArgumentNotNull(nameof(expectedSubstring), expectedSubstring);
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,
#if XUNIT_NULLABLE
string? actualString) =>
#else
string actualString) =>
#endif
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,
#if XUNIT_NULLABLE
string? actualString,
#else
string actualString,
#endif
StringComparison comparisonType)
{
GuardArgumentNotNull(nameof(expectedSubstring), expectedSubstring);
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(
#if XUNIT_NULLABLE
string? expectedStartString,
string? actualString) =>
#else
string expectedStartString,
string actualString) =>
#endif
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(
#if XUNIT_NULLABLE
string? expectedStartString,
string? actualString,
#else
string expectedStartString,
string actualString,
#endif
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(
#if XUNIT_NULLABLE
string? expectedEndString,
string? actualString) =>
#else
string expectedEndString,
string actualString) =>
#endif
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(
#if XUNIT_NULLABLE
string? expectedEndString,
string? actualString,
#else
string expectedEndString,
string actualString,
#endif
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,
#if XUNIT_NULLABLE
string? actualString)
#else
string actualString)
#endif
{
GuardArgumentNotNull(nameof(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,
#if XUNIT_NULLABLE
string? actualString)
#else
string actualString)
#endif
{
GuardArgumentNotNull(nameof(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,
#if XUNIT_NULLABLE
string? actualString)
#else
string actualString)
#endif
{
GuardArgumentNotNull(nameof(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,
#if XUNIT_NULLABLE
string? actualString)
#else
string actualString)
#endif
{
GuardArgumentNotNull(nameof(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(
#if XUNIT_NULLABLE
string? expected,
string? actual) =>
#else
string expected,
string actual) =>
#endif
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>
/// <param name="ignoreAllWhiteSpace">If set to <c>true</c>, ignores all white space differences during comparison.</param>
/// <exception cref="EqualException">Thrown when the strings are not equivalent.</exception>
public static void Equal(
#if XUNIT_NULLABLE
string? expected,
string? actual,
#else
string expected,
string actual,
#endif
bool ignoreCase = false,
bool ignoreLineEndingDifferences = false,
bool ignoreWhiteSpaceDifferences = false,
bool ignoreAllWhiteSpace = false)
{
#if XUNIT_SPAN
if (expected == null && actual == null)
return;
if (expected == null || actual == null)
throw new EqualException(expected, actual, -1, -1);
Equal(expected.AsSpan(), actual.AsSpan(), ignoreCase, ignoreLineEndingDifferences, ignoreWhiteSpaceDifferences, ignoreAllWhiteSpace);
#else
// 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;
// Block used to fix edge case of Equal("", " ") when ignoreAllWhiteSpace enabled.
if (ignoreAllWhiteSpace)
{
if (expectedLength == 0 && SkipWhitespace(actual, 0) == actualLength)
return;
if (actualLength == 0 && SkipWhitespace(expected, 0) == expectedLength)
return;
}
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 (ignoreAllWhiteSpace && (IsWhiteSpace(expectedChar) || IsWhiteSpace(actualChar)))
{
expectedIndex = SkipWhitespace(expected, expectedIndex);
actualIndex = SkipWhitespace(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);
#endif
}
#if !XUNIT_SPAN
static bool IsLineEnding(char c) =>
c == '\r' || c == '\n';
static bool IsWhiteSpace(char c) =>
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;
}
#endif
}
}