Skip to content

xUnit TheoryData Migration Code Fix #2464

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

Merged
merged 2 commits into from
May 27, 2025
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
56 changes: 56 additions & 0 deletions TUnit.Analyzers.CodeFixers/XUnitMigrationCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,66 @@ private static async Task<Document> ConvertCodeAsync(Document document, Cancella
updatedRoot = RemoveUsingDirectives(updatedRoot);
UpdateSyntaxTrees(ref compilation, ref syntaxTree, updatedRoot);

updatedRoot = ConvertTheoryData(compilation, updatedRoot);
UpdateSyntaxTrees(ref compilation, ref syntaxTree, updatedRoot);

// Apply all changes in one step
return document.WithSyntaxRoot(updatedRoot);
}

private static SyntaxNode ConvertTheoryData(Compilation compilation, SyntaxNode root)
{
var currentRoot = root;
foreach (var objectCreationExpressionSyntax in currentRoot.DescendantNodes().OfType<BaseObjectCreationExpressionSyntax>())
{
var type = objectCreationExpressionSyntax switch
{
ObjectCreationExpressionSyntax explicitObjectCreationExpressionSyntax => explicitObjectCreationExpressionSyntax.Type,
ImplicitObjectCreationExpressionSyntax implicitObjectCreationExpressionSyntax => SyntaxFactory.ParseTypeName(compilation.GetSemanticModel(implicitObjectCreationExpressionSyntax.SyntaxTree).GetTypeInfo(implicitObjectCreationExpressionSyntax).Type!.ToDisplayString()),
_ => null
};

if (type is not GenericNameSyntax genericNameSyntax ||
genericNameSyntax.Identifier.Text != "TheoryData")
{
continue;
}

var collectionItems = objectCreationExpressionSyntax.Initializer!
.ChildNodes()
.Select(x => x.DescendantNodesAndSelf().OfType<ExpressionSyntax>().First());

var arrayCreationExpressionSyntax = SyntaxFactory.ArrayCreationExpression(
SyntaxFactory.ArrayType(genericNameSyntax.TypeArgumentList.Arguments[0],
SyntaxFactory.SingletonList(
SyntaxFactory.ArrayRankSpecifier(
SyntaxFactory.SingletonSeparatedList<ExpressionSyntax>(
SyntaxFactory.OmittedArraySizeExpression()
)
)
)
),
SyntaxFactory.InitializerExpression(
SyntaxKind.ArrayInitializerExpression,
SyntaxFactory.SeparatedList(collectionItems)
)
).NormalizeWhitespace();

currentRoot = currentRoot.ReplaceNode(objectCreationExpressionSyntax, arrayCreationExpressionSyntax);
}

foreach (var genericTheoryDataTypeSyntax in currentRoot.DescendantNodes().OfType<GenericNameSyntax>().Where(x => x.Identifier.Text == "TheoryData"))
{
var enumerableTypeSyntax = SyntaxFactory.GenericName(
SyntaxFactory.Identifier("IEnumerable"),
SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(genericTheoryDataTypeSyntax.TypeArgumentList.Arguments)));

currentRoot = currentRoot.ReplaceNode(genericTheoryDataTypeSyntax, enumerableTypeSyntax);
}

return currentRoot.NormalizeWhitespace();
}

private static SyntaxNode UpdateInitializeDispose(Compilation compilation, SyntaxNode root)
{
// Always operate on the latest root
Expand Down
59 changes: 59 additions & 0 deletions TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,63 @@ public ValueTask DisposeAsync()
"""
);
}

[Test]
public async Task TheoryData_Is_Flagged()
{
await CodeFixer
.VerifyAnalyzerAsync(
"""
{|#0:using System;
using Xunit;

public class MyClass
{
public static readonly TheoryData<TimeSpan> Times = new()
{
TimeSpan.FromSeconds(1),
TimeSpan.FromHours(1),
TimeSpan.FromMilliseconds(10)
};
}|}
""",
Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0)
);
}

[Test]
public async Task TheoryData_Can_Be_Converted()
{
await CodeFixer
.VerifyCodeFixAsync(
"""
{|#0:using TUnit.Core;
using Xunit;

public class MyClass
{
public static readonly TheoryData<TimeSpan> Times = new()
{
TimeSpan.FromSeconds(1),
TimeSpan.FromHours(1),
TimeSpan.FromMilliseconds(10)
};
}|}
""",
Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0),
"""
using TUnit.Core;

public class MyClass
{
public static readonly IEnumerable<TimeSpan> Times = new TimeSpan[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromHours(1),
TimeSpan.FromMilliseconds(10)
};
}
"""
);
}
}
Loading