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

Commit ccb7dbf

Browse files
committed
Unit tests are passing again
1 parent e222939 commit ccb7dbf

11 files changed

+113
-80
lines changed

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

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.Formatting;
1112
using Microsoft.CodeAnalysis.Text;
1213
using Microsoft.DotNet.CodeFormatting;
1314
using Xunit;
@@ -25,13 +26,6 @@ public abstract class CodeFormattingTestBase
2526
private const string VBFileExtension = ".vb";
2627
private const string TestProjectName = "TestProject";
2728

28-
internal abstract IFormattingRule GetFormattingRule();
29-
30-
internal static IFormattingRule GetDefaultVSFormatter()
31-
{
32-
return new Rules.IsFormattedFormattingRule();
33-
}
34-
3529
protected virtual IEnumerable<MetadataReference> GetSolutionMetadataReferences()
3630
{
3731
yield return s_CorlibReference;
@@ -61,29 +55,26 @@ private Solution CreateSolution(string[] sources, string language = LanguageName
6155
return solution;
6256
}
6357

64-
private static async Task<Solution> Format(Solution solution, IFormattingRule rule, bool runFormatter)
58+
private async Task<Solution> Format(Solution solution, bool runFormatter)
6559
{
6660
var documentIds = solution.Projects.SelectMany(p => p.DocumentIds);
6761

6862
foreach (var id in documentIds)
6963
{
7064
var document = solution.GetDocument(id);
71-
var newDocument = await RewriteDocumentAsync(document, rule, runFormatter);
72-
solution = newDocument.Project.Solution;
65+
document = await RewriteDocumentAsync(document);
66+
if (runFormatter)
67+
{
68+
document = await Formatter.FormatAsync(document);
69+
}
70+
71+
solution = document.Project.Solution;
7372
}
7473

7574
return solution;
7675
}
7776

78-
private static async Task<Document> RewriteDocumentAsync(Document document, IFormattingRule rule, bool runFormatter)
79-
{
80-
document = await rule.ProcessAsync(document, CancellationToken.None);
81-
if (runFormatter)
82-
return await GetDefaultVSFormatter().ProcessAsync(document, CancellationToken.None);
83-
return document;
84-
}
85-
86-
private static void AssertSolutionEqual(Solution expectedSolution, Solution actualSolution)
77+
private void AssertSolutionEqual(Solution expectedSolution, Solution actualSolution)
8778
{
8879
var expectedDocuments = expectedSolution.Projects.SelectMany(p => p.Documents);
8980
var actualDocuments = actualSolution.Projects.SelectMany(p => p.Documents);
@@ -100,26 +91,67 @@ private static void AssertSolutionEqual(Solution expectedSolution, Solution actu
10091
}
10192
}
10293

103-
private void Verify(string[] sources, string[] expected, IFormattingRule rule, bool runFormatter)
94+
protected abstract Task<Document> RewriteDocumentAsync(Document document);
95+
96+
protected void Verify(string[] sources, string[] expected, bool runFormatter)
10497
{
10598
var inputSolution = CreateSolution(sources);
10699
var expectedSolution = CreateSolution(expected);
107-
var actualSolution = Format(inputSolution, rule, runFormatter).Result;
100+
var actualSolution = Format(inputSolution, runFormatter).Result;
108101

109102
if (actualSolution == null)
110103
Assert.False(true, "Solution is null. Test Failed.");
111104

112105
AssertSolutionEqual(expectedSolution, actualSolution);
113106
}
114107

115-
protected void Verify(string[] source, string[] expected, bool runFormatter)
108+
protected void Verify(string source, string expected, bool runFormatter = true)
116109
{
117-
Verify(source, expected, GetFormattingRule(), runFormatter);
110+
Verify(new string[] { source }, new string[] { expected }, runFormatter);
118111
}
112+
}
119113

120-
protected void Verify(string source, string expected, bool runFormatter = true)
114+
public abstract class SyntaxRuleTestBase : CodeFormattingTestBase
115+
{
116+
internal abstract ISyntaxFormattingRule Rule
121117
{
122-
Verify(new string[] { source }, new string[] { expected }, runFormatter);
118+
get;
119+
}
120+
121+
protected override async Task<Document> RewriteDocumentAsync(Document document)
122+
{
123+
var syntaxRoot = await document.GetSyntaxRootAsync();
124+
syntaxRoot = Rule.Process(syntaxRoot);
125+
return document.WithSyntaxRoot(syntaxRoot);
126+
}
127+
}
128+
129+
public abstract class LocalSemanticRuleTestBase : CodeFormattingTestBase
130+
{
131+
internal abstract ILocalSemanticFormattingRule Rule
132+
{
133+
get;
134+
}
135+
136+
protected override async Task<Document> RewriteDocumentAsync(Document document)
137+
{
138+
var syntaxRoot = await document.GetSyntaxRootAsync();
139+
syntaxRoot = await Rule.ProcessAsync(document, syntaxRoot, CancellationToken.None);
140+
return document.WithSyntaxRoot(syntaxRoot);
141+
}
142+
}
143+
144+
public abstract class GlobalSemanticRuleTestBase : CodeFormattingTestBase
145+
{
146+
internal abstract IGlobalSemanticFormattingRule Rule
147+
{
148+
get;
149+
}
150+
151+
protected override async Task<Document> RewriteDocumentAsync(Document document)
152+
{
153+
var solution = await Rule.ProcessAsync(document, await document.GetSyntaxRootAsync(), CancellationToken.None);
154+
return solution.GetDocument(document.Id);
123155
}
124156
}
125157
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/BracesRuleTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.DotNet.CodeFormatting.Tests
78
{
8-
public class BraceNewLineTests : CodeFormattingTestBase
9+
public class BraceNewLineTests : SyntaxRuleTestBase
910
{
10-
internal override IFormattingRule GetFormattingRule()
11+
internal override ISyntaxFormattingRule Rule
1112
{
12-
return new Rules.BraceNewLineRule();
13+
get { return new Rules.BraceNewLineRule(); }
1314
}
1415

1516
[Fact]

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/ExplicitThisRuleTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Microsoft.DotNet.CodeFormatting.Tests
99
{
10-
public sealed class ExplicitThisRuleTests : CodeFormattingTestBase
10+
public sealed class ExplicitThisRuleTests : LocalSemanticRuleTestBase
1111
{
12-
internal override IFormattingRule GetFormattingRule()
12+
internal override ILocalSemanticFormattingRule Rule
1313
{
14-
return new Rules.ExplicitThisRule();
14+
get { return new Rules.ExplicitThisRule(); }
1515
}
1616

1717
[Fact]
@@ -22,7 +22,7 @@ class C1
2222
{
2323
int _field1;
2424
string _field2;
25-
string field3;
25+
internal string field3;
2626
2727
void Use(int i) { }
2828
@@ -43,7 +43,7 @@ class C1
4343
{
4444
int _field1;
4545
string _field2;
46-
string field3;
46+
internal string field3;
4747
4848
void Use(int i) { }
4949
@@ -69,7 +69,7 @@ class C1
6969
{
7070
int _field1;
7171
string _field2;
72-
string field3;
72+
internal string field3;
7373
7474
void M()
7575
{
@@ -85,7 +85,7 @@ class C1
8585
{
8686
int _field1;
8787
string _field2;
88-
string field3;
88+
internal string field3;
8989
9090
void M()
9191
{

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/ExplicitVisibilityRuleTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
namespace Microsoft.DotNet.CodeFormatting.Tests
1212
{
13-
public sealed class ExplicitVisibilityRuleTests : CodeFormattingTestBase
13+
public sealed class ExplicitVisibilityRuleTests : LocalSemanticRuleTestBase
1414
{
15-
internal override IFormattingRule GetFormattingRule()
15+
internal override ILocalSemanticFormattingRule Rule
1616
{
17-
return new Rules.ExplicitVisibilityRule();
17+
get { return new Rules.ExplicitVisibilityRule(); }
1818
}
1919

2020
[Fact]

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.DotNet.CodeFormatting.Tests
78
{
8-
public class HasNewLineBeforeFirstNamespaceFormattingRuleTests : CodeFormattingTestBase
9+
public class HasNewLineBeforeFirstNamespaceFormattingRuleTests : SyntaxRuleTestBase
910
{
11+
internal override ISyntaxFormattingRule Rule
12+
{
13+
get { return new Rules.HasNewLineBeforeFirstNamespaceFormattingRule(); }
14+
}
15+
1016
[Fact]
1117
public void TestNewLineBeforeFirstNamespace01()
1218
{
@@ -86,10 +92,5 @@ namespace N { }
8692
";
8793
Verify(text, expected);
8894
}
89-
90-
internal override IFormattingRule GetFormattingRule()
91-
{
92-
return new Rules.HasNewLineBeforeFirstNamespaceFormattingRule();
93-
}
9495
}
9596
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/HasNewLineBeforeFirstUsingFormattingRuleTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.DotNet.CodeFormatting.Tests
78
{
8-
public class HasNewLineBeforeFirstUsingFormattingRuleTests : CodeFormattingTestBase
9+
public class HasNewLineBeforeFirstUsingFormattingRuleTests : SyntaxRuleTestBase
910
{
11+
internal override ISyntaxFormattingRule Rule
12+
{
13+
get { return new Rules.HasNewLineBeforeFirstUsingFormattingRule(); }
14+
}
15+
1016
[Fact]
1117
public void TestNewLineBeforeFirstUsing01()
1218
{
@@ -97,10 +103,5 @@ public void TestNewLineBeforeFirstUsing05()
97103
";
98104
Verify(text, expected);
99105
}
100-
101-
internal override IFormattingRule GetFormattingRule()
102-
{
103-
return new Rules.HasNewLineBeforeFirstUsingFormattingRule();
104-
}
105106
}
106107
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/HasNoIllegalHeadersFormattingRuleTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using Xunit;
67

78
namespace Microsoft.DotNet.CodeFormatting.Tests
89
{
9-
public class HasNoIllegalHeadersFormattingRuleTests : CodeFormattingTestBase
10+
public class HasNoIllegalHeadersFormattingRuleTests : LocalSemanticRuleTestBase
1011
{
12+
internal override ILocalSemanticFormattingRule Rule
13+
{
14+
get { return new Rules.HasNoIllegalHeadersFormattingRule(); }
15+
}
16+
1117
[Fact]
1218
public void TestHasNoIllegalHeadersFormattingRule01()
1319
{
@@ -244,10 +250,5 @@ public void TestHasNoIllegalHeadersFormattingRule14()
244250
";
245251
Verify(text, expected);
246252
}
247-
248-
internal override IFormattingRule GetFormattingRule()
249-
{
250-
return new Rules.HasNoIllegalHeadersFormattingRule();
251-
}
252253
}
253254
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
45
using Xunit;
56

67
namespace Microsoft.DotNet.CodeFormatting.Tests
78
{
8-
public class NonAsciiCharactersAreEscapedInLiteralsTests : CodeFormattingTestBase
9+
public class NonAsciiCharactersAreEscapedInLiteralsTests : SyntaxRuleTestBase
910
{
11+
internal override ISyntaxFormattingRule Rule
12+
{
13+
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(); }
14+
}
15+
1016
[Fact]
1117
public void CanUseNonAsciiCharactersInComments()
1218
{
@@ -62,10 +68,5 @@ class Test
6268

6369
Verify(text, text);
6470
}
65-
66-
internal override IFormattingRule GetFormattingRule()
67-
{
68-
return new Rules.NonAsciiCharactersAreEscapedInLiterals();
69-
}
7071
}
7172
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/PrivateFieldNamingRuleTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace Microsoft.DotNet.CodeFormatting.Tests
77
{
8-
public class PrivateFieldNamingRuleTests : CodeFormattingTestBase
8+
public class PrivateFieldNamingRuleTests : GlobalSemanticRuleTestBase
99
{
10-
internal override IFormattingRule GetFormattingRule()
10+
internal override IGlobalSemanticFormattingRule Rule
1111
{
12-
return new Rules.PrivateFieldNamingRule();
12+
get { return new Rules.PrivateFieldNamingRule(); }
1313
}
1414

1515
[Fact]

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/UsesXunitForTestsFormattingRuleTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111

1212
namespace Microsoft.DotNet.CodeFormatting.Tests
1313
{
14-
public class UsesXunitForTestsFormattingRuleTests : CodeFormattingTestBase
14+
public class UsesXunitForTestsFormattingRuleTests : GlobalSemanticRuleTestBase
1515
{
16+
internal override IGlobalSemanticFormattingRule Rule
17+
{
18+
get { return new Rules.UsesXunitForTestsFormattingRule(); }
19+
}
20+
1621
[Fact]
1722
public void TestUpdatesUsingStatements()
1823
{
@@ -200,10 +205,5 @@ protected override IEnumerable<MetadataReference> GetSolutionMetadataReferences(
200205
s_XunitReference
201206
});
202207
}
203-
204-
internal override IFormattingRule GetFormattingRule()
205-
{
206-
return new Rules.UsesXunitForTestsFormattingRule();
207-
}
208208
}
209209
}

0 commit comments

Comments
 (0)