Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocations when chaining contexts #2613

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions Src/FluentAssertions/Equivalency/EquivalencyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ private void TryToProveNodesAreEquivalent(Comparands comparands, IEquivalencyVal
{
using var _ = context.Tracer.WriteBlock(node => node.Description);

Func<IEquivalencyStep, GetTraceMessage> getMessage = step => _ => $"Equivalency was proven by {step.GetType().Name}";

foreach (IEquivalencyStep step in AssertionOptions.EquivalencyPlan)
{
var result = step.Handle(comparands, context, this);
if (result == EquivalencyResult.AssertionCompleted)
{
context.Tracer.WriteLine(getMessage(step));
context.Tracer.WriteLine(GetMessage(step));

static GetTraceMessage GetMessage(IEquivalencyStep step) =>
_ => $"Equivalency was proven by {step.GetType().Name}";

return;
}
}
Expand Down
15 changes: 12 additions & 3 deletions Src/FluentAssertions/Execution/AssertionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,26 @@ private AssertionScope(IAssertionStrategy assertionStrategy, Lazy<string> contex
reason = parent.reason;
callerIdentityProvider = parent.callerIdentityProvider;
FormattingOptions = parent.FormattingOptions.Clone();
Context = new Lazy<string>(() => JoinContext(parent.Context, context));
Context = JoinContexts(parent.Context, context);
}
else
{
Context = context;
}
}

private static string JoinContext(params Lazy<string>[] contexts)
private static Lazy<string> JoinContexts(Lazy<string> outer, Lazy<string> inner)
{
return string.Join("/", contexts.Where(ctx => ctx is not null).Select(x => x.Value));
return (outer, inner) switch
{
(null, null) => null,
({ } a, null) => a,
(null, { } b) => b,
({ } a, { } b) => Join(a, b)
};

static Lazy<string> Join(Lazy<string> outer, Lazy<string> inner) =>
new(() => outer.Value + "/" + inner.Value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public void Nested_scopes_use_the_name_of_their_outer_scope_as_context()
.WithMessage("Expected outer/inner to be equal to*");
}

[Fact]
public void The_inner_scope_is_used_when_the_outer_scope_does_not_have_a_context()
{
// Act
Action act = () =>
{
using var outerScope = new AssertionScope();
using var innerScope = new AssertionScope("inner");
new[] { 1, 2, 3 }.Should().Equal(3, 2, 1);
};

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected inner to be equal to*");
}

[Fact]
public void Message_should_contain_each_unique_failed_assertion_seperately()
{
Expand Down
Loading