From 4ecea0508e51c4d1ea5f28b2214ff93623ba00f6 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Thu, 15 Jan 2026 14:34:12 +0000 Subject: [PATCH] test: add xUnit migration tests for Assert.Empty and using directive removal Add test coverage proving that the xUnit code fixer correctly handles: - Assert.Empty(List) -> await Assert.That(list).IsEmpty() - Assert.NotEmpty(List) -> await Assert.That(list).IsNotEmpty() - Assert.Empty(Array) -> await Assert.That(array).IsEmpty() - Using directive removal (using Xunit; and using Xunit.Abstractions;) These tests verify that issues #4416, #4417, #4418, #4419, #4420 are working correctly with the existing code fixer implementation. Co-Authored-By: Claude Opus 4.5 --- .../XUnitMigrationAnalyzerTests.cs | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs b/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs index 53606f8f59..8e777ea0f3 100644 --- a/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs +++ b/TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs @@ -1364,6 +1364,153 @@ public async Task TypeAssertions() ); } + [Test] + public async Task XUnit_Assert_Empty_List_Converted() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using Xunit; + using System.Collections.Generic; + + public class MyClass + { + [Fact] + public void MyTest() + { + var items = new List(); + Assert.Empty(items); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using System.Collections.Generic; + using System.Threading.Tasks; + + public class MyClass + { + [Test] + public async Task MyTest() + { + var items = new List(); + await Assert.That(items).IsEmpty(); + } + } + """, + ConfigureXUnitTest + ); + } + + [Test] + public async Task XUnit_Assert_NotEmpty_List_Converted() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using Xunit; + using System.Collections.Generic; + + public class MyClass + { + [Fact] + public void MyTest() + { + var items = new List { "item" }; + Assert.NotEmpty(items); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using System.Collections.Generic; + using System.Threading.Tasks; + + public class MyClass + { + [Test] + public async Task MyTest() + { + var items = new List { "item" }; + await Assert.That(items).IsNotEmpty(); + } + } + """, + ConfigureXUnitTest + ); + } + + [Test] + public async Task XUnit_Assert_Empty_Array_Converted() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using Xunit; + + public class MyClass + { + [Fact] + public void MyTest() + { + var items = new string[0]; + Assert.Empty(items); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using System.Threading.Tasks; + + public class MyClass + { + [Test] + public async Task MyTest() + { + var items = new string[0]; + await Assert.That(items).IsEmpty(); + } + } + """, + ConfigureXUnitTest + ); + } + + [Test] + public async Task XUnit_Using_Directive_Removed() + { + await CodeFixer + .VerifyCodeFixAsync( + """ + {|#0:using Xunit; + using Xunit.Abstractions; + + public class MyClass + { + [Fact] + public void MyTest() + { + Assert.True(true); + } + }|} + """, + Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0), + """ + using System.Threading.Tasks; + + public class MyClass + { + [Test] + public async Task MyTest() + { + await Assert.That(true).IsTrue(); + } + } + """, + ConfigureXUnitTest + ); + } + private static void ConfigureXUnitTest(Verifier.Test test) { var globalUsings = ("GlobalUsings.cs", SourceText.From("global using Xunit;"));