-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathTestBuilderContext.cs
More file actions
71 lines (57 loc) · 2.22 KB
/
TestBuilderContext.cs
File metadata and controls
71 lines (57 loc) · 2.22 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
using System.Collections.Concurrent;
using TUnit.Core.Interfaces;
namespace TUnit.Core;
/// <summary>
/// Represents the context for building tests.
/// </summary>
public record TestBuilderContext
{
private static readonly AsyncLocal<TestBuilderContext?> BuilderContexts = new();
/// <summary>
/// Gets the current test builder context.
/// </summary>
public static TestBuilderContext? Current
{
get => BuilderContexts.Value;
internal set => BuilderContexts.Value = value;
}
public string DefinitionId { get; } = Guid.NewGuid().ToString();
[Obsolete("Use StateBag property instead.")]
public ConcurrentDictionary<string, object?> ObjectBag => StateBag;
/// <summary>
/// Gets the state bag for storing arbitrary data during test building.
/// </summary>
public ConcurrentDictionary<string, object?> StateBag { get; set; } = new();
public TestContextEvents Events { get; set; } = new();
public IDataSourceAttribute? DataSourceAttribute { get; set; }
/// <summary>
/// Gets the test method information, if available during source generation.
/// </summary>
public required MethodMetadata TestMetadata { get; init; }
internal IClassConstructor? ClassConstructor { get; set; }
/// <summary>
/// Cached and initialized attributes for the test
/// </summary>
internal Attribute[]? InitializedAttributes { get; set; }
public void RegisterForInitialization(object? obj)
{
Events.OnInitialize += async (sender, args) =>
{
await ObjectInitializer.InitializeAsync(obj);
};
}
internal static TestBuilderContext FromTestContext(TestContext testContext, IDataSourceAttribute? dataSourceAttribute)
{
return new TestBuilderContext
{
Events = testContext.InternalEvents, TestMetadata = testContext.Metadata.TestDetails.MethodMetadata, DataSourceAttribute = dataSourceAttribute, StateBag = testContext.StateBag.Items,
};
}
}
/// <summary>
/// Provides access to the current <see cref="TestBuilderContext"/>.
/// </summary>
public class TestBuilderContextAccessor(TestBuilderContext context)
{
public TestBuilderContext Current { get; set; } = context;
}