Skip to content

Commit bac0e43

Browse files
committed
MSTEST0030: add codefix
1 parent 4b6ef7b commit bac0e43

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

src/Analyzers/MSTest.Analyzers.CodeFixes/PublicTypeShouldBeTestClassFixer.cs src/Analyzers/MSTest.Analyzers.CodeFixes/AddTestClassFixer.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
namespace MSTest.Analyzers;
2020

21-
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PublicTypeShouldBeTestClassFixer))]
21+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AddTestClassFixer))]
2222
[Shared]
23-
public sealed class PublicTypeShouldBeTestClassFixer : CodeFixProvider
23+
public sealed class AddTestClassFixer : CodeFixProvider
2424
{
2525
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
26-
= ImmutableArray.Create(DiagnosticIds.PublicTypeShouldBeTestClassRuleId);
26+
= ImmutableArray.Create(
27+
DiagnosticIds.PublicTypeShouldBeTestClassRuleId,
28+
DiagnosticIds.TypeContainingTestMethodShouldBeATestClassRuleId);
2729

2830
public override FixAllProvider GetFixAllProvider()
2931
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
@@ -49,7 +51,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4951
CodeAction.Create(
5052
title: CodeFixResources.PublicTypeShouldBeTestClassFix,
5153
createChangedDocument: c => AddTestClassAttributeAsync(context.Document, declaration, c),
52-
equivalenceKey: nameof(PublicTypeShouldBeTestClassFixer)),
54+
equivalenceKey: nameof(AddTestClassFixer)),
5355
diagnostic);
5456
}
5557

test/UnitTests/MSTest.Analyzers.UnitTests/PublicTypeShouldBeTestClassAnalyzerTests.cs

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

44
using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
55
MSTest.Analyzers.PublicTypeShouldBeTestClassAnalyzer,
6-
MSTest.Analyzers.PublicTypeShouldBeTestClassFixer>;
6+
MSTest.Analyzers.AddTestClassFixer>;
77

88
namespace MSTest.Analyzers.UnitTests;
99

test/UnitTests/MSTest.Analyzers.UnitTests/TypeContainingTestMethodShouldBeATestClassAnalyzer.cs

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

44
using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
55
MSTest.Analyzers.TypeContainingTestMethodShouldBeATestClassAnalyzer,
6-
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
6+
MSTest.Analyzers.AddTestClassFixer>;
77

88
namespace MSTest.Analyzers.Test;
99

@@ -42,7 +42,18 @@ public void TestMethod1() {}
4242
}
4343
""";
4444

45-
await VerifyCS.VerifyAnalyzerAsync(code);
45+
string fixedCode = """
46+
using Microsoft.VisualStudio.TestTools.UnitTesting;
47+
48+
[TestClass]
49+
public class MyTestClass
50+
{
51+
[TestMethod]
52+
public void TestMethod1() {}
53+
}
54+
""";
55+
56+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
4657
}
4758

4859
[TestMethod]
@@ -71,7 +82,31 @@ public void TestMethod2()
7182
}
7283
""";
7384

74-
await VerifyCS.VerifyAnalyzerAsync(code);
85+
string fixedCode = """
86+
using Microsoft.VisualStudio.TestTools.UnitTesting;
87+
88+
[TestClass]
89+
public class MyTestClass : WithTestMethods_WithoutTestClass
90+
{
91+
92+
}
93+
94+
[TestClass]
95+
public class WithTestMethods_WithoutTestClass
96+
{
97+
[TestMethod]
98+
public void TestMethod1()
99+
{
100+
}
101+
102+
[TestMethod]
103+
public void TestMethod2()
104+
{
105+
}
106+
}
107+
""";
108+
109+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
75110
}
76111

77112
[TestMethod]
@@ -100,7 +135,31 @@ public void TestMethod2()
100135
}
101136
""";
102137

103-
await VerifyCS.VerifyAnalyzerAsync(code);
138+
string fixedCode = """
139+
using Microsoft.VisualStudio.TestTools.UnitTesting;
140+
141+
[TestClass]
142+
public class Base
143+
{
144+
145+
}
146+
147+
[TestClass]
148+
public class MyTestClass : Base
149+
{
150+
[TestMethod]
151+
public void TestMethod1()
152+
{
153+
}
154+
155+
[TestMethod]
156+
public void TestMethod2()
157+
{
158+
}
159+
}
160+
""";
161+
162+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
104163
}
105164

106165
[TestMethod]
@@ -149,7 +208,24 @@ public void MyTestMethod()
149208
}
150209
""";
151210

152-
await VerifyCS.VerifyAnalyzerAsync(code);
211+
string fixedCode = """
212+
using Microsoft.VisualStudio.TestTools.UnitTesting;
213+
214+
public class DerivedTestMethod : TestMethodAttribute
215+
{
216+
}
217+
218+
[TestClass]
219+
public class MyTestClass
220+
{
221+
[DerivedTestMethod]
222+
public void MyTestMethod()
223+
{
224+
}
225+
}
226+
""";
227+
228+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
153229
}
154230

155231
[TestMethod]
@@ -192,6 +268,26 @@ public void TestMethod1()
192268
}
193269
""";
194270

195-
await VerifyCS.VerifyAnalyzerAsync(code);
271+
string fixedCode = """
272+
using Microsoft.VisualStudio.TestTools.UnitTesting;
273+
274+
[TestClass]
275+
public class TestClass
276+
{
277+
[TestInitialize]
278+
public void Initialize()
279+
{
280+
281+
}
282+
283+
[TestMethod]
284+
public void TestMethod1()
285+
{
286+
287+
}
288+
}
289+
""";
290+
291+
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
196292
}
197293
}

0 commit comments

Comments
 (0)