-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #5019 Implement TupleExpression and VariableTupleExpression
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...ttp-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/TupleExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.Generator.CSharp.Expressions | ||
{ | ||
public record TupleExpression(params ValueExpression[] ValueExpressions) : ValueExpression | ||
{ | ||
internal override void Write(CodeWriter writer) | ||
{ | ||
writer.AppendRaw("("); | ||
for (int i = 0; i < ValueExpressions.Length; i++) | ||
{ | ||
ValueExpressions[i].Write(writer); | ||
if (i < ValueExpressions.Length - 1) | ||
{ | ||
writer.AppendRaw(", "); | ||
} | ||
} | ||
writer.AppendRaw(")"); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...nt-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/VariableTupleExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.Generator.CSharp.Expressions | ||
{ | ||
public record VariableTupleExpression(bool IsRef = false, params VariableExpression[] Variables) : ValueExpression | ||
{ | ||
internal override void Write(CodeWriter writer) | ||
{ | ||
writer.AppendRawIf("ref ", IsRef); | ||
writer.AppendRaw("("); | ||
for (int i = 0; i < Variables.Length; i++) | ||
{ | ||
var variable = Variables[i]; | ||
writer.Append($"{variable.Type} {variable.Declaration}"); | ||
if (i < Variables.Length - 1) | ||
{ | ||
writer.AppendRaw(", "); | ||
} | ||
} | ||
writer.AppendRaw(")"); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ient-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using NUnit.Framework; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Microsoft.Generator.CSharp.Tests.Expressions | ||
{ | ||
internal class TupleExpressionTests | ||
{ | ||
[Test] | ||
public void VerifyTupleExpressionWrite() | ||
{ | ||
var tupleExpression = new TupleExpression(Null, Static<CancellationToken>().Property("None")); | ||
using CodeWriter writer = new CodeWriter(); | ||
tupleExpression.Write(writer); | ||
|
||
Assert.AreEqual("(null, global::System.Threading.CancellationToken.None)", writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void VerifyTupleExpressionAssignment() | ||
{ | ||
var item1 = new ParameterProvider("item1", FormattableStringHelpers.Empty, new CSharpType(typeof(int))); | ||
var item2 = new ParameterProvider("item2", FormattableStringHelpers.Empty, new CSharpType(typeof(string))); | ||
var variableTupleExpression = new VariableTupleExpression(false, item1, item2); | ||
using CodeWriter writer = new CodeWriter(); | ||
variableTupleExpression.Assign(new TupleExpression(Literal(1), Literal("a"))).Write(writer); | ||
Assert.AreEqual("(int item1, string item2) = (1, \"a\")", writer.ToString(false)); | ||
} | ||
|
||
[Test] | ||
public void VerifyTupleExpressionWithRef() | ||
{ | ||
var item1 = new ParameterProvider("item1", FormattableStringHelpers.Empty, new CSharpType(typeof(int))); | ||
var item2 = new ParameterProvider("item2", FormattableStringHelpers.Empty, new CSharpType(typeof(string))); | ||
var tupleVariableExpression = new VariableTupleExpression(true, item1, item2); | ||
using CodeWriter writer = new CodeWriter(); | ||
tupleVariableExpression.Write(writer); | ||
Assert.AreEqual("ref (int item1, string item2)", writer.ToString(false)); | ||
} | ||
} | ||
} |