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

Commit 70793e5

Browse files
committed
Merge pull request #5 from jaredpar/visibility
Added enforcement for visibility modifiers
2 parents b2e566e + b6c68f2 commit 70793e5

File tree

6 files changed

+477
-128
lines changed

6 files changed

+477
-128
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@
105105
</ItemGroup>
106106
<ItemGroup>
107107
<Compile Include="CodeFormattingTestBase.cs" />
108+
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
108109
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
109110
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
110111
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
111112
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRuleTests.cs" />
112113
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
113-
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRuleTests.cs" />
114114
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRuleTests.cs" />
115115
<Compile Include="Rules\UsesXunitForTestsFormattingRuleTests.cs" />
116116
</ItemGroup>
@@ -122,7 +122,9 @@
122122
<Link>IllegalHeaders.md</Link>
123123
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
124124
</None>
125-
<None Include="packages.config" />
125+
<None Include="packages.config">
126+
<SubType>Designer</SubType>
127+
</None>
126128
</ItemGroup>
127129
<ItemGroup>
128130
<ProjectReference Include="..\Microsoft.DotNet.CodeFormatting\Microsoft.DotNet.CodeFormatting.csproj">
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Microsoft.DotNet.CodeFormatting.Tests
9+
{
10+
public sealed class ExplicitVisibilityRuleTests : CodeFormattingTestBase
11+
{
12+
internal override IFormattingRule GetFormattingRule()
13+
{
14+
return new Rules.ExplicitVisibilityRule();
15+
}
16+
17+
[Fact]
18+
public void TestTypeVisibility()
19+
{
20+
var text = @"
21+
class C1 { }
22+
sealed partial class C2 { }
23+
struct S1 { }
24+
interface I1 { }
25+
enum E1 { }
26+
delegate void D1() { }
27+
";
28+
29+
var expected = @"
30+
internal class C1 { }
31+
internal sealed partial class C2 { }
32+
internal struct S1 { }
33+
internal interface I1 { }
34+
internal enum E1 { }
35+
internal delegate void D1() { }
36+
";
37+
38+
Verify(text, expected, runFormatter: false);
39+
}
40+
41+
[Fact]
42+
public void TestNestedTypeVisibility()
43+
{
44+
var text = @"
45+
class C
46+
{
47+
class C { }
48+
struct S { }
49+
interface I { }
50+
enum E { }
51+
delegate void D() { }
52+
}
53+
";
54+
55+
var expected = @"
56+
internal class C
57+
{
58+
private class C { }
59+
private struct S { }
60+
private interface I { }
61+
private enum E { }
62+
private delegate void D() { }
63+
}
64+
";
65+
66+
Verify(text, expected, runFormatter: false);
67+
}
68+
69+
[Fact]
70+
public void TestMethodVisibility()
71+
{
72+
var text = @"
73+
internal class C
74+
{
75+
void M1();
76+
internal void M2();
77+
}
78+
79+
internal struct S
80+
{
81+
void M1();
82+
internal void M2();
83+
}
84+
85+
internal interface I
86+
{
87+
void M1();
88+
void M2();
89+
}
90+
";
91+
92+
var expected = @"
93+
internal class C
94+
{
95+
private void M1();
96+
internal void M2();
97+
}
98+
99+
internal struct S
100+
{
101+
private void M1();
102+
internal void M2();
103+
}
104+
105+
internal interface I
106+
{
107+
void M1();
108+
void M2();
109+
}
110+
";
111+
112+
Verify(text, expected, runFormatter: false);
113+
}
114+
115+
[Fact]
116+
public void TestExplicitInterfaceImplementation()
117+
{
118+
var text = @"
119+
interface I1
120+
{
121+
int this[int index] { get; set; }
122+
int Prop { get; set; }
123+
void M();
124+
event EventHandler E;
125+
}
126+
127+
class C : I1
128+
{
129+
int I1.Prop
130+
{
131+
get { return 0; }
132+
set { }
133+
}
134+
int I1.this[int index]
135+
{
136+
get { return 0; }
137+
set { }
138+
}
139+
void I1.M() { }
140+
event EventHandler I1.E;
141+
void M() { }
142+
}
143+
";
144+
145+
var expected = @"
146+
internal interface I1
147+
{
148+
int this[int index] { get; set; }
149+
int Prop { get; set; }
150+
void M();
151+
event EventHandler E;
152+
}
153+
154+
internal class C : I1
155+
{
156+
int I1.Prop
157+
{
158+
get { return 0; }
159+
set { }
160+
}
161+
int I1.this[int index]
162+
{
163+
get { return 0; }
164+
set { }
165+
}
166+
void I1.M() { }
167+
event EventHandler I1.E;
168+
private void M() { }
169+
}
170+
";
171+
172+
Verify(text, expected, runFormatter: false);
173+
}
174+
175+
[Fact]
176+
public void TestFieldImplementation()
177+
{
178+
var text = @"
179+
class C
180+
{
181+
const int Max;
182+
int Field1;
183+
public int Field2;
184+
event EventHandler E1;
185+
public event EventHandler E2;
186+
}
187+
188+
struct C
189+
{
190+
const int Max;
191+
int Field1;
192+
public int Field2;
193+
event EventHandler E1;
194+
public event EventHandler E2;
195+
}
196+
";
197+
198+
var expected = @"
199+
internal class C
200+
{
201+
private const int Max;
202+
private int Field1;
203+
public int Field2;
204+
private event EventHandler E1;
205+
public event EventHandler E2;
206+
}
207+
208+
internal struct C
209+
{
210+
private const int Max;
211+
private int Field1;
212+
public int Field2;
213+
private event EventHandler E1;
214+
public event EventHandler E2;
215+
}
216+
";
217+
218+
Verify(text, expected, runFormatter: false);
219+
}
220+
221+
[Fact]
222+
public void TestConstructor()
223+
{
224+
var text = @"
225+
class C
226+
{
227+
static C() { }
228+
C() { }
229+
internal C(int p) { }
230+
}
231+
232+
struct S
233+
{
234+
static S() { }
235+
S(int p) { }
236+
internal S(int p1, int p2) { }
237+
}
238+
";
239+
var expected = @"
240+
internal class C
241+
{
242+
static C() { }
243+
private C() { }
244+
internal C(int p) { }
245+
}
246+
247+
internal struct S
248+
{
249+
static S() { }
250+
private S(int p) { }
251+
internal S(int p1, int p2) { }
252+
}
253+
";
254+
255+
Verify(text, expected, runFormatter: false);
256+
}
257+
258+
[Fact]
259+
public void TestPrivateFields()
260+
{
261+
var text = @"
262+
using System;
263+
class T
264+
{
265+
static int x;
266+
private static int y;
267+
// some trivia
268+
protected internal int z;
269+
// some trivia
270+
int k = 1, s = 2;
271+
// some trivia
272+
}";
273+
var expected = @"
274+
using System;
275+
internal class T
276+
{
277+
private static int x;
278+
private static int y;
279+
// some trivia
280+
protected internal int z;
281+
// some trivia
282+
private int k = 1, s = 2;
283+
// some trivia
284+
}";
285+
Verify(text, expected);
286+
}
287+
}
288+
}

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

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@
8787
<Compile Include="RuleOrderAttribute.cs" />
8888
<Compile Include="Properties\AssemblyInfo.cs" />
8989
<Compile Include="IOrderMetadata.cs" />
90+
<Compile Include="Rules\ExplicitVisibilityRule.cs" />
9091
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
9192
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRule.cs" />
9293
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRule.cs" />
9394
<Compile Include="Rules\HasNoIllegalHeadersFormattingRule.cs" />
9495
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRule.cs" />
9596
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRule.cs" />
9697
<Compile Include="Rules\HasNoCustomCopyrightHeaderFormattingRule.cs" />
97-
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRule.cs" />
9898
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRule.cs" />
9999
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
100100
<Compile Include="Rules\IsFormattedFormattingRule.cs" />

0 commit comments

Comments
 (0)