-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathCucumberMessagesValidator.cs
More file actions
622 lines (557 loc) · 33.9 KB
/
Copy pathCucumberMessagesValidator.cs
File metadata and controls
622 lines (557 loc) · 33.9 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using FluentAssertions;
using FluentAssertions.Equivalency;
using FluentAssertions.Execution;
using Io.Cucumber.Messages.Types;
using Reqnroll.Formatters.PayloadProcessing.Cucumber;
using System.Reflection;
namespace Reqnroll.Formatters.Tests;
public partial class CucumberMessagesValidator
{
private readonly IEnumerable<Envelope> _actualEnvelopes;
private readonly IEnumerable<Envelope> _expectedEnvelopes;
// cross-reference metadata
private readonly Dictionary<Type, HashSet<string>> _actualIDsByType = new();
private readonly Dictionary<Type, HashSet<string>> _expectedIDsByType = new();
private readonly Dictionary<Type, HashSet<object>> _actualElementsByType = new();
private readonly Dictionary<Type, HashSet<object>> _expectedElementsByType = new();
private readonly Dictionary<string, object> _actualElementsById = new();
private readonly Dictionary<string, object> _expectedElementsById = new();
private readonly FluentAssertionCucumberMessagePropertySelectionRule _customCucumberMessagesPropertySelector;
Dictionary<object, int> _actualPartitions;
Dictionary<object, int> _expectedPartitions;
private readonly int _numPartitions;
// Envelope types - these are the top level types in CucumberMessages
// Meta is excluded from the list as there is nothing there for us to compare
private IEnumerable<Type> EnvelopeTypes
{
get
{
var ect = EnvelopeExtensions.EnvelopeContentTypes.ToList();
ect.Remove(typeof(Meta));
return ect;
}
}
public CucumberMessagesValidator(Envelope[] actual, Envelope[] expected)
{
_actualEnvelopes = actual;
_expectedEnvelopes = expected;
SetupCrossReferences(_actualEnvelopes, _actualIDsByType, _actualElementsByType, _actualElementsById);
SetupCrossReferences(expected, _expectedIDsByType, _expectedElementsByType, _expectedElementsById);
// assign a 'partition' number to each Envelope id so that envelopes can be split apart by Feature
// this will allow Envelope sequence comparisons to be isolated to each feature (ensuring that parallel feature execution won't interfere with comparisons)
_actualPartitions = Partition(actual);
_expectedPartitions = Partition(expected);
_numPartitions = _expectedPartitions.Values.Max();
_customCucumberMessagesPropertySelector = new FluentAssertionCucumberMessagePropertySelectionRule(_expectedElementsByType.Keys.ToList());
}
private Dictionary<object, int> Partition(Envelope[] envelopes)
{
var partitioner = new EnvelopePartitioner();
var result = new Dictionary<object, int>();
foreach (var envelope in envelopes)
{
result.Add(envelope.Content(), partitioner.AssignPartition(envelope));
}
return result;
}
private void SetupCrossReferences(IEnumerable<Envelope> messages, Dictionary<Type, HashSet<string>> idsByType, Dictionary<Type, HashSet<object>> elementsByType, Dictionary<string, object> elementsById)
{
var xrefBuilder = new CrossReferenceBuilder(msg =>
{
InsertIntoElementsByType(msg, elementsByType);
if (msg.HasId())
{
InsertIntoElementsById(msg, elementsById);
InsertIntoIDsByType(msg, idsByType);
}
});
foreach (var message in messages)
{
var msg = message.Content();
xrefBuilder.Accept(msg);
}
}
private static void InsertIntoIDsByType(object msg, Dictionary<Type, HashSet<string>> idsByType)
{
if (!idsByType.ContainsKey(msg.GetType()))
{
idsByType.Add(msg.GetType(), new HashSet<string>());
}
idsByType[msg.GetType()].Add(msg.Id());
}
private static void InsertIntoElementsById(object msg, Dictionary<string, object> elementsById)
{
elementsById.Add(msg.Id(), msg);
}
private static void InsertIntoElementsByType(object msg, Dictionary<Type, HashSet<object>> elementsByType)
{
if (!elementsByType.ContainsKey(msg.GetType()))
{
elementsByType.Add(msg.GetType(), new HashSet<object>());
}
elementsByType[msg.GetType()].Add(msg);
}
public void ResultShouldPassAllComparisonTests()
{
var method = typeof(CucumberMessagesValidator).GetMethod(nameof(CompareMessageType), BindingFlags.NonPublic | BindingFlags.Instance);
using (new AssertionScope("Comparing Actual to Expected Envelopes"))
{
foreach (Type t in EnvelopeTypes)
{
var genMethod = method!.MakeGenericMethod(t);
for (int i = 0; i <= _numPartitions; i++)
{
genMethod.Invoke(this, [i]);
}
}
}
}
private int MapPartitionNumber(int partitionNumber)
{
if (partitionNumber == 0) return 0;
var gd = _expectedPartitions.First(kvp => kvp.Value == partitionNumber && kvp.Key is GherkinDocument).Key as GherkinDocument;
var featureName = gd!.Feature.Name;
return _actualPartitions.First(kvp => kvp.Key is GherkinDocument document && document.Feature.Name == featureName).Value;
}
// by partition
private void CompareMessageType<T>(int partitionNumber)
{
if (!_expectedElementsByType.ContainsKey(typeof(T)))
return;
int actualPartitionNumber = MapPartitionNumber(partitionNumber);
var actual = _actualElementsByType.TryGetValue(typeof(T), out HashSet<object>? actualElements) && actualElements.Count > 0 ?
actualElements.OfType<T>().Where(e => _actualPartitions[e!] == actualPartitionNumber).ToList() : new List<T>();
var expected = _expectedElementsByType[typeof(T)].AsEnumerable().OfType<T>().Where(e => _expectedPartitions[e!] == partitionNumber).ToList();
if (!(typeof(T) == typeof(TestStepFinished)))
{
actual.Should().BeEquivalentTo(expected, options => ArrangeFluentAssertionOptions(options).WithTracing(), "When comparing " + typeof(T).Name + "s");
}
else
{
// For TestStepFinished, we will separate out those related to hooks;
// the regular comparison will be done for TestStepFinished related to PickleSteps/StepDefinitions
// Hook related TestStepFinished - the order is indeterminate, so we will check quantity, and count of Statuses
// Hook related TestSteps are found by following the testStepId of the Finished message to the related TestStep. If the TestStep has a value for pickleStepId, then it is a regular step.
// if it has a hookId, it is a hook step
var actualHookRelatedTestStepFinished = actual.OfType<TestStepFinished>().Where(tsf => _actualElementsById[tsf.TestStepId].As<TestStep>().HookId != null).ToList();
var actualStepRelatedTestStepFinished = actual.OfType<TestStepFinished>().Where(tsf => _actualElementsById[tsf.TestStepId].As<TestStep>().HookId == null).ToList();
var expectedHookRelatedTestStepFinished = expected.OfType<TestStepFinished>().Where(tsf => _expectedElementsById[tsf.TestStepId].As<TestStep>().HookId != null).ToList();
var expectedStepRelatedTestStepFinished = expected.OfType<TestStepFinished>().Where(tsf => _expectedElementsById[tsf.TestStepId].As<TestStep>().HookId == null).ToList();
actualStepRelatedTestStepFinished.Should().BeEquivalentTo(expectedStepRelatedTestStepFinished, options => ArrangeFluentAssertionOptions(options).WithTracing(), "when comparing TestStepFinished messages");
actualHookRelatedTestStepFinished.Should().BeEquivalentTo(expectedHookRelatedTestStepFinished, options => ArrangeFluentAssertionOptions(options).WithoutStrictOrdering().WithTracing(), "when comparing Hook TestStepFinished messages");
}
}
public void ResultShouldPassSanityChecks()
{
using (new AssertionScope("Sanity Checks"))
{
EachTestCaseAndStepsShouldProperlyReferToAPickleAndStepDefinitionOrHook();
EachPickleAndPickleStepShouldBeReferencedByTestStepsAtLeastOnce();
TestExecutionStepsShouldProperlyReferenceTestCases();
TestExecutionMessagesShouldProperlyNest();
ActualTestExecutionMessagesShouldReferBackToTheSameStepTextAsExpected();
}
}
// by partition
private void ActualTestExecutionMessagesShouldReferBackToTheSameStepTextAsExpected()
{
// IF the expected results contains no TestStepStarted messages, then there is nothing to check
if (!_expectedElementsByType.Keys.Contains(typeof(TestStepStarted)))
return;
for (int i = 0; i < _numPartitions; i++)
{
var partitionNumber = i + 1;
int actualPartitionNumber = MapPartitionNumber(partitionNumber);
// For each TestStepStarted message, ensure that the pickle step referred to is the same in Actual and Expected for the corresponding testStepStarted message
var actualTestStepStartedTestStepIds = _actualElementsByType[typeof(TestStepStarted)].OfType<TestStepStarted>().Where(e => _actualPartitions[e] == actualPartitionNumber).Select(tss => tss.TestStepId).ToList();
var expectedTestStepStartedTestStepIds = _expectedElementsByType[typeof(TestStepStarted)].OfType<TestStepStarted>().Where(e => _expectedPartitions[e] == partitionNumber).Select(tss => tss.TestStepId).ToList();
// Making the assumption here that the order of TestStepStarted messages is the same in both Actual and Expected within a Partition
// pair these up, and walk back to the pickle step text and compare
actualTestStepStartedTestStepIds
.Zip(expectedTestStepStartedTestStepIds, (a, e) => (a, e))
.ToList()
.ForEach(t =>
{
_actualElementsById[t.a].Should().BeAssignableTo<TestStep>();
var actualTestStep = _actualElementsById[t.a] as TestStep;
_expectedElementsById[t.e].Should().BeAssignableTo<TestStep>();
var expectedTestStep = _expectedElementsById[t.e] as TestStep;
if (actualTestStep!.PickleStepId != null && expectedTestStep!.PickleStepId != null)
{
_actualElementsById[actualTestStep.PickleStepId].Should().BeAssignableTo<PickleStep>();
var actualPickleStep = _actualElementsById[actualTestStep.PickleStepId] as PickleStep;
_expectedElementsById[expectedTestStep.PickleStepId].Should().BeAssignableTo<PickleStep>();
var expectedPickleStep = _expectedElementsById[expectedTestStep.PickleStepId] as PickleStep;
actualPickleStep!.Text.Should().Be(expectedPickleStep!.Text, $"expecting the text of the pickle step {actualPickleStep.Id} to match that of {expectedPickleStep.Id}");
}
else
{ // confirm that both are null or not null, if one is null, throw an exception
actualTestStep.PickleStepId.Should().Be(expectedTestStep!.PickleStepId, "expecting both PickleStepIds to be null or not null");
}
});
}
}
private void TestExecutionStepsShouldProperlyReferenceTestCases()
{
var testCaseIds = _actualElementsByType[typeof(TestCase)].OfType<TestCase>().Select(tc => tc.Id).ToList();
var testCaseStartedItems = _actualElementsByType[typeof(TestCaseStarted)].OfType<TestCaseStarted>().ToList();
testCaseIds.Should().Contain(id => testCaseStartedItems.Any(tcs => tcs.TestCaseId == id), "a test case should be referenced by a test case started message");
var testCaseStartedIds = testCaseStartedItems.Select(tcs => tcs.Id).ToList();
var testCaseFinishedItems = _actualElementsByType[typeof(TestCaseFinished)].OfType<TestCaseFinished>().ToList();
testCaseStartedIds.Should().Contain(id => testCaseFinishedItems.Any(tcf => tcf.TestCaseStartedId == id), "a test case started should be referenced by a test case finished message");
// IF the Scenario has no steps, return early.
if (!_actualElementsByType.Keys.Contains(typeof(TestStepStarted)))
return;
var testStepStartedItems = _actualElementsByType[typeof(TestStepStarted)].OfType<TestStepStarted>().ToList();
var testStepFinishedItems = _actualElementsByType[typeof(TestStepFinished)].OfType<TestStepFinished>().ToList();
testCaseStartedIds.Should().Contain(id => testStepStartedItems.Any(tss => tss.TestCaseStartedId == id), "a test case started should be referenced by at least one test step started message");
testCaseStartedIds.Should().Contain(id => testStepFinishedItems.Any(tsf => tsf.TestCaseStartedId == id), "a test case started should be referenced by at least one test step finished message");
}
private void TestExecutionMessagesShouldProperlyNest()
{
var closedIds = new List<string>();
var openTestCaseStartedIds = new List<string>();
var openTestStepIds = new List<string>();
var numberOfEnvelopes = _actualEnvelopes.Count();
var testRunStartedSeenAtEnvelopeIndex = numberOfEnvelopes + 1;
var testRunFinishedSeenAtEnvelopeIndex = -1;
int currentIndex = 0;
foreach (object msg in _actualEnvelopes.Select(e => e.Content()))
{
switch (msg)
{
case TestRunStarted:
testRunStartedSeenAtEnvelopeIndex = currentIndex;
if (testRunFinishedSeenAtEnvelopeIndex != -1)
testRunStartedSeenAtEnvelopeIndex.Should().BeLessThan(testRunFinishedSeenAtEnvelopeIndex, "TestRunStarted events must be before TestRunFinished event");
break;
case TestRunFinished:
testRunFinishedSeenAtEnvelopeIndex = currentIndex;
testRunFinishedSeenAtEnvelopeIndex.Should().BeGreaterThan(testRunStartedSeenAtEnvelopeIndex, "TestRunFinished events must be after TestRunStarted event");
testRunFinishedSeenAtEnvelopeIndex.Should().Be(numberOfEnvelopes - 1, "TestRunFinished events must be the last event");
break;
case TestCaseStarted testCaseStarted:
currentIndex.Should().BeGreaterThan(testRunStartedSeenAtEnvelopeIndex, "TestCaseStarted events must be after TestRunStarted event");
if (testRunFinishedSeenAtEnvelopeIndex != -1)
currentIndex.Should().BeLessThan(testRunFinishedSeenAtEnvelopeIndex, "TestCaseStarted events must be before TestRunFinished event");
closedIds.Should().NotContain(testCaseStarted.Id, "a test case should not be Started twice");
openTestCaseStartedIds.Add(testCaseStarted.Id);
break;
case TestCaseFinished testCaseFinished:
currentIndex.Should().BeGreaterThan(testRunStartedSeenAtEnvelopeIndex, "TestCaseFinished events must be after TestRunStarted event");
if (testRunFinishedSeenAtEnvelopeIndex != -1)
currentIndex.Should().BeLessThan(testRunFinishedSeenAtEnvelopeIndex, "TestCaseFinished events must be before TestRunFinished event");
closedIds.Should().NotContain(testCaseFinished.TestCaseStartedId, "a test case should not be Finished twice");
openTestCaseStartedIds.Should().Contain(testCaseFinished.TestCaseStartedId, "a test case should be Started and active before it is Finished");
openTestCaseStartedIds.Remove(testCaseFinished.TestCaseStartedId);
closedIds.Add(testCaseFinished.TestCaseStartedId);
openTestCaseStartedIds.Remove(testCaseFinished.TestCaseStartedId);
break;
case TestStepStarted testStepStarted:
currentIndex.Should().BeGreaterThan(testRunStartedSeenAtEnvelopeIndex, "TestStepStarted events must be after TestRunStarted event");
if (testRunFinishedSeenAtEnvelopeIndex != -1)
currentIndex.Should().BeLessThan(testRunFinishedSeenAtEnvelopeIndex, "TestStepStarted events must be before TestRunFinished event");
closedIds.Should().NotContain(testStepStarted.TestCaseStartedId, "a TestStepStarted event must refer to an active test case");
openTestCaseStartedIds.Should().Contain(testStepStarted.TestCaseStartedId, "a TestStepStarted event must refer to an active test case");
openTestStepIds.Add(testStepStarted.TestStepId);
break;
case TestStepFinished testStepFinished:
currentIndex.Should().BeGreaterThan(testRunStartedSeenAtEnvelopeIndex, "TestStepFinished events must be after TestRunStarted event");
if (testRunFinishedSeenAtEnvelopeIndex != -1)
currentIndex.Should().BeLessThan(testRunFinishedSeenAtEnvelopeIndex, "TestStepFinished events must be before TestRunFinished event");
closedIds.Should().NotContain(testStepFinished.TestCaseStartedId, "a TestStepFinished event must refer to an active test case");
// Can not assert the following as TestStepIds are re-used when a step is re-executed during a Retry
// ClosedIDs.Should().NotContain(testStepFinished.TestStepId, "a TestStepFinished event must refer to an active test step");
openTestCaseStartedIds.Should().Contain(testStepFinished.TestCaseStartedId, "a TestStepFinished event must refer to an active test case");
openTestStepIds.Should().Contain(testStepFinished.TestStepId, "a TestStepFinished event must refer to an active test step");
closedIds.Add(testStepFinished.TestStepId);
openTestStepIds.Remove(testStepFinished.TestStepId);
break;
}
currentIndex++;
}
}
private void EachTestCaseAndStepsShouldProperlyReferToAPickleAndStepDefinitionOrHook()
{
var testCases = _actualElementsByType[typeof(TestCase)].OfType<TestCase>();
foreach (var testCase in testCases)
{
var pickle = testCase.PickleId;
_actualElementsById.Should().ContainKey(pickle, "a pickle should be referenced by the test case");
var steps = testCase.TestSteps.OfType<TestStep>();
foreach (var step in steps)
{
if (step.HookId != null)
_actualElementsById.Should().ContainKey(step.HookId, "a step references a hook that doesn't exist");
if (step.PickleStepId != null)
_actualElementsById.Should().ContainKey(step.PickleStepId, "a step references a pickle step that doesn't exist");
if (step.StepDefinitionIds is { Count: > 0 })
{
foreach (var stepDefinitionId in step.StepDefinitionIds)
_actualElementsById.Should().ContainKey(stepDefinitionId, "a step references a step definition that doesn't exist");
}
}
}
}
private void EachPickleAndPickleStepShouldBeReferencedByTestStepsAtLeastOnce()
{
var pickles = _actualElementsByType[typeof(Pickle)].OfType<Pickle>();
foreach (var pickle in pickles)
{
var testCases = _actualElementsByType[typeof(TestCase)].OfType<TestCase>();
testCases.Should().Contain(tc => tc.PickleId == pickle.Id, "a pickle should be referenced by a test case");
var pickleSteps = pickle.Steps.OfType<PickleStep>();
foreach (var pickleStep in pickleSteps)
{
var testSteps = _actualElementsByType[typeof(TestStep)].OfType<TestStep>();
testSteps.Should().Contain(ts => ts.PickleStepId == pickleStep.Id, "a pickle step should be referenced by a test step");
}
}
}
public void ShouldPassBasicStructuralChecks()
{
var actual = _actualEnvelopes;
var expected = _expectedEnvelopes;
using (new AssertionScope("Basic Structural Comparison Tests"))
{
// This checks that each top level Envelope content type present in the actual is present in the expected in the same number (except for hooks)
foreach (var messageType in EnvelopeExtensions.EnvelopeContentTypes)
{
if (_actualElementsByType.ContainsKey(messageType) && !_expectedElementsByType.ContainsKey(messageType))
{
throw new System.Exception($"{messageType} present in the actual but not in the expected.");
}
if (!_actualElementsByType.ContainsKey(messageType) && _expectedElementsByType.ContainsKey(messageType))
{
throw new System.Exception($"{messageType} present in the expected but not in the actual.");
}
if (messageType != typeof(Hook) && _actualElementsByType.TryGetValue(messageType, out var nonHookElement))
{
nonHookElement.Should().HaveCount(_expectedElementsByType[messageType].Count);
}
if (messageType == typeof(Hook) && _actualElementsByType.TryGetValue(messageType, out var hookElement))
hookElement.Should().HaveCountGreaterThanOrEqualTo(_expectedElementsByType[messageType].Count);
}
actual.Should().HaveCountGreaterThanOrEqualTo(expected.Count(), "the total number of envelopes in the actual should be at least as many as in the expected");
}
}
private bool GroupListIsEmpty(List<Group>? groups)
{
if (groups == null || groups.Count == 0) return true;
foreach (var group in groups)
{
if (!string.IsNullOrEmpty(group.Value) || group.Start.HasValue)
return false;
if (!GroupListIsEmpty(group.Children)) return false;
}
return true;
}
private EquivalencyAssertionOptions<T> ArrangeFluentAssertionOptions<T>(EquivalencyAssertionOptions<T> options)
{
// invoking these for each Type in CucumberMessages so that FluentAssertions DOES NOT call .Equal when comparing instances
return options
.ComparingByMembers<Attachment>()
.ComparingByMembers<Background>()
.ComparingByMembers<Ci>()
.ComparingByMembers<Comment>()
.ComparingByMembers<Io.Cucumber.Messages.Types.DataTable>()
.ComparingByMembers<DocString>()
.ComparingByMembers<Envelope>()
.ComparingByMembers<Examples>()
.ComparingByMembers<Feature>()
.ComparingByMembers<FeatureChild>()
.ComparingByMembers<GherkinDocument>()
.ComparingByMembers<Group>()
.ComparingByMembers<Hook>()
.ComparingByMembers<Location>()
.ComparingByMembers<Meta>()
.ComparingByMembers<ParameterType>()
.ComparingByMembers<ParseError>()
.ComparingByMembers<Pickle>()
.ComparingByMembers<PickleDocString>()
.ComparingByMembers<PickleStep>()
.ComparingByMembers<PickleStepArgument>()
.ComparingByMembers<PickleTable>()
.ComparingByMembers<PickleTableCell>()
.ComparingByMembers<PickleTableRow>()
.ComparingByMembers<PickleTag>()
.ComparingByMembers<Product>()
.ComparingByMembers<Rule>()
.ComparingByMembers<RuleChild>()
.ComparingByMembers<Scenario>()
.ComparingByMembers<Source>()
.ComparingByMembers<SourceReference>()
.ComparingByMembers<Step>()
.ComparingByMembers<StepDefinition>()
.ComparingByMembers<StepDefinitionPattern>()
.ComparingByMembers<StepMatchArgument>()
.ComparingByMembers<StepMatchArgumentsList>()
.ComparingByMembers<TableCell>()
.ComparingByMembers<TableRow>()
.ComparingByMembers<Tag>()
.ComparingByMembers<TestCase>()
.ComparingByMembers<TestCaseFinished>()
.ComparingByMembers<TestCaseStarted>()
.ComparingByMembers<TestRunFinished>()
.ComparingByMembers<TestRunStarted>()
.ComparingByMembers<TestStep>()
.ComparingByMembers<TestStepFinished>()
.ComparingByMembers<TestStepResult>()
.ComparingByMembers<TestStepStarted>()
.ComparingByMembers<UndefinedParameterType>()
.ComparingByMembers<TestRunHookStarted>()
.ComparingByMembers<TestRunHookFinished>()
.ComparingByMembers<Suggestion>()
.ComparingByMembers<Snippet>()
// Using a custom Property Selector so that we can ignore the properties that are not comparable
.Using(_customCucumberMessagesPropertySelector)
// Using a custom string comparison that deals with ISO language codes when the property name ends with "Language"
//.Using<string>(ctx =>
//{
// var actual = ctx.Subject.Split("-")[0];
// var expected = ctx.Expectation.Split("-")[0];
// actual.Should().Be(expected);
//})
// Using special logic to assert that suggestions must contain at least one snippet among those specified in the Expected set
// We can't compare snippet content as the Language and Code properties won't match
.Using<Suggestion>(ctx =>
{
var actualSnippets = ctx.Subject.Snippets ?? new List<Snippet>();
var expectedSnippets = ctx.Expectation.Snippets ?? new List<Snippet>();
actualSnippets.Should().HaveCountGreaterThanOrEqualTo(1).And.HaveCountLessThanOrEqualTo(expectedSnippets.Count);
})
.WhenTypeIs<Suggestion>()
// Using special logic to compare regular expression strings (ignoring the differences of the regex anchor characters)
.Using<List<string>>(ctx =>
{
var subjects = ctx.Subject;
var expectations = ctx.Expectation;
subjects.Should().HaveSameCount(expectations);
int count = subjects.Count;
for (int i = 0; i < count; i++)
{
string subject = subjects[i];
string expectation = expectations[i];
if (subject.Length > 0 && subject[0] == '^' || expectation.Length > 0 && expectation[0] == '^' ||
subject.Length > 0 && subject[^1] == '$' || expectation.Length > 0 && expectation[^1] == '$')
{
// If the first or last character is '^' or '$', remove it before comparing
subject = subject.Length > 0 && subject[0] == '^' ? subject.Substring(1) : subject;
subject = subject.Length > 0 && subject[^1] == '$' ? subject.Substring(0, subject.Length - 1) : subject;
expectation = expectation.Length > 0 && expectation[0] == '^' ? expectation.Substring(1) : expectation;
expectation = expectation.Length > 0 && expectation[^1] == '$' ? expectation.Substring(0, expectation.Length - 1) : expectation;
}
subject.Should().Be(expectation);
}
})
.When(info => info.Path.EndsWith("RegularExpressions"))
// Using special logic to ignore ParameterTypeName except when the value is one of the basic types
.Using<string>((ctx) =>
{
if (ctx.Expectation == "string" || ctx.Expectation == "int" || ctx.Expectation == "long" || ctx.Expectation == "double" || ctx.Expectation == "float"
|| ctx.Expectation == "short" || ctx.Expectation == "byte" || ctx.Expectation == "biginteger")
{
ctx.Subject.Should().Be(ctx.Expectation);
}
// Any other ParameterTypeName should be ignored, including {word} (no .NET equivalent) and custom type names
else
{
1.Should().Be(1);
}
})
.When(info => info.Path.EndsWith("ParameterTypeName"))
// Using a custom string comparison to ignore the differences in platform line endings
.Using<string>((ctx) =>
{
var subject = ctx.Subject ?? string.Empty;
var expectation = ctx.Expectation ?? string.Empty;
subject = subject.Replace("\r\n", "\n");
expectation = expectation.Replace("\r\n", "\n");
subject.Should().Be(expectation);
})
.When(info => info.Path.EndsWith("Description") || info.Path.EndsWith("Text") || info.Path.EndsWith("Data") || info.Path.EndsWith("Content"))
// The list of hooks should contain at least as many items as the list of expected hooks
// Because Reqnroll does not support Tag Expressions, these are represented in RnR as multiple Hooks or multiple Tags on Hooks Binding methods
// which result in multiple Hook messages.
.Using<List<Hook>>(ctx =>
{
if (ctx.SelectedNode.IsRoot)
{
var actualList = ctx.Subject;
var expectedList = ctx.Expectation;
if (expectedList == null || !expectedList.Any())
{
return; // If expected is null or empty, we don't need to check anything
}
actualList.Should().NotBeNull();
actualList.Should().HaveCountGreaterThanOrEqualTo(expectedList.Count,
"actual collection should have at least as many items as expected");
// Difficult to compare individual Hook messages: Ids aren't comparable, the Source references aren't compatible,
// and After Hook execution ordering is different between Reqnroll and CCK.
foreach (var expectedItem in expectedList)
{
actualList.Should().Contain(actualItem =>
actualItem.TagExpression == expectedItem.TagExpression
&& actualItem.Type == expectedItem.Type,
"actual collection should contain an item equivalent to {0}", expectedItem);
}
}
})
.WhenTypeIs<List<Hook>>()
// Groups are nested self-referential objects inside StepMatchArgument(s). Other Cucumber implementations support a more sophisticated
// version of this structure in which multiple regex capture groups are conveyed inside a single StepMatchArgument
// For Reqnroll, we will only compare the outermost Group of the actual.
.Using<Group>((ctx) =>
{
var actual = ctx.Subject;
var expected = ctx.Expectation;
if (expected != null
&& expected.Value.Length > 2
&& ((expected.Value.StartsWith("\"") && expected.Value.EndsWith("\""))
|| (expected.Value.StartsWith("'") && expected.Value.EndsWith("'")))
&& !GroupListIsEmpty(expected.Children))
{
actual.Value.Should().Be(expected.Children[0].Value);
actual.Start.Should().Be(expected.Children[0].Start);
}
else
{
actual.Value.Should().Be(ctx.Expectation.Value);
actual.Start.Should().Be(ctx.Expectation.Start);
}
})
.WhenTypeIs<Group>()
// A bit of trickery here to tell FluentAssertions that Timestamps are always equal
// We can't simply omit Timestamp from comparison because then TestRunStarted has nothing else to compare (which causes an error)
.Using<Timestamp>(_ => 1.Should().Be(1))
.WhenTypeIs<Timestamp>()
.Using<List<Group>>((ctx) =>
{
var actual = ctx.Subject;
var expected = ctx.Expectation;
// The CCK cucumber implementation sometimes renders empty nested groups, which we will ignore
if (GroupListIsEmpty(expected)) { 1.Should().Be(1); }
actual.Should().BeEquivalentTo(expected);
})
.WhenTypeIs<List<Group>>()
// TestStepResult.Message contains Exception message string. If one is expected, then the result should have content
.Using<string>((ctx) =>
{
var actual = ctx.Subject;
var expected = ctx.Expectation;
if (!string.IsNullOrEmpty(expected))
{
actual.Should().NotBeNullOrEmpty();
}
else
{
actual.Should().BeNullOrEmpty();
}
})
.When(info => info.Path.EndsWith("Message"))
.AllowingInfiniteRecursion()
//.RespectingRuntimeTypes()
.ExcludingFields()
.WithStrictOrdering();
}
}