Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit bf5d9ed

Browse files
author
Lakshmi Priya Sekar
committed
Add minor fixes
1. Adding more tests 2. Fixing code review comments
1 parent e844f8e commit bf5d9ed

31 files changed

+537
-130
lines changed

src/CodeFormatter/Program.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.ComponentModel.Composition.Hosting;
36
using System.IO;
4-
using System.Linq;
57
using System.Threading;
68
using System.Threading.Tasks;
9+
710
using Microsoft.CodeAnalysis.MSBuild;
811
using Microsoft.DotNet.CodeFormatting;
912

@@ -13,9 +16,9 @@ internal static class Program
1316
{
1417
private static int Main(string[] args)
1518
{
16-
if (args.Length < 1)
19+
if (args.Length != 1)
1720
{
18-
Console.Error.WriteLine("CodeFormatter <solution> [<rule types>]");
21+
Console.Error.WriteLine("CodeFormatter <solution>");
1922
return -1;
2023
}
2124

@@ -26,33 +29,23 @@ private static int Main(string[] args)
2629
return -1;
2730
}
2831

29-
var ruleTypes = args.Skip(1);
30-
3132
var cts = new CancellationTokenSource();
3233
var ct = cts.Token;
3334

3435
Console.CancelKeyPress += delegate { cts.Cancel(); };
3536

36-
RunAsync(solutionPath, ruleTypes, ct).Wait(ct);
37+
RunAsync(solutionPath, ct).Wait(ct);
3738
Console.WriteLine("Completed formatting.");
3839
return 0;
3940
}
4041

41-
private static async Task RunAsync(string solutionFilePath, IEnumerable<string> ruleTypes, CancellationToken cancellationToken)
42+
private static async Task RunAsync(string solutionFilePath, CancellationToken cancellationToken)
4243
{
43-
try
44-
{
45-
var workspace = MSBuildWorkspace.Create();
46-
await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken);
44+
var workspace = MSBuildWorkspace.Create();
45+
await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken);
4746

48-
var engine = FormattingEngine.Create(ruleTypes);
49-
await engine.RunAsync(workspace, cancellationToken);
50-
}
51-
catch (Exception ex)
52-
{
53-
Console.WriteLine(ex.ToString());
54-
throw;
55-
}
47+
var engine = FormattingEngine.Create();
48+
await engine.RunAsync(workspace, cancellationToken);
5649
}
5750
}
5851
}

src/CodeFormatter/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Reflection;
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Reflection;
25
using System.Runtime.CompilerServices;
36
using System.Runtime.InteropServices;
47

src/Microsoft.DotNet.CodeFormatting.Tests/CodeFormattingTestBase.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System;
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
25
using System.Collections.Generic;
36
using System.Linq;
47
using System.Text;
@@ -13,9 +16,9 @@ namespace Microsoft.DotNet.CodeFormatting.Tests
1316
{
1417
public abstract class CodeFormattingTestBase
1518
{
16-
private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
17-
private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly);
18-
private static readonly MetadataReference CodeFormatterReference = MetadataReference.CreateFromAssembly(typeof(IFormattingEngine).Assembly);
19+
private static readonly MetadataReference s_CorlibReference = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
20+
private static readonly MetadataReference s_SystemCoreReference = MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly);
21+
private static readonly MetadataReference s_CodeFormatterReference = MetadataReference.CreateFromAssembly(typeof(IFormattingEngine).Assembly);
1922

2023
private const string FileNamePrefix = "Test";
2124
private const string CSharpFileExtension = ".cs";
@@ -37,12 +40,12 @@ private static Solution CreateSolution(string[] sources, string language = Langu
3740
var solution = new CustomWorkspace()
3841
.CurrentSolution
3942
.AddProject(projectId, TestProjectName, TestProjectName, language)
40-
.AddMetadataReference(projectId, CorlibReference)
41-
.AddMetadataReference(projectId, SystemCoreReference)
42-
.AddMetadataReference(projectId, CodeFormatterReference);
43+
.AddMetadataReference(projectId, s_CorlibReference)
44+
.AddMetadataReference(projectId, s_SystemCoreReference)
45+
.AddMetadataReference(projectId, s_CodeFormatterReference);
4346

4447
int count = 0;
45-
foreach(var source in sources)
48+
foreach (var source in sources)
4649
{
4750
var fileName = FileNamePrefix + count + fileExtension;
4851
var documentId = DocumentId.CreateNewId(projectId, fileName);
@@ -52,24 +55,26 @@ private static Solution CreateSolution(string[] sources, string language = Langu
5255
return solution;
5356
}
5457

55-
private static async Task<Solution> Format(Solution solution, IFormattingRule rule)
58+
private static async Task<Solution> Format(Solution solution, IFormattingRule rule, bool runFormatter)
5659
{
5760
var documentIds = solution.Projects.SelectMany(p => p.DocumentIds);
5861

5962
foreach (var id in documentIds)
6063
{
6164
var document = solution.GetDocument(id);
62-
var newDocument = await RewriteDocumentAsync(document, rule);
65+
var newDocument = await RewriteDocumentAsync(document, rule, runFormatter);
6366
solution = newDocument.Project.Solution;
6467
}
6568

6669
return solution;
6770
}
6871

69-
private static async Task<Document> RewriteDocumentAsync(Document document, IFormattingRule rule)
72+
private static async Task<Document> RewriteDocumentAsync(Document document, IFormattingRule rule, bool runFormatter)
7073
{
7174
document = await rule.ProcessAsync(document, CancellationToken.None);
72-
return await GetDefaultVSFormatter().ProcessAsync(document, CancellationToken.None);
75+
if (runFormatter)
76+
return await GetDefaultVSFormatter().ProcessAsync(document, CancellationToken.None);
77+
return document;
7378
}
7479

7580
private static void AssertSolutionEqual(Solution expectedSolution, Solution actualSolution)
@@ -89,26 +94,26 @@ private static void AssertSolutionEqual(Solution expectedSolution, Solution actu
8994
}
9095
}
9196

92-
private static void Verify(string[] sources, string[] expected, IFormattingRule rule)
97+
private static void Verify(string[] sources, string[] expected, IFormattingRule rule, bool runFormatter)
9398
{
9499
var inputSolution = CreateSolution(sources);
95100
var expectedSolution = CreateSolution(expected);
96-
var actualSolution = Format(inputSolution, rule).Result;
101+
var actualSolution = Format(inputSolution, rule, runFormatter).Result;
97102

98103
if (actualSolution == null)
99104
Assert.False(true, "Solution is null. Test Failed.");
100105

101106
AssertSolutionEqual(expectedSolution, actualSolution);
102107
}
103108

104-
protected void Verify(string[] source, string[] expected)
109+
protected void Verify(string[] source, string[] expected, bool runFormatter)
105110
{
106-
Verify(source, expected, GetFormattingRule());
111+
Verify(source, expected, GetFormattingRule(), runFormatter);
107112
}
108113

109-
protected void Verify(string source, string expected)
114+
protected void Verify(string source, string expected, bool runFormatter = true)
110115
{
111-
Verify(new string[] { source }, new string[] { expected });
116+
Verify(new string[] { source }, new string[] { expected }, runFormatter);
112117
}
113118
}
114119
}

src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
</ItemGroup>
103103
<ItemGroup>
104104
<Compile Include="CodeFormattingTestBase.cs" />
105+
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
106+
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
107+
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRuleTests.cs" />
108+
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
105109
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRuleTests.cs" />
106110
</ItemGroup>
107111
<ItemGroup>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.DotNet.CodeFormatting.Tests
7+
{
8+
public class HasNewLineBeforeFirstNamespaceFormattingRuleTests : CodeFormattingTestBase
9+
{
10+
[Fact]
11+
public void TestNewLineBeforeFirstNamespace01()
12+
{
13+
var text = @"
14+
using System;
15+
//some comment
16+
#if true
17+
namespace N { }
18+
#endif
19+
";
20+
var expected = @"
21+
using System;
22+
//some comment
23+
24+
#if true
25+
namespace N { }
26+
#endif
27+
";
28+
Verify(text, expected);
29+
}
30+
31+
[Fact]
32+
public void TestNewLineBeforeFirstNamespace02()
33+
{
34+
var text = @"
35+
using System;
36+
/*some comment1
37+
some comment2
38+
*/
39+
namespace N { }
40+
";
41+
var expected = @"
42+
using System;
43+
/*some comment1
44+
some comment2
45+
*/
46+
47+
namespace N { }
48+
";
49+
Verify(text, expected);
50+
}
51+
52+
[Fact]
53+
public void TestNewLineBeforeFirstNamespace03()
54+
{
55+
var text = @"
56+
using System;
57+
#pragma warning disable 1591
58+
namespace N { }
59+
";
60+
var expected = @"
61+
using System;
62+
63+
#pragma warning disable 1591
64+
namespace N { }
65+
";
66+
Verify(text, expected);
67+
}
68+
69+
[Fact]
70+
public void TestNewLineBeforeFirstNamespace04()
71+
{
72+
var text = @"
73+
// some comment
74+
#pragma warning disable 1561
75+
#if true
76+
#endif
77+
namespace N { }
78+
";
79+
var expected = @"
80+
// some comment
81+
82+
#pragma warning disable 1561
83+
#if true
84+
#endif
85+
namespace N { }
86+
";
87+
Verify(text, expected);
88+
}
89+
90+
internal override IFormattingRule GetFormattingRule()
91+
{
92+
return new Rules.HasNewLineBeforeFirstNamespaceFormattingRule();
93+
}
94+
}
95+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.DotNet.CodeFormatting.Tests
7+
{
8+
public class HasNewLineBeforeFirstUsingFormattingRuleTests : CodeFormattingTestBase
9+
{
10+
[Fact]
11+
public void TestNewLineBeforeFirstUsing01()
12+
{
13+
var text = @"
14+
//some comment
15+
using System;
16+
";
17+
var expected = @"
18+
//some comment
19+
20+
using System;
21+
";
22+
Verify(text, expected);
23+
}
24+
25+
[Fact]
26+
public void TestNewLineBeforeFirstUsing02()
27+
{
28+
var text = @"
29+
/*some comment1
30+
some comment2
31+
*/
32+
using System;
33+
";
34+
var expected = @"
35+
/*some comment1
36+
some comment2
37+
*/
38+
39+
using System;
40+
";
41+
Verify(text, expected);
42+
}
43+
44+
[Fact]
45+
public void TestNewLineBeforeFirstUsing03()
46+
{
47+
var text = @"
48+
// copyright comment
49+
#pragma warning disable 1591
50+
using System;
51+
";
52+
var expected = @"
53+
// copyright comment
54+
55+
#pragma warning disable 1591
56+
using System;
57+
";
58+
Verify(text, expected);
59+
}
60+
61+
[Fact]
62+
public void TestNewLineBeforeFirstUsing04()
63+
{
64+
var text = @"
65+
// some comment
66+
#pragma warning disable 1561
67+
#if true
68+
using System;
69+
#endif
70+
";
71+
var expected = @"
72+
// some comment
73+
74+
#pragma warning disable 1561
75+
#if true
76+
using System;
77+
#endif
78+
";
79+
Verify(text, expected);
80+
}
81+
82+
[Fact]
83+
public void TestNewLineBeforeFirstUsing05()
84+
{
85+
var text = @"
86+
// some comment
87+
#if true
88+
#endif
89+
using System;
90+
";
91+
var expected = @"
92+
// some comment
93+
94+
#if true
95+
#endif
96+
using System;
97+
";
98+
Verify(text, expected);
99+
}
100+
101+
internal override IFormattingRule GetFormattingRule()
102+
{
103+
return new Rules.HasNewLineBeforeFirstUsingFormattingRule();
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)