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

Commit 776d50c

Browse files
committed
Added some tests which cover a few end to end sceanrios
1 parent 7b3352d commit 776d50c

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<ItemGroup>
107107
<Compile Include="CodeFormattingTestBase.cs" />
108108
<Compile Include="Rules\BracesRuleTests.cs" />
109+
<Compile Include="Rules\CombinationTest.cs" />
109110
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
110111
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
111112
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis;
8+
using Xunit;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Tests
11+
{
12+
/// <summary>
13+
/// A test which runs all rules on a given piece of code
14+
/// </summary>
15+
public sealed class CombinationTest : CodeFormattingTestBase
16+
{
17+
private static FormattingEngineImplementation s_formattingEngine;
18+
19+
static CombinationTest()
20+
{
21+
s_formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create(Enumerable.Empty<string>(), Enumerable.Empty<string>());
22+
}
23+
24+
protected override async Task<Document> RewriteDocumentAsync(Document document)
25+
{
26+
var solution = await s_formattingEngine.FormatCoreAsync(
27+
document.Project.Solution,
28+
new[] { document.Id },
29+
CancellationToken.None);
30+
return solution.GetDocument(document.Id);
31+
}
32+
33+
[Fact]
34+
public void FieldUse()
35+
{
36+
var text = @"
37+
class C {
38+
int field;
39+
40+
void M() {
41+
N(this.field);
42+
}
43+
}";
44+
45+
var expected = @"
46+
internal class C
47+
{
48+
private int _field;
49+
50+
private void M()
51+
{
52+
N(_field);
53+
}
54+
}";
55+
56+
Verify(text, expected, runFormatter: false);
57+
}
58+
59+
[Fact]
60+
public void FieldAssignment()
61+
{
62+
var text = @"
63+
class C {
64+
int field;
65+
66+
void M() {
67+
this.field = 42;
68+
}
69+
}";
70+
71+
var expected = @"
72+
internal class C
73+
{
74+
private int _field;
75+
76+
private void M()
77+
{
78+
_field = 42;
79+
}
80+
}";
81+
82+
Verify(text, expected, runFormatter: false);
83+
}
84+
}
85+
}

src/Microsoft.DotNet.CodeFormatting/Filters/IgnoreDesignerGeneratedCodeFilter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ internal sealed class IgnoreDesignerGeneratedCodeFilter : IFormattingFilter
1414
{
1515
public Task<bool> ShouldBeProcessedAsync(Document document)
1616
{
17+
if (document.FilePath == null)
18+
{
19+
return Task.FromResult(true);
20+
}
21+
1722
var isDesignerGenerated = document.FilePath.EndsWith(".Designer.cs", StringComparison.OrdinalIgnoreCase);
1823
return Task.FromResult(!isDesignerGenerated);
1924
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,21 @@ private async Task FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> do
6363
watch.Start();
6464

6565
var originalSolution = workspace.CurrentSolution;
66+
var solution = await FormatCoreAsync(originalSolution, documentIds, cancellationToken);
67+
68+
watch.Stop();
69+
70+
await SaveChanges(solution, originalSolution, cancellationToken);
71+
Console.WriteLine("Total time {0}", watch.Elapsed);
72+
}
73+
74+
internal async Task<Solution> FormatCoreAsync(Solution originalSolution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
75+
{
6676
var solution = originalSolution;
6777
solution = await RunSyntaxPass(solution, documentIds, cancellationToken);
6878
solution = await RunLocalSemanticPass(solution, documentIds, cancellationToken);
6979
solution = await RunGlobalSemanticPass(solution, documentIds, cancellationToken);
70-
71-
await SaveChanges(solution, originalSolution, cancellationToken);
72-
73-
watch.Stop();
74-
Console.WriteLine("Total time {0}", watch.Elapsed);
80+
return solution;
7581
}
7682

7783
private async Task SaveChanges(Solution solution, Solution originalSolution, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)