-
Notifications
You must be signed in to change notification settings - Fork 64
/
AllureContext.cs
473 lines (431 loc) · 15.3 KB
/
AllureContext.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
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
#nullable enable
namespace Allure.Net.Commons;
/// <summary>
/// Represents allure-related contextual information required to collect
/// the report data during a test execution. Comprises four contexts:
/// container, fxiture, test, and step, as well as methods to query and
/// modify them.
/// </summary>
/// <remarks>
/// Instances of this class are immutable to ensure proper isolation
/// between different tests and steps that may potentially be run
/// cuncurrently either by a test framework or by an end user.
/// </remarks>
[DebuggerDisplay(
"Containers = {ContainerContextDepth}, HasFixture = {HasFixture}, " +
"HasTest = {HasTest}, Steps = {StepContextDepth}"
)]
public record class AllureContext
{
/// <summary>
/// Returns true if a container context is active.
/// </summary>
public bool HasContainer => !this.ContainerContext.IsEmpty;
/// <summary>
/// Returns the number of containers in the container context.
/// </summary>
public int ContainerContextDepth => this.ContainerContext.Count();
/// <summary>
/// Returns true if a fixture context is active.
/// </summary>
public bool HasFixture => this.FixtureContext is not null;
/// <summary>
/// Returns true if a test context is active.
/// </summary>
public bool HasTest => this.TestContext is not null;
/// <summary>
/// Returns true if a step context is active.
/// </summary>
public bool HasStep => !this.StepContext.IsEmpty;
/// <summary>
/// Returns the number of steps in the step context.
/// </summary>
public int StepContextDepth => this.StepContext.Count();
/// <summary>
/// A stack of fixture containers affecting subsequent tests.
/// </summary>
/// <remarks>
/// Activating this context allows operations on the current container
/// (including adding a fixture to or removing a fixture from the
/// current container).
/// </remarks>
internal IImmutableStack<TestResultContainer> ContainerContext
{
get;
private init;
} = ImmutableStack<TestResultContainer>.Empty;
/// <summary>
/// A fixture that is being currently executed.
/// </summary>
/// <remarks>
/// Activating this context allows operations on the current fixture
/// result.<br/>
/// This property differs from <see cref="CurrentFixture"/> in that
/// instead of throwing it returns null if a fixture context isn't
/// active.
/// </remarks>
internal FixtureResult? FixtureContext { get; private init; }
/// <summary>
/// A test that is being executed.
/// </summary>
/// <remarks>
/// Activating this context allows operations on the current test
/// result.<br/>
///
/// This property differs from <see cref="CurrentTest"/> in that
/// instead of throwing it returns null if a test context isn't active.
/// </remarks>
internal TestResult? TestContext { get; private init; }
/// <summary>
/// A stack of nested steps that are being executed.
/// </summary>
/// <remarks>
/// Activating this context allows operations on the current step.
/// </remarks>
internal IImmutableStack<StepResult> StepContext
{
get;
private init;
} = ImmutableStack<StepResult>.Empty;
/// <summary>
/// The most recently added container from the container context.
/// </summary>
/// <remarks>
/// It throws <see cref="InvalidOperationException"/> if a container
/// context isn't active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
internal TestResultContainer CurrentContainer
{
get => this.ContainerContext.FirstOrDefault()
?? throw new InvalidOperationException(
"No container context is active."
);
}
/// <summary>
/// A fixture that is being executed.
/// </summary>
/// <remarks>
/// It throws <see cref="InvalidOperationException"/> if a fixture
/// context isn't active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
internal FixtureResult CurrentFixture =>
this.FixtureContext ?? throw new InvalidOperationException(
"No fixture context is active."
);
/// <summary>
/// A test that is being executed.
/// </summary>
/// <remarks>
/// It throws <see cref="InvalidOperationException"/> if a test context
/// isn't active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
internal TestResult CurrentTest =>
this.TestContext ?? throw new InvalidOperationException(
"No test context is active."
);
/// <summary>
/// A step that is being executed.
/// </summary>
/// <remarks>
/// It throws <see cref="InvalidOperationException"/> if a step context
/// isn't active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
internal StepResult CurrentStep =>
this.StepContext.FirstOrDefault()
?? throw new InvalidOperationException(
"No step context is active."
);
/// <summary>
/// A step container a next step should be put in.
/// </summary>
/// <remarks>
/// A step container can be a fixture, a test of an another step.<br/>
/// It throws <see cref="InvalidOperationException"/> if neither
/// fixture, nor test, nor step context is active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
internal ExecutableItem CurrentStepContainer =>
this.StepContext.FirstOrDefault() as ExecutableItem
?? this.RootStepContainer
?? throw new InvalidOperationException(
"No fixture, test, or step context is active."
);
/// <summary>
/// Used by <see cref="ToString"/> to serialize proeprties of the
/// context.
/// </summary>
protected virtual bool PrintMembers(StringBuilder stringBuilder)
{
var containers =
RepresentStack(this.ContainerContext, c => c.name ?? c.uuid);
var fixture = this.FixtureContext?.name ?? "null";
var test = this.TestContext?.name
?? this.TestContext?.uuid
?? "null";
var steps = RepresentStack(this.StepContext, s => s.name);
stringBuilder.AppendFormat("Containers = [{0}], ", containers);
stringBuilder.AppendFormat("Fixture = {0}, ", fixture);
stringBuilder.AppendFormat("Test = {0}, ", test);
stringBuilder.AppendFormat("Steps = [{0}]", steps);
return true;
}
/// <summary>
/// Creates a new <see cref="AllureContext"/> with the active container
/// context and the specified container pushed on top of it.
/// </summary>
/// <remarks>
/// Can't be called if a fixture or a test context is active.
/// </remarks>
/// <param name="container">
/// A container to push on top of the container context.
/// </param>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (always active) container context.
/// </returns>
/// <exception cref="ArgumentNullException"/>
internal AllureContext WithContainer(TestResultContainer container) =>
this.ValidateContainerContextCanBeModified() with
{
ContainerContext = this.ContainerContext.Push(
container ?? throw new ArgumentNullException(
nameof(container)
)
)
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> without the most recently
/// added container in its container context. Requires an active
/// container context. Deactivates a container context if it consists
/// of one container only before the call.
/// </summary>
/// <remarks>
/// Can't be called if a fixture or a test context is active.
/// </remarks>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (possibly inactive) container context.
/// </returns>
/// <exception cref="InvalidOperationException"/>
internal AllureContext WithNoLastContainer() =>
this with
{
ContainerContext = this.ValidateContainerCanBeRemoved()
.ContainerContext.Pop()
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> with the active fixture
/// context that is set to the specified fixture. Requires the
/// container context to be active.
/// </summary>
/// <remarks>
/// Only one fixture context can be active at a time.
/// </remarks>
/// <param name="fixtureResult">
/// A new fixture context.
/// </param>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (always active) fixture context.
/// </returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="InvalidOperationException"/>
internal AllureContext WithFixtureContext(FixtureResult fixtureResult) =>
this with
{
FixtureContext = this.ValidateNewFixtureContext(
fixtureResult ?? throw new ArgumentNullException(
nameof(fixtureResult)
)
),
StepContext = this.StepContext.Clear()
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> with inactive fixture and
/// step contexts.
/// </summary>
internal AllureContext WithNoFixtureContext() =>
this with
{
FixtureContext = null,
StepContext = this.StepContext.Clear()
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> with the active test
/// context that is set to the specified test result.
/// Can't be used if a fixture context is active.
/// </summary>
/// <param name="testResult">
/// A new test context.
/// </param>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (always active) test context.
/// </returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="InvalidOperationException"/>
internal AllureContext WithTestContext(TestResult testResult) =>
this with
{
TestContext = this.ValidateNewTestContext(
testResult ?? throw new ArgumentNullException(
nameof(testResult)
)
)
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> with inactive test,
/// fixture and step contexts.
/// </summary>
internal AllureContext WithNoTestContext() =>
this with
{
FixtureContext = null,
TestContext = null,
StepContext = this.StepContext.Clear()
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> with the active step
/// context and the specified step result pushed on top of it.
/// </summary>
/// <remarks>
/// Can't be called if neither fixture, nor test context is active.
/// </remarks>
/// <param name="stepResult">
/// A new step result to push on top of the step context.
/// </param>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (always active) step context.
/// </returns>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="InvalidOperationException"/>
internal AllureContext WithStep(StepResult stepResult) =>
this with
{
StepContext = this.StepContext.Push(
this.ValidateNewStep(
stepResult ?? throw new ArgumentNullException(
nameof(stepResult)
)
)
)
};
/// <summary>
/// Creates a new <see cref="AllureContext"/> without the most recently
/// added step in its step context. Requires an active step context.
/// Deactivates a step context if it consists of one step only before
/// the call.
/// </summary>
/// <returns>
/// A new instance of <see cref="AllureContext"/> with the modified
/// (possibly inactive) step context.
/// </returns>
/// <exception cref="InvalidOperationException"/>
internal AllureContext WithNoLastStep() =>
this with
{
StepContext = this.HasStep
? this.StepContext.Pop()
: throw new InvalidOperationException(
"Unable to deactivate the step context because it " +
"isn't active."
)
};
AllureContext ValidateContainerContextCanBeModified()
{
if (this.FixtureContext is not null)
{
throw new InvalidOperationException(
"Unable to change the container context because the " +
"fixture context is active."
);
}
if (this.TestContext is not null)
{
throw new InvalidOperationException(
"Unable to change the container context because the " +
"test context is active."
);
}
return this;
}
AllureContext ValidateContainerCanBeRemoved()
{
if (!this.HasContainer)
{
throw new InvalidOperationException(
"Unable to deactivate the container context because it " +
"is not active."
);
}
return this.ValidateContainerContextCanBeModified();
}
ExecutableItem? RootStepContainer
{
get => this.FixtureContext as ExecutableItem ?? this.TestContext;
}
FixtureResult ValidateNewFixtureContext(FixtureResult fixture)
{
if (!this.HasContainer)
{
throw new InvalidOperationException(
"Unable to activate the fixture context " +
"because the container context is not active."
);
}
if (this.HasFixture)
{
throw new InvalidOperationException(
"Unable to activate the fixture context " +
"because it's already active."
);
}
return fixture;
}
TestResult ValidateNewTestContext(TestResult testResult)
{
if (this.HasFixture)
{
throw new InvalidOperationException(
"Unable to activate the test context " +
"because the fixture context is active."
);
}
if (this.HasTest)
{
throw new InvalidOperationException(
"Unable to activate the test context " +
"because it is already active."
);
}
return testResult;
}
StepResult ValidateNewStep(StepResult stepResult)
{
if (!this.HasTest && !this.HasFixture)
{
throw new InvalidOperationException(
"Unable to activate the step context because neither " +
"test, nor fixture context is active."
);
}
return stepResult;
}
static string RepresentStack<T>(
IImmutableStack<T> stack,
Func<T, string> projection
) => string.Join(
" <- ",
stack.Select(projection)
);
}