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

Commit ecb3ecd

Browse files
committed
Tests for disabling rules
1 parent dee2ba5 commit ecb3ecd

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

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

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
using Microsoft.CodeAnalysis;
1111
using Xunit;
1212
using System.Collections.Immutable;
13+
using Microsoft.DotNet.CodeFormatting.Rules;
1314

1415
namespace Microsoft.DotNet.CodeFormatting.Tests
1516
{
1617
/// <summary>
1718
/// A test which runs all rules on a given piece of code
1819
/// </summary>
19-
public sealed class CombinationTest : CodeFormattingTestBase, IDisposable
20+
public sealed class CombinationTest : CodeFormattingTestBase
2021
{
2122
private FormattingEngineImplementation _formattingEngine;
2223

@@ -29,9 +30,18 @@ public CombinationTest()
2930
_formattingEngine.PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3031
}
3132

32-
public void Dispose()
33+
private void DisableAllRules()
3334
{
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);
3545
}
3646

3747
protected override async Task<Document> RewriteDocumentAsync(Document document)
@@ -70,6 +80,64 @@ private void M()
7080
Verify(text, expected, runFormatter: false);
7181
}
7282

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+
73141
[Fact]
74142
public void FieldAssignment()
75143
{

0 commit comments

Comments
 (0)