Skip to content
392 changes: 386 additions & 6 deletions TUnit.Analyzers.CodeFixers/NUnitMigrationCodeFixProvider.cs

Large diffs are not rendered by default.

355 changes: 355 additions & 0 deletions TUnit.Analyzers.Tests/NUnitMigrationAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3249,6 +3249,361 @@ public async Task TestMethod([Matrix(1, 2)] int a, [Matrix("x", "y")] string b)
);
}

[Test]
public async Task NUnit_FileAssert_Exists_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
FileAssert.Exists("test.txt");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(File.Exists("test.txt")).IsTrue();
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_FileAssert_DoesNotExist_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
FileAssert.DoesNotExist("test.txt");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(File.Exists("test.txt")).IsFalse();
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_FileAssert_AreEqual_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
FileAssert.AreEqual("expected.txt", "actual.txt");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(new FileInfo("actual.txt")).HasSameContentAs(new FileInfo("expected.txt"));
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_FileAssert_AreNotEqual_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
FileAssert.AreNotEqual("expected.txt", "actual.txt");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(new FileInfo("actual.txt")).DoesNotHaveSameContentAs(new FileInfo("expected.txt"));
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_DirectoryAssert_Exists_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
DirectoryAssert.Exists("testDir");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(Directory.Exists("testDir")).IsTrue();
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_DirectoryAssert_DoesNotExist_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
DirectoryAssert.DoesNotExist("testDir");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(Directory.Exists("testDir")).IsFalse();
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_DirectoryAssert_AreEqual_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using NUnit.Framework;

{|#0:public class MyClass|}
{
[Test]
public void TestMethod()
{
DirectoryAssert.AreEqual("expectedDir", "actualDir");
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System.IO;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.That(new DirectoryInfo("actualDir")).IsEquivalentTo(new DirectoryInfo("expectedDir"));
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_ExpectedException_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using System;
using NUnit.Framework;

public class MyClass
{
{|#0:[Test]|}
[ExpectedException(typeof(InvalidOperationException))]
public void TestMethod()
{
throw new InvalidOperationException();
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.ThrowsAsync<InvalidOperationException>(() =>
{
throw new InvalidOperationException();
});
}
}
""",
ConfigureNUnitTest
);
}

[Test]
public async Task NUnit_ExpectedException_With_Async_Code_Converted()
{
await CodeFixer.VerifyCodeFixAsync(
"""
using System;
using System.Threading.Tasks;
using NUnit.Framework;

public class MyClass
{
{|#0:[Test]|}
[ExpectedException(typeof(InvalidOperationException))]
public async Task TestMethod()
{
await Task.Delay(1);
throw new InvalidOperationException();
}
}
""",
Verifier.Diagnostic(Rules.NUnitMigration).WithLocation(0),
"""
using System;
using System.Threading.Tasks;
using TUnit.Core;
using TUnit.Assertions;
using static TUnit.Assertions.Assert;
using TUnit.Assertions.Extensions;

public class MyClass
{
[Test]
public async Task TestMethod()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await Task.Delay(1);
throw new InvalidOperationException();
});
}
}
""",
ConfigureNUnitTest
);
}

private static void ConfigureNUnitTest(Verifier.Test test)
{
test.TestState.AdditionalReferences.Add(typeof(NUnit.Framework.TestAttribute).Assembly);
Expand Down
Loading
Loading