Skip to content
Merged
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
42 changes: 26 additions & 16 deletions TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,18 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
MethodDeclarationSyntax methodSyntax,
AttributeData testAttribute)
{
// Prioritize TestAttribute's File/Line from [CallerFilePath]/[CallerLineNumber] first
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
if (!string.IsNullOrEmpty(attrFilePath))
{
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ?? 0;
if (attrLineNumber > 0)
{
return (attrFilePath!, attrLineNumber);
}
}

// Fall back to method syntax location
var methodLocation = methodSyntax.GetLocation();
var filePath = methodLocation.SourceTree?.FilePath;
if (!string.IsNullOrEmpty(filePath))
Expand All @@ -2308,14 +2320,7 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
return (filePath!, lineNumber);
}

var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
if (!string.IsNullOrEmpty(attrFilePath))
{
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ??
methodLocation.GetLineSpan().StartLinePosition.Line + 1;
return (attrFilePath!, attrLineNumber);
}

// Final fallback
filePath = methodSyntax.SyntaxTree.FilePath ?? "";
var fallbackLineNumber = methodLocation.GetLineSpan().StartLinePosition.Line + 1;
return (filePath, fallbackLineNumber);
Expand All @@ -2326,6 +2331,18 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
AttributeData testAttribute,
InheritsTestsClassMetadata classInfo)
{
// Prioritize TestAttribute's File/Line from [CallerFilePath]/[CallerLineNumber] first
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
if (!string.IsNullOrEmpty(attrFilePath))
{
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ?? 0;
if (attrLineNumber > 0)
{
return (attrFilePath!, attrLineNumber);
}
}

// Fall back to method symbol location
var methodLocation = method.Locations.FirstOrDefault();
if (methodLocation != null && methodLocation.IsInSource)
{
Expand All @@ -2337,14 +2354,7 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
}
}

var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
if (!string.IsNullOrEmpty(attrFilePath))
{
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ??
classInfo.ClassSyntax.GetLocation().GetLineSpan().StartLinePosition.Line + 1;
return (attrFilePath!, attrLineNumber);
}

// Final fallback to class location
var classLocation = classInfo.ClassSyntax.GetLocation();
var derivedFilePath = classLocation.SourceTree?.FilePath ?? classInfo.ClassSyntax.SyntaxTree.FilePath ?? "";
var derivedLineNumber = classLocation.GetLineSpan().StartLinePosition.Line + 1;
Expand Down
Loading