Skip to content
Merged
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
Prev Previous commit
Next Next commit
Simplify fix to use TestAttribute's CallerFilePath and CallerLineNumber
Improved the source location fix by leveraging the existing CallerFilePath
and CallerLineNumber parameters in TestAttribute constructor arguments,
which are automatically captured when the [Test] attribute is applied.

This approach is:
- Simpler and more reliable than parsing syntax trees
- Uses the built-in compiler-provided location information
- Removes the need for the GetMethodLocation helper method
- More consistent with how regular test methods get their source location

The TestAttribute already captures the exact file and line number via
[CallerFilePath] and [CallerLineNumber] attributes, making this the
preferred approach for getting accurate source locations.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
  • Loading branch information
thomhurst and claude committed Sep 8, 2025
commit 487cf33afe6d968f2a2cddee91d13cd73309f7f7
22 changes: 5 additions & 17 deletions TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ private static void GenerateInheritedTestSources(SourceProductionContext context
// Calculate inheritance depth for this test
int inheritanceDepth = CalculateInheritanceDepth(classInfo.TypeSymbol, method);

// Try to get the method's actual source location instead of the class location
var methodLocation = GetMethodLocation(method);
var filePath = methodLocation?.SourceTree?.FilePath ??
testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString() ??
// Use the TestAttribute's CallerFilePath and CallerLineNumber for accurate source location
// These are automatically captured when the [Test] attribute is applied
var filePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString() ??
classInfo.ClassSyntax.GetLocation().SourceTree?.FilePath ??
classInfo.ClassSyntax.SyntaxTree.FilePath;
var lineNumber = methodLocation?.GetLineSpan().StartLinePosition.Line + 1 ??

var lineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ??
classInfo.ClassSyntax.GetLocation().GetLineSpan().StartLinePosition.Line + 1;

var testMethodMetadata = new TestMethodMetadata
Expand All @@ -161,18 +161,6 @@ private static void GenerateInheritedTestSources(SourceProductionContext context
}
}

private static Location? GetMethodLocation(IMethodSymbol method)
{
// Try to get the method's original declaration location
var syntaxRef = method.DeclaringSyntaxReferences.FirstOrDefault();
if (syntaxRef != null)
{
var syntaxNode = syntaxRef.GetSyntax();
return syntaxNode.GetLocation();
}
return null;
}

private static int CalculateInheritanceDepth(INamedTypeSymbol testClass, IMethodSymbol testMethod)
{
// If the method is declared directly in the test class, depth is 0
Expand Down