|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using CSharpGuidelinesAnalyzer.Rules.Maintainability; |
3 | 4 | using CSharpGuidelinesAnalyzer.Test.TestDataBuilders; |
| 5 | +using FluentAssertions; |
4 | 6 | using Microsoft.CodeAnalysis.Diagnostics; |
5 | 7 | using Xunit; |
6 | 8 |
|
@@ -716,6 +718,107 @@ void M() |
716 | 718 | "Local function 'L' returns a tuple with 3 elements, which exceeds the maximum of 2 elements."); |
717 | 719 | } |
718 | 720 |
|
| 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 | + |
719 | 822 | protected override DiagnosticAnalyzer CreateAnalyzer() |
720 | 823 | { |
721 | 824 | return new AvoidSignatureWithManyParametersAnalyzer(); |
|
0 commit comments