Skip to content

Commit

Permalink
Ensure all code from the core assembly in a stack trace is ignored by…
Browse files Browse the repository at this point in the history
… the caller identification logic
  • Loading branch information
dennisdoomen committed Oct 26, 2023
1 parent 287b667 commit 55ec677
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Src/FluentAssertions/CallerIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static string DetermineCallerIdentity()
if (frame.GetMethod() is not null
&& !IsDynamic(frame)
&& !IsDotNet(frame)
&& !IsCustomAssertion(frame))
&& !IsCustomAssertion(frame)
&& !IsCurrentAssembly(frame))
{
caller = ExtractVariableNameFrom(frame);
break;
Expand Down
24 changes: 24 additions & 0 deletions Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Equivalency;
using FluentAssertions.Execution;
using FluentAssertions.Extensions;
using FluentAssertions.Primitives;
using Xunit;
using Xunit.Sdk;

Expand Down Expand Up @@ -548,6 +550,28 @@ public void An_object_initializer_preceding_an_assertion_is_not_an_identifier()
act.Should().Throw<XunitException>()
.WithMessage("Expected object to be*");
}

[Fact]
public void All_core_code_anywhere_in_the_stack_trace_is_ignored()
{
/*
We want to test this specific scenario.
1. CallerIdentifier.DetermineCallerIdentity
2. FluentAssertions code
3. Custom extension <--- pointed to by lastUserStackFrameBeforeFluentAssertionsCodeIndex
4. FluentAssertions code <--- this is where DetermineCallerIdentity tried to get the variable name from before the fix
5. Test
*/

var node = Node.From<Foo>(GetSubjectId);

// Assert
node.Description.Should().StartWith("node.Description");
}

[CustomAssertion]
private string GetSubjectId() => AssertionScope.Current.CallerIdentity;
}

#pragma warning disable IDE0060, RCS1163 // Remove unused parameter
Expand Down

0 comments on commit 55ec677

Please sign in to comment.