Skip to content

Commit

Permalink
Implement TupleExpression (#5021)
Browse files Browse the repository at this point in the history
Resolves #5019

Implement TupleExpression and VariableTupleExpression
  • Loading branch information
live1206 authored Nov 11, 2024
1 parent a2b314e commit c7dc05a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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(")");
}
}
}
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(")");
}
}
}
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));
}
}
}

0 comments on commit c7dc05a

Please sign in to comment.