Skip to content

Commit c293c49

Browse files
committed
refactor: move StateBag propagation onto TestBuilderContext itself
CopyStateBagTo accesses _stateBag directly, eliminating the need for the RawStateBag internal property and keeping the logic closer to the data it operates on.
1 parent e5d0eab commit c293c49

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

TUnit.Core/TestBuilderContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public static TestBuilderContext? Current
4040
set => _stateBag = value;
4141
}
4242

43-
internal ConcurrentDictionary<string, object?>? RawStateBag => _stateBag;
43+
internal void CopyStateBagTo(TestBuilderContext target)
44+
{
45+
if (_stateBag is { IsEmpty: false } bag)
46+
{
47+
target.StateBag = new ConcurrentDictionary<string, object?>(bag);
48+
}
49+
}
4450

4551
public TestContextEvents Events
4652
{

TUnit.Engine/Building/TestBuilder.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ await _objectLifecycleService.RegisterObjectAsync(
287287
ClassConstructor = testBuilderContext.ClassConstructor
288288
};
289289

290-
PropagateStateBag(testBuilderContext, contextAccessor.Current);
290+
testBuilderContext.CopyStateBagTo(contextAccessor.Current);
291291

292292
var (classDataUnwrapped, classRowMetadata) = DataUnwrapper.UnwrapWithMetadata(await classDataFactory() ?? []);
293293
classData = classDataUnwrapped;
@@ -486,7 +486,7 @@ await _objectLifecycleService.RegisterObjectAsync(
486486
InitializedAttributes = attributes
487487
};
488488

489-
PropagateStateBag(testBuilderContext, testSpecificContext);
489+
testBuilderContext.CopyStateBagTo(testSpecificContext);
490490

491491
var test = await BuildTestAsync(metadata, testData, testSpecificContext, cancellationToken: cancellationToken);
492492
test.Context.SkipReason = skipReason;
@@ -546,7 +546,7 @@ await _objectLifecycleService.RegisterObjectAsync(
546546
InitializedAttributes = attributes
547547
};
548548

549-
PropagateStateBag(testBuilderContext, testSpecificContext);
549+
testBuilderContext.CopyStateBagTo(testSpecificContext);
550550

551551
var test = await BuildTestAsync(metadata, testData, testSpecificContext, cancellationToken: cancellationToken);
552552
test.Context.SkipReason = skipReason;
@@ -1887,11 +1887,4 @@ internal bool CouldTestMatchFilter(ITestExecutionFilter filter, TestMetadata met
18871887
return _filterMatcher.CouldMatchFilter(metadata, filter);
18881888
}
18891889

1890-
private static void PropagateStateBag(TestBuilderContext source, TestBuilderContext target)
1891-
{
1892-
if (source.RawStateBag is { IsEmpty: false } bag)
1893-
{
1894-
target.StateBag = new ConcurrentDictionary<string, object?>(bag);
1895-
}
1896-
}
18971890
}

0 commit comments

Comments
 (0)