Skip to content

Fix #827 Pester TestName w/expandable str returns nothing #851

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

Merged
Merged
Changes from 1 commit
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 @@ -32,8 +32,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(

return commandAsts.OfType<CommandAst>()
.Where(IsPesterCommand)
.Select(ast => ConvertPesterAstToSymbolReference(scriptFile, ast))
.Where(pesterSymbol => pesterSymbol?.TestName != null);
.Select(ast => ConvertPesterAstToSymbolReference(scriptFile, ast));
}

/// <summary>
Expand Down Expand Up @@ -105,20 +104,21 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
// Check for an explicit "-Name" parameter
if (currentCommandElement is CommandParameterAst parameterAst)
{
// Found -Name parameter, move to next element which is the argument for -TestName
i++;
if (parameterAst.ParameterName == "Name" && i < pesterCommandAst.CommandElements.Count)

if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
{
testName = alreadySawName ? null : (pesterCommandAst.CommandElements[i] as StringConstantExpressionAst)?.Value;
alreadySawName = true;
}

continue;
}

// Otherwise, if an argument is given with no parameter, we assume it's the name
// If we've already seen a name, we set the name to null
if (pesterCommandAst.CommandElements[i] is StringConstantExpressionAst testNameStrAst)
if (!alreadySawName && TryGetTestNameArgument(pesterCommandAst.CommandElements[i], out testName))
{
testName = alreadySawName ? null : testNameStrAst.Value;
alreadySawName = true;
}
}
Expand All @@ -131,6 +131,23 @@ private static PesterSymbolReference ConvertPesterAstToSymbolReference(ScriptFil
pesterCommandAst.Extent
);
}

private static bool TryGetTestNameArgument(CommandElementAst commandElementAst, out string testName)
{
testName = null;

if (commandElementAst is StringConstantExpressionAst testNameStrAst)
{
testName = testNameStrAst.Value;
return true;
}
else if (commandElementAst is ExpandableStringExpressionAst)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the else here -- above clause always returns

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a pattern match too since it just tests type overloads. But I'm easy either way :)

{
return true;
}

return false;
}
}

/// <summary>
Expand Down