diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/TupleExpression.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/TupleExpression.cs new file mode 100644 index 0000000000..006ebc62fa --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/TupleExpression.cs @@ -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(")"); + } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/VariableTupleExpression.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/VariableTupleExpression.cs new file mode 100644 index 0000000000..e9ec416134 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/VariableTupleExpression.cs @@ -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(")"); + } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs new file mode 100644 index 0000000000..f5fc7f775d --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs @@ -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().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)); + } + } +}