88using System . Threading ;
99using System . Threading . Tasks ;
1010using Microsoft . CodeAnalysis ;
11+ using Microsoft . CodeAnalysis . Formatting ;
1112using Microsoft . CodeAnalysis . Text ;
1213using Microsoft . DotNet . CodeFormatting ;
1314using 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 ;
@@ -55,34 +49,32 @@ private Solution CreateSolution(string[] sources, string language = LanguageName
5549 var fileName = FileNamePrefix + count + fileExtension ;
5650 var documentId = DocumentId . CreateNewId ( projectId , fileName ) ;
5751 solution = solution . AddDocument ( documentId , fileName , SourceText . From ( source ) ) ;
52+ count ++ ;
5853 }
5954
6055 return solution ;
6156 }
6257
63- private static async Task < Solution > Format ( Solution solution , IFormattingRule rule , bool runFormatter )
58+ private async Task < Solution > Format ( Solution solution , bool runFormatter )
6459 {
6560 var documentIds = solution . Projects . SelectMany ( p => p . DocumentIds ) ;
6661
6762 foreach ( var id in documentIds )
6863 {
6964 var document = solution . GetDocument ( id ) ;
70- var newDocument = await RewriteDocumentAsync ( document , rule , runFormatter ) ;
71- 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 ;
7272 }
7373
7474 return solution ;
7575 }
7676
77- private static async Task < Document > RewriteDocumentAsync ( Document document , IFormattingRule rule , bool runFormatter )
78- {
79- document = await rule . ProcessAsync ( document , CancellationToken . None ) ;
80- if ( runFormatter )
81- return await GetDefaultVSFormatter ( ) . ProcessAsync ( document , CancellationToken . None ) ;
82- return document ;
83- }
84-
85- private static void AssertSolutionEqual ( Solution expectedSolution , Solution actualSolution )
77+ private void AssertSolutionEqual ( Solution expectedSolution , Solution actualSolution )
8678 {
8779 var expectedDocuments = expectedSolution . Projects . SelectMany ( p => p . Documents ) ;
8880 var actualDocuments = actualSolution . Projects . SelectMany ( p => p . Documents ) ;
@@ -99,26 +91,67 @@ private static void AssertSolutionEqual(Solution expectedSolution, Solution actu
9991 }
10092 }
10193
102- 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 )
10397 {
10498 var inputSolution = CreateSolution ( sources ) ;
10599 var expectedSolution = CreateSolution ( expected ) ;
106- var actualSolution = Format ( inputSolution , rule , runFormatter ) . Result ;
100+ var actualSolution = Format ( inputSolution , runFormatter ) . Result ;
107101
108102 if ( actualSolution == null )
109103 Assert . False ( true , "Solution is null. Test Failed." ) ;
110104
111105 AssertSolutionEqual ( expectedSolution , actualSolution ) ;
112106 }
113107
114- protected void Verify ( string [ ] source , string [ ] expected , bool runFormatter )
108+ protected void Verify ( string source , string expected , bool runFormatter = true )
115109 {
116- Verify ( source , expected , GetFormattingRule ( ) , runFormatter ) ;
110+ Verify ( new string [ ] { source } , new string [ ] { expected } , runFormatter ) ;
117111 }
112+ }
118113
119- protected void Verify ( string source , string expected , bool runFormatter = true )
114+ public abstract class SyntaxRuleTestBase : CodeFormattingTestBase
115+ {
116+ internal abstract ISyntaxFormattingRule Rule
120117 {
121- 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 ) ;
122155 }
123156 }
124157}
0 commit comments