-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefStructProjectionStorageTests.cs
More file actions
316 lines (253 loc) · 10.2 KB
/
Copy pathRefStructProjectionStorageTests.cs
File metadata and controls
316 lines (253 loc) · 10.2 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
#if NET9_0_OR_GREATER
using Mockolate.Parameters;
using Mockolate.Setup;
namespace Mockolate.Internal.Tests;
/// <summary>
/// White-box tests for the projection-based storage on
/// <see cref="RefStructIndexerGetterSetup{TValue, T}" /> and the
/// <see cref="RefStructIndexerSetterSetup{TValue, T}" /> <c>BoundGetter</c> slot. These
/// exercise the internal <c>StoreValue</c> / <c>TryGetStoredValue</c> surface that the
/// end-to-end tests in <c>Mockolate.Tests</c> cannot reach.
/// </summary>
public sealed class RefStructProjectionStorageTests
{
/// <summary>
/// Local ref struct used instead of the test-project <c>Packet</c> (which lives in
/// <c>Mockolate.Tests</c> — not referenced from this project).
/// </summary>
private readonly ref struct Key(int id)
{
public int Id { get; } = id;
}
[Fact]
public async Task GetterSetup_WithProjection_StoreAndTryGet_RoundTrip()
{
IParameter<Key> matcher = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item", (IParameterMatch<Key>)matcher);
getter.StoreValue(new Key(42), "forty-two");
bool found = getter.TryGetStoredValue(new Key(42), out string? value);
await That(found).IsTrue();
await That(value).IsEqualTo("forty-two");
}
[Fact]
public async Task GetterSetup_WithoutProjection_StoreIsNoOp_TryGetReturnsFalse()
{
IParameter<Key> matcher = It.IsAnyRefStruct<Key>();
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item", (IParameterMatch<Key>)matcher);
getter.StoreValue(new Key(1), "ignored");
bool found = getter.TryGetStoredValue(new Key(1), out string? _);
await That(found).IsFalse();
}
[Fact]
public async Task GetterSetup_NullMatcher_HasNoStorage()
{
RefStructIndexerGetterSetup<string, Key> getter = new("get_Item", null);
getter.StoreValue(new Key(1), "ignored");
bool found = getter.TryGetStoredValue(new Key(1), out _);
await That(found).IsFalse();
}
[Fact]
public async Task GetterSetup_WithProjectionAndPredicate_StoreSkipsNonMatchingKey()
{
IParameter<Key> matcher = It.IsRefStructBy<Key, int>(k => k.Id, id => id > 10);
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item", (IParameterMatch<Key>)matcher);
getter.StoreValue(new Key(5), "small"); // rejected by predicate
getter.StoreValue(new Key(42), "big"); // accepted
bool smallFound = getter.TryGetStoredValue(new Key(5), out _);
bool bigFound = getter.TryGetStoredValue(new Key(42), out string? bigValue);
await That(smallFound).IsFalse();
await That(bigFound).IsTrue();
await That(bigValue).IsEqualTo("big");
}
[Fact]
public async Task GetterSetup_WithProjection_Invoke_ReturnsStoredValue_BeforeReturnFactory()
{
IRefStructIndexerGetterSetup<string, Key> setup;
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item",
(IParameterMatch<Key>)It.IsRefStructBy<Key, int>(k => k.Id));
setup = getter;
setup.Returns("fallback");
getter.StoreValue(new Key(1), "stored");
string read = getter.Invoke(new Key(1));
await That(read).IsEqualTo("stored");
}
[Fact]
public async Task GetterSetup_WithProjection_EmptyBucket_Invoke_FallsBackToReturns()
{
IRefStructIndexerGetterSetup<string, Key> setup;
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item",
(IParameterMatch<Key>)It.IsRefStructBy<Key, int>(k => k.Id));
setup = getter;
setup.Returns("fallback");
string read = getter.Invoke(new Key(99));
await That(read).IsEqualTo("fallback");
}
[Fact]
public async Task GetterSetup_WithProjection_HasReturnValue_IsTrueEvenWithoutReturnFactory()
{
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item",
(IParameterMatch<Key>)It.IsRefStructBy<Key, int>(k => k.Id));
await That(getter.HasReturnValue).IsTrue()
.Because(
"activating the storage dictionary must flip HasReturnValue, so that the generator's emitted getter body takes the return path and serves stored values");
}
[Fact]
public async Task GetterSetup_WithoutProjection_HasReturnValue_IsFalse_UntilReturnsConfigured()
{
RefStructIndexerGetterSetup<string, Key> getter = new(
"get_Item", (IParameterMatch<Key>)It.IsAnyRefStruct<Key>());
await That(getter.HasReturnValue).IsFalse();
((IRefStructIndexerGetterSetup<string, Key>)getter).Returns("x");
await That(getter.HasReturnValue).IsTrue();
}
[Fact]
public async Task CombinedSetup_Constructor_WiresSetterBoundGetter()
{
IParameter<Key> matcher = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerSetup<string, Key> setup = new(
"get_Item", "set_Item", (IParameterMatch<Key>)matcher);
await That(setup.Setter.BoundGetter).IsSameAs(setup.Getter);
}
[Fact]
public async Task CombinedSetup_WithProjection_SetterInvoke_StoresIntoGetter()
{
IParameter<Key> matcher = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerSetup<string, Key> setup = new(
"get_Item", "set_Item", (IParameterMatch<Key>)matcher);
setup.Setter.Invoke(new Key(7), "seven");
bool found = setup.Getter.TryGetStoredValue(new Key(7), out string? value);
await That(found).IsTrue();
await That(value).IsEqualTo("seven");
}
[Fact]
public async Task SetterOnlySetup_BoundGetter_DefaultsToNull_InvokeIsStorageNoOp()
{
IParameter<Key> matcher = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerSetterSetup<string, Key> setter = new(
"set_Item", (IParameterMatch<Key>)matcher);
await That(setter.BoundGetter).IsNull();
// Should be a no-op for storage but still execute any OnSet/Throws slots (neither set
// here, so this must simply not throw).
setter.Invoke(new Key(7), "seven");
}
[Fact]
public async Task CombinedSetup_WithoutProjection_NoStorageActivated()
{
IParameter<Key> matcher = It.IsAnyRefStruct<Key>();
RefStructIndexerSetup<string, Key> setup = new(
"get_Item", "set_Item", (IParameterMatch<Key>)matcher);
await That(setup.Setter.BoundGetter).IsSameAs(setup.Getter)
.Because("the combined setup always wires the bound getter, projection or not");
setup.Setter.Invoke(new Key(7), "seven");
bool found = setup.Getter.TryGetStoredValue(new Key(7), out _);
await That(found).IsFalse()
.Because("without a projection the getter's storage is never activated, so writes cannot be stored");
}
public sealed class Arity2Tests
{
[Fact]
public async Task AllProjections_StoreAndTryGet_RoundTrip()
{
IParameter<Key> p1 = It.IsRefStructBy<Key, int>(k => k.Id);
IParameter<Key> p2 = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerGetterSetup<string, Key, Key> getter = new(
"get_Item",
(IParameterMatch<Key>)p1, (IParameterMatch<Key>)p2);
getter.StoreValue(new Key(1), new Key(2), "hit", null, null);
bool found = getter.TryGetStoredValue(new Key(1), new Key(2), null, null, out string? value);
bool miss = getter.TryGetStoredValue(new Key(1), new Key(99), null, null, out _);
await That(found).IsTrue();
await That(value).IsEqualTo("hit");
await That(miss).IsFalse();
}
[Fact]
public async Task MixedNormalAndProjection_StoreAndTryGet_RoundTrip()
{
IParameter<int> normal = It.IsAny<int>();
IParameter<Key> projected = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerGetterSetup<string, int, Key> getter = new(
"get_Item",
(IParameterMatch<int>)normal, (IParameterMatch<Key>)projected);
getter.StoreValue(3, new Key(7), "hit", 3, null);
bool hit = getter.TryGetStoredValue(3, new Key(7), 3, null, out string? value);
bool missOnFirst = getter.TryGetStoredValue(4, new Key(7), 4, null, out _);
bool missOnSecond = getter.TryGetStoredValue(3, new Key(8), 3, null, out _);
await That(hit).IsTrue();
await That(value).IsEqualTo("hit");
await That(missOnFirst).IsFalse();
await That(missOnSecond).IsFalse();
}
[Fact]
public async Task OneRefStructWithoutProjection_StorageInactive()
{
IParameter<int> normal = It.IsAny<int>();
IParameter<Key> refNoProjection = It.IsAnyRefStruct<Key>();
RefStructIndexerGetterSetup<string, int, Key> getter = new(
"get_Item",
(IParameterMatch<int>)normal, (IParameterMatch<Key>)refNoProjection);
getter.StoreValue(1, new Key(2), "ignored", 1, null);
bool found = getter.TryGetStoredValue(1, new Key(2), 1, null, out _);
await That(found).IsFalse();
await That(getter.HasReturnValue).IsFalse();
}
[Fact]
public async Task CombinedSetup_WiresBoundGetter_SetterWrite_FeedsGetterRead()
{
IParameter<int> normal = It.IsAny<int>();
IParameter<Key> projected = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerSetup<string, int, Key> setup = new(
"get_Item", "set_Item",
(IParameterMatch<int>)normal, (IParameterMatch<Key>)projected);
await That(setup.Setter.BoundGetter).IsSameAs(setup.Getter);
setup.Setter.Invoke(3, new Key(7), "stored", 3, null);
bool found = setup.Getter.TryGetStoredValue(3, new Key(7), 3, null, out string? value);
await That(found).IsTrue();
await That(value).IsEqualTo("stored");
}
}
public sealed class Arity3Tests
{
[Fact]
public async Task MixedProjectionAndNormal_RoundTrip()
{
IParameter<int> p1 = It.IsAny<int>();
IParameter<Key> p2 = It.IsRefStructBy<Key, int>(k => k.Id);
IParameter<string> p3 = It.IsAny<string>();
RefStructIndexerGetterSetup<string, int, Key, string> getter = new(
"get_Item",
(IParameterMatch<int>)p1,
(IParameterMatch<Key>)p2,
(IParameterMatch<string>)p3);
getter.StoreValue(1, new Key(42), "tag", "v", 1, null, "tag");
bool found = getter.TryGetStoredValue(1, new Key(42), "tag", 1, null, "tag", out string? value);
await That(found).IsTrue();
await That(value).IsEqualTo("v");
}
}
public sealed class Arity4Tests
{
[Fact]
public async Task AllRefStructWithProjection_RoundTrip()
{
IParameter<Key> p = It.IsRefStructBy<Key, int>(k => k.Id);
RefStructIndexerGetterSetup<string, Key, Key, Key, Key> getter = new(
"get_Item",
(IParameterMatch<Key>)p, (IParameterMatch<Key>)p,
(IParameterMatch<Key>)p, (IParameterMatch<Key>)p);
getter.StoreValue(new Key(1), new Key(2), new Key(3), new Key(4), "v",
null, null, null, null);
bool found = getter.TryGetStoredValue(
new Key(1), new Key(2), new Key(3), new Key(4),
null, null, null, null, out string? value);
await That(found).IsTrue();
await That(value).IsEqualTo("v");
}
}
}
#endif