Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 4 additions & 22 deletions src/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,26 +1135,8 @@ private static void EmitConcreteInvokeBody(CodeWriter writer, TestMethodMetadata
/// </summary>
private static void EmitConcreteAttributeFactory(CodeWriter writer, TestMethodMetadata testMethod, ConcreteInstantiation entry)
{
// Build filtered attributes
var filteredAttributes = new List<AttributeData>();

foreach (var attr in testMethod.MethodSymbol.GetAttributes())
{
if (attr.AttributeClass?.Name == "ArgumentsAttribute" && entry.SpecificArgumentsAttribute != null)
{
if (AreSameAttribute(attr, entry.SpecificArgumentsAttribute))
{
filteredAttributes.Add(attr);
}
}
else
{
filteredAttributes.Add(attr);
}
}

filteredAttributes.AddRange(testMethod.TypeSymbol.GetAttributesIncludingBaseTypes());
filteredAttributes.AddRange(testMethod.TypeSymbol.ContainingAssembly.GetAttributes());
// Data source attributes are emitted separately for the selected row by TestBuilder.
var filteredAttributes = GetTestAttributes(testMethod).ToList();

writer.AppendLine("return new global::System.Attribute[]");
writer.AppendLine("{");
Expand Down Expand Up @@ -2846,9 +2828,9 @@ private static void WriteSynchronousExceptionHandler(CodeWriter writer)
private static IEnumerable<AttributeData> GetTestAttributes(TestMethodMetadata testMethod)
{
return testMethod.MethodSymbol.GetAttributes()
.Where(a => !DataSourceAttributeHelper.IsDataSourceAttribute(a.AttributeClass))
.Concat(testMethod.TypeSymbol.GetAttributesIncludingBaseTypes())
.Concat(testMethod.TypeSymbol.ContainingAssembly.GetAttributes());
.Concat(testMethod.TypeSymbol.ContainingAssembly.GetAttributes())
.Where(a => !DataSourceAttributeHelper.IsDataSourceAttribute(a.AttributeClass));
}

private static (bool HasCancellationToken, IParameterSymbol[] ParametersFromArgs) ParseInvokerParameters(IMethodSymbol methodSymbol)
Expand Down
5 changes: 5 additions & 0 deletions src/TUnit.Engine/Building/TestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,11 @@ private async ValueTask<TestContext> CreateTestContextAsync(string testId, TestM
attributes = [..attributes, (Attribute)testBuilderContext.DataSourceAttribute];
}

if (testBuilderContext.ClassDataSourceAttribute != null && testBuilderContext.ClassDataSourceAttribute is not NoDataSource)
{
attributes = [..attributes, (Attribute)testBuilderContext.ClassDataSourceAttribute];
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

var testDetails = new TestDetails(attributes)
{
TestId = testId,
Expand Down
29 changes: 26 additions & 3 deletions src/TUnit.Engine/Discovery/ReflectionAttributeExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,32 @@ public static Attribute[] GetAllAttributes(Type testClass, MethodInfo testMethod

// Add in reverse order of precedence so method attributes come first
// This ensures ScopedAttributeFilter will keep method-level attributes over class/assembly
attributes.AddRange(key.Item2.GetCustomAttributes());
attributes.AddRange(key.Item1.GetCustomAttributes());
attributes.AddRange(key.Item1.Assembly.GetCustomAttributes());
// Method-level data source attributes are left out, matching the source generator. The
// data source that produced each test case is appended per test by the TestBuilder, so
// per-row settings such as [Arguments(Skip = "...")] only apply to their own row.
foreach (var attribute in key.Item2.GetCustomAttributes())
{
if (attribute is not IDataSourceAttribute)
Comment thread
thomhurst marked this conversation as resolved.
Resolved
Comment thread
thomhurst marked this conversation as resolved.
Resolved
Comment thread
thomhurst marked this conversation as resolved.
Resolved
Comment thread
thomhurst marked this conversation as resolved.
Resolved
Comment thread
thomhurst marked this conversation as resolved.
Resolved
{
attributes.Add(attribute);
}
}

foreach (var attribute in key.Item1.GetCustomAttributes())
{
if (attribute is not IDataSourceAttribute)
{
attributes.Add(attribute);
}
}

foreach (var attribute in key.Item1.Assembly.GetCustomAttributes())
{
if (attribute is not IDataSourceAttribute)
{
attributes.Add(attribute);
}
}

return attributes.ToArray();
});
Expand Down
7 changes: 3 additions & 4 deletions src/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ private static TestMetadata CreateFailedMethodGenericMetadata(
FilePath = ExtractFilePath(method) ?? "Unknown",
LineNumber = ExtractLineNumber(method) ?? 0,
MethodMetadata = ReflectionMetadataBuilder.CreateMethodMetadata(type, method),
AttributeFactory = () => method.GetCustomAttributes().ToArray(),
AttributeFactory = () => ReflectionAttributeExtractor.GetAllAttributes(type, method),
DataSources = [],
ClassDataSources = [],
PropertyDataSources = []
Expand Down Expand Up @@ -1432,8 +1432,7 @@ private static TestMetadata CreateFailedTestMetadata(
FilePath = ExtractFilePath(method) ?? "Unknown",
LineNumber = ExtractLineNumber(method) ?? 0,
MethodMetadata = ReflectionMetadataBuilder.CreateMethodMetadata(type, method),
AttributeFactory = () => method.GetCustomAttributes()
.ToArray(),
AttributeFactory = () => ReflectionAttributeExtractor.GetAllAttributes(type, method),
DataSources =
[
],
Expand Down Expand Up @@ -2462,7 +2461,7 @@ private static TestMetadata CreateFailedTestMetadataForDynamicBuilder(
FilePath = ExtractFilePath(method) ?? "Unknown",
LineNumber = ExtractLineNumber(method) ?? 0,
MethodMetadata = ReflectionMetadataBuilder.CreateMethodMetadata(type, method),
AttributeFactory = () => method.GetCustomAttributes().ToArray(),
AttributeFactory = () => ReflectionAttributeExtractor.GetAllAttributes(type, method),
DataSources = [],
ClassDataSources = [],
PropertyDataSources = []
Expand Down
4 changes: 3 additions & 1 deletion src/TUnit.Engine/Services/TestRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ public async Task<TestVariantInfo> CreateTestVariant(
}

var lambda = Expression.Lambda<Func<T, Task>>(body, parameter);
var attributes = new List<Attribute>(currentContext.Metadata.TestDetails.GetAllAttributes());
var attributes = currentContext.Metadata.TestDetails.GetAllAttributes()
.Where(static attribute => attribute is not IDataSourceAttribute)
.ToList();

var discoveryResult = new DynamicDiscoveryResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ internal static class TUnit_TestProject_ClassAndMethodArgumentsTests__TestSource
{
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.ArgumentsAttribute("1"),
new global::TUnit.Core.ArgumentsAttribute("2")
new global::TUnit.Core.TestAttribute()
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ internal static class TUnit_TestProject_ClassDataSourceDrivenTests2__TestSource
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.ClassDataSourceAttribute<global::TUnit.TestProject.ClassDataSourceDrivenTests2.Derived1>(),
new global::TUnit.Core.ClassDataSourceAttribute<global::TUnit.TestProject.ClassDataSourceDrivenTests2.Derived2>()
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ internal static class TUnit_TestProject_ClassTupleDataSourceDrivenTests__TestSou
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.MethodDataSourceAttribute("TupleMethod"),
new global::TUnit.Core.MethodDataSourceAttribute("NamedTupleMethod")
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ internal static class TUnit_TestProject_DataSourceClassCombinedWithDataSourceMet
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.MethodDataSourceAttribute(typeof(global::TUnit.TestProject.CommonTestData), "One"),
new global::TUnit.Core.MethodDataSourceAttribute(typeof(global::TUnit.TestProject.CommonTestData), "Two"),
new global::TUnit.Core.MethodDataSourceAttribute(typeof(global::TUnit.TestProject.CommonTestData), "Three")
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ internal static class TUnit_TestProject_DataSourceGeneratorTests__TestSource
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.TestProject.Attributes.AutoFixtureGeneratorAttribute<int, string, bool>(),
new global::TUnit.TestProject.Attributes.AutoFixtureGeneratorAttribute()
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(object), "expected", n
return new global::System.Attribute[]
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.MethodDataSourceAttribute("AggregateBy_Numeric_TestData"),
new global::TUnit.Core.MethodDataSourceAttribute("AggregateBy_String_TestData"),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
};
}
Expand All @@ -83,8 +81,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(object), "expected", n
return new global::System.Attribute[]
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.MethodDataSourceAttribute("AggregateBy_Numeric_TestData"),
new global::TUnit.Core.MethodDataSourceAttribute("AggregateBy_String_TestData"),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "number", new gl
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.MethodDataSourceAttribute("GetNumbers"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand Down Expand Up @@ -205,7 +204,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "number", new gl
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.MethodDataSourceAttribute("GetNumbers"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand Down Expand Up @@ -468,8 +466,7 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(string), "input", new
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetStrings")
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double))
};
}
case 1:
Expand All @@ -479,8 +476,7 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(string), "input", new
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetStrings")
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double))
};
}
default:
Expand Down Expand Up @@ -1303,7 +1299,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(bool), "flag", new glo
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetBooleans"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand All @@ -1316,7 +1311,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(bool), "flag", new glo
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetBooleans"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand Down Expand Up @@ -1412,7 +1406,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(bool), "flag", new glo
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetBooleans"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand All @@ -1425,7 +1418,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(bool), "flag", new glo
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(int)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(double)),
new global::TUnit.Core.MethodDataSourceAttribute("GetBooleans"),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeA)),
new global::TUnit.Core.GenerateGenericTestAttribute(typeof(global::TUnit.TestProject.Bugs._4431.TypeB))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ internal static class TUnit_TestProject_Bugs__1589_MyTests__TestSource
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.ClassDataSourceAttribute<global::TUnit.TestProject.Bugs._1589.MyFixture>()
{Shared = global::TUnit.Core.SharedType.None,}
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ internal static class TUnit_TestProject_Bugs__1594_MyTests__TestSource
return
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass),
new global::TUnit.Core.ClassDataSourceAttribute<global::TUnit.TestProject.Bugs._1594.MyFixture>()
{Shared = global::TUnit.Core.SharedType.None,}
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
];
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new glo
return new global::System.Attribute[]
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.ArgumentsAttribute(1),
new global::TUnit.Core.ArgumentsAttribute(2),
new global::TUnit.Core.ArgumentsAttribute(3),
new global::TUnit.Core.RepeatAttribute(10),
new global::TUnit.Core.InheritsTestsAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
Expand Down Expand Up @@ -277,9 +274,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new glo
return new global::System.Attribute[]
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.ArgumentsAttribute(1),
new global::TUnit.Core.ArgumentsAttribute(2),
new global::TUnit.Core.ArgumentsAttribute(3),
new global::TUnit.Core.RepeatAttribute(10),
new global::TUnit.Core.InheritsTestsAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
Expand Down Expand Up @@ -372,9 +366,6 @@ global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new glo
return new global::System.Attribute[]
{
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.ArgumentsAttribute(1),
new global::TUnit.Core.ArgumentsAttribute(2),
new global::TUnit.Core.ArgumentsAttribute(3),
new global::TUnit.Core.RepeatAttribute(10),
new global::TUnit.Core.InheritsTestsAttribute(),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
Expand Down
Loading
Loading