From 923cb83152647b2b72d026f911d42e07e80eb9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 5 Nov 2018 14:48:13 +0100 Subject: [PATCH] Add FluentAssertions analyzer and fix raised issues (#2072) --- .../Common/MultiValueDictionaryTest.cs | 2 +- .../Common/RuleDescriptorTest.cs | 3 +- .../Common/RuleFinderTest.cs | 4 +- .../ControlFlowGraph/ControlFlowGraphTest.cs | 8 +-- .../Helpers/FlowAnalysis/ExplodedGraphTest.cs | 4 +- .../NullableSymbolicValue_TrySetConstraint.cs | 56 +++++++++---------- ...eSymbolicValue_TrySetOppositeConstraint.cs | 6 +- .../Helpers/IfDirectiveHelperTest.cs | 12 ++-- .../Helpers/NavigationHelperTest.cs | 6 +- .../Helpers/SonarAnalysisContextTest.cs | 2 +- .../Helpers/SymbolHelperTest.cs | 8 +-- .../Rules/CheckFileLicenseTest.cs | 16 +++--- ...thNeutralResourcesLanguageAttributeTest.cs | 3 +- .../SonarAnalyzer.UnitTest.csproj | 1 + .../TestFramework/DiagnosticVerifier.cs | 2 +- ...ssueLocationCollector_GetIssueLocations.cs | 10 ++-- ...ationCollector_GetPreciseIssueLocations.cs | 12 ++-- .../IssueLocationCollector_MergeLocations.cs | 6 +- 18 files changed, 80 insertions(+), 81 deletions(-) diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/MultiValueDictionaryTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/MultiValueDictionaryTest.cs index 539671ff23f..088a4adbacb 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/MultiValueDictionaryTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/MultiValueDictionaryTest.cs @@ -51,7 +51,7 @@ public void MultiValueDictionary_Add_Set() mvd.Add(42, 42); mvd.Keys.Should().HaveCount(2); - mvd[5].Should().HaveCount(1); + mvd[5].Should().ContainSingle(); } [TestMethod] diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleDescriptorTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleDescriptorTest.cs index 4fa1e81d529..8fef2b6ff18 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleDescriptorTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleDescriptorTest.cs @@ -82,8 +82,7 @@ private static void CheckRuleDescriptorsNotEmpty(AnalyzerLanguage language) ruleDetail.Title.Should().NotBeNull(); } - ruleDetails.Should().HaveSameCount( - ruleDetails.Select(descriptor => descriptor.Key).Distinct()); + ruleDetails.Should().OnlyHaveUniqueItems(); } } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleFinderTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleFinderTest.cs index 1e30e1b037d..6c50ffed1bf 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleFinderTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Common/RuleFinderTest.cs @@ -60,10 +60,10 @@ public void GetAllAnalyzerTypes() var finder = new RuleFinder(); var countParameterless = finder.GetParameterlessAnalyzerTypes(AnalyzerLanguage.CSharp).Count(); - finder.AllAnalyzerTypes.Count().Should().BeGreaterThan(countParameterless); + finder.AllAnalyzerTypes.Should().HaveCountGreaterThan(countParameterless); countParameterless = finder.GetParameterlessAnalyzerTypes(AnalyzerLanguage.VisualBasic).Count(); - finder.AllAnalyzerTypes.Count().Should().BeGreaterThan(countParameterless); + finder.AllAnalyzerTypes.Should().HaveCountGreaterThan(countParameterless); } } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/ControlFlowGraph/ControlFlowGraphTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/ControlFlowGraph/ControlFlowGraphTest.cs index 96cb09a4071..29ca633bcf3 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/ControlFlowGraph/ControlFlowGraphTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/ControlFlowGraph/ControlFlowGraphTest.cs @@ -1381,7 +1381,7 @@ public void Cfg_Coalesce_Multiple() dBlock.SuccessorBlocks.Should().OnlyContain(bcdBlock); bcdBlock.SuccessorBlocks.Should().OnlyContain(exitBlock); - bcdBlock.Instructions.Should().HaveCount(1); + bcdBlock.Instructions.Should().ContainSingle(); bcdBlock.Instructions.Should().Contain(i => i.ToString() == "a = b ?? c ?? d"); } @@ -1502,7 +1502,7 @@ public void Cfg_ConditionalMultiple() .SuccessorBlocks.First().Should().Be(cfg.ExitBlock); var assignmentBlock = cfg.ExitBlock.PredecessorBlocks.First(); - assignmentBlock.Instructions.Should().HaveCount(1); + assignmentBlock.Instructions.Should().ContainSingle(); assignmentBlock.Instructions.Should().Contain(i => i.ToString() == "a = cond1 ? (cond2?x:y) : (cond3?p:q)"); } @@ -1529,7 +1529,7 @@ public void Cfg_ConditionalAccess() branchBlock.BranchingNode.Kind().Should().Be(SyntaxKind.ConditionalAccessExpression); - branchBlock.Instructions.Should().HaveCount(1); + branchBlock.Instructions.Should().ContainSingle(); branchBlock.Instructions.Should().Contain(i => i.ToString() == "o"); VerifyAllInstructions(branchBlock, "o"); @@ -3312,7 +3312,7 @@ private static IControlFlowGraph Build(string methodBody) private void VerifyInstructions(Block block, int fromIndex, params string[] instructions) { - block.Instructions.Count.Should().BeGreaterOrEqualTo(fromIndex + instructions.Length); + block.Instructions.Should().HaveCountGreaterOrEqualTo(fromIndex + instructions.Length); for (var i = 0; i < instructions.Length; i++) { block.Instructions[fromIndex + i].ToString().Should().Be(instructions[i]); diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/ExplodedGraphTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/ExplodedGraphTest.cs index 9eac33f79c8..f7cfd9099b9 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/ExplodedGraphTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/ExplodedGraphTest.cs @@ -353,7 +353,7 @@ public void ExplodedGraph_BothBranchesVisited() "and thus should be processed only once."); numberOfLastInstructionVisits.Should().Be(2); - visitedBlocks.Count.Should().Be(cfg.Blocks.Count() - 1 /* Exit block*/); + visitedBlocks.Should().HaveCount(cfg.Blocks.Count() - 1 /* Exit block*/); } [TestMethod] @@ -427,7 +427,7 @@ public void ExplodedGraph_BothBranchesVisited_NonCondition() explodedGraph.Walk(); explorationEnded.Should().BeTrue(); - visitedBlocks.Count.Should().Be(cfg.Blocks.Count() - 1 /* Exit block */); + visitedBlocks.Should().HaveCount(cfg.Blocks.Count() - 1 /* Exit block */); countConditionEvaluated.Should().Be(0); } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetConstraint.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetConstraint.cs index eb176b9b7f8..a35781598fe 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetConstraint.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetConstraint.cs @@ -45,7 +45,7 @@ public void TrySetConstraint_Existing_No_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.True); } @@ -57,7 +57,7 @@ public void TrySetConstraint_Existing_No_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.False); } @@ -69,7 +69,7 @@ public void TrySetConstraint_Existing_No_Set_NoValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.NoValue); } @@ -80,7 +80,7 @@ public void TrySetConstraint_Existing_No_Set_HasValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.HasValue, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); } @@ -91,7 +91,7 @@ public void TrySetConstraint_Existing_No_Set_Null() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.NoValue); } @@ -102,7 +102,7 @@ public void TrySetConstraint_Existing_No_Set_NotNull() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.NotNull, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); } @@ -133,7 +133,7 @@ public void TrySetConstraint_Existing_True_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -143,7 +143,7 @@ public void TrySetConstraint_Existing_True_Set_NoValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -163,7 +163,7 @@ public void TrySetConstraint_Existing_True_Set_Null() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -193,7 +193,7 @@ public void TrySetConstraint_Existing_False_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -213,7 +213,7 @@ public void TrySetConstraint_Existing_False_Set_NoValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -233,7 +233,7 @@ public void TrySetConstraint_Existing_False_Set_Null() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -263,7 +263,7 @@ public void TrySetConstraint_Existing_NoValue_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -273,7 +273,7 @@ public void TrySetConstraint_Existing_NoValue_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -293,7 +293,7 @@ public void TrySetConstraint_Existing_NoValue_Set_HasValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.HasValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -313,7 +313,7 @@ public void TrySetConstraint_Existing_NoValue_Set_NotNull() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.NotNull, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -333,7 +333,7 @@ public void TrySetConstraint_Existing_HasValue_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.True); } @@ -345,7 +345,7 @@ public void TrySetConstraint_Existing_HasValue_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.False); } @@ -357,7 +357,7 @@ public void TrySetConstraint_Existing_HasValue_Set_NoValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -377,7 +377,7 @@ public void TrySetConstraint_Existing_HasValue_Set_Null() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -407,7 +407,7 @@ public void TrySetConstraint_Existing_Null_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -417,7 +417,7 @@ public void TrySetConstraint_Existing_Null_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -437,7 +437,7 @@ public void TrySetConstraint_Existing_Null_Set_HasValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.HasValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -457,7 +457,7 @@ public void TrySetConstraint_Existing_Null_Set_NotNull() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.NotNull, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -477,7 +477,7 @@ public void TrySetConstraint_Existing_NotNull_Set_True() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.True, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.True); } @@ -489,7 +489,7 @@ public void TrySetConstraint_Existing_NotNull_Set_False() var newProgramStates = this.sv_0.TrySetConstraint(BoolConstraint.False, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); ShouldHaveConstraint(newProgramStates[0], this.sv_w, BoolConstraint.False); } @@ -501,7 +501,7 @@ public void TrySetConstraint_Existing_NotNull_Set_NoValue() var newProgramStates = this.sv_0.TrySetConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] @@ -521,7 +521,7 @@ public void TrySetConstraint_Existing_NotNull_Set_Null() var newProgramStates = this.sv_0.TrySetConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(0); + newProgramStates.Should().BeEmpty(); } [TestMethod] diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetOppositeConstraint.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetOppositeConstraint.cs index a286033f3fc..39450ca01c9 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetOppositeConstraint.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/FlowAnalysis/NullableSymbolicValue_TrySetOppositeConstraint.cs @@ -73,7 +73,7 @@ public void TrySetOppositeConstraint_Existing_No_Set_None() var newProgramStates = this.sv_0.TrySetOppositeConstraint(NullableValueConstraint.NoValue, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); } @@ -84,7 +84,7 @@ public void TrySetOppositeConstraint_Existing_No_Set_Some() var newProgramStates = this.sv_0.TrySetOppositeConstraint(NullableValueConstraint.HasValue, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.NoValue); } @@ -95,7 +95,7 @@ public void TrySetOppositeConstraint_Existing_No_Set_Null() var newProgramStates = this.sv_0.TrySetOppositeConstraint(ObjectConstraint.Null, ps).ToList(); - newProgramStates.Should().HaveCount(1); + newProgramStates.Should().ContainSingle(); ShouldHaveConstraint(newProgramStates[0], this.sv_0, NullableValueConstraint.HasValue); } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/IfDirectiveHelperTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/IfDirectiveHelperTest.cs index 9abb7d35bde..5827c131a42 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/IfDirectiveHelperTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/IfDirectiveHelperTest.cs @@ -55,7 +55,7 @@ void Method1(){} // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(0); + activeSections.Should().BeEmpty(); } [TestMethod] @@ -99,7 +99,7 @@ void Method1() { } // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(1); + activeSections.Should().ContainSingle(); activeSections.Should().BeEquivalentTo(new[] { "BLOCK3" }); } @@ -142,7 +142,7 @@ void Method1() { } // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(3); + activeSections.Should().HaveCount(3); activeSections.Should().BeEquivalentTo(new[] { "BLOCK1", "BLOCK2", "BLOCK3" }); } @@ -249,7 +249,7 @@ void Method1() { } // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(0); + activeSections.Should().BeEmpty(); } [TestMethod] @@ -277,7 +277,7 @@ void Method1() { } // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(0); + activeSections.Should().BeEmpty(); } [TestMethod] @@ -335,7 +335,7 @@ void Method1() {} // Assert activeSections.Should().NotBeNull(); - activeSections.Count().Should().Be(0); + activeSections.Should().BeEmpty(); } [TestMethod] diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/NavigationHelperTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/NavigationHelperTest.cs index 6c02cafcf57..48a9e3411f6 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/NavigationHelperTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/NavigationHelperTest.cs @@ -86,7 +86,7 @@ public void GetPrecedingIfsInConditionChain() var ifStatement2 = this.ifMethod.DescendantNodes().OfType().Last(); var preceding = ifStatement2.GetPrecedingIfsInConditionChain(); - preceding.Should().HaveCount(1); + preceding.Should().ContainSingle(); ifStatement1.Should().BeEquivalentTo(preceding[0]); } @@ -100,7 +100,7 @@ public void GetPrecedingStatementsInConditionChain() var ifStatement2 = this.ifMethod.DescendantNodes().OfType().Last(); var preceding = ifStatement2.GetPrecedingStatementsInConditionChain().ToList(); - preceding.Should().HaveCount(1); + preceding.Should().ContainSingle(); ifStatement1.Statement.Should().BeEquivalentTo(preceding[0]); } @@ -114,7 +114,7 @@ public void GetPrecedingConditionsInConditionChain() var ifStatement2 = this.ifMethod.DescendantNodes().OfType().Last(); var preceding = ifStatement2.GetPrecedingConditionsInConditionChain().ToList(); - preceding.Should().HaveCount(1); + preceding.Should().ContainSingle(); ifStatement1.Condition.Should().BeEquivalentTo(preceding[0]); } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SonarAnalysisContextTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SonarAnalysisContextTest.cs index 0e288524034..2d81494f58f 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SonarAnalysisContextTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SonarAnalysisContextTest.cs @@ -79,7 +79,7 @@ public void SonarAnalysis_ByDefault_ExecuteRule() [TestMethod] public void SonarAnalysis_WhenAnalysisDisabledBaseOnSyntaxTree_ReportIssuesForEnabledRules() { - this.TestCases.Count.Should().BeGreaterThan(2); + this.TestCases.Should().HaveCountGreaterThan(2); try { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SymbolHelperTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SymbolHelperTest.cs index a7489ebf225..fbf0ad20cf5 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SymbolHelperTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Helpers/SymbolHelperTest.cs @@ -244,15 +244,15 @@ public void Symbol_GetSelfAndBaseTypes() { var objectType = this.semanticModel.Compilation.GetTypeByMetadataName("System.Object"); var baseTypes = objectType.GetSelfAndBaseTypes().ToList(); - baseTypes.Should().HaveCount(1); + baseTypes.Should().ContainSingle(); baseTypes.First().Should().Be(objectType); var derived1Type = this.semanticModel.GetDeclaredSymbol(this.derivedClassDeclaration1) as INamedTypeSymbol; baseTypes = derived1Type.GetSelfAndBaseTypes().ToList(); baseTypes.Should().HaveCount(3); - baseTypes[0].Should().Be(derived1Type); - baseTypes[1].Should().Be(this.semanticModel.GetDeclaredSymbol(this.baseClassDeclaration) as INamedTypeSymbol); - baseTypes[2].Should().Be(objectType); + baseTypes.Should().HaveElementAt(0, derived1Type); + baseTypes.Should().HaveElementAt(1, this.semanticModel.GetDeclaredSymbol(this.baseClassDeclaration) as INamedTypeSymbol); + baseTypes.Should().HaveElementAt(2, objectType); } [TestMethod] diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/CheckFileLicenseTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/CheckFileLicenseTest.cs index fa67142b74a..7f02abef3d4 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/CheckFileLicenseTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/CheckFileLicenseTest.cs @@ -224,16 +224,16 @@ public void CheckFileLicense_WhenEmptyFile_ShouldBeNonCompliant_CS() [TestCategory("Rule")] public void CheckFileLicense_WhenProvidingAnInvalidRegex_ShouldThrowException_CS() { - const string expectedErrorMessage = - "Expected diagnostics.Where(d => d.Id == AnalyzerFailedDiagnosticId) to be empty, but found {error AD0001: " + - "* 'SonarAnalyzer.Rules.CSharp.CheckFileLicense' *System.InvalidOperationException*Invalid regular expression: " + - FailingSingleLineRegexHeader + "'.}."; + Action action = () => Verifier.VerifyAnalyzer(@"TestCases\CheckFileLicense_NoLicenseStartWithUsing.cs", + new CheckFileLicense { HeaderFormat = FailingSingleLineRegexHeader, IsRegularExpression = true }); - Action action = - () => Verifier.VerifyAnalyzer(@"TestCases\CheckFileLicense_NoLicenseStartWithUsing.cs", - new CheckFileLicense { HeaderFormat = FailingSingleLineRegexHeader, IsRegularExpression = true }); action.Should().Throw() - .WithMessage(expectedErrorMessage); + .WithMessage("* error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.CheckFileLicense' threw an exception of " + + "type 'System.InvalidOperationException' with message 'Invalid regular expression: ['.} to not have any " + + "items matching (d.Id == \"AD0001\"), but found {error AD0001: Analyzer " + + "'SonarAnalyzer.Rules.CSharp.CheckFileLicense' threw an exception of type " + + "'System.InvalidOperationException' with message 'Invalid regular expression: " + + FailingSingleLineRegexHeader + "'.}."); } [TestMethod] diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/MarkAssemblyWithNeutralResourcesLanguageAttributeTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/MarkAssemblyWithNeutralResourcesLanguageAttributeTest.cs index 4e0136cdd2c..0d8c0bfb8b4 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/MarkAssemblyWithNeutralResourcesLanguageAttributeTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/MarkAssemblyWithNeutralResourcesLanguageAttributeTest.cs @@ -62,8 +62,7 @@ public void MarkAssemblyWithNeutralResourcesLanguageAttribute_WhenResxButNoAttri @"TestCases\MarkAssemblyWithNeutralResourcesLanguageAttributeNonCompliant.cs", @"ResourceTests\SomeResources.Designer.cs" }, new MarkAssemblyWithNeutralResourcesLanguageAttribute()); - action.Should().Throw().And - .Message.Should().Be("Issue with message 'Mark this assembly with 'System.Resources.NeutralResourcesLanguageAttribute'.' not expected on line 1"); + action.Should().Throw().WithMessage("Issue with message 'Mark this assembly with 'System.Resources.NeutralResourcesLanguageAttribute'.' not expected on line 1"); } } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/SonarAnalyzer.UnitTest.csproj b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/SonarAnalyzer.UnitTest.csproj index f7c0a6fb89e..af695640eab 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/SonarAnalyzer.UnitTest.csproj +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/SonarAnalyzer.UnitTest.csproj @@ -14,6 +14,7 @@ + diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/DiagnosticVerifier.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/DiagnosticVerifier.cs index efa9dbb4168..683ac2faa31 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/DiagnosticVerifier.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/DiagnosticVerifier.cs @@ -160,7 +160,7 @@ private static void VerifyBuildErrors(ImmutableArray diagnostics, Co } private static void VerifyNoExceptionThrown(IEnumerable diagnostics) => - diagnostics.Where(d => d.Id == AnalyzerFailedDiagnosticId).Should().BeEmpty(); + diagnostics.Should().NotContain(d => d.Id == AnalyzerFailedDiagnosticId); private static void VerifyIssue(IList expectedIssues, Func issueFilter, Location location, string message, string extraInfo, out string issueId) diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetIssueLocations.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetIssueLocations.cs index 75058ce2fda..4c73d4e4046 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetIssueLocations.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetIssueLocations.cs @@ -92,7 +92,7 @@ public void GetIssueLocations_Noncompliant_With_Reversed_Message_And_Flows() }"); var result = new IssueLocationCollector().GetIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -110,7 +110,7 @@ public void GetIssueLocations_Noncompliant_With_Offset() }"); var result = new IssueLocationCollector().GetIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -146,7 +146,7 @@ public void GetIssueLocations_Noncompliant_With_Message() }"); var result = new IssueLocationCollector().GetIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -164,7 +164,7 @@ public void GetIssueLocations_Noncompliant_With_Invalid_Offset() }"); var result = new IssueLocationCollector().GetIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -200,7 +200,7 @@ public void GetIssueLocations_Noncompliant() }"); var result = new IssueLocationCollector().GetIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetPreciseIssueLocations.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetPreciseIssueLocations.cs index a05f5437ac9..9b1fb5274c7 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetPreciseIssueLocations.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_GetPreciseIssueLocations.cs @@ -58,7 +58,7 @@ public void GetPreciseIssueLocations_NoMessage_NoIds() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -77,7 +77,7 @@ public void GetPreciseIssueLocations_With_Offset() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -96,7 +96,7 @@ public void GetPreciseIssueLocations_NoMessage_NoIds_Secondary() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { false }, @@ -115,7 +115,7 @@ public void GetPreciseIssueLocations_Secondary_With_Offset() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { false }, @@ -210,7 +210,7 @@ public void GetPreciseIssueLocations_Message() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { true }, @@ -229,7 +229,7 @@ public void GetPreciseIssueLocations_Message_Secondary() }"); var result = new IssueLocationCollector().GetPreciseIssueLocations(line).ToList(); - result.Should().HaveCount(1); + result.Should().ContainSingle(); VerifyIssueLocations(result, expectedIsPrimary: new[] { false }, diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_MergeLocations.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_MergeLocations.cs index b17486961f3..32c8f9da68d 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_MergeLocations.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/IssueLocationCollectorTests/IssueLocationCollector_MergeLocations.cs @@ -46,7 +46,7 @@ public void MergeLocations_Issues_Same_Line() new[] { new IssueLocation { LineNumber = 3, Message = "message 1" } }, new[] { new IssueLocation { LineNumber = 3, Start = 10, Length = 5, Message = "message 2" } }); - result.Should().HaveCount(1); + result.Should().ContainSingle(); result[0].Message.Should().Be("message 1"); @@ -94,7 +94,7 @@ public void MergeLocations_Empty_Issues_NonEmpty_PreciseLocations() Enumerable.Empty(), new[] { new IssueLocation { LineNumber = 3 } }); - result.Should().HaveCount(1); + result.Should().ContainSingle(); } [TestMethod] @@ -104,7 +104,7 @@ public void MergeLocations_NonEmpty_Issues_Empty_PreciseLocations() new[] { new IssueLocation { LineNumber = 3 } }, Enumerable.Empty()); - result.Should().HaveCount(1); + result.Should().ContainSingle(); } } }