Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Jan 29, 2022
1 parent 8d59e4d commit 6a80ee0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Src/FluentAssertions/CallerIdentifier.cs
Expand Up @@ -173,7 +173,7 @@ private static string ExtractVariableNameFrom(StackFrame frame)
{
Logger(statement);
if (!IsBooleanLiteral(statement) && !IsNumeric(statement) && !IsStringLiteral(statement) &&
!UsesNewKeyword(statement))
!StartsWithNewInitializer(statement))
{
caller = statement;
}
Expand Down Expand Up @@ -233,9 +233,9 @@ private static string GetSourceCodeStatementFrom(StackFrame frame, StreamReader
return sb.ToString();
}

private static bool UsesNewKeyword(string candidate)
private static bool StartsWithNewInitializer(string candidate)
{
return Regex.IsMatch(candidate, @"new(?:\s?\[|\s?\{|\s\w+)");
return Regex.IsMatch(candidate, @"(^|s+)new(?:\s?\[|\s?\{|\s\w+)");
}

private static bool IsStringLiteral(string candidate)
Expand Down
39 changes: 39 additions & 0 deletions Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
Expand Down Expand Up @@ -468,6 +469,44 @@ public async Task Caller_identification_should_also_work_for_statements_followin
act.Should().Throw<XunitException>()
.WithMessage("*someText*", "it should capture the variable name");
}

[Fact]
public void A_method_taking_an_array_initializer_is_an_identifier()
{
// Arrange
var foo = new Foo();

// Act
Action act = () => foo.GetFoo(new[] { 1, 2, 3 }.Sum() + "")
.Should()
.BeNull();

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected foo.GetFoo(new[]{1,2,3}.Sum()+\"\") to be <null>*");
}

[Fact]
public void An_array_initializer_preceding_an_assertion_is_not_an_identifier()
{
// Act
Action act = () => new[] { 1, 2, 3 }.Should().BeEmpty();

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

[Fact]
public void An_object_initializer_preceding_an_assertion_is_not_an_identifier()
{
// Act
Action act = () => new { Property = "blah" }.Should().BeNull();

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

[SuppressMessage("The name of a C# element does not begin with an upper-case letter", "SA1300")]
Expand Down

0 comments on commit 6a80ee0

Please sign in to comment.