Repository navigation
Expand file tree
/
Copy pathMemoryAsserts.cs
More file actions
240 lines (219 loc) · 8.77 KB
/
Copy pathMemoryAsserts.cs
File metadata and controls
240 lines (219 loc) · 8.77 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
#pragma warning disable CA1052 // The Assert class is not static due to extensibility
#if XUNIT_NULLABLE
#nullable enable
#endif
using System;
using Xunit.Sdk;
namespace Xunit
{
partial class Assert
{
// While there is an implicit conversion operator from Memory<T> to ReadOnlyMemory<T>, the
// compiler still stumbles to do this automatically, which means we end up with lots of overloads
// with various arrangements of Memory<T> and ReadOnlyMemory<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 Memory contains a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-Memory is not present inside the Memory</exception>
public static void Contains<T>(
Memory<T> expectedSubMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Contains((ReadOnlyMemory<T>)expectedSubMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that a Memory contains a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-Memory is not present inside the Memory</exception>
public static void Contains<T>(
Memory<T> expectedSubMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Contains((ReadOnlyMemory<T>)expectedSubMemory, actualMemory);
/// <summary>
/// Verifies that a Memory contains a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-Memory is not present inside the Memory</exception>
public static void Contains<T>(
ReadOnlyMemory<T> expectedSubMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Contains(expectedSubMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that a Memory contains a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="ContainsException">Thrown when the sub-Memory is not present inside the Memory</exception>
public static void Contains<T>(
ReadOnlyMemory<T> expectedSubMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>?
#else
where T : IEquatable<T>
#endif
{
GuardArgumentNotNull(nameof(expectedSubMemory), expectedSubMemory);
if (actualMemory.Span.IndexOf(expectedSubMemory.Span) < 0)
throw ContainsException.ForSubMemoryNotFound(
CollectionTracker<T>.FormatStart(expectedSubMemory.Span),
CollectionTracker<T>.FormatStart(actualMemory.Span)
);
}
/// <summary>
/// Verifies that a Memory does not contain a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected not to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-Memory is present inside the Memory</exception>
public static void DoesNotContain<T>(
Memory<T> expectedSubMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
DoesNotContain((ReadOnlyMemory<T>)expectedSubMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that a Memory does not contain a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected not to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-Memory is present inside the Memory</exception>
public static void DoesNotContain<T>(
Memory<T> expectedSubMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
DoesNotContain((ReadOnlyMemory<T>)expectedSubMemory, actualMemory);
/// <summary>
/// Verifies that a Memory does not contain a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected not to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-Memory is present inside the Memory</exception>
public static void DoesNotContain<T>(
ReadOnlyMemory<T> expectedSubMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
DoesNotContain(expectedSubMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that a Memory does not contain a given sub-Memory
/// </summary>
/// <param name="expectedSubMemory">The sub-Memory expected not to be in the Memory</param>
/// <param name="actualMemory">The Memory to be inspected</param>
/// <exception cref="DoesNotContainException">Thrown when the sub-Memory is present inside the Memory</exception>
public static void DoesNotContain<T>(
ReadOnlyMemory<T> expectedSubMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>?
#else
where T : IEquatable<T>
#endif
{
GuardArgumentNotNull(nameof(expectedSubMemory), expectedSubMemory);
var expectedSpan = expectedSubMemory.Span;
var actualSpan = actualMemory.Span;
var idx = actualSpan.IndexOf(expectedSpan);
if (idx > -1)
{
var formattedExpected = CollectionTracker<T>.FormatStart(expectedSpan);
var formattedActual = CollectionTracker<T>.FormatIndexedMismatch(actualSpan, idx, out var failurePointerIndent);
throw DoesNotContainException.ForSubMemoryFound(formattedExpected, idx, failurePointerIndent, formattedActual);
}
}
/// <summary>
/// Verifies that two Memory values are equivalent.
/// </summary>
/// <param name="expectedMemory">The expected Memory value.</param>
/// <param name="actualMemory">The actual Memory value.</param>
/// <exception cref="EqualException">Thrown when the Memory values are not equivalent.</exception>
public static void Equal<T>(
Memory<T> expectedMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Equal((ReadOnlyMemory<T>)expectedMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that two Memory values are equivalent.
/// </summary>
/// <param name="expectedMemory">The expected Memory value.</param>
/// <param name="actualMemory">The actual Memory value.</param>
/// <exception cref="EqualException">Thrown when the Memory values are not equivalent.</exception>
public static void Equal<T>(
Memory<T> expectedMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Equal((ReadOnlyMemory<T>)expectedMemory, actualMemory);
/// <summary>
/// Verifies that two Memory values are equivalent.
/// </summary>
/// <param name="expectedMemory">The expected Memory value.</param>
/// <param name="actualMemory">The actual Memory value.</param>
/// <exception cref="EqualException">Thrown when the Memory values are not equivalent.</exception>
public static void Equal<T>(
ReadOnlyMemory<T> expectedMemory,
Memory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>? =>
#else
where T : IEquatable<T> =>
#endif
Equal(expectedMemory, (ReadOnlyMemory<T>)actualMemory);
/// <summary>
/// Verifies that two Memory values are equivalent.
/// </summary>
/// <param name="expectedMemory">The expected Memory value.</param>
/// <param name="actualMemory">The actual Memory value.</param>
/// <exception cref="EqualException">Thrown when the Memory values are not equivalent.</exception>
public static void Equal<T>(
ReadOnlyMemory<T> expectedMemory,
ReadOnlyMemory<T> actualMemory)
#if XUNIT_NULLABLE
where T : IEquatable<T>?
#else
where T : IEquatable<T>
#endif
{
GuardArgumentNotNull(nameof(expectedMemory), expectedMemory);
if (!expectedMemory.Span.SequenceEqual(actualMemory.Span))
Equal(expectedMemory.Span.ToArray(), actualMemory.Span.ToArray(), new AssertEqualityComparer<T>());
}
}
}