-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSpanAsserts.cs
213 lines (193 loc) · 8.17 KB
/
SpanAsserts.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
#pragma warning disable CA1052 // Static holder types should be static
#pragma warning disable IDE0018 // Inline variable declaration
#pragma warning disable IDE0161 // Convert to file-scoped namespace
#if XUNIT_SPAN
#if XUNIT_NULLABLE
#nullable enable
#endif
using System;
using Xunit.Sdk;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
// While there is an implicit conversion operator from Span<T> to ReadOnlySpan<T>, the
// compiler still stumbles to do this automatically, which means we end up with lots of overloads
// with various arrangements of Span<T> and ReadOnlySpan<T>.
// Also note that these classes will convert nulls into empty arrays automatically, since there
// is no way to represent a null readonly struct.
/// <summary>
/// Verifies that a span contains a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-span is not present inside the span</exception>
public static void Contains<T>(
Span<T> expectedSubSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
Contains((ReadOnlySpan<T>)expectedSubSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that a span contains a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-span is not present inside the span</exception>
public static void Contains<T>(
Span<T> expectedSubSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T> =>
Contains((ReadOnlySpan<T>)expectedSubSpan, actualSpan);
/// <summary>
/// Verifies that a span contains a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-span is not present inside the span</exception>
public static void Contains<T>(
ReadOnlySpan<T> expectedSubSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
Contains(expectedSubSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that a span contains a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-span is not present inside the span</exception>
public static void Contains<T>(
ReadOnlySpan<T> expectedSubSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T>
{
if (actualSpan.IndexOf(expectedSubSpan) < 0)
throw ContainsException.ForSubSpanNotFound(
CollectionTracker<T>.FormatStart(expectedSubSpan),
CollectionTracker<T>.FormatStart(actualSpan)
);
}
/// <summary>
/// Verifies that a span does not contain a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected not to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-span is present inside the span</exception>
public static void DoesNotContain<T>(
Span<T> expectedSubSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
DoesNotContain((ReadOnlySpan<T>)expectedSubSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that a span does not contain a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected not to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-span is present inside the span</exception>
public static void DoesNotContain<T>(
Span<T> expectedSubSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T> =>
DoesNotContain((ReadOnlySpan<T>)expectedSubSpan, actualSpan);
/// <summary>
/// Verifies that a span does not contain a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected not to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-span is present inside the span</exception>
public static void DoesNotContain<T>(
ReadOnlySpan<T> expectedSubSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
DoesNotContain(expectedSubSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that a span does not contain a given sub-span
/// </summary>
/// <param name="expectedSubSpan">The sub-span expected not to be in the span</param>
/// <param name="actualSpan">The span to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-span is present inside the span</exception>
public static void DoesNotContain<T>(
ReadOnlySpan<T> expectedSubSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T>
{
var idx = actualSpan.IndexOf(expectedSubSpan);
if (idx > -1)
{
int? failurePointerIndent;
var formattedExpected = CollectionTracker<T>.FormatStart(expectedSubSpan);
var formattedActual = CollectionTracker<T>.FormatIndexedMismatch(actualSpan, idx, out failurePointerIndent);
throw DoesNotContainException.ForSubSpanFound(
formattedExpected,
idx,
failurePointerIndent,
formattedActual
);
}
}
/// <summary>
/// Verifies that a span and an array contain the same values in the same order.
/// </summary>
/// <param name="expectedSpan">The expected span value.</param>
/// <param name="actualArray">The actual array value.</param>
/// <exception cref="EqualException">Thrown when the collections are not equal.</exception>
// This overload exists per https://github.com/xunit/xunit/discussions/3021
public static void Equal<T>(
ReadOnlySpan<T> expectedSpan,
T[] actualArray)
where T : IEquatable<T> =>
Equal(expectedSpan, actualArray.AsSpan());
/// <summary>
/// Verifies that two spans contain the same values in the same order.
/// </summary>
/// <param name="expectedSpan">The expected span value.</param>
/// <param name="actualSpan">The actual span value.</param>
/// <exception cref="EqualException">Thrown when the spans are not equal.</exception>
public static void Equal<T>(
Span<T> expectedSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
Equal((ReadOnlySpan<T>)expectedSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that two spans contain the same values in the same order.
/// </summary>
/// <param name="expectedSpan">The expected span value.</param>
/// <param name="actualSpan">The actual span value.</param>
/// <exception cref="EqualException">Thrown when the spans are not equal.</exception>
public static void Equal<T>(
Span<T> expectedSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T> =>
Equal((ReadOnlySpan<T>)expectedSpan, actualSpan);
/// <summary>
/// Verifies that two spans contain the same values in the same order.
/// </summary>
/// <param name="expectedSpan">The expected span value.</param>
/// <param name="actualSpan">The actual span value.</param>
/// <exception cref="EqualException">Thrown when the spans are not equal.</exception>
public static void Equal<T>(
ReadOnlySpan<T> expectedSpan,
Span<T> actualSpan)
where T : IEquatable<T> =>
Equal(expectedSpan, (ReadOnlySpan<T>)actualSpan);
/// <summary>
/// Verifies that two spans contain the same values in the same order.
/// </summary>
/// <param name="expectedSpan">The expected span value.</param>
/// <param name="actualSpan">The actual span value.</param>
/// <exception cref="EqualException">Thrown when the spans are not equal.</exception>
public static void Equal<T>(
ReadOnlySpan<T> expectedSpan,
ReadOnlySpan<T> actualSpan)
where T : IEquatable<T>
{
if (!expectedSpan.SequenceEqual(actualSpan))
Equal<object>(expectedSpan.ToArray(), actualSpan.ToArray());
}
}
}
#endif