From 027f79e06f9bf65105d7f187b0806d9dd32b60cb Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Fri, 8 Nov 2024 11:40:18 +0800 Subject: [PATCH 1/5] Implement TupleExpression --- .../src/Expressions/TupleExpression.cs | 22 ++++++++++++++++++ .../test/Expressions/TupleExpressionTests.cs | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/TupleExpression.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs 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/test/Expressions/TupleExpressionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs new file mode 100644 index 0000000000..92efa950b2 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Expressions/TupleExpressionTests.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Threading; +using Microsoft.Generator.CSharp.Expressions; +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)); + } + } +} From 1c2a0230feb791eaaf0baed63385dc5b220b158c Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Fri, 8 Nov 2024 17:00:52 +0800 Subject: [PATCH 2/5] Add VariableTupleExpression --- .../Expressions/VariableTupleExpression.cs | 24 +++++++++++++++++++ .../test/Expressions/TupleExpressionTests.cs | 13 ++++++++++ 2 files changed, 37 insertions(+) create mode 100644 packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/VariableTupleExpression.cs 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 index 92efa950b2..244a8f9943 100644 --- 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 @@ -3,6 +3,8 @@ 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; @@ -19,5 +21,16 @@ public void VerifyTupleExpressionWrite() 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(int))); + var variableTupleExpression = new VariableTupleExpression(false, item1, item2); + using CodeWriter writer = new CodeWriter(); + variableTupleExpression.Assign(new TupleExpression(Literal(1), Literal(2))).Write(writer); + Assert.AreEqual("(int item1, int item2) = (1, 2)", writer.ToString(false)); + } } } From 35116f6ae897108e453b3a4d26fe6172636e4231 Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Fri, 8 Nov 2024 17:04:33 +0800 Subject: [PATCH 3/5] update test --- .../test/Expressions/TupleExpressionTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 244a8f9943..742e6fa022 100644 --- 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 @@ -26,11 +26,11 @@ public void VerifyTupleExpressionWrite() public void VerifyTUpleExpressionAssignment() { var item1 = new ParameterProvider("item1", FormattableStringHelpers.Empty, new CSharpType(typeof(int))); - var item2 = new ParameterProvider("item2", 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(2))).Write(writer); - Assert.AreEqual("(int item1, int item2) = (1, 2)", writer.ToString(false)); + variableTupleExpression.Assign(new TupleExpression(Literal(1), Literal("a"))).Write(writer); + Assert.AreEqual("(int item1, string item2) = (1, \"a\")", writer.ToString(false)); } } } From 859ede99e25ed4b1fe0ae05f18367fb3d0ca0f3f Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Fri, 8 Nov 2024 17:09:38 +0800 Subject: [PATCH 4/5] typo --- .../test/Expressions/TupleExpressionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 742e6fa022..74777f490d 100644 --- 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 @@ -23,7 +23,7 @@ public void VerifyTupleExpressionWrite() } [Test] - public void VerifyTUpleExpressionAssignment() + 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))); From 71613162f0fa7a3b3cf42517525988dadb483fea Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Sat, 9 Nov 2024 10:01:51 +0800 Subject: [PATCH 5/5] Add test --- .../test/Expressions/TupleExpressionTests.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 index 74777f490d..f5fc7f775d 100644 --- 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 @@ -32,5 +32,16 @@ public void VerifyTupleExpressionAssignment() 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)); + } } }