Skip to content

Commit cdd9a01

Browse files
committed
added dictionary analyzers
1 parent 55ef45f commit cdd9a01

17 files changed

+280
-100
lines changed

src/FluentAssertions.BestPractices.Tests/GenerateCode.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static class GenerateCode
4242
.AppendLine("{")
4343
.AppendLine(" public class TestClass")
4444
.AppendLine(" {")
45-
.AppendLine($" public void TestMethod(Dictionary<string, TestComplexClass> {ActualVariableName}, IDictionary<string, TestComplexClass> expected, IDictionary<string, TestComplexClass> unexpected, string expectedKey, TestComplexClass expectedValue, string unexpectedKey, TestComplexClass unexpectedValue)")
45+
.AppendLine($" public void TestMethod(Dictionary<string, TestComplexClass> {ActualVariableName}, IDictionary<string, TestComplexClass> expected, IDictionary<string, TestComplexClass> unexpected, string expectedKey, TestComplexClass expectedValue, string unexpectedKey, TestComplexClass unexpectedValue, KeyValuePair<string, TestComplexClass> pair)")
4646
.AppendLine(" {")
4747
.AppendLine($" {assertion}")
4848
.AppendLine(" }")

src/FluentAssertions.BestPractices.Tests/Tips/DictionaryTests.cs

+52-16
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ public class DictionaryTests
7171
public void DictionaryShouldNotContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldNotContainValueCodeFix, DictionaryShouldNotContainValueAnalyzer>(oldAssertion, newAssertion);
7272

7373
[AssertionDataTestMethod]
74-
[AssertionDiagnostic("actual.Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue{0});")]
75-
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue{0}).And.ToString();")]
76-
[NotImplemented]
74+
[AssertionDiagnostic("actual.Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue);")]
75+
[AssertionDiagnostic("actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue{0});")]
76+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue).And.ToString();")]
77+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey).And.ContainValue(expectedValue{0}).And.ToString();")]
78+
[AssertionDiagnostic("actual.Should().ContainValue(expectedValue{0}).And.ContainKey(expectedKey);")]
79+
[AssertionDiagnostic("actual.Should().ContainValue(expectedValue).And.ContainKey(expectedKey{0});")]
80+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue{0}).And.ContainKey(expectedKey).And.ToString();")]
81+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue).And.ContainKey(expectedKey{0}).And.ToString();")]
82+
[Implemented]
7783
public void DictionaryShouldContainKeyAndValue_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainKeyAndValueAnalyzer>(assertion);
7884

7985
[AssertionDataTestMethod]
@@ -89,29 +95,59 @@ public class DictionaryTests
8995
[AssertionCodeFix(
9096
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey).And.ContainValue(expectedValue{0}).And.ToString();",
9197
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
92-
[NotImplemented]
98+
[AssertionCodeFix(
99+
oldAssertion: "actual.Should().ContainValue(expectedValue{0}).And.ContainKey(expectedKey);",
100+
newAssertion: "actual.Should().Contain(expectedKey, expectedValue{0});")]
101+
[AssertionCodeFix(
102+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue).And.ContainKey(expectedKey{0}).And.ToString();",
103+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
104+
[AssertionCodeFix(
105+
oldAssertion: "actual.Should().ContainValue(expectedValue).And.ContainKey(expectedKey{0});",
106+
newAssertion: "actual.Should().Contain(expectedKey, expectedValue{0});")]
107+
[AssertionCodeFix(
108+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue{0}).And.ContainKey(expectedKey).And.ToString();",
109+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
110+
[Implemented]
93111
public void DictionaryShouldContainKeyAndValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainKeyAndValueCodeFix, DictionaryShouldContainKeyAndValueAnalyzer>(oldAssertion, newAssertion);
94112

95113
[AssertionDataTestMethod]
96-
[AssertionDiagnostic("actual.Should().ContainKey(expected.Key{0}).And.ContainValue(expected.Value);")]
97-
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expected.Key{0}).And.ContainValue(expected.Value).And.ToString();")]
98-
[NotImplemented]
114+
[AssertionDiagnostic("actual.Should().ContainKey(pair.Key{0}).And.ContainValue(pair.Value);")]
115+
[AssertionDiagnostic("actual.Should().ContainKey(pair.Key).And.ContainValue(pair.Value{0});")]
116+
[AssertionDiagnostic("actual.Should().ContainValue(pair.Value{0}).And.ContainKey(pair.Key);")]
117+
[AssertionDiagnostic("actual.Should().ContainValue(pair.Value).And.ContainKey(pair.Key{0});")]
118+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(pair.Key{0}).And.ContainValue(pair.Value).And.ToString();")]
119+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(pair.Key).And.ContainValue(pair.Value{0}).And.ToString();")]
120+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(pair.Value{0}).And.ContainKey(pair.Key).And.ToString();")]
121+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(pair.Value).And.ContainKey(pair.Key{0}).And.ToString();")]
122+
[Implemented]
99123
public void DictionaryShouldContainPair_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainPairAnalyzer>(assertion);
100124

101125
[AssertionDataTestMethod]
102126
[AssertionCodeFix(
103-
oldAssertion: "actual.Should().ContainKey(expected.Key{0}).And.ContainValue(expected.Value);",
104-
newAssertion: "actual.Should().Contain(expected{0});")]
127+
oldAssertion: "actual.Should().ContainKey(pair.Key{0}).And.ContainValue(pair.Value);",
128+
newAssertion: "actual.Should().Contain(pair{0});")]
129+
[AssertionCodeFix(
130+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(pair.Key{0}).And.ContainValue(pair.Value).And.ToString();",
131+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(pair{0}).And.ToString();")]
105132
[AssertionCodeFix(
106-
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expected.Key{0}).And.ContainValue(expected.Value).And.ToString();",
107-
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expected{0}).And.ToString();")]
133+
oldAssertion: "actual.Should().ContainKey(pair.Key).And.ContainValue(pair.Value{0});",
134+
newAssertion: "actual.Should().Contain(pair{0});")]
108135
[AssertionCodeFix(
109-
oldAssertion: "actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value{0});",
110-
newAssertion: "actual.Should().Contain(expected{0});")]
136+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(pair.Key).And.ContainValue(pair.Value{0}).And.ToString();",
137+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(pair{0}).And.ToString();")]
111138
[AssertionCodeFix(
112-
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expected.Key).And.ContainValue(expected.Value{0}).And.ToString();",
113-
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expected{0}).And.ToString();")]
114-
[NotImplemented]
139+
oldAssertion: "actual.Should().ContainValue(pair.Value{0}).And.ContainKey(pair.Key);",
140+
newAssertion: "actual.Should().Contain(pair{0});")]
141+
[AssertionCodeFix(
142+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(pair.Value{0}).And.ContainKey(pair.Key).And.ToString();",
143+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(pair{0}).And.ToString();")]
144+
[AssertionCodeFix(
145+
oldAssertion: "actual.Should().ContainValue(pair.Value).And.ContainKey(pair.Key{0});",
146+
newAssertion: "actual.Should().Contain(pair{0});")]
147+
[AssertionCodeFix(
148+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(pair.Value).And.ContainKey(pair.Key{0}).And.ToString();",
149+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(pair{0}).And.ToString();")]
150+
[Implemented]
115151
public void DictionaryShouldContainPair_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainPairCodeFix, DictionaryShouldContainPairAnalyzer>(oldAssertion, newAssertion);
116152

117153
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string sourceAssersion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()

src/FluentAssertions.BestPractices/FluentAssertions.BestPractices.nuspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>FluentAssertions.BestPractices</id>
55
<title>Fluent Assertions Best Practice</title>
6-
<version>0.4.0</version>
6+
<version>0.5.0</version>
77
<owners>Meir Blachman</owners>
88
<authors>Meir Blachman</authors>
99
<summary>

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldHaveCount.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class CollectionShouldHaveCountCodeFix : FluentAssertionsCodeFixProvider
5555

5656
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
5757
{
58-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("Be", "HaveCount"));
58+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
5959
}
6060
}
6161
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldHaveCountGreaterOrEqualTo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CollectionShouldHaveCountGreaterOrEqualToCodeFix : FluentAssertions
4141

4242
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("BeGreaterOrEqualTo", "HaveCountGreaterOrEqualTo"));
44+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterOrEqualTo", "HaveCountGreaterOrEqualTo"));
4545
}
4646
}
4747
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldHaveCountGreaterThan.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class CollectionShouldHaveCountGreaterThanCodeFix : FluentAssertionsCodeF
4242

4343
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
4444
{
45-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("BeGreaterThan", "HaveCountGreaterThan"));
45+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterThan", "HaveCountGreaterThan"));
4646
}
4747
}
4848
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldHaveCountLessOrEqualTo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CollectionShouldHaveCountLessOrEqualToCodeFix : FluentAssertionsCod
4141

4242
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("BeLessOrEqualTo", "HaveCountLessOrEqualTo"));
44+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessOrEqualTo", "HaveCountLessOrEqualTo"));
4545
}
4646
}
4747
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldHaveCountLessThan.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CollectionShouldHaveCountLessThanCodeFix : FluentAssertionsCodeFixP
4141

4242
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("BeLessThan", "HaveCountLessThan"));
44+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessThan", "HaveCountLessThan"));
4545
}
4646
}
4747
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldNotBeEmpty.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax sta
4646
{
4747
NodeReplacement[] replacements =
4848
{
49-
new NodeReplacement.RemoveNodeReplacement("Any"),
50-
new NodeReplacement.RenameNodeReplacement("BeTrue", "NotBeEmpty")
49+
NodeReplacement.Remove("Any"),
50+
NodeReplacement.Rename("BeTrue", "NotBeEmpty")
5151
};
5252

5353
return GetNewStatement(statement, replacements);

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldNotContainProperty.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax sta
7777
var remove = new NodeReplacement.RemoveAndExtractArgumentsNodeReplacement("Any");
7878
var newStatement = GetNewStatement(statement, remove);
7979

80-
return GetNewStatement(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeFalse", "NotContain", remove.Arguments));
80+
return GetNewStatement(newStatement, NodeReplacement.RenameAndPrependArguments("BeFalse", "NotContain", remove.Arguments));
8181
}
8282
else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.WhereShouldBeEmptySyntaxVisitor))
8383
{
8484
var remove = new NodeReplacement.RemoveAndExtractArgumentsNodeReplacement("Where");
8585
var newStatement = GetNewStatement(statement, remove);
8686

87-
return GetNewStatement(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeEmpty", "NotContain", remove.Arguments));
87+
return GetNewStatement(newStatement, NodeReplacement.RenameAndPrependArguments("BeEmpty", "NotContain", remove.Arguments));
8888
}
8989
else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.ShouldOnlyContainNotSyntaxVisitor))
9090
{
91-
return GetNewStatement(statement, new NodeReplacement.RenameAndNegateLambdaNodeReplacement("OnlyContain", "NotContain"));
91+
return GetNewStatement(statement, NodeReplacement.RenameAndNegateLambda("OnlyContain", "NotContain"));
9292
}
9393
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
9494
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldNotHaveCount.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class CollectionShouldNotHaveCountCodeFix : FluentAssertionsCodeFixProvid
4848

4949
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
5050
{
51-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameNodeReplacement("NotBe", "NotHaveCount"));
51+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("NotBe", "NotHaveCount"));
5252
}
5353
}
5454
}

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldNotHaveSameCount.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class CollectionShouldNotHaveSameCountCodeFix : FluentAssertionsCodeFixPr
4949

5050
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
5151
{
52-
return GetNewStatement(statement, new NodeReplacement.RemoveNodeReplacement("Count"), new NodeReplacement.RenameAndRemoveInvocationOfMethodOnFirstArgumentNodeReplacement("NotBe", "NotHaveSameCount"));
52+
return GetNewStatement(statement, NodeReplacement.Remove("Count"), new NodeReplacement.RenameAndRemoveInvocationOfMethodOnFirstArgumentNodeReplacement("NotBe", "NotHaveSameCount"));
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)