10
10
using Microsoft . CodeAnalysis ;
11
11
using Xunit ;
12
12
using System . Collections . Immutable ;
13
+ using Microsoft . DotNet . CodeFormatting . Rules ;
13
14
14
15
namespace Microsoft . DotNet . CodeFormatting . Tests
15
16
{
16
17
/// <summary>
17
18
/// A test which runs all rules on a given piece of code
18
19
/// </summary>
19
- public sealed class CombinationTest : CodeFormattingTestBase , IDisposable
20
+ public sealed class CombinationTest : CodeFormattingTestBase
20
21
{
21
22
private FormattingEngineImplementation _formattingEngine ;
22
23
@@ -29,9 +30,18 @@ public CombinationTest()
29
30
_formattingEngine . PreprocessorConfigurations = ImmutableArray < string [ ] > . Empty ;
30
31
}
31
32
32
- public void Dispose ( )
33
+ private void DisableAllRules ( )
33
34
{
34
- _formattingEngine . AllowTables = false ;
35
+ foreach ( var rule in _formattingEngine . AllRules )
36
+ {
37
+ _formattingEngine . ToggleRuleEnabled ( rule , enabled : false ) ;
38
+ }
39
+ }
40
+
41
+ private void ToggleRule ( string name , bool enabled )
42
+ {
43
+ var rule = _formattingEngine . AllRules . Where ( x => x . Name == name ) . Single ( ) ;
44
+ _formattingEngine . ToggleRuleEnabled ( rule , enabled ) ;
35
45
}
36
46
37
47
protected override async Task < Document > RewriteDocumentAsync ( Document document )
@@ -70,6 +80,64 @@ private void M()
70
80
Verify ( text , expected , runFormatter : false ) ;
71
81
}
72
82
83
+ /// <summary>
84
+ /// Ensure the engine respects the rule map
85
+ /// </summary>
86
+ [ Fact ]
87
+ public void FieldOnly ( )
88
+ {
89
+ var text = @"
90
+ class C {
91
+ int field;
92
+
93
+ void M() {
94
+ N(this.field);
95
+ }
96
+ }" ;
97
+
98
+ var expected = @"
99
+ class C {
100
+ int _field;
101
+
102
+ void M() {
103
+ N(this._field);
104
+ }
105
+ }" ;
106
+
107
+ DisableAllRules ( ) ;
108
+ ToggleRule ( PrivateFieldNamingRule . Name , enabled : true ) ;
109
+
110
+ Verify ( text , expected , runFormatter : false ) ;
111
+ }
112
+
113
+ [ Fact ]
114
+ public void FieldNameExcluded ( )
115
+ {
116
+ var text = @"
117
+ class C {
118
+ int field;
119
+
120
+ void M() {
121
+ N(this.field);
122
+ }
123
+ }" ;
124
+
125
+ var expected = @"// header
126
+
127
+ internal class C
128
+ {
129
+ private int field;
130
+
131
+ private void M()
132
+ {
133
+ N(field);
134
+ }
135
+ }" ;
136
+
137
+ ToggleRule ( PrivateFieldNamingRule . Name , enabled : false ) ;
138
+ Verify ( text , expected , runFormatter : false ) ;
139
+ }
140
+
73
141
[ Fact ]
74
142
public void FieldAssignment ( )
75
143
{
0 commit comments