Skip to content

Commit

Permalink
Improve the structure of a trace log
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Feb 6, 2022
1 parent 223e9e4 commit d373e90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Src/FluentAssertions/Equivalency/EquivalencyValidator.cs
Expand Up @@ -62,13 +62,14 @@ private static void UpdateScopeWithReportableContext(AssertionScope scope, Compa

private void RunStepsUntilEquivalencyIsProven(Comparands comparands, IEquivalencyValidationContext context)
{
using var _ = context.Tracer.WriteBlock(node => $"{node.Description}");

foreach (IEquivalencyStep step in AssertionOptions.EquivalencyPlan)
{
var result = step.Handle(comparands, context, this);
context.Tracer.WriteLine(_ => $"Step {step.GetType().Name} returned {result}");

if (result == EquivalencyResult.AssertionCompleted)
{
context.Tracer.WriteLine(_ => $"Equivalency was proven by {step.GetType().Name}");
return;
}
}
Expand Down
Expand Up @@ -18,7 +18,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
context.Tracer.WriteLine(member =>
{
string strategyName = (strategy == EqualityStrategy.Equals)
? "Equals must be used" : "object overrides Equals";
? $"{expectationType} overrides Equals" : "we are forced to use Equals";
return $"Treating {member.Description} as a value type because {strategyName}.";
});
Expand Down
Expand Up @@ -195,7 +195,34 @@ public void When_collection_does_contain_equivalent_by_including_single_property
var item = new Customer { Name = "John", Age = 20 };

// Act / Assert
collection.Should().ContainEquivalentOf(item, options => options.Including(x => x.Name));
collection.Should().ContainEquivalentOf(item, options => options.Including(x => x.Name).WithTracing());
}

[Fact]
public void Tracing_should_be_included_in_the_assertion_output()
{
// Arrange
var collection = new[]
{
new Customer
{
Name = "John",
Age = 18
},
new Customer
{
Name = "Jane",
Age = 18
}
};

var item = new Customer { Name = "John", Age = 21 };

// Act
Action act = () => collection.Should().ContainEquivalentOf(item, options => options.WithTracing());

// Assert
act.Should().Throw<XunitException>().WithMessage("*Equivalency was proven*");
}

[Fact]
Expand Down

0 comments on commit d373e90

Please sign in to comment.