Skip to content

Commit 5325006

Browse files
committed
added some Dictionary asserts
1 parent d8c1b19 commit 5325006

File tree

7 files changed

+130
-93
lines changed

7 files changed

+130
-93
lines changed

src/FluentAssertions.BestPractices.Tests/DiagnosticVerifier.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ static DiagnosticVerifier()
3333
typeof(Compilation), // Microsoft.CodeAnalysis
3434
typeof(AssertionScope), // FluentAssertions.Core
3535
typeof(AssertionExtensions), // FluentAssertions
36-
// typeof(System.Linq.Expressions.Expression) // System.Linq.Expressions
3736
}.Select(type => type.GetTypeInfo().Assembly.Location)
3837
.Append(GetSystemAssemblyPathByName("System.Threading.Tasks.dll"))
3938
.Append(GetSystemAssemblyPathByName("System.Runtime.dll"))
@@ -42,6 +41,7 @@ static DiagnosticVerifier()
4241
.Append(GetSystemAssemblyPathByName("System.Xml.XDocument.dll"))
4342
.Append(GetSystemAssemblyPathByName("System.Private.Xml.Linq.dll"))
4443
.Append(GetSystemAssemblyPathByName("System.Linq.Expressions.dll"))
44+
.Append(GetSystemAssemblyPathByName("System.Collections.dll"))
4545
.Select(location => (MetadataReference)MetadataReference.CreateFromFile(location))
4646
.ToImmutableArray();
4747

@@ -277,7 +277,9 @@ private static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer
277277
var allDiagnostics = compilationWithAnalyzers.GetAllDiagnosticsAsync().Result;
278278
var other = allDiagnostics.Except(relevantDiagnostics).ToArray();
279279

280-
other.Should().BeEmpty("there should be no error diagnostics that are not related to the test");
280+
var code = documents[0].GetSyntaxRootAsync().Result.ToFullString();
281+
282+
other.Should().BeEmpty($"there should be no error diagnostics that are not related to the test.{Environment.NewLine}code: {code}");
281283

282284
foreach (var diag in relevantDiagnostics)
283285
{

src/FluentAssertions.BestPractices.Tests/GenerateCode.cs

+15-28
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
1-
using System.Collections;
2-
using System.Text;
1+
using System.Text;
32

43
namespace FluentAssertions.BestPractices.Tests
54
{
65
public static class GenerateCode
76
{
8-
public static string NamespaceName => "TestNamespace";
9-
public static string ClassName => "TestClass";
10-
public static string MethodName => "TestMethod";
11-
127
public static string ActualVariableName => "actual";
13-
public static string ExpectedVariableName => "expected";
14-
public static string ExpectedItemVariableName => "expectedItem";
15-
public static string UnexpectedVariableName => "unexpected";
16-
public static string UnexpectedItemVariableName => "unexpectedItem";
17-
public static string CountVariable => "k";
18-
19-
public static string ComplexClassName => "TestComplexClass";
20-
public static string ComplexClassBooleanpropertyName => "BooleanProperty";
218

229
public static string EnumerableAssertion(string assertion) => new StringBuilder()
2310
.AppendLine("using System.Collections.Generic;")
2411
.AppendLine("using System.Linq;")
2512
.AppendLine("using System;")
2613
.AppendLine("using FluentAssertions;")
27-
.AppendLine($"namespace {NamespaceName}")
14+
.AppendLine("namespace TestNamespace")
2815
.AppendLine("{")
29-
.AppendLine($" public class {ClassName}")
16+
.AppendLine(" public class TestClass")
3017
.AppendLine(" {")
31-
.AppendLine($" public void {MethodName}({nameof(IList)}<{ComplexClassName}> {ActualVariableName}, {nameof(IList)}<{ComplexClassName}> {ExpectedVariableName}, {nameof(IList)}<{ComplexClassName}> {UnexpectedVariableName}, {ComplexClassName} {ExpectedItemVariableName}, {ComplexClassName} {UnexpectedItemVariableName}, int {CountVariable})")
18+
.AppendLine($" public void TestMethod(IList<TestComplexClass> {ActualVariableName}, IList<TestComplexClass> expected, IList<TestComplexClass> unexpected, TestComplexClass expectedItem, TestComplexClass unexpectedItem, int k)")
3219
.AppendLine(" {")
3320
.AppendLine($" {assertion}")
3421
.AppendLine(" }")
3522
.AppendLine(" }")
36-
.AppendLine($" public class {ComplexClassName}")
23+
.AppendLine(" public class TestComplexClass")
3724
.AppendLine(" {")
38-
.AppendLine($" public bool {ComplexClassBooleanpropertyName} {{ get; set; }}")
25+
.AppendLine(" public bool BooleanProperty { get; set; }")
3926
.AppendLine(" }")
4027
.AppendLine(" class Program")
4128
.AppendLine(" {")
42-
.AppendLine($" public static void Main()")
29+
.AppendLine(" public static void Main()")
4330
.AppendLine(" {")
4431
.AppendLine(" }")
4532
.AppendLine(" }")
@@ -51,18 +38,18 @@ public static class GenerateCode
5138
.AppendLine("using System.Linq;")
5239
.AppendLine("using System;")
5340
.AppendLine("using FluentAssertions;")
54-
.AppendLine($"namespace {NamespaceName}")
41+
.AppendLine("namespace TestNamespace")
5542
.AppendLine("{")
56-
.AppendLine($" public class {ClassName}")
43+
.AppendLine(" public class TestClass")
5744
.AppendLine(" {")
58-
.AppendLine($" public void {MethodName}({nameof(IDictionary)}<string, {ComplexClassName}> {ActualVariableName}, {nameof(IDictionary)}<string, {ComplexClassName}> {ExpectedVariableName}, {nameof(IList)}<{ComplexClassName}> {UnexpectedVariableName}, {ComplexClassName} {ExpectedItemVariableName}, {ComplexClassName} {UnexpectedItemVariableName}, int {CountVariable})")
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)")
5946
.AppendLine(" {")
6047
.AppendLine($" {assertion}")
6148
.AppendLine(" }")
6249
.AppendLine(" }")
63-
.AppendLine($" public class {ComplexClassName}")
50+
.AppendLine($" public class TestComplexClass")
6451
.AppendLine(" {")
65-
.AppendLine($" public bool {ComplexClassBooleanpropertyName} {{ get; set; }}")
52+
.AppendLine(" public bool BooleanProperty { get; set; }")
6653
.AppendLine(" }")
6754
.AppendLine(" class Program")
6855
.AppendLine(" {")
@@ -74,11 +61,11 @@ public static class GenerateCode
7461
.ToString();
7562

7663
public static string NumericAssertion(string assertion) => new StringBuilder()
77-
.AppendLine($"namespace {NamespaceName}")
64+
.AppendLine("namespace TestNamespace")
7865
.AppendLine("{")
79-
.AppendLine($" class {ClassName}")
66+
.AppendLine(" class TestClass")
8067
.AppendLine(" {")
81-
.AppendLine($" void {MethodName}(int {ActualVariableName}, int {ExpectedVariableName})")
68+
.AppendLine($" void TestMethod(int {ActualVariableName}, int expected)")
8269
.AppendLine(" {")
8370
.AppendLine($" {assertion}")
8471
.AppendLine(" }")

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

+74-38
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,111 @@ namespace FluentAssertions.BestPractices.Tests
66
[TestClass]
77
public class DictionaryTests
88
{
9-
[TestMethod]
10-
[AssertionDiagnostic("actual.ContainsKey(expected).Should().BeTrue();")]
11-
[NotImplemented]
9+
[AssertionDataTestMethod]
10+
[AssertionDiagnostic("actual.ContainsKey(expectedKey).Should().BeTrue({0});")]
11+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeTrue({0}).And.ToString();")]
12+
[Implemented]
1213
public void DictionaryShouldContainKey_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainKeyAnalyzer>(assertion);
1314

14-
[TestMethod]
15+
[AssertionDataTestMethod]
1516
[AssertionCodeFix(
16-
oldAssertion: "actual.ContainsKey(expected).Should().BeTrue();",
17-
newAssertion: "actual.Should().ContainKey(expected);")]
18-
[NotImplemented]
17+
oldAssertion: "actual.ContainsKey(expectedKey).Should().BeTrue({0});",
18+
newAssertion: "actual.Should().ContainKey(expectedKey{0});")]
19+
[AssertionCodeFix(
20+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeTrue({0}).And.ToString();",
21+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ToString();")]
22+
[Implemented]
1923
public void DictionaryShouldContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainKeyCodeFix, DictionaryShouldContainKeyAnalyzer>(oldAssertion, newAssertion);
2024

21-
[TestMethod]
22-
[AssertionDiagnostic("actual.ContainsKey(expected).Should().BeFalse();")]
23-
[NotImplemented]
25+
[AssertionDataTestMethod]
26+
[AssertionDiagnostic("actual.ContainsKey(expectedKey).Should().BeFalse({0});")]
27+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeFalse({0}).And.ToString();")]
28+
[Implemented]
2429
public void DictionaryShouldNotContainKey_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldNotContainKeyAnalyzer>(assertion);
2530

26-
[TestMethod]
31+
[AssertionDataTestMethod]
2732
[AssertionCodeFix(
28-
oldAssertion: "actual.ContainsKey(expected).Should().BeFalse();",
29-
newAssertion: "actual.Should().NotContainKey(expected);")]
30-
[NotImplemented]
33+
oldAssertion: "actual.ContainsKey(expectedKey).Should().BeFalse({0});",
34+
newAssertion: "actual.Should().NotContainKey(expectedKey{0});")]
35+
[AssertionCodeFix(
36+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsKey(expectedKey).Should().BeFalse({0}).And.ToString();",
37+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().NotContainKey(expectedKey{0}).And.ToString();")]
38+
[Implemented]
3139
public void DictionaryShouldNotContainKey_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldNotContainKeyCodeFix, DictionaryShouldNotContainKeyAnalyzer>(oldAssertion, newAssertion);
3240

33-
[TestMethod]
34-
[AssertionDiagnostic("actual.ContainsValue(expected).Should().BeTrue();")]
35-
[NotImplemented]
41+
[AssertionDataTestMethod]
42+
[AssertionDiagnostic("actual.ContainsValue(expectedValue).Should().BeTrue({0});")]
43+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeTrue({0}).And.ToString();")]
44+
[Implemented]
3645
public void DictionaryShouldContainValue_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainValueAnalyzer>(assertion);
3746

38-
[TestMethod]
47+
[AssertionDataTestMethod]
3948
[AssertionCodeFix(
40-
oldAssertion: "actual.ContainsValue(expected).Should().BeTrue();",
41-
newAssertion: "actual.Should().ContainValue(expected);")]
42-
[NotImplemented]
49+
oldAssertion: "actual.ContainsValue(expectedValue).Should().BeTrue({0});",
50+
newAssertion: "actual.Should().ContainValue(expectedValue{0});")]
51+
[AssertionCodeFix(
52+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeTrue({0}).And.ToString();",
53+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainValue(expectedValue{0}).And.ToString();")]
54+
[Implemented]
4355
public void DictionaryShouldContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainValueCodeFix, DictionaryShouldContainValueAnalyzer>(oldAssertion, newAssertion);
4456

45-
[TestMethod]
46-
[AssertionDiagnostic("actual.ContainsValue(expected).Should().BeFalse();")]
47-
[NotImplemented]
57+
[AssertionDataTestMethod]
58+
[AssertionDiagnostic("actual.ContainsValue(expectedValue).Should().BeFalse({0});")]
59+
[AssertionDiagnostic("actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeFalse({0}).And.ToString();")]
60+
[Implemented]
4861
public void DictionaryShouldNotContainValue_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldNotContainValueAnalyzer>(assertion);
4962

50-
[TestMethod]
63+
[AssertionDataTestMethod]
5164
[AssertionCodeFix(
52-
oldAssertion: "actual.ContainsValue(expected).Should().BeFalse();",
53-
newAssertion: "actual.Should().NotContainValue(expected);")]
54-
[NotImplemented]
65+
oldAssertion: "actual.ContainsValue(expectedValue).Should().BeFalse({0});",
66+
newAssertion: "actual.Should().NotContainValue(expectedValue{0});")]
67+
[AssertionCodeFix(
68+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).ContainsValue(expectedValue).Should().BeFalse({0}).And.ToString();",
69+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().NotContainValue(expectedValue{0}).And.ToString();")]
70+
[Implemented]
5571
public void DictionaryShouldNotContainValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldNotContainValueCodeFix, DictionaryShouldNotContainValueAnalyzer>(oldAssertion, newAssertion);
5672

57-
[TestMethod]
58-
[AssertionDiagnostic("actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue);")]
73+
[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();")]
5976
[NotImplemented]
6077
public void DictionaryShouldContainKeyAndValue_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainKeyAndValueAnalyzer>(assertion);
6178

62-
[TestMethod]
79+
[AssertionDataTestMethod]
80+
[AssertionCodeFix(
81+
oldAssertion: "actual.Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue);",
82+
newAssertion: "actual.Should().Contain(expectedKey, expectedValue{0});")]
83+
[AssertionCodeFix(
84+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey{0}).And.ContainValue(expectedValue).And.ToString();",
85+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
6386
[AssertionCodeFix(
64-
oldAssertion: "actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue);",
65-
newAssertion: "actual.Should().Contain(expectedKey, expectedValue);")]
87+
oldAssertion: "actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue{0});",
88+
newAssertion: "actual.Should().Contain(expectedKey, expectedValue{0});")]
89+
[AssertionCodeFix(
90+
oldAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().ContainKey(expectedKey).And.ContainValue(expectedValue{0}).And.ToString();",
91+
newAssertion: "actual.ToDictionary(p => p.Key, p=> p.Value).Should().Contain(expectedKey, expectedValue{0}).And.ToString();")]
6692
[NotImplemented]
6793
public void DictionaryShouldContainKeyAndValue_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainKeyAndValueCodeFix, DictionaryShouldContainKeyAndValueAnalyzer>(oldAssertion, newAssertion);
6894

69-
[TestMethod]
70-
[AssertionDiagnostic("actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value);")]
95+
[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();")]
7198
[NotImplemented]
7299
public void DictionaryShouldContainPair_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<DictionaryShouldContainPairAnalyzer>(assertion);
73100

74-
[TestMethod]
101+
[AssertionDataTestMethod]
102+
[AssertionCodeFix(
103+
oldAssertion: "actual.Should().ContainKey(expected.Key{0}).And.ContainValue(expected.Value);",
104+
newAssertion: "actual.Should().Contain(expected{0});")]
105+
[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();")]
108+
[AssertionCodeFix(
109+
oldAssertion: "actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value{0});",
110+
newAssertion: "actual.Should().Contain(expected{0});")]
75111
[AssertionCodeFix(
76-
oldAssertion: "actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value);",
77-
newAssertion: "actual.Should().Contain(expected);")]
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();")]
78114
[NotImplemented]
79115
public void DictionaryShouldContainPair_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<DictionaryShouldContainPairCodeFix, DictionaryShouldContainPairAnalyzer>(oldAssertion, newAssertion);
80116

src/FluentAssertions.BestPractices/Tips/Dictionaries/DictionaryShouldContainKey.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DictionaryShouldContainKeyAnalyzer : FluentAssertionsAnalyzer
1414
public const string DiagnosticId = Constants.Tips.Dictionaries.DictionaryShouldContainKey;
1515
public const string Category = Constants.Tips.Category;
1616

17-
public const string Message = "Use {0} .Should() followed by ### instead.";
17+
public const string Message = "Use {0} .Should() followed by .ContainKey() instead.";
1818

1919
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
2020
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
@@ -40,7 +40,10 @@ public class DictionaryShouldContainKeyCodeFix : FluentAssertionsCodeFixProvider
4040

4141
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
4242
{
43-
throw new System.NotImplementedException();
43+
var remove = NodeReplacement.RemoveAndExtractArguments("ContainsKey");
44+
var newStatement = GetNewStatement(statement, remove);
45+
46+
return GetNewStatement(newStatement, NodeReplacement.RenameAndPrependArguments("BeTrue", "ContainKey", remove.Arguments));
4447
}
4548
}
4649
}

0 commit comments

Comments
 (0)