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

Fix for async helper methods #639

Merged
merged 3 commits into from
Jul 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ public static void UpdateFile(CallerContext context, InlineSnapshotSettings sett
span = new TextSpan(span.Start + context.ColumnNumber, 1);
}

var nodes = FindInvocations(root, span).Where(invocation =>
// It can be hard to find the method name from the call stack (compiler state machine from async/await, iterators, async iterators, etc.)
// If there is only one invocation, let's use it
var nodes = FindInvocations(root, span).ToArray();
if (nodes.Length > 1)
{
nodes = FindInvocations(root, span).Where(invocation =>
{
// Dummy.MethodName()
if (invocation.Expression is MemberAccessExpressionSyntax { Name.Identifier.Text: string memberName } && memberName == context.MethodName)
Expand All @@ -100,6 +105,7 @@ public static void UpdateFile(CallerContext context, InlineSnapshotSettings sett
return false;
})
.ToArray();
}

if (nodes.Length == 0)
throw new InlineSnapshotException("Cannot find the SyntaxNode to update");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Enables verification of objects using inline snapshots</Description>
<DefineConstants Condition="'$(IsOfficialBuild)' != 'true'">$(DefineConstants);DEBUG_TaskDialogPrompt</DefineConstants>

<Version>3.0.5</Version>
<Version>3.0.6</Version>
<NoWarn>$(NoWarn);NU5100</NoWarn>

<!-- Buggy because of netstandard2.0 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,48 @@ static System.Threading.Tasks.Task Helper(string expected, [CallerFilePath] stri
"""");
}

[Fact]
public async Task SupportAsyncHelperMethods_WithAsyncCode()
{
await AssertSnapshot(
$$""""
await Helper("");

[InlineSnapshotAssertion(nameof(expected))]
static async System.Threading.Tasks.Task Helper(string expected, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = -1)
{
await System.Threading.Tasks.Task.Yield();
var data = new
{
FirstName = "Gérald",
LastName = "Barré",
NickName = "meziantou",
};
{{nameof(InlineSnapshot)}}.{{nameof(InlineSnapshot.Validate)}}(data, expected, filePath, lineNumber);
}
"""",
$$""""
await Helper("""
FirstName: Gérald
LastName: Barré
NickName: meziantou
""");

[InlineSnapshotAssertion(nameof(expected))]
static async System.Threading.Tasks.Task Helper(string expected, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = -1)
{
await System.Threading.Tasks.Task.Yield();
var data = new
{
FirstName = "Gérald",
LastName = "Barré",
NickName = "meziantou",
};
{{nameof(InlineSnapshot)}}.{{nameof(InlineSnapshot.Validate)}}(data, expected, filePath, lineNumber);
}
"""");
}

[Fact]
public async Task SupportAsyncGenericHelperMethods()
{
Expand Down