Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSTEST0030: add codefix #4769

Merged
merged 4 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

namespace MSTest.Analyzers;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PublicTypeShouldBeTestClassFixer))]
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AddTestClassFixer))]
[Shared]
public sealed class PublicTypeShouldBeTestClassFixer : CodeFixProvider
public sealed class AddTestClassFixer : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create(DiagnosticIds.PublicTypeShouldBeTestClassRuleId);
= ImmutableArray.Create(
DiagnosticIds.PublicTypeShouldBeTestClassRuleId,
DiagnosticIds.TypeContainingTestMethodShouldBeATestClassRuleId);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
MSTest.Analyzers.PublicTypeShouldBeTestClassAnalyzer,
MSTest.Analyzers.PublicTypeShouldBeTestClassFixer>;
MSTest.Analyzers.AddTestClassFixer>;

namespace MSTest.Analyzers.UnitTests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
MSTest.Analyzers.TypeContainingTestMethodShouldBeATestClassAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
MSTest.Analyzers.AddTestClassFixer>;

namespace MSTest.Analyzers.Test;

Expand Down Expand Up @@ -42,7 +42,18 @@ public void TestMethod1() {}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
public void TestMethod1() {}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down Expand Up @@ -71,7 +82,31 @@ public void TestMethod2()
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass : WithTestMethods_WithoutTestClass
{

}

[TestClass]
public class WithTestMethods_WithoutTestClass
{
[TestMethod]
public void TestMethod1()
{
}

[TestMethod]
public void TestMethod2()
{
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down Expand Up @@ -100,7 +135,31 @@ public void TestMethod2()
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class Base
{

}

[TestClass]
public class MyTestClass : Base
{
[TestMethod]
public void TestMethod1()
{
}

[TestMethod]
public void TestMethod2()
{
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down Expand Up @@ -149,7 +208,24 @@ public void MyTestMethod()
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class DerivedTestMethod : TestMethodAttribute
{
}

[TestClass]
public class MyTestClass
{
[DerivedTestMethod]
public void MyTestMethod()
{
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
Expand Down Expand Up @@ -192,6 +268,26 @@ public void TestMethod1()
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class TestClass
{
[TestInitialize]
public void Initialize()
{

}

[TestMethod]
public void TestMethod1()
{

}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}
}
Loading