Skip to content

Commit 4820a45

Browse files
committed
Remove region usage
1 parent 84ffd88 commit 4820a45

16 files changed

+5647
-5582
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/AvoidMemberWithManyStatementsSpecs.CodeBlocks.cs

Lines changed: 666 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
using CSharpGuidelinesAnalyzer.Test.TestDataBuilders;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
// @formatter:keep_existing_linebreaks true
6+
7+
namespace CSharpGuidelinesAnalyzer.Test.Specs.Maintainability;
8+
9+
public partial class AvoidMemberWithManyStatementsSpecs
10+
{
11+
[Fact]
12+
internal async Task When_using_editor_config_setting_it_must_be_applied()
13+
{
14+
// Arrange
15+
ParsedSourceCode source = new TypeSourceCodeBuilder()
16+
.WithOptions(new AnalyzerOptionsBuilder()
17+
.ForEditorConfig(new EditorConfigSettingsBuilder()
18+
.Including(DiagnosticId, "max_statement_count", "16")))
19+
.InGlobalScope(@"
20+
class C
21+
{
22+
int [|M|](string s)
23+
{
24+
; ; ; ;
25+
; ; ; ;
26+
; ; ; ;
27+
; ; ; ;
28+
throw null;
29+
}
30+
}
31+
")
32+
.Build();
33+
34+
// Act and assert
35+
await VerifyGuidelineDiagnosticAsync(source,
36+
"Method 'C.M(string)' contains 17 statements, which exceeds the maximum of 16 statements");
37+
}
38+
39+
[Fact]
40+
internal async Task When_using_xml_config_setting_it_must_be_applied()
41+
{
42+
// Arrange
43+
ParsedSourceCode source = new TypeSourceCodeBuilder()
44+
.WithOptions(new AnalyzerOptionsBuilder()
45+
.ForXmlSettings(new XmlSettingsBuilder()
46+
.Including(DiagnosticId, "MaxStatementCount", "16")))
47+
.InGlobalScope(@"
48+
class C
49+
{
50+
int [|M|](string s)
51+
{
52+
; ; ; ;
53+
; ; ; ;
54+
; ; ; ;
55+
; ; ; ;
56+
throw null;
57+
}
58+
}
59+
")
60+
.Build();
61+
62+
// Act and assert
63+
await VerifyGuidelineDiagnosticAsync(source,
64+
"Method 'C.M(string)' contains 17 statements, which exceeds the maximum of 16 statements");
65+
}
66+
67+
[Fact]
68+
internal async Task When_using_xml_and_editor_config_setting_it_must_use_editor_config_value()
69+
{
70+
// Arrange
71+
ParsedSourceCode source = new TypeSourceCodeBuilder()
72+
.WithOptions(new AnalyzerOptionsBuilder()
73+
.ForXmlSettings(new XmlSettingsBuilder()
74+
.Including(DiagnosticId, "MaxStatementCount", "12"))
75+
.ForEditorConfig(new EditorConfigSettingsBuilder()
76+
.Including(DiagnosticId, "max_statement_count", "16")))
77+
.InGlobalScope(@"
78+
class C
79+
{
80+
int [|M|](string s)
81+
{
82+
; ; ; ;
83+
; ; ; ;
84+
; ; ; ;
85+
; ; ; ;
86+
throw null;
87+
}
88+
}
89+
")
90+
.Build();
91+
92+
// Act and assert
93+
await VerifyGuidelineDiagnosticAsync(source,
94+
"Method 'C.M(string)' contains 17 statements, which exceeds the maximum of 16 statements");
95+
}
96+
97+
[Fact]
98+
internal async Task When_xml_file_is_corrupt_it_must_use_default_value()
99+
{
100+
// Arrange
101+
ParsedSourceCode source = new TypeSourceCodeBuilder()
102+
.WithOptions(new AnalyzerOptionsBuilder()
103+
.ForXmlText("*** BAD XML ***"))
104+
.InGlobalScope(@"
105+
class C
106+
{
107+
int [|M|](string s)
108+
{
109+
; ; ; ;
110+
; ; ;
111+
throw null;
112+
}
113+
}
114+
")
115+
.Build();
116+
117+
// Act and assert
118+
await VerifyGuidelineDiagnosticAsync(source,
119+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements");
120+
}
121+
122+
[Fact]
123+
internal async Task When_settings_are_missing_it_must_use_default_value()
124+
{
125+
// Arrange
126+
ParsedSourceCode source = new TypeSourceCodeBuilder()
127+
.WithOptions(new AnalyzerOptionsBuilder()
128+
.ForEditorConfig(new EditorConfigSettingsBuilder()
129+
.Including(DiagnosticId, "other-unused-setting", "some-value"))
130+
.ForXmlSettings(new XmlSettingsBuilder()
131+
.Including(DiagnosticId, "OtherUnusedSetting", "SomeValue")))
132+
.InGlobalScope(@"
133+
class C
134+
{
135+
int [|M|](string s)
136+
{
137+
; ; ; ;
138+
; ; ;
139+
throw null;
140+
}
141+
}
142+
")
143+
.Build();
144+
145+
// Act and assert
146+
await VerifyGuidelineDiagnosticAsync(source,
147+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements");
148+
}
149+
150+
[Fact]
151+
internal async Task When_xml_setting_value_is_missing_it_must_use_default_value()
152+
{
153+
// Arrange
154+
ParsedSourceCode source = new TypeSourceCodeBuilder()
155+
.WithOptions(new AnalyzerOptionsBuilder()
156+
.ForXmlSettings(new XmlSettingsBuilder()
157+
.Including(DiagnosticId, "MaxStatementCount", null)))
158+
.InGlobalScope(@"
159+
class C
160+
{
161+
int [|M|](string s)
162+
{
163+
; ; ; ;
164+
; ; ;
165+
throw null;
166+
}
167+
}
168+
")
169+
.Build();
170+
171+
// Act and assert
172+
await VerifyGuidelineDiagnosticAsync(source,
173+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements");
174+
}
175+
176+
[Fact]
177+
internal async Task When_editor_config_setting_value_is_missing_it_must_use_default_value()
178+
{
179+
// Arrange
180+
ParsedSourceCode source = new TypeSourceCodeBuilder()
181+
.WithOptions(new AnalyzerOptionsBuilder()
182+
.ForEditorConfig(new EditorConfigSettingsBuilder()
183+
.Including(DiagnosticId, "max_statement_count", null)))
184+
.InGlobalScope(@"
185+
class C
186+
{
187+
int [|M|](string s)
188+
{
189+
; ; ; ;
190+
; ; ;
191+
throw null;
192+
}
193+
}
194+
")
195+
.Build();
196+
197+
// Act and assert
198+
await VerifyGuidelineDiagnosticAsync(source,
199+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements");
200+
}
201+
202+
[Fact]
203+
internal async Task When_xml_setting_value_is_invalid_it_must_fail()
204+
{
205+
// Arrange
206+
ParsedSourceCode source = new TypeSourceCodeBuilder()
207+
.WithOptions(new AnalyzerOptionsBuilder()
208+
.ForXmlSettings(new XmlSettingsBuilder()
209+
.Including(DiagnosticId, "MaxStatementCount", "bad")))
210+
.InGlobalScope(@"
211+
class C
212+
{
213+
int [|M|](string s)
214+
{
215+
; ; ; ;
216+
; ; ;
217+
throw null;
218+
}
219+
}
220+
")
221+
.Build();
222+
223+
// Act
224+
Func<Task> action = async () => await VerifyGuidelineDiagnosticAsync(source);
225+
226+
// Assert
227+
await action.Should().ThrowAsync<Exception>()
228+
.WithMessage("*Value for 'AV1500:MaxStatementCount' in 'CSharpGuidelinesAnalyzer.config' must be in range 0-255.*");
229+
}
230+
231+
[Fact]
232+
internal async Task When_editor_config_setting_value_is_invalid_it_must_fail()
233+
{
234+
// Arrange
235+
ParsedSourceCode source = new TypeSourceCodeBuilder()
236+
.WithOptions(new AnalyzerOptionsBuilder()
237+
.ForEditorConfig(new EditorConfigSettingsBuilder()
238+
.Including(DiagnosticId, "max_statement_count", "bad")))
239+
.InGlobalScope(@"
240+
class C
241+
{
242+
int [|M|](string s)
243+
{
244+
; ; ; ;
245+
; ; ;
246+
throw null;
247+
}
248+
}
249+
")
250+
.Build();
251+
252+
// Act
253+
Func<Task> action = async () => await VerifyGuidelineDiagnosticAsync(source);
254+
255+
// Assert
256+
await action.Should().ThrowAsync<Exception>()
257+
.WithMessage("*Value for 'dotnet_diagnostic.av1500.max_statement_count' in '.editorconfig' must be in range 0-255.*");
258+
}
259+
260+
[Fact]
261+
internal async Task When_xml_setting_value_is_out_of_range_it_must_fail()
262+
{
263+
// Arrange
264+
ParsedSourceCode source = new TypeSourceCodeBuilder()
265+
.WithOptions(new AnalyzerOptionsBuilder()
266+
.ForXmlSettings(new XmlSettingsBuilder()
267+
.Including(DiagnosticId, "MaxStatementCount", "-1")))
268+
.InGlobalScope(@"
269+
class C
270+
{
271+
int [|M|](string s)
272+
{
273+
; ; ; ;
274+
; ; ;
275+
throw null;
276+
}
277+
}
278+
")
279+
.Build();
280+
281+
// Act
282+
Func<Task> action = async () => await VerifyGuidelineDiagnosticAsync(source);
283+
284+
// Assert
285+
await action.Should().ThrowAsync<Exception>()
286+
.WithMessage("*Value for 'AV1500:MaxStatementCount' in 'CSharpGuidelinesAnalyzer.config' must be in range 0-255.*");
287+
}
288+
289+
[Fact]
290+
internal async Task When_editor_config_setting_value_is_out_of_range_it_must_fail()
291+
{
292+
// Arrange
293+
ParsedSourceCode source = new TypeSourceCodeBuilder()
294+
.WithOptions(new AnalyzerOptionsBuilder()
295+
.ForEditorConfig(new EditorConfigSettingsBuilder()
296+
.Including(DiagnosticId, "max_statement_count", "-1")))
297+
.InGlobalScope(@"
298+
class C
299+
{
300+
int [|M|](string s)
301+
{
302+
; ; ; ;
303+
; ; ;
304+
throw null;
305+
}
306+
}
307+
")
308+
.Build();
309+
310+
// Act
311+
Func<Task> action = async () => await VerifyGuidelineDiagnosticAsync(source);
312+
313+
// Assert
314+
await action.Should().ThrowAsync<Exception>()
315+
.WithMessage("*Value for 'dotnet_diagnostic.av1500.max_statement_count' in '.editorconfig' must be in range 0-255.*");
316+
}
317+
}

0 commit comments

Comments
 (0)