Fix Indentation in several statements (#3622)
Fixes the indentation in several of the statements and also fixes writing arguments in the code writer.
This commit is contained in:
Родитель
4830f07c12
Коммит
aed8ebc215
|
@ -15,10 +15,10 @@ namespace Microsoft.Generator.CSharp.Statements
|
|||
If = ifStatement;
|
||||
Else = elseStatement;
|
||||
}
|
||||
|
||||
internal override void Write(CodeWriter writer)
|
||||
{
|
||||
writer.WriteLine($"#if {Condition}");
|
||||
writer.AppendRaw("\t\t\t\t");
|
||||
If.Write(writer);
|
||||
if (Else is not null)
|
||||
{
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace Microsoft.Generator.CSharp.Statements
|
|||
writer.AppendRaw("default");
|
||||
}
|
||||
|
||||
using var scope = writer.ScopeRaw(":", "", false);
|
||||
using var scope = writer.ScopeRaw(":", "", newLine: false);
|
||||
Statement.Write(writer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,12 +51,13 @@ namespace Microsoft.Generator.CSharp.Statements
|
|||
Value.Write(writer);
|
||||
writer.WriteRawLine(")");
|
||||
|
||||
writer.WriteRawLine("{");
|
||||
foreach (var bodyStatement in Body)
|
||||
using (writer.ScopeRaw())
|
||||
{
|
||||
bodyStatement.Write(writer);
|
||||
foreach (var bodyStatement in Body)
|
||||
{
|
||||
bodyStatement.Write(writer);
|
||||
}
|
||||
}
|
||||
writer.WriteRawLine("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -619,7 +619,7 @@ namespace Microsoft.Generator.CSharp
|
|||
if (span.Length == 0 )
|
||||
return this;
|
||||
|
||||
AddSpaces();
|
||||
AddSpaces(span);
|
||||
|
||||
var destination = _builder.GetSpan(span.Length);
|
||||
span.CopyTo(destination);
|
||||
|
@ -629,8 +629,14 @@ namespace Microsoft.Generator.CSharp
|
|||
return this;
|
||||
}
|
||||
|
||||
private void AddSpaces()
|
||||
private void AddSpaces(ReadOnlySpan<char> span)
|
||||
{
|
||||
// pre-processor directives do not need indentation
|
||||
if (span[0] == '#')
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int spaces = _atBeginningOfLine ? (_scopes.Peek().Depth) * 4 : 0;
|
||||
if (spaces == 0)
|
||||
return;
|
||||
|
@ -936,19 +942,18 @@ namespace Microsoft.Generator.CSharp
|
|||
}
|
||||
else
|
||||
{
|
||||
WriteRawLine("(");
|
||||
AppendRaw("(");
|
||||
var iterator = arguments.GetEnumerator();
|
||||
if (iterator.MoveNext())
|
||||
{
|
||||
AppendRaw("\t");
|
||||
iterator.Current.Write(this);
|
||||
WriteRawLine(",");
|
||||
while (iterator.MoveNext())
|
||||
using (ScopeRaw(string.Empty, string.Empty, false))
|
||||
{
|
||||
AppendRaw(", ");
|
||||
AppendRaw("\t");
|
||||
iterator.Current.Write(this);
|
||||
WriteRawLine(",");
|
||||
while (iterator.MoveNext())
|
||||
{
|
||||
WriteRawLine(",");
|
||||
iterator.Current.Write(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
AppendRaw(")");
|
||||
|
|
|
@ -783,5 +783,28 @@ namespace Microsoft.Generator.CSharp.Tests
|
|||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWriteArguments()
|
||||
{
|
||||
var arg1 = Literal("arg1");
|
||||
var arg2 = Literal("arg2");
|
||||
var arg3 = Literal("arg3");
|
||||
|
||||
using var codeWriter = new CodeWriter();
|
||||
codeWriter.WriteArguments(new List<ValueExpression> { arg1, arg2, arg3 }, false);
|
||||
|
||||
var expected = new StringBuilder()
|
||||
.Append("(")
|
||||
.Append(NewLine)
|
||||
.Append(" \"arg1\",")
|
||||
.Append(NewLine)
|
||||
.Append(" \"arg2\",")
|
||||
.Append(NewLine)
|
||||
.Append(" \"arg3\")")
|
||||
.ToString();
|
||||
var result = codeWriter.ToString(false);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.Generator.CSharp.Expressions;
|
||||
using Microsoft.Generator.CSharp.Providers;
|
||||
using Microsoft.Generator.CSharp.Snippets;
|
||||
using Microsoft.Generator.CSharp.Statements;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using static Microsoft.Generator.CSharp.Snippets.Snippet;
|
||||
|
||||
namespace Microsoft.Generator.CSharp.Tests.Expressions
|
||||
{
|
||||
internal class ExpressionsTest
|
||||
{
|
||||
private readonly string _mocksFolder = "Mocks";
|
||||
private string _testDataPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "Expressions");
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Setup()
|
||||
{
|
||||
Mock<ApiTypes> apiTypes = new Mock<ApiTypes>();
|
||||
Mock<ExtensibleSnippets> extensibleSnippets = new Mock<ExtensibleSnippets>();
|
||||
apiTypes.SetupGet(x => x.ResponseParameterName).Returns("result");
|
||||
|
||||
string outputFolder = "./outputFolder";
|
||||
string projectPath = outputFolder;
|
||||
|
||||
var configFilePath = Path.Combine(AppContext.BaseDirectory, _mocksFolder);
|
||||
// initialize the singleton instance of the plugin
|
||||
_ = new MockCodeModelPlugin(new GeneratorContext(Configuration.Load(configFilePath)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestInvokeInstanceMethodExpression()
|
||||
{
|
||||
// declare the instance method
|
||||
var mockTypeProvider = new Mock<TypeProvider>();
|
||||
var barMethod = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Bar",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: typeof(bool),
|
||||
Parameters: [
|
||||
new ParameterProvider("p1", $"p1", new CSharpType(typeof(bool))),
|
||||
new ParameterProvider("p2", $"p2", new CSharpType(typeof(bool))),
|
||||
new ParameterProvider("p3", $"p3", new CSharpType(typeof(bool)))
|
||||
],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { Return(True) },
|
||||
mockTypeProvider.Object);
|
||||
var returnInstanceMethod = Return(new InvokeInstanceMethodExpression(null, barMethod.Signature.Name, [Bool(true), Bool(false), Bool(false)]));
|
||||
var fooMethod = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Foo",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: typeof(bool),
|
||||
Parameters: [],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { returnInstanceMethod },
|
||||
mockTypeProvider.Object);
|
||||
|
||||
// Verify the expected behavior
|
||||
using var writer = new CodeWriter();
|
||||
writer.WriteMethod(barMethod);
|
||||
writer.WriteMethod(fooMethod);
|
||||
|
||||
var expectedResult = File.ReadAllText(Path.Combine(_testDataPath, "InvokeInstanceMethodExpression.cs"));
|
||||
var test = writer.ToString(false);
|
||||
Assert.AreEqual(expectedResult, test);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNewInstanceExpression()
|
||||
{
|
||||
// declare the instance method
|
||||
var mockTypeProvider = new Mock<TypeProvider>();
|
||||
var newInstanceExpression = new NewInstanceExpression(new CSharpType(typeof(object)), []);
|
||||
var variableX = new VariableReferenceSnippet(typeof(object), "x");
|
||||
var xDeclaration = Declare(variableX, newInstanceExpression);
|
||||
var fooMethod = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Foo",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: null,
|
||||
Parameters: [],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { xDeclaration },
|
||||
mockTypeProvider.Object);
|
||||
|
||||
// Verify the expected behavior
|
||||
using var writer = new CodeWriter();
|
||||
writer.WriteMethod(fooMethod);
|
||||
|
||||
var expectedResult = File.ReadAllText(Path.Combine(_testDataPath, "NewInstanceExpression.cs"));
|
||||
var test = writer.ToString(false);
|
||||
Assert.AreEqual(expectedResult, test);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,4 +11,11 @@
|
|||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="TestData\**\*.cs" />
|
||||
<None Include="TestData\**\*.cs">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -3,16 +3,37 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Generator.CSharp.Expressions;
|
||||
using Microsoft.Generator.CSharp.Providers;
|
||||
using Microsoft.Generator.CSharp.Snippets;
|
||||
using Microsoft.Generator.CSharp.Statements;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using static Microsoft.Generator.CSharp.Snippets.Snippet;
|
||||
|
||||
namespace Microsoft.Generator.CSharp.Tests.Statements
|
||||
{
|
||||
public class StatementTests
|
||||
{
|
||||
private readonly string _mocksFolder = "Mocks";
|
||||
private string _testDataPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "Statements");
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Setup()
|
||||
{
|
||||
Mock<ApiTypes> apiTypes = new Mock<ApiTypes>();
|
||||
Mock<ExtensibleSnippets> extensibleSnippets = new Mock<ExtensibleSnippets>();
|
||||
apiTypes.SetupGet(x => x.ResponseParameterName).Returns("result");
|
||||
|
||||
string outputFolder = "./outputFolder";
|
||||
string projectPath = outputFolder;
|
||||
var configFilePath = Path.Combine(AppContext.BaseDirectory, _mocksFolder);
|
||||
// initialize the singleton instance of the plugin
|
||||
_ = new MockCodeModelPlugin(new GeneratorContext(Configuration.Load(configFilePath)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AssignValueIfNullStatement()
|
||||
{
|
||||
|
@ -241,6 +262,91 @@ namespace Microsoft.Generator.CSharp.Tests.Statements
|
|||
CollectionAssert.AreEqual(caseStatements, enumeratedCases);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSwitchStatementWithMultipleCasesWrite()
|
||||
{
|
||||
var variableFoo = new VariableReferenceSnippet(typeof(bool), "foo");
|
||||
var fooDeclaration = Declare(variableFoo, Bool(true));
|
||||
var switchStatement = new SwitchStatement(variableFoo);
|
||||
|
||||
var caseStatements = new List<SwitchCaseStatement>
|
||||
{
|
||||
new SwitchCaseStatement(Bool(true), Return(variableFoo)),
|
||||
SwitchCaseStatement.Default(Return(False))
|
||||
};
|
||||
|
||||
foreach (var switchCase in caseStatements)
|
||||
{
|
||||
switchStatement.Add(switchCase);
|
||||
}
|
||||
|
||||
var mockTypeProvider = new Mock<TypeProvider>();
|
||||
|
||||
// Create a method declaration statement
|
||||
var method = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Foo",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: new CSharpType(typeof(bool)),
|
||||
Parameters: [],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { fooDeclaration, switchStatement },
|
||||
mockTypeProvider.Object);
|
||||
|
||||
// Verify the expected behavior
|
||||
using var writer = new CodeWriter();
|
||||
writer.WriteMethod(method);
|
||||
|
||||
var expectedResult = File.ReadAllText(Path.Combine(_testDataPath, "SwitchCaseStatement.cs"));
|
||||
var test = writer.ToString(false);
|
||||
Assert.AreEqual(expectedResult, test);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSwitchStatementWithUsingStatementWrite()
|
||||
{
|
||||
var variableFoo = new VariableReferenceSnippet(typeof(bool), "foo");
|
||||
var fooDeclaration = Declare(variableFoo, Bool(true));
|
||||
var switchStatement = new SwitchStatement(variableFoo);
|
||||
var usingStatement = new UsingScopeStatement(null, new CodeWriterDeclaration("x"), New.Instance(typeof(MemoryStream)))
|
||||
{
|
||||
Return(variableFoo)
|
||||
};
|
||||
|
||||
var caseStatements = new List<SwitchCaseStatement>
|
||||
{
|
||||
new SwitchCaseStatement(Bool(true), usingStatement),
|
||||
SwitchCaseStatement.Default(Return(False))
|
||||
};
|
||||
|
||||
foreach (var switchCase in caseStatements)
|
||||
{
|
||||
switchStatement.Add(switchCase);
|
||||
}
|
||||
|
||||
var mockTypeProvider = new Mock<TypeProvider>();
|
||||
|
||||
// Create a method declaration statement
|
||||
var method = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Foo",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: new CSharpType(typeof(bool)),
|
||||
Parameters: [],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { fooDeclaration, switchStatement },
|
||||
mockTypeProvider.Object);
|
||||
|
||||
// Verify the expected behavior
|
||||
using var writer = new CodeWriter();
|
||||
writer.WriteMethod(method);
|
||||
|
||||
var expectedResult = File.ReadAllText(Path.Combine(_testDataPath, "SwitchCaseStatementWithUsing.cs"));
|
||||
var test = writer.ToString(false);
|
||||
Assert.AreEqual(expectedResult, test);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void TryCatchFinallyStatementWithTryOnly()
|
||||
{
|
||||
|
@ -304,5 +410,39 @@ namespace Microsoft.Generator.CSharp.Tests.Statements
|
|||
|
||||
Assert.AreEqual(operatorExpression, unaryOperatorStatement.Expression);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestIfElsePreprocessorStatement()
|
||||
{
|
||||
// Set up test conditions and variables
|
||||
var condition = "MOCKCONDITION";
|
||||
var variableX = new VariableReferenceSnippet(typeof(int), "x");
|
||||
var variableFoo = new VariableReferenceSnippet(typeof(int), "foo");
|
||||
var variableBar = new VariableReferenceSnippet(typeof(int), "bar");
|
||||
var xDeclaration = Declare(variableX, Int(1));
|
||||
var ifStatementBody = Declare(variableFoo, Int(2));
|
||||
var elseStatementBody = Declare(variableBar, Int(2));
|
||||
var ifElsePreprocessor = new IfElsePreprocessorStatement(condition, ifStatementBody, elseStatementBody);
|
||||
var mockTypeProvider = new Mock<TypeProvider>();
|
||||
|
||||
// Create a method declaration statement
|
||||
var method = new MethodProvider(
|
||||
new MethodSignature(
|
||||
Name: "Foo",
|
||||
Modifiers: MethodSignatureModifiers.Public,
|
||||
ReturnType: null,
|
||||
Parameters: [],
|
||||
Summary: null, Description: null, ReturnDescription: null),
|
||||
new MethodBodyStatement[] { xDeclaration, ifElsePreprocessor },
|
||||
mockTypeProvider.Object);
|
||||
|
||||
// Verify the expected behavior
|
||||
using var writer = new CodeWriter();
|
||||
writer.WriteMethod(method);
|
||||
|
||||
var expectedResult = File.ReadAllText(Path.Combine(_testDataPath, "IfElsePreprocessorStatement.cs"));
|
||||
var test = writer.ToString(false);
|
||||
Assert.AreEqual(expectedResult, test);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
/// <param name="p1"> p1. </param>
|
||||
/// <param name="p2"> p2. </param>
|
||||
/// <param name="p3"> p3. </param>
|
||||
public bool Bar(bool p1, bool p2, bool p3)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
public bool Foo()
|
||||
{
|
||||
return Bar(true, false, false);
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
public void Foo()
|
||||
{
|
||||
object x = new object();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
public void Foo()
|
||||
{
|
||||
int x = 1;
|
||||
#if MOCKCONDITION
|
||||
int foo = 2;
|
||||
#else
|
||||
int bar = 2;
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
public bool Foo()
|
||||
{
|
||||
bool foo = true;
|
||||
switch (foo)
|
||||
{
|
||||
case true:
|
||||
return foo;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
public bool Foo()
|
||||
{
|
||||
bool foo = true;
|
||||
switch (foo)
|
||||
{
|
||||
case true:
|
||||
using (var x = new global::System.IO.MemoryStream())
|
||||
{
|
||||
return foo;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче