1+ using Microsoft . CodeAnalysis ;
2+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using System . Text ;
7+ using System . Threading . Tasks ;
8+
9+ namespace FluentAssertions . BestPractices . Tests
10+ {
11+ [ TestClass ]
12+ public class DictionaryTests
13+ {
14+ [ TestMethod ]
15+ [ AssertionDiagnostic ( "actual.ContainsKey(expected).Should().BeTrue();" ) ]
16+ [ NotImplemented ]
17+ public void DictionaryShouldContainKey_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldContainKeyAnalyzer > ( assertion ) ;
18+
19+ [ TestMethod ]
20+ [ AssertionCodeFix (
21+ oldAssertion : "actual.ContainsKey(expected).Should().BeTrue();" ,
22+ newAssertion : "actual.Should().ContainKey(expected);" ) ]
23+ [ NotImplemented ]
24+ public void DictionaryShouldContainKey_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldContainKeyCodeFix , DictionaryShouldContainKeyAnalyzer > ( oldAssertion , newAssertion ) ;
25+
26+ [ TestMethod ]
27+ [ AssertionDiagnostic ( "actual.ContainsKey(expected).Should().BeFalse();" ) ]
28+ [ NotImplemented ]
29+ public void DictionaryShouldNotContainKey_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldNotContainKeyAnalyzer > ( assertion ) ;
30+
31+ [ TestMethod ]
32+ [ AssertionCodeFix (
33+ oldAssertion : "actual.ContainsKey(expected).Should().BeFalse();" ,
34+ newAssertion : "actual.Should().NotContainKey(expected);" ) ]
35+ [ NotImplemented ]
36+ public void DictionaryShouldNotContainKey_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldNotContainKeyCodeFix , DictionaryShouldNotContainKeyAnalyzer > ( oldAssertion , newAssertion ) ;
37+
38+ [ TestMethod ]
39+ [ AssertionDiagnostic ( "actual.ContainsValue(expected).Should().BeTrue();" ) ]
40+ [ NotImplemented ]
41+ public void DictionaryShouldContainValue_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldContainValueAnalyzer > ( assertion ) ;
42+
43+ [ TestMethod ]
44+ [ AssertionCodeFix (
45+ oldAssertion : "actual.ContainsValue(expected).Should().BeTrue();" ,
46+ newAssertion : "actual.Should().ContainValue(expected);" ) ]
47+ [ NotImplemented ]
48+ public void DictionaryShouldContainValue_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldContainValueCodeFix , DictionaryShouldContainValueAnalyzer > ( oldAssertion , newAssertion ) ;
49+
50+ [ TestMethod ]
51+ [ AssertionDiagnostic ( "actual.ContainsValue(expected).Should().BeFalse();" ) ]
52+ [ NotImplemented ]
53+ public void DictionaryShouldNotContainValue_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldNotContainValueAnalyzer > ( assertion ) ;
54+
55+ [ TestMethod ]
56+ [ AssertionCodeFix (
57+ oldAssertion : "actual.ContainsValue(expected).Should().BeFalse();" ,
58+ newAssertion : "actual.Should().NotContainValue(expected);" ) ]
59+ [ NotImplemented ]
60+ public void DictionaryShouldNotContainValue_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldNotContainValueCodeFix , DictionaryShouldNotContainValueAnalyzer > ( oldAssertion , newAssertion ) ;
61+
62+ [ TestMethod ]
63+ [ AssertionDiagnostic ( "actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue);" ) ]
64+ [ NotImplemented ]
65+ public void DictionaryShouldContainKeyAndValue_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldContainKeyAndValueAnalyzer > ( assertion ) ;
66+
67+ [ TestMethod ]
68+ [ AssertionCodeFix (
69+ oldAssertion : "actual.Should().ContainKey(expectedKey).And.ContainValue(expectedValue);" ,
70+ newAssertion : "actual.Should().Contain(expectedKey, expectedValue);" ) ]
71+ [ NotImplemented ]
72+ public void DictionaryShouldContainKeyAndValue_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldContainKeyAndValueCodeFix , DictionaryShouldContainKeyAndValueAnalyzer > ( oldAssertion , newAssertion ) ;
73+
74+ [ TestMethod ]
75+ [ AssertionDiagnostic ( "actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value);" ) ]
76+ [ NotImplemented ]
77+ public void DictionaryShouldContainPair_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < DictionaryShouldContainPairAnalyzer > ( assertion ) ;
78+
79+ [ TestMethod ]
80+ [ AssertionCodeFix (
81+ oldAssertion : "actual.Should().ContainKey(expected.Key).And.ContainValue(expected.Value);" ,
82+ newAssertion : "actual.Should().Contain(expected);" ) ]
83+ [ NotImplemented ]
84+ public void DictionaryShouldContainPair_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < DictionaryShouldContainPairCodeFix , DictionaryShouldContainPairAnalyzer > ( oldAssertion , newAssertion ) ;
85+
86+ private void VerifyCSharpDiagnostic < TDiagnosticAnalyzer > ( string sourceAssersion ) where TDiagnosticAnalyzer : Microsoft . CodeAnalysis . Diagnostics . DiagnosticAnalyzer , new ( )
87+ {
88+ var source = GenerateCode . DictionaryAssertion ( sourceAssersion ) ;
89+
90+ var type = typeof ( TDiagnosticAnalyzer ) ;
91+ var diagnosticId = ( string ) type . GetField ( "DiagnosticId" ) . GetValue ( null ) ;
92+ var message = ( string ) type . GetField ( "Message" ) . GetValue ( null ) ;
93+
94+ DiagnosticVerifier . VerifyCSharpDiagnosticUsingAllAnalyzers ( source , new DiagnosticResult
95+ {
96+ Id = diagnosticId ,
97+ Message = string . Format ( message , GenerateCode . ActualVariableName ) ,
98+ Locations = new DiagnosticResultLocation [ ]
99+ {
100+ new DiagnosticResultLocation ( "Test0.cs" , 11 , 13 )
101+ } ,
102+ Severity = DiagnosticSeverity . Info
103+ } ) ;
104+ }
105+
106+ private void VerifyCSharpFix < TCodeFixProvider , TDiagnosticAnalyzer > ( string oldSourceAssertion , string newSourceAssertion )
107+ where TCodeFixProvider : Microsoft . CodeAnalysis . CodeFixes . CodeFixProvider , new ( )
108+ where TDiagnosticAnalyzer : Microsoft . CodeAnalysis . Diagnostics . DiagnosticAnalyzer , new ( )
109+ {
110+ var oldSource = GenerateCode . DictionaryAssertion ( oldSourceAssertion ) ;
111+ var newSource = GenerateCode . DictionaryAssertion ( newSourceAssertion ) ;
112+
113+ DiagnosticVerifier . VerifyCSharpFix < TCodeFixProvider , TDiagnosticAnalyzer > ( oldSource , newSource ) ;
114+ }
115+ }
116+ }
0 commit comments