-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathBindingInvokerTests.cs
490 lines (416 loc) · 20.2 KB
/
BindingInvokerTests.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
using Reqnroll.BoDi;
using FluentAssertions;
using Moq;
using Reqnroll.Bindings;
using Reqnroll.Bindings.Reflection;
using Reqnroll.Configuration;
using Reqnroll.Infrastructure;
using Reqnroll.RuntimeTests.ErrorHandling;
using Reqnroll.Tracing;
using Xunit;
using Xunit.Abstractions;
namespace Reqnroll.RuntimeTests.Bindings;
public class BindingInvokerTests
{
// ReSharper disable once NotAccessedField.Local
private static ITestOutputHelper _testOutputHelperInstance;
private readonly ITestOutputHelper _testOutputHelper;
public BindingInvokerTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_testOutputHelperInstance = _testOutputHelper; // to be able to access it from step def classes
}
private BindingInvoker CreateSut()
{
return new BindingInvoker(ConfigurationLoader.GetDefault(), new StubErrorProvider(), new BindingDelegateInvoker());
}
class TestableMethodBinding : MethodBinding
{
public TestableMethodBinding(IBindingMethod bindingMethod) : base(bindingMethod)
{
}
}
private async Task<object> InvokeBindingAsync(BindingInvoker sut, IContextManager contextManager, Type stepDefType, string methodName, params object[] args)
{
var testTracerMock = new Mock<ITestTracer>();
var setMethodBinding = new TestableMethodBinding(new RuntimeBindingMethod(stepDefType.GetMethod(methodName)));
return await sut.InvokeBindingAsync(setMethodBinding, contextManager, args, testTracerMock.Object, new DurationHolder());
}
private IContextManager CreateContextManagerWith()
{
var contextManagerMock = new Mock<IContextManager>();
var scenarioContainer = new ObjectContainer();
var scenarioContext = new ScenarioContext(scenarioContainer, new ScenarioInfo("S1", null, null, null), new TestObjectResolver());
contextManagerMock.SetupGet(ctx => ctx.ScenarioContext).Returns(scenarioContext);
var contextManager = contextManagerMock.Object;
return contextManager;
}
#region Generic invokation tests
class GenericStepDefClass
{
public bool WasInvoked = false;
public int CapturedIntParam = 0;
public string CapturedStringParam = null;
public void SyncStepDef(int intParam, string stringParam)
{
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
}
public async Task AsyncStepDef(int intParam, string stringParam)
{
await Task.Delay(10);
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
}
public async Task<int> AsyncConverter(int intParam, string stringParam)
{
await Task.Delay(10);
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
return 42;
}
public async Task<int> AsyncConverterWithoutAwait(int intParam, string stringParam)
{
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
return 42;
}
public int SyncConverter(int intParam, string stringParam)
{
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
return 42;
}
public void SyncStepDefWithException(int intParam, string stringParam)
{
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
throw new Exception("simulated error");
}
public async Task AsyncStepDefWithException(int intParam, string stringParam)
{
await Task.Delay(10);
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
throw new Exception("simulated error");
}
public async Task AsyncStepDefWithoutAwaitWithException(int intParam, string stringParam)
{
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
throw new Exception("simulated error");
}
public async Task<int> AsyncConverterWithException(int intParam, string stringParam)
{
await Task.Delay(10);
WasInvoked = true;
CapturedIntParam = intParam;
CapturedStringParam = stringParam;
throw new Exception("simulated error");
}
}
[Theory]
[InlineData(nameof(GenericStepDefClass.SyncStepDef), null)]
[InlineData(nameof(GenericStepDefClass.AsyncStepDef), null)]
[InlineData(nameof(GenericStepDefClass.SyncConverter), 42)]
[InlineData(nameof(GenericStepDefClass.AsyncConverter), 42)]
[InlineData(nameof(GenericStepDefClass.AsyncConverterWithoutAwait), 42)]
public async Task Can_invoke_different_methods(string methodName, object expectedResult)
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
// call step definition methods
var result = await InvokeBindingAsync(sut, contextManager, typeof(GenericStepDefClass), methodName, 24, "foo");
// this is how to get THE instance of the step definition class
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<GenericStepDefClass>();
stepDefClass.WasInvoked.Should().BeTrue();
stepDefClass.CapturedIntParam.Should().Be(24);
stepDefClass.CapturedStringParam.Should().Be("foo");
result.Should().Be(expectedResult);
}
[Theory]
[InlineData(nameof(GenericStepDefClass.SyncStepDefWithException), typeof(Exception))]
[InlineData(nameof(GenericStepDefClass.AsyncStepDefWithException), typeof(Exception))]
[InlineData(nameof(GenericStepDefClass.AsyncStepDefWithoutAwaitWithException), typeof(Exception))]
[InlineData(nameof(GenericStepDefClass.AsyncConverterWithException), typeof(Exception))]
public async Task Can_invoke_different_failing_methods(string methodName, Type expectedExceptionType)
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
// call step definition methods
var ex = await Assert.ThrowsAsync(expectedExceptionType, () => InvokeBindingAsync(sut, contextManager, typeof(GenericStepDefClass), methodName, 24, "foo"));
_testOutputHelper.WriteLine(ex.ToString());
// this is how to get THE instance of the step definition class
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<GenericStepDefClass>();
stepDefClass.WasInvoked.Should().BeTrue();
stepDefClass.CapturedIntParam.Should().Be(24);
stepDefClass.CapturedStringParam.Should().Be("foo");
}
#endregion
#region ValueTask related tests
class StepDefClassWithValueTask
{
public bool WasInvokedAsyncValueTaskStepDef = false;
public bool WasInvokedAsyncValueTaskOfTStepDef = false;
public async ValueTask AsyncValueTaskStepDef()
{
await Task.Delay(50); // we need to wait a bit otherwise the assertion passes even if the method is called sync
WasInvokedAsyncValueTaskStepDef = true;
}
public async ValueTask<string> AsyncValueTaskOfTStepDef()
{
await Task.Delay(50); // we need to wait a bit otherwise the assertion passes even if the method is called sync
WasInvokedAsyncValueTaskOfTStepDef = true;
return "foo";
}
}
[Fact]
public async Task Async_methods_of_ValueTask_return_type_can_be_invoked()
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
// call step definition methods
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithValueTask), nameof(StepDefClassWithValueTask.AsyncValueTaskStepDef));
// this is how to get THE instance of the step definition class
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<StepDefClassWithValueTask>();
stepDefClass.WasInvokedAsyncValueTaskStepDef.Should().BeTrue();
}
[Fact]
public async Task Async_methods_of_ValueTaskOfT_return_type_can_be_invoked()
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithValueTask), nameof(StepDefClassWithValueTask.AsyncValueTaskOfTStepDef));
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<StepDefClassWithValueTask>();
stepDefClass.WasInvokedAsyncValueTaskOfTStepDef.Should().BeTrue();
}
#endregion
#region Async void related tests
class StepDefClassWithAsyncVoid
{
public static bool WasInvokedAsyncVoidStepDef = false;
public async void AsyncVoidStepDef()
{
await Task.Delay(50); // we need to wait a bit otherwise the assertion passes even if the method is called sync
WasInvokedAsyncVoidStepDef = true;
}
}
[Fact]
public async Task Async_void_binding_methods_are_not_supported()
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
await FluentActions.Awaiting(() => InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncVoid), nameof(StepDefClassWithAsyncVoid.AsyncVoidStepDef)))
.Should()
.ThrowAsync<ReqnrollException>()
.WithMessage("*async void*");
StepDefClassWithAsyncVoid.WasInvokedAsyncVoidStepDef.Should().BeFalse();
}
#endregion
#region ExecutionContext / AsyncLocal<T> related tests
public enum AsyncLocalType
{
Uninitialized,
CtorInitialized,
Boxed
}
class StepDefClassWithAsyncLocal
{
private readonly AsyncLocal<string> _uninitializedAsyncLocal = new();
private readonly AsyncLocal<string> _ctorInitializedAsyncLocal = new() { Value = "ctor-value" };
private readonly AsyncLocal<StrongBox<string>> _boxedAsyncLocal = new() { Value = new StrongBox<string>("ctor-value") };
public string LoadedValue { get; set; }
// ReSharper disable once UnusedMember.Local
public void SetAsyncLocal_Sync(AsyncLocalType asyncLocalType, string content)
{
switch (asyncLocalType)
{
case AsyncLocalType.Uninitialized:
_uninitializedAsyncLocal.Value = content;
break;
case AsyncLocalType.CtorInitialized:
_ctorInitializedAsyncLocal.Value = content;
break;
case AsyncLocalType.Boxed:
_boxedAsyncLocal.Value!.Value = content;
break;
}
}
// ReSharper disable once UnusedMember.Local
public async Task SetAsyncLocal_Async(AsyncLocalType asyncLocalType, string content)
{
switch (asyncLocalType)
{
case AsyncLocalType.Uninitialized:
_uninitializedAsyncLocal.Value = content;
break;
case AsyncLocalType.CtorInitialized:
_ctorInitializedAsyncLocal.Value = content;
break;
case AsyncLocalType.Boxed:
_boxedAsyncLocal.Value!.Value = content;
break;
}
await Task.Delay(1);
}
public async Task GetAsyncLocal_Async(AsyncLocalType asyncLocalType, string expectedResult)
{
switch (asyncLocalType)
{
case AsyncLocalType.Uninitialized:
LoadedValue = _uninitializedAsyncLocal.Value;
break;
case AsyncLocalType.CtorInitialized:
LoadedValue = _ctorInitializedAsyncLocal.Value;
break;
case AsyncLocalType.Boxed:
LoadedValue = _boxedAsyncLocal.Value?.Value;
break;
}
Assert.Equal(expectedResult, LoadedValue);
await Task.Delay(1);
}
}
[Theory]
[InlineData("Value is initialized in ctor", AsyncLocalType.CtorInitialized, null, "ctor-value")]
[InlineData("Value is initialized in ctor and also set in sync step def", AsyncLocalType.CtorInitialized, "Sync", "42")]
[InlineData("Value is initialized in ctor and also set in async step def", AsyncLocalType.CtorInitialized, "Async", "ctor-value")] // the change in async step def is discarded
[InlineData("Value is set in sync step def", AsyncLocalType.Uninitialized, "Sync", "42")]
[InlineData("Value is set in async step def", AsyncLocalType.Uninitialized, "Async", null)] // the change in async step def is discarded
[InlineData("Boxed value is initialized in ctor", AsyncLocalType.Boxed, null, "ctor-value")]
[InlineData("Boxed value is initialized in ctor and also set in sync step def", AsyncLocalType.Boxed, "Sync", "42")]
[InlineData("Boxed value is initialized in ctor and also set in async step def", AsyncLocalType.Boxed, "Async", "42")] // the change in async step def is preserved because of boxing
public async Task ExecutionContext_is_flowing_down_correctly_to_step_definitions(string description, AsyncLocalType asyncLocalType, string setAs, string expectedResult)
{
_testOutputHelper.WriteLine(description);
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
if (setAs != null)
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncLocal), "SetAsyncLocal_" + setAs, asyncLocalType, "42");
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncLocal), nameof(StepDefClassWithAsyncLocal.GetAsyncLocal_Async), asyncLocalType, expectedResult);
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<StepDefClassWithAsyncLocal>();
stepDefClass.LoadedValue.Should().Be(expectedResult, $"Error was not propagated for {description}");
}
[Fact]
public async Task ExecutionContext_can_be_changed_multiple_times()
{
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncLocal), "SetAsyncLocal_Sync", AsyncLocalType.Uninitialized, "14");
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncLocal), "SetAsyncLocal_Sync", AsyncLocalType.Uninitialized, "42");
await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassWithAsyncLocal), nameof(StepDefClassWithAsyncLocal.GetAsyncLocal_Async), AsyncLocalType.Uninitialized, "42");
var stepDefClass = contextManager.ScenarioContext.ScenarioContainer.Resolve<StepDefClassWithAsyncLocal>();
stepDefClass.LoadedValue.Should().Be("42", $"Error was not propagated");
}
#endregion
#region Exception Handling related tests - regression tests for SF2649
public enum ExceptionKind
{
Normal,
Aggregate
}
public enum InnerExceptionContentKind
{
WithInnerException,
WithoutInnerException
}
public enum StepDefInvocationStyle
{
Sync,
Async
}
class StepDefClassThatThrowsExceptions
{
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task AsyncThrow(ExceptionKind kindOfExceptionToThrow, InnerExceptionContentKind innerExceptionKind)
{
await Task.Run(() => ConstructAndThrowSync(kindOfExceptionToThrow, innerExceptionKind));
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void SyncThrow(ExceptionKind kindOfExceptionToThrow, InnerExceptionContentKind innerExceptionKind)
{
ConstructAndThrowSync(kindOfExceptionToThrow, innerExceptionKind);
}
private void ConstructAndThrowSync(ExceptionKind typeOfExceptionToThrow, InnerExceptionContentKind innerExceptionContentKind)
{
switch (typeOfExceptionToThrow, innerExceptionContentKind)
{
case (ExceptionKind.Normal, InnerExceptionContentKind.WithoutInnerException):
throw new Exception("Normal Exception message (No InnerException expected).");
case (ExceptionKind.Aggregate, InnerExceptionContentKind.WithoutInnerException):
throw new AggregateException("AggregateEx (without Inners)");
case (ExceptionKind.Normal, InnerExceptionContentKind.WithInnerException):
try
{
throw new Exception("This is the message from the Inner Exception");
}
catch (Exception e)
{
throw new Exception("Normal Exception (with InnerException)", e);
}
case (ExceptionKind.Aggregate, InnerExceptionContentKind.WithInnerException):
{
var tasks = new List<Task>
{
Task.Run(async () => throw new Exception("This is the first Exception embedded in the AggregateException")),
Task.Run(async () => throw new Exception("This is the second Exception embedded in the AggregateException"))
};
var continuation = Task.WhenAll(tasks);
// This will throw an AggregateException with two Inner Exceptions
continuation.Wait();
return;
}
}
}
}
[Theory]
[InlineData(StepDefInvocationStyle.Sync, InnerExceptionContentKind.WithoutInnerException)]
[InlineData(StepDefInvocationStyle.Sync, InnerExceptionContentKind.WithInnerException)]
[InlineData(StepDefInvocationStyle.Async, InnerExceptionContentKind.WithoutInnerException)]
[InlineData(StepDefInvocationStyle.Async, InnerExceptionContentKind.WithInnerException)]
public async Task InvokeBindingAsync_WhenStepDefThrowsExceptions_ProperlyPreservesExceptionContext(StepDefInvocationStyle style, InnerExceptionContentKind inner)
{
_testOutputHelper.WriteLine($"starting Exception Handling test: {style}, {inner}");
var sut = CreateSut();
var contextManager = CreateContextManagerWith();
string methodToInvoke;
ExceptionKind kindOfExceptionToThrow;
Exception thrown;
// call step definition methods
if (style == StepDefInvocationStyle.Sync)
{
methodToInvoke = nameof(StepDefClassThatThrowsExceptions.SyncThrow);
kindOfExceptionToThrow = ExceptionKind.Normal;
thrown = await Assert.ThrowsAsync<Exception>(async () => await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassThatThrowsExceptions), methodToInvoke, kindOfExceptionToThrow, inner));
}
else // if (style == StepDefInvocationStyle.Async)
{
methodToInvoke = nameof(StepDefClassThatThrowsExceptions.AsyncThrow);
kindOfExceptionToThrow = ExceptionKind.Aggregate;
thrown = await Assert.ThrowsAsync<AggregateException>(async () => await InvokeBindingAsync(sut, contextManager, typeof(StepDefClassThatThrowsExceptions), methodToInvoke, kindOfExceptionToThrow, inner));
}
_testOutputHelper.WriteLine($"Exception detail: {thrown}");
// Assert that the InnerException detail is preserved
if (inner == InnerExceptionContentKind.WithInnerException)
{
if (thrown is AggregateException) (thrown as AggregateException).InnerExceptions.Count.Should().BeGreaterThan(1);
else thrown.InnerException.Should().NotBeNull();
}
// Assert that the stack trace properly shows that the exception came from the throwing method (and not hidden by the Reqnroll infrastructure)
thrown.StackTrace.Should().Contain(methodToInvoke);
}
#endregion
}