Skip to content

Commit d89c9b2

Browse files
Bart KoelmanBart Koelman
authored andcommitted
Added configuration support for AV1500 and AV1561
1 parent 81a0507 commit d89c9b2

File tree

10 files changed

+772
-59
lines changed

10 files changed

+772
-59
lines changed

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using CSharpGuidelinesAnalyzer.Rules.Maintainability;
77
using CSharpGuidelinesAnalyzer.Test.TestDataBuilders;
8+
using FluentAssertions;
89
using Microsoft.CodeAnalysis.Diagnostics;
910
using Xunit;
1011

@@ -2762,6 +2763,139 @@ class C
27622763
"Method 'C.M(bool)' contains 8 statements, which exceeds the maximum of 7 statements.");
27632764
}
27642765

2766+
#region Non-default configuration
2767+
2768+
[Fact]
2769+
internal void When_method_body_contains_seventeen_statements_it_must_be_reported()
2770+
{
2771+
// Arrange
2772+
ParsedSourceCode source = new TypeSourceCodeBuilder()
2773+
.WithSettings(new AnalyzerSettingsBuilder()
2774+
.Including(DiagnosticId, "MaxStatementCount", "16"))
2775+
.InGlobalScope(@"
2776+
class C
2777+
{
2778+
int [|M|](string s)
2779+
{
2780+
; ; ; ;
2781+
; ; ; ;
2782+
; ; ; ;
2783+
; ; ; ;
2784+
throw null;
2785+
}
2786+
}
2787+
")
2788+
.Build();
2789+
2790+
// Act and assert
2791+
VerifyGuidelineDiagnostic(source,
2792+
"Method 'C.M(string)' contains 17 statements, which exceeds the maximum of 16 statements.");
2793+
}
2794+
2795+
[Fact]
2796+
public void When_settings_are_corrupt_it_must_use_default_value()
2797+
{
2798+
// Arrange
2799+
ParsedSourceCode source = new TypeSourceCodeBuilder()
2800+
.WithSettings("*** BAD XML ***")
2801+
.InGlobalScope(@"
2802+
class C
2803+
{
2804+
int [|M|](string s)
2805+
{
2806+
; ; ; ;
2807+
; ; ;
2808+
throw null;
2809+
}
2810+
}
2811+
")
2812+
.Build();
2813+
2814+
// Act and assert
2815+
VerifyGuidelineDiagnostic(source,
2816+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements.");
2817+
}
2818+
2819+
[Fact]
2820+
public void When_setting_is_missing_it_must_use_default_value()
2821+
{
2822+
// Arrange
2823+
ParsedSourceCode source = new TypeSourceCodeBuilder()
2824+
.WithSettings(new AnalyzerSettingsBuilder()
2825+
.Including(DiagnosticId, "OtherUnusedSetting", "SomeValue"))
2826+
.InGlobalScope(@"
2827+
class C
2828+
{
2829+
int [|M|](string s)
2830+
{
2831+
; ; ; ;
2832+
; ; ;
2833+
throw null;
2834+
}
2835+
}
2836+
")
2837+
.Build();
2838+
2839+
// Act and assert
2840+
VerifyGuidelineDiagnostic(source,
2841+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements.");
2842+
}
2843+
2844+
[Fact]
2845+
public void When_setting_value_is_missing_it_must_use_default_value()
2846+
{
2847+
// Arrange
2848+
ParsedSourceCode source = new TypeSourceCodeBuilder()
2849+
.WithSettings(new AnalyzerSettingsBuilder()
2850+
.Including(DiagnosticId, "MaxStatementCount", null))
2851+
.InGlobalScope(@"
2852+
class C
2853+
{
2854+
int [|M|](string s)
2855+
{
2856+
; ; ; ;
2857+
; ; ;
2858+
throw null;
2859+
}
2860+
}
2861+
")
2862+
.Build();
2863+
2864+
// Act and assert
2865+
VerifyGuidelineDiagnostic(source,
2866+
"Method 'C.M(string)' contains 8 statements, which exceeds the maximum of 7 statements.");
2867+
}
2868+
2869+
[Fact]
2870+
public void When_setting_value_is_out_of_range_it_must_fail()
2871+
{
2872+
// Arrange
2873+
ParsedSourceCode source = new TypeSourceCodeBuilder()
2874+
.WithSettings(new AnalyzerSettingsBuilder()
2875+
.Including(DiagnosticId, "MaxStatementCount", "-1"))
2876+
.InGlobalScope(@"
2877+
class C
2878+
{
2879+
int [|M|](string s)
2880+
{
2881+
; ; ; ;
2882+
; ; ;
2883+
throw null;
2884+
}
2885+
}
2886+
")
2887+
.Build();
2888+
2889+
// Act
2890+
Action action = () => VerifyGuidelineDiagnostic(source);
2891+
2892+
// Assert
2893+
action.Should().Throw<Exception>()
2894+
.WithMessage("*Value for AV1500:MaxStatementCount configuration setting must be in range 0-255.*");
2895+
}
2896+
2897+
#endregion
2898+
27652899
protected override DiagnosticAnalyzer CreateAnalyzer()
27662900
{
27672901
return new AvoidMemberWithManyStatementsAnalyzer();

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/AvoidSignatureWithManyParametersSpecs.cs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using CSharpGuidelinesAnalyzer.Rules.Maintainability;
34
using CSharpGuidelinesAnalyzer.Test.TestDataBuilders;
5+
using FluentAssertions;
46
using Microsoft.CodeAnalysis.Diagnostics;
57
using Xunit;
68

@@ -716,6 +718,107 @@ void M()
716718
"Local function 'L' returns a tuple with 3 elements, which exceeds the maximum of 2 elements.");
717719
}
718720

721+
#region Non-default configuration
722+
723+
[Fact]
724+
internal void When_method_contains_nine_parameters_it_must_be_reported()
725+
{
726+
// Arrange
727+
ParsedSourceCode source = new MemberSourceCodeBuilder()
728+
.WithSettings(new AnalyzerSettingsBuilder()
729+
.Including(DiagnosticId, "MaxParameterCount", "8"))
730+
.InDefaultClass(@"
731+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
732+
{
733+
}
734+
")
735+
.Build();
736+
737+
// Act and assert
738+
VerifyGuidelineDiagnostic(source,
739+
"Method 'M' contains 9 parameters, which exceeds the maximum of 8 parameters.");
740+
}
741+
742+
[Fact]
743+
internal void When_settings_are_corrupt_it_must_use_default_value()
744+
{
745+
// Arrange
746+
ParsedSourceCode source = new MemberSourceCodeBuilder()
747+
.WithSettings("*** BAD XML ***")
748+
.InDefaultClass(@"
749+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
750+
{
751+
}
752+
")
753+
.Build();
754+
755+
// Act and assert
756+
VerifyGuidelineDiagnostic(source,
757+
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
758+
}
759+
760+
[Fact]
761+
internal void When_setting_is_missing_it_must_use_default_value()
762+
{
763+
// Arrange
764+
ParsedSourceCode source = new MemberSourceCodeBuilder()
765+
.WithSettings(new AnalyzerSettingsBuilder()
766+
.Including(DiagnosticId, "OtherUnusedSetting", "SomeValue"))
767+
.InDefaultClass(@"
768+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
769+
{
770+
}
771+
")
772+
.Build();
773+
774+
// Act and assert
775+
VerifyGuidelineDiagnostic(source,
776+
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
777+
}
778+
779+
[Fact]
780+
internal void When_setting_value_is_missing_it_must_use_default_value()
781+
{
782+
// Arrange
783+
ParsedSourceCode source = new MemberSourceCodeBuilder()
784+
.WithSettings(new AnalyzerSettingsBuilder()
785+
.Including(DiagnosticId, "MaxParameterCount", null))
786+
.InDefaultClass(@"
787+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
788+
{
789+
}
790+
")
791+
.Build();
792+
793+
// Act and assert
794+
VerifyGuidelineDiagnostic(source,
795+
"Method 'M' contains 9 parameters, which exceeds the maximum of 3 parameters.");
796+
}
797+
798+
[Fact]
799+
internal void When_setting_value_is_out_of_range_it_must_fail()
800+
{
801+
// Arrange
802+
ParsedSourceCode source = new MemberSourceCodeBuilder()
803+
.WithSettings(new AnalyzerSettingsBuilder()
804+
.Including(DiagnosticId, "MaxParameterCount", "-1"))
805+
.InDefaultClass(@"
806+
void [|M|](int first, string second, double third, float fourth, byte fifth, char sixth, DateTime seventh, TimeSpan eighth, ushort ninth)
807+
{
808+
}
809+
")
810+
.Build();
811+
812+
// Act
813+
Action action = () => VerifyGuidelineDiagnostic(source);
814+
815+
// Assert
816+
action.Should().Throw<Exception>()
817+
.WithMessage("*Value for AV1561:MaxParameterCount configuration setting must be in range 0-255.*");
818+
}
819+
820+
#endregion
821+
719822
protected override DiagnosticAnalyzer CreateAnalyzer()
720823
{
721824
return new AvoidSignatureWithManyParametersAnalyzer();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Collections.Immutable;
2+
using System.Text;
3+
using System.Threading;
4+
using CSharpGuidelinesAnalyzer.Settings;
5+
using JetBrains.Annotations;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.Diagnostics;
8+
using Microsoft.CodeAnalysis.Text;
9+
10+
namespace CSharpGuidelinesAnalyzer.Test.TestDataBuilders
11+
{
12+
internal sealed class AnalyzerSettingsBuilder : ITestDataBuilder<AnalyzerSettingsRegistry>
13+
{
14+
[NotNull]
15+
private readonly AnalyzerSettingsRegistry registry = new AnalyzerSettingsRegistry();
16+
17+
public AnalyzerSettingsRegistry Build()
18+
{
19+
return registry;
20+
}
21+
22+
[NotNull]
23+
public AnalyzerSettingsBuilder Including([NotNull] string rule, [NotNull] string name, [CanBeNull] string value)
24+
{
25+
registry.Add(rule, name, value);
26+
return this;
27+
}
28+
29+
[NotNull]
30+
public static AnalyzerOptions ToOptions([NotNull] AnalyzerSettingsRegistry registry)
31+
{
32+
Guard.NotNull(registry, nameof(registry));
33+
34+
string content = AnalyzerSettingsProvider.ToFileContent(registry);
35+
return ToOptions(content);
36+
}
37+
38+
[NotNull]
39+
public static AnalyzerOptions ToOptions([NotNull] string settingsText)
40+
{
41+
Guard.NotNull(settingsText, nameof(settingsText));
42+
43+
AdditionalText additionalText = new FakeAdditionalText(settingsText);
44+
return new AnalyzerOptions(ImmutableArray.Create(additionalText));
45+
}
46+
47+
private sealed class FakeAdditionalText : AdditionalText
48+
{
49+
[NotNull]
50+
private readonly SourceText sourceText;
51+
52+
[NotNull]
53+
public override string Path { get; } = AnalyzerSettingsProvider.SettingsFileName;
54+
55+
public FakeAdditionalText([NotNull] string content)
56+
{
57+
sourceText = new FakeSourceText(content, AnalyzerSettingsProvider.CreateEncoding());
58+
}
59+
60+
[NotNull]
61+
public override SourceText GetText(CancellationToken cancellationToken = new CancellationToken())
62+
{
63+
return sourceText;
64+
}
65+
66+
private sealed class FakeSourceText : SourceText
67+
{
68+
[NotNull]
69+
private readonly string content;
70+
71+
[NotNull]
72+
public override Encoding Encoding { get; }
73+
74+
public override int Length => content.Length;
75+
76+
public override char this[int position] => content[position];
77+
78+
public FakeSourceText([NotNull] string content, [NotNull] Encoding encoding)
79+
{
80+
Guard.NotNull(content, nameof(content));
81+
Guard.NotNull(encoding, nameof(encoding));
82+
83+
this.content = content;
84+
Encoding = encoding;
85+
}
86+
87+
public override void CopyTo(int sourceIndex, [NotNull] char[] destination, int destinationIndex, int count)
88+
{
89+
content.CopyTo(sourceIndex, destination, destinationIndex, count);
90+
}
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)