Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -404,6 +404,84 @@ private void SomePrivateMethod()
await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestInitializeMethodInNonTestClass_Diagnostic()
{
// The analyzer fires regardless of whether the containing class has [TestClass],
// and the fixer replaces the method with a constructor carrying its body.
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyClass
{
[TestInitialize]
public void [|MyInit|]()
{
int x = 1;
}
}
""";
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyClass
{
public MyClass()
{
int x = 1;
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestClassHasOnlyParameterizedCtorAndTestInitialize_CodeFix_MergesIntoParameterizedCtor()
{
// The fixer merges the TestInitialize body into the first non-static constructor found,
// even when that constructor has parameters and there is no default constructor.
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
private int _x;
private int _y;

public MyTestClass(int y)
{
_y = y;
}

[TestInitialize]
public void [|MyTestInit|]()
{
_x = 1;
}
}
""";
string fixedCode = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
private int _x;
private int _y;

public MyTestClass(int y)
{
_y = y;
_x = 1;
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestClassHasTestInitializeWithMultiLineBody_PreservesIndentation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,39 @@ public class MyTestClass
}
#endif

[TestMethod]
public async Task WhenTestCleanupMethodInNonTestClass_Diagnostic()
{
// The analyzer fires regardless of whether the containing class has [TestClass],
// and the fixer adds IDisposable to the base list and replaces the method with Dispose().
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyClass
{
[TestCleanup]
public void [|MyCleanup|]()
{
int x = 1;
}
}
""";
string fixedCode = """
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyClass : IDisposable
{
public void Dispose()
{
int x = 1;
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenTestClassHasTestCleanupWithMultiLineBody_PreservesIndentation()
{
Expand Down