Skip to content

Commit 4cdb492

Browse files
committed
Fix tests
Resolves #1
1 parent 91ad05b commit 4cdb492

20 files changed

+80
-37
lines changed

Diff for: WindowsForms.Analyzers.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowsForms.Package", "src
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowsForms.Vsix", "src\WindowsForms.Vsix\WindowsForms.Vsix.csproj", "{4D4CB9B1-CD8F-4EDD-B2AB-A84FD9011DA5}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsForms.Test", "tests\WindowsForms.Test\WindowsForms.Test.csproj", "{47296EEB-161E-4445-B90F-55E5D2C15BC8}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowsForms.Analyzers.Tests", "tests\WindowsForms.Analyzers.Tests\WindowsForms.Analyzers.Tests.csproj", "{47296EEB-161E-4445-B90F-55E5D2C15BC8}"
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApp", "tests\App\TestApp.csproj", "{6B254D4B-E3D5-46FB-9024-1E944FD712A6}"
2121
EndProject

Diff for: src/WindowsForms.Analyzers/ControlTabOrderAnalyzer.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class DiagnosticIds
2828

2929
private const string Category = "Accessibility";
3030

31-
private static readonly DiagnosticDescriptor NonNumericTabIndexValueRule
31+
internal static readonly DiagnosticDescriptor s_nonNumericTabIndexValueRule
3232
= new(DiagnosticIds.NonNumericTabIndexValue,
3333
"Ensure numeric controls tab order value",
3434
"Control '{0}' has unexpected TabIndex value: '{1}'.",
@@ -37,7 +37,7 @@ private static readonly DiagnosticDescriptor NonNumericTabIndexValueRule
3737
isEnabledByDefault: true,
3838
"Avoid manually editing \"InitializeComponent()\" method.");
3939

40-
private static readonly DiagnosticDescriptor InconsistentTabIndexRule
40+
internal static readonly DiagnosticDescriptor s_inconsistentTabIndexRule
4141
= new(DiagnosticIds.InconsistentTabIndex,
4242
"Verify correct controls tab order",
4343
"Control '{0}' has ordinal index of {1} but sets a different TabIndex of {2}.",
@@ -54,7 +54,7 @@ private static readonly DiagnosticDescriptor InconsistentTabIndexRule
5454

5555

5656
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
57-
=> ImmutableArray.Create(InconsistentTabIndexRule, NonNumericTabIndexValueRule);
57+
=> ImmutableArray.Create(s_inconsistentTabIndexRule, s_nonNumericTabIndexValueRule);
5858

5959
public override void Initialize(AnalysisContext context)
6060
{
@@ -67,9 +67,7 @@ public override void Initialize(AnalysisContext context)
6767
private void CodeBlockAction(OperationBlockAnalysisContext context)
6868
{
6969
// We only care about "InitializeComponent" method.
70-
if (context.OwningSymbol is
71-
not { Kind: SymbolKind.Method, Name: "InitializeComponent" } and
72-
not { Kind: SymbolKind.Field }) // TODO: fields contained in the same class as InitializeComponent
70+
if (context.OwningSymbol is not { Kind: SymbolKind.Method, Name: "InitializeComponent" })
7371
{
7472
return;
7573
}
@@ -155,10 +153,7 @@ private void CodeBlockAction(OperationBlockAnalysisContext context)
155153
continue;
156154
}
157155

158-
// If the key exists in _controlsAddIndex, it exists _controlsAddIndexLocations
159-
var syntaxTree = _controlsAddIndexLocations[key];
160-
161-
Diagnostic diagnostic = Diagnostic.Create(InconsistentTabIndexRule,
156+
Diagnostic diagnostic = Diagnostic.Create(s_inconsistentTabIndexRule,
162157
location: _controlsAddIndexLocations[key],
163158
key, addIndex, tabIndex);
164159
context.ReportDiagnostic(diagnostic);
@@ -230,7 +225,7 @@ private void ParseTabIndexAssignments(OperationBlockAnalysisContext context, Ass
230225

231226
if (expressionSyntax.Right is not LiteralExpressionSyntax propertyValueExpressionSyntax)
232227
{
233-
var diagnostic = Diagnostic.Create(NonNumericTabIndexValueRule,
228+
var diagnostic = Diagnostic.Create(s_nonNumericTabIndexValueRule,
234229
Location.Create(expressionSyntax.Right.SyntaxTree, expressionSyntax.Right.Span),
235230
controlName,
236231
expressionSyntax.Right.ToString());

Diff for: src/WindowsForms.Analyzers/InternalsVisibleTo.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("WindowsForms.Analyzers.Tests")]

Diff for: tests/WindowsForms.Test/Verifiers/CSharpAnalyzerVerifier`1+Test.cs renamed to tests/WindowsForms.Analyzers.Tests/Verifiers/CSharpAnalyzerVerifier`1+Test.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis.CSharp.Testing;
22
using Microsoft.CodeAnalysis.Diagnostics;
3+
using Microsoft.CodeAnalysis.Testing;
34
using Microsoft.CodeAnalysis.Testing.Verifiers;
45

56
namespace WindowsForms.Test
@@ -11,6 +12,8 @@ public class Test : CSharpAnalyzerTest<TAnalyzer, MSTestVerifier>
1112
{
1213
public Test()
1314
{
15+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.WindowsForms;
16+
1417
SolutionTransforms.Add((solution, projectId) =>
1518
{
1619
var compilationOptions = solution.GetProject(projectId).CompilationOptions;

Diff for: tests/WindowsForms.Test/Verifiers/CSharpCodeFixVerifier`2+Test.cs renamed to tests/WindowsForms.Analyzers.Tests/Verifiers/CSharpCodeFixVerifier`2+Test.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.CodeAnalysis.CodeFixes;
22
using Microsoft.CodeAnalysis.CSharp.Testing;
33
using Microsoft.CodeAnalysis.Diagnostics;
4+
using Microsoft.CodeAnalysis.Testing;
45
using Microsoft.CodeAnalysis.Testing.Verifiers;
56

67
namespace WindowsForms.Test
@@ -13,6 +14,8 @@ public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, MSTestVerifier>
1314
{
1415
public Test()
1516
{
17+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.WindowsForms;
18+
1619
SolutionTransforms.Add((solution, projectId) =>
1720
{
1821
var compilationOptions = solution.GetProject(projectId).CompilationOptions;

Diff for: tests/WindowsForms.Test/Verifiers/CSharpCodeFixVerifier`2.cs renamed to tests/WindowsForms.Analyzers.Tests/Verifiers/CSharpCodeFixVerifier`2.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Microsoft.CodeAnalysis;
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.CodeAnalysis;
24
using Microsoft.CodeAnalysis.CodeFixes;
35
using Microsoft.CodeAnalysis.CSharp.Testing;
46
using Microsoft.CodeAnalysis.Diagnostics;
57
using Microsoft.CodeAnalysis.Testing;
68
using Microsoft.CodeAnalysis.Testing.Verifiers;
7-
using System.Threading;
8-
using System.Threading.Tasks;
99

1010
namespace WindowsForms.Test
1111
{

Diff for: tests/WindowsForms.Test/Verifiers/CSharpCodeRefactoringVerifier`1+Test.cs renamed to tests/WindowsForms.Analyzers.Tests/Verifiers/CSharpCodeRefactoringVerifier`1+Test.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis.CodeRefactorings;
22
using Microsoft.CodeAnalysis.CSharp.Testing;
3+
using Microsoft.CodeAnalysis.Testing;
34
using Microsoft.CodeAnalysis.Testing.Verifiers;
45

56
namespace WindowsForms.Test
@@ -11,6 +12,8 @@ public class Test : CSharpCodeRefactoringTest<TCodeRefactoring, MSTestVerifier>
1112
{
1213
public Test()
1314
{
15+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.WindowsForms;
16+
1417
SolutionTransforms.Add((solution, projectId) =>
1518
{
1619
var compilationOptions = solution.GetProject(projectId).CompilationOptions;

Diff for: tests/WindowsForms.Test/Verifiers/VisualBasicAnalyzerVerifier`1+Test.cs renamed to tests/WindowsForms.Analyzers.Tests/Verifiers/VisualBasicAnalyzerVerifier`1+Test.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.CodeAnalysis.Diagnostics;
2+
using Microsoft.CodeAnalysis.Testing;
23
using Microsoft.CodeAnalysis.Testing.Verifiers;
34
using Microsoft.CodeAnalysis.VisualBasic.Testing;
45

@@ -11,6 +12,7 @@ public class Test : VisualBasicAnalyzerTest<TAnalyzer, MSTestVerifier>
1112
{
1213
public Test()
1314
{
15+
ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.WindowsForms;
1416
}
1517
}
1618
}

Diff for: tests/WindowsForms.Test/WinFormsAccessibilityTests.InconsistentTabIndexRule.cs renamed to tests/WindowsForms.Analyzers.Tests/WinFormsAccessibilityTests.InconsistentTabIndexRule.cs

+50-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Threading.Tasks;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using WindowsForms.Analyzers;
67
using VerifyCS = WindowsForms.Test.CSharpCodeFixVerifier<
78
WindowsForms.Analyzers.ControlTabOrderAnalyzer,
89
WindowsForms.WinFormsAccessibilityCodeFixProvider>;
@@ -17,10 +18,12 @@ public class InconsistentTabIndexRule
1718
[TestMethod]
1819
public async Task No_fields_no_locals_should_produce_no_diagnostics()
1920
{
20-
var test = @"
21+
string code = @"
22+
using System.Windows.Forms;
23+
2124
namespace WinFormsApp1
2225
{
23-
partial class Form1
26+
partial class Form1 : Form
2427
{
2528
private void InitializeComponent()
2629
{
@@ -38,16 +41,18 @@ private void InitializeComponent()
3841
}
3942
";
4043

41-
await VerifyCS.VerifyAnalyzerAsync(test);
44+
await VerifyCS.VerifyAnalyzerAsync(code);
4245
}
4346

4447
[TestMethod]
4548
public async Task Fields_no_TabIndex_should_produce_no_diagnostics()
4649
{
47-
var test = @"
50+
string code = @"
51+
using System.Windows.Forms;
52+
4853
namespace WinFormsApp1
4954
{
50-
partial class Form1
55+
partial class Form1 : Form
5156
{
5257
private void InitializeComponent()
5358
{
@@ -57,7 +62,6 @@ private void InitializeComponent()
5762
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
5863
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
5964
this.ClientSize = new System.Drawing.Size(578, 398);
60-
this.Controls.Add(button3);
6165
this.Controls.Add(this.button1);
6266
this.Controls.Add(this.treeView1);
6367
this.Name = ""Form1"";
@@ -73,16 +77,18 @@ private void InitializeComponent()
7377
}
7478
";
7579

76-
await VerifyCS.VerifyAnalyzerAsync(test);
80+
await VerifyCS.VerifyAnalyzerAsync(code);
7781
}
7882

7983
[TestMethod]
8084
public async Task Fields_with_correct_TabIndex_should_produce_no_diagnostics()
8185
{
82-
var test = @"
86+
string code = @"
87+
using System.Windows.Forms;
88+
8389
namespace WinFormsApp1
8490
{
85-
partial class Form1
91+
partial class Form1 : Form
8692
{
8793
private void InitializeComponent()
8894
{
@@ -111,16 +117,18 @@ private void InitializeComponent()
111117
}
112118
";
113119

114-
await VerifyCS.VerifyAnalyzerAsync(test);
120+
await VerifyCS.VerifyAnalyzerAsync(code);
115121
}
116122

117123
[TestMethod]
118124
public async Task Local_no_TabIndex_should_produce_no_diagnostics()
119125
{
120-
var test = @"
126+
string code = @"
127+
using System.Windows.Forms;
128+
121129
namespace WinFormsApp1
122130
{
123-
partial class Form1
131+
partial class Form1 : Form
124132
{
125133
private void InitializeComponent()
126134
{
@@ -143,16 +151,18 @@ private void InitializeComponent()
143151
}
144152
";
145153

146-
await VerifyCS.VerifyAnalyzerAsync(test);
154+
await VerifyCS.VerifyAnalyzerAsync(code);
147155
}
148156

149157
[TestMethod]
150158
public async Task Local_with_correct_TabIndex_should_produce_no_diagnostics()
151159
{
152-
var test = @"
160+
string code = @"
161+
using System.Windows.Forms;
162+
153163
namespace WinFormsApp1
154164
{
155-
partial class Form1
165+
partial class Form1 : Form
156166
{
157167
private void InitializeComponent()
158168
{
@@ -177,13 +187,19 @@ private void InitializeComponent()
177187
}
178188
";
179189

180-
await VerifyCS.VerifyAnalyzerAsync(test);
190+
await VerifyCS.VerifyAnalyzerAsync(code);
181191
}
182192

183193
[TestMethod]
184-
public async Task TestMethod1()
194+
public async Task Fields_and_locals_with_incorrect_TabIndex_should_produce_diagnostics()
185195
{
186-
var test = @"
196+
string code = @"
197+
using System.Windows.Forms;
198+
199+
namespace WinFormsApp1
200+
{
201+
partial class Form1 : Form
202+
{
187203
private void InitializeComponent()
188204
{
189205
this.treeView1 = new System.Windows.Forms.TreeView();
@@ -246,9 +262,24 @@ private void InitializeComponent()
246262
this.ResumeLayout(false);
247263
248264
}
265+
266+
private System.Windows.Forms.TreeView treeView1;
267+
private System.Windows.Forms.Button button1;
268+
private System.Windows.Forms.Button button2;
269+
}
270+
}
249271
";
250272

251-
await VerifyCS.VerifyAnalyzerAsync(test);
273+
await VerifyCS.VerifyAnalyzerAsync(code,
274+
// /0/Test0.cs(61,13): warning WF0010: Control 'this.button2' has ordinal index of 0 but sets a different TabIndex of 2.
275+
VerifyCS.Diagnostic(ControlTabOrderAnalyzer.s_inconsistentTabIndexRule).WithSpan(61, 13, 61, 30).WithArguments("this.button2", "0", "2"),
276+
// /0/Test0.cs(62,13): warning WF0010: Control 'button3' has ordinal index of 1 but sets a different TabIndex of 0.
277+
VerifyCS.Diagnostic(ControlTabOrderAnalyzer.s_inconsistentTabIndexRule).WithSpan(62, 13, 62, 30).WithArguments("button3", "1", "0"),
278+
// /0/Test0.cs(63,13): warning WF0010: Control 'this.button1' has ordinal index of 2 but sets a different TabIndex of 1.
279+
VerifyCS.Diagnostic(ControlTabOrderAnalyzer.s_inconsistentTabIndexRule).WithSpan(63, 13, 63, 30).WithArguments("this.button1", "2", "1"),
280+
// /0/Test0.cs(64,13): warning WF0010: Control 'this.treeView1' has ordinal index of 3 but sets a different TabIndex of 0.
281+
VerifyCS.Diagnostic(ControlTabOrderAnalyzer.s_inconsistentTabIndexRule).WithSpan(64, 13, 64, 30).WithArguments("this.treeView1", "3", "0")
282+
);
252283
}
253284
}
254285
}

Diff for: tests/WindowsForms.Test/WinFormsAccessibilityTests.NonNumericTabIndexValueRule.cs renamed to tests/WindowsForms.Analyzers.Tests/WinFormsAccessibilityTests.NonNumericTabIndexValueRule.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Threading.Tasks;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using WindowsForms.Analyzers;
67
using VerifyCS = WindowsForms.Test.CSharpCodeFixVerifier<
78
WindowsForms.Analyzers.ControlTabOrderAnalyzer,
89
WindowsForms.WinFormsAccessibilityCodeFixProvider>;
@@ -18,14 +19,15 @@ public class NonNumericTabIndexValueRule
1819
public async Task Non_numeric_TabIndex_should_produce_diagnostics()
1920
{
2021
var test = @"
22+
using System.Windows.Forms;
23+
2124
namespace WinFormsApp1
2225
{
23-
partial class Form1
26+
partial class Form1 : Form
2427
{
2528
private void InitializeComponent()
2629
{
2730
System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
28-
button1.TabIndex = 0;
2931
System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
3032
button2.TabIndex = INDEX;
3133
//
@@ -47,7 +49,8 @@ private void InitializeComponent()
4749
}
4850
";
4951

50-
await VerifyCS.VerifyAnalyzerAsync(test);
52+
await VerifyCS.VerifyAnalyzerAsync(test,
53+
VerifyCS.Diagnostic(ControlTabOrderAnalyzer.s_nonNumericTabIndexValueRule).WithSpan(12, 32, 12, 37).WithArguments("button2", "INDEX"));
5154
}
5255
}
5356
}

0 commit comments

Comments
 (0)