Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Sep 1, 2017
1 parent 41a275b commit 05ab33a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,32 @@ void M()
Console.WriteLine(t1.Unknown);
}
(string name, int age) GetPerson() => default;
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseDeconstruction)]
public async Task TestTrivia1()
{
await TestInRegularAndScriptAsync(
@"class C
{
void M()
{
/*1*/(/*2*/int/*3*/ name, /*4*/int/*5*/ age)/*6*/ [|t1|] = GetPerson();
Console.WriteLine(/*7*/t1.name/*8*/ + "" "" + /*9*/t1.age/*10*/);
}
(string name, int age) GetPerson() => default;
}",
@"class C
{
void M()
{
/*1*/(/*2*/int/*3*/ name, /*4*/int/*5*/ age)/*6*/ = GetPerson();
Console.WriteLine(/*7*/name/*8*/ + "" "" + /*9*/age/*10*/);
}
(string name, int age) GetPerson() => default;
}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,45 @@ private SyntaxNode UpdateRoot(SemanticModel semanticModel, SyntaxNode root, Synt
}

private ForEachVariableStatementSyntax CreateForEachVariableStatement(INamedTypeSymbol tupleType, ForEachStatementSyntax forEachStatement)
=> SyntaxFactory.ForEachVariableStatement(
{
// Copy all the tokens/nodes from the existing foreach statement to the new foreach statement.
// However, convert the existing declaration over to a "var (x, y)" declaration or (int x, int y)
// tuple expression.
return SyntaxFactory.ForEachVariableStatement(
forEachStatement.ForEachKeyword,
forEachStatement.OpenParenToken,
CreateTupleOrDeclarationExpression(tupleType, forEachStatement.Type),
forEachStatement.InKeyword,
forEachStatement.Expression,
forEachStatement.CloseParenToken,
forEachStatement.Statement);
}

private ExpressionStatementSyntax CreateDeconstructionStatement(
INamedTypeSymbol tupleType, LocalDeclarationStatementSyntax declarationStatement, VariableDeclaratorSyntax variableDeclarator)
=> SyntaxFactory.ExpressionStatement(
{
// Copy all the tokens/nodes from the existing declaration statement to the new assignment
// statement. However, convert the existing declaration over to a "var (x, y)" declaration
// or (int x, int y) tuple expression.
return SyntaxFactory.ExpressionStatement(
SyntaxFactory.AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
CreateTupleOrDeclarationExpression(tupleType, declarationStatement.Declaration.Type),
variableDeclarator.Initializer.EqualsToken,
variableDeclarator.Initializer.Value),
declarationStatement.SemicolonToken);
}

private ExpressionSyntax CreateTupleOrDeclarationExpression(INamedTypeSymbol tupleType, TypeSyntax typeNode)
=> typeNode.IsKind(SyntaxKind.TupleType)
{
// If we have an explicit tuple type in code, convert that over to a tuple expression.
// i.e. (int x, int y) t = ... will be converted to (int x, int y) = ...
//
// If we had the "var t" form we'll conver that to the declaration expression "var (x, y)"
return typeNode.IsKind(SyntaxKind.TupleType)
? (ExpressionSyntax)CreateTupleExpression((TupleTypeSyntax)typeNode)
: CreateDeclarationExpression(tupleType, typeNode);
}

private DeclarationExpressionSyntax CreateDeclarationExpression(INamedTypeSymbol tupleType, TypeSyntax typeNode)
=> SyntaxFactory.DeclarationExpression(
Expand All @@ -145,9 +161,12 @@ private SyntaxNodeOrToken ConvertTupleTypeElementComponent(SyntaxNodeOrToken nod
{
if (nodeOrToken.IsToken)
{
// return commas directly as is.
return nodeOrToken;
}

// "int x" as a tuple element directly translates to "int x" (a declaration expression
// with a variable designation 'x').
var node = (TupleElementSyntax)nodeOrToken.AsNode();
return SyntaxFactory.Argument(
SyntaxFactory.DeclarationExpression(
Expand Down

0 comments on commit 05ab33a

Please sign in to comment.