Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions TUnit.Assertions/Core/Assertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,42 @@ public Assertion<TValue> Because(string message)
}

// Normal single-assertion execution (never delegates to wrapper)
var (value, exception) = await Context.GetAsync();
var contextResult = await Context.GetAsync();
var result = await CreateMetadataAndCheckAsync(contextResult.Value, contextResult.Exception);

if (!result.IsPassed)
{
ThrowOrAccumulateFailure(result);
}

return contextResult.Value;
}

// Create EvaluationMetadata in a separate scope to avoid creating additional
// DateTimeOffset fields in the state machine
private Task<AssertionResult> CreateMetadataAndCheckAsync(TValue? value, Exception? exception)
{
var (startTime, endTime) = Context.GetTiming();

var metadata = new EvaluationMetadata<TValue>(value, exception, startTime, endTime);
var result = await CheckAsync(metadata);
return CheckAsync(metadata);
}

if (!result.IsPassed)
private void ThrowOrAccumulateFailure(AssertionResult result)
{
var assertionException = CreateException(result);
var currentScope = AssertionScope.GetCurrentAssertionScope();

if (currentScope != null)
{
var assertionException = CreateException(result);
var currentScope = AssertionScope.GetCurrentAssertionScope();

if (currentScope != null)
{
// Within Assert.Multiple - accumulate exception instead of throwing
currentScope.AddException((AssertionException)assertionException);
}
else
{
// No scope - throw immediately
throw assertionException;
}
// Within Assert.Multiple - accumulate exception instead of throwing
currentScope.AddException((AssertionException)assertionException);
}
else
{
// No scope - throw immediately
throw assertionException;
}

return value;
}

/// <summary>
Expand Down
Loading