1+ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+ namespace StyleCop . Analyzers . Test . NamingRules
5+ {
6+ using System . Collections . Generic ;
7+ using System . Threading ;
8+ using System . Threading . Tasks ;
9+ using Microsoft . CodeAnalysis . CodeFixes ;
10+ using Microsoft . CodeAnalysis . Diagnostics ;
11+ using StyleCop . Analyzers . NamingRules ;
12+ using TestHelper ;
13+ using Xunit ;
14+
15+ public class SA1654UnitTests : CodeFixVerifier
16+ {
17+ [ Fact ]
18+ public async Task TestGenericParameterDoesNotStartWithTAsync ( )
19+ {
20+ var testCode = @"
21+ public interface IFoo<Key>
22+ {
23+ }" ;
24+
25+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 2 , 23 ) ;
26+
27+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
28+
29+ var fixedCode = @"
30+ public interface IFoo<TKey>
31+ {
32+ }" ;
33+
34+ await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
35+ }
36+
37+ [ Fact ]
38+ public async Task TestGenericParameterDoesNotStartWithTPlusParameterUsedAsync ( )
39+ {
40+ var testCode = @"
41+ public class Foo<Key>
42+ {
43+ void Test()
44+ {
45+ var key = typeof(Key);
46+ }
47+ }
48+ " ;
49+
50+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 2 , 18 ) ;
51+
52+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
53+
54+ var fixedCode = @"
55+ public class Foo<TKey>
56+ {
57+ void Test()
58+ {
59+ var key = typeof(TKey);
60+ }
61+ }
62+ " ;
63+
64+ await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
65+ }
66+
67+ [ Fact ]
68+ public async Task TestGenericParameterStartsWithLowerTAsync ( )
69+ {
70+ var testCode = @"
71+ public interface IFoo<tKey>
72+ {
73+ }" ;
74+
75+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 2 , 23 ) ;
76+
77+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
78+
79+ var fixedCode = @"
80+ public interface IFoo<TtKey>
81+ {
82+ }" ;
83+
84+ await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
85+ }
86+
87+ [ Fact ]
88+ public async Task TestInnerGenericParameterDoesNotStartWithTAsync ( )
89+ {
90+ var testCode = @"
91+ public class Bar
92+ {
93+ public class Foo<Key>
94+ {
95+ }
96+ }" ;
97+
98+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 22 ) ;
99+
100+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
101+
102+ var fixedCode = @"
103+ public class Bar
104+ {
105+ public class Foo<TKey>
106+ {
107+ }
108+ }" ;
109+
110+ await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
111+ }
112+
113+ [ Fact ]
114+ public async Task TestGenericParameterDoesStartWithTAsync ( )
115+ {
116+ var testCode = @"public interface IFoo<TKey>
117+ {
118+ }" ;
119+
120+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
121+ }
122+
123+ [ Fact ]
124+ public async Task TestInnerGenericParameterDoesStartWithTAsync ( )
125+ {
126+ var testCode = @"
127+ public class Bar
128+ {
129+ public class Foo<TKey>
130+ {
131+ }
132+ }" ;
133+
134+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
135+ }
136+
137+ [ Fact ]
138+ public async Task TestGenericParameterDoesNotStartWithTWithMemberMatchingTargetTypeAsync ( )
139+ {
140+ string testCode = @"
141+ public class Foo<Key>
142+ {
143+ Key Bar { get; }
144+ }" ;
145+
146+ string fixedCode = @"
147+ public class Foo<TKey>
148+ {
149+ TKey Bar { get; }
150+ }" ;
151+
152+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 2 , 18 ) ;
153+
154+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
155+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
156+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
157+ }
158+
159+ [ Fact ]
160+ public async Task TestNestedGenericParameterDoesNotStartWithTWithConflictAsync ( )
161+ {
162+ string testCode = @"
163+ public class Outer<TKey>
164+ {
165+ public class Foo<Key>
166+ {
167+ }
168+ }" ;
169+ string fixedCode = @"
170+ public class Outer<TKey>
171+ {
172+ public class Foo<TKey1>
173+ {
174+ }
175+ }" ;
176+
177+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 22 ) ;
178+
179+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
180+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
181+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
182+ }
183+
184+ [ Fact ]
185+ public async Task TestNestedGenericParameterDoesNotStartWithTWithMemberConflictAsync ( )
186+ {
187+ string testCode = @"
188+ public class Outer<TKey>
189+ {
190+ public class Foo<Key>
191+ {
192+ Key Bar { get; }
193+ }
194+ }" ;
195+ string fixedCode = @"
196+ public class Outer<TKey>
197+ {
198+ public class Foo<TKey1>
199+ {
200+ TKey1 Bar { get; }
201+ }
202+ }" ;
203+
204+ DiagnosticResult expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 22 ) ;
205+
206+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
207+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
208+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
209+ }
210+
211+ protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
212+ {
213+ yield return new SA1654GenericParameterNamesMustBeginWithT ( ) ;
214+ }
215+
216+ protected override CodeFixProvider GetCSharpCodeFixProvider ( )
217+ {
218+ return new SA1654CodeFixProvider ( ) ;
219+ }
220+ }
221+ }
0 commit comments