forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryAsserts.cs
197 lines (176 loc) · 8.05 KB
/
MemoryAsserts.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
#pragma warning disable CA1052 // Static holder types should be static
#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 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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T>
{
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T>
{
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T> =>
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)
where T : IEquatable<T>
{
GuardArgumentNotNull(nameof(expectedMemory), expectedMemory);
if (!expectedMemory.Span.SequenceEqual(actualMemory.Span))
Equal<object>(expectedMemory.Span.ToArray(), actualMemory.Span.ToArray());
}
}
}