diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs index 93ad4d06a4..9e04ee7391 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs @@ -5,24 +5,20 @@ namespace TUnit.Core; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public sealed class ClassDataSourceAttribute<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T> : DataSourceGeneratorAttribute where T : new() { - private DataGeneratorMetadata? _dataGeneratorMetadata; - public SharedType Shared { get; set; } = SharedType.None; public string Key { get; set; } = string.Empty; public override IEnumerable> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) { - _dataGeneratorMetadata = dataGeneratorMetadata; - yield return () => { - var item = ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + var item = ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .Get(Shared, dataGeneratorMetadata.TestClassType, Key); dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( context.TestContext, - _dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true, + dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true, Shared, Key, item); @@ -30,9 +26,9 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, - _dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true, + dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true, Shared, Key, item); @@ -40,23 +36,29 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item); + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item); }; dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item); + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => + if (Shared is SharedType.PerClass) { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).IfLastTestInClass(Shared); - }; + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); + }; + } - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => + if (Shared is SharedType.PerAssembly) { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).IfLastTestInAssembly(Shared); - }; + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInAssembly(context.Item2); + }; + } return item; }; diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs index 78b1f1fe26..8bce612144 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs @@ -10,24 +10,20 @@ public sealed class ClassDataSourceAttribute< where T1 : new() where T2 : new() { - private DataGeneratorMetadata? _dataGeneratorMetadata; - public SharedType[] Shared { get; set; } = [SharedType.None, SharedType.None, SharedType.None, SharedType.None, SharedType.None]; public string[] Keys { get; set; } = [string.Empty, string.Empty, string.Empty, string.Empty, string.Empty]; public override IEnumerable> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) { - _dataGeneratorMetadata = dataGeneratorMetadata; - yield return () => { ( (T1 T, SharedType SharedType, string Key), (T2 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys) ); @@ -35,14 +31,14 @@ public sealed class ClassDataSourceAttribute< { var testContext = context.TestContext; - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item2.SharedType, @@ -52,14 +48,14 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item2.SharedType, @@ -69,12 +65,12 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); @@ -82,31 +78,43 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item2.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass(context.Item2); + } }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item2.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } }; return ( diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs index f1dc88de66..09653866cf 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_3.cs @@ -12,15 +12,11 @@ public sealed class ClassDataSourceAttribute< where T2 : new() where T3 : new() { - private DataGeneratorMetadata? _dataGeneratorMetadata; - public SharedType[] Shared { get; set; } = [SharedType.None, SharedType.None, SharedType.None, SharedType.None, SharedType.None]; public string[] Keys { get; set; } = [string.Empty, string.Empty, string.Empty, string.Empty, string.Empty]; public override IEnumerable> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) { - _dataGeneratorMetadata = dataGeneratorMetadata; - yield return () => { ( @@ -28,11 +24,11 @@ public sealed class ClassDataSourceAttribute< (T2 T, SharedType SharedType, string Key), (T3 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys) ); @@ -40,21 +36,21 @@ public sealed class ClassDataSourceAttribute< { var testContext = context.TestContext; - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item3.SharedType, @@ -64,21 +60,21 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item3.SharedType, @@ -88,17 +84,17 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); @@ -106,40 +102,62 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item3.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item3.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } }; return ( diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs index b52a477980..b89b56a140 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_4.cs @@ -14,15 +14,11 @@ public sealed class ClassDataSourceAttribute< where T3 : new() where T4 : new() { - private DataGeneratorMetadata? _dataGeneratorMetadata; - public SharedType[] Shared { get; set; } = [SharedType.None, SharedType.None, SharedType.None, SharedType.None, SharedType.None]; public string[] Keys { get; set; } = [string.Empty, string.Empty, string.Empty, string.Empty, string.Empty]; public override IEnumerable> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) { - _dataGeneratorMetadata = dataGeneratorMetadata; - yield return () => { ( @@ -31,13 +27,13 @@ public sealed class ClassDataSourceAttribute< (T3 T, SharedType SharedType, string Key), (T4 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys) ); @@ -45,28 +41,28 @@ public sealed class ClassDataSourceAttribute< { var testContext = context.TestContext; - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item4.SharedType, @@ -76,28 +72,28 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item4.SharedType, @@ -107,22 +103,22 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); @@ -130,49 +126,79 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item3.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item4.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(3) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item3.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item4.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(3) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } }; return ( diff --git a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs index c2700785a6..a61bd92455 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_5.cs @@ -16,15 +16,11 @@ public sealed class ClassDataSourceAttribute< where T4 : new() where T5 : new() { - private DataGeneratorMetadata? _dataGeneratorMetadata; - public SharedType[] Shared { get; set; } = [SharedType.None, SharedType.None, SharedType.None, SharedType.None, SharedType.None]; public string[] Keys { get; set; } = [string.Empty, string.Empty, string.Empty, string.Empty, string.Empty]; public override IEnumerable> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) { - _dataGeneratorMetadata = dataGeneratorMetadata; - yield return () => { ( @@ -34,15 +30,15 @@ public sealed class ClassDataSourceAttribute< (T4 T, SharedType SharedType, string Key), (T5 T, SharedType SharedType, string Key) ) itemsWithMetadata = ( - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(0, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(1, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(2, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(3, dataGeneratorMetadata.TestClassType, Shared, Keys), - ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) + ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) .GetItemForIndex(4, dataGeneratorMetadata.TestClassType, Shared, Keys) ); @@ -50,35 +46,35 @@ public sealed class ClassDataSourceAttribute< { var testContext = context.TestContext; - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered( testContext, false, itemsWithMetadata.Item5.SharedType, @@ -88,35 +84,35 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart( context, false, itemsWithMetadata.Item5.SharedType, @@ -126,27 +122,27 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item5.SharedType, itemsWithMetadata.Item5.Key, itemsWithMetadata.Item5.T); @@ -154,58 +150,96 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item1.SharedType, itemsWithMetadata.Item1.Key, itemsWithMetadata.Item1.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item2.SharedType, itemsWithMetadata.Item2.Key, itemsWithMetadata.Item2.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item3.SharedType, itemsWithMetadata.Item3.Key, itemsWithMetadata.Item3.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item4.SharedType, itemsWithMetadata.Item4.Key, itemsWithMetadata.Item4.T); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd( + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd( itemsWithMetadata.Item5.SharedType, itemsWithMetadata.Item5.Key, itemsWithMetadata.Item5.T); }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item3.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item4.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInClass(itemsWithMetadata.Item5.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(3) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } + + if (Shared.ElementAtOrDefault(4) is SharedType.PerClass) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInClass(context.Item2); + } }; - dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) => + dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) => { - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item1.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item2.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item3.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item4.SharedType); - await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId) - .IfLastTestInAssembly(itemsWithMetadata.Item5.SharedType); + if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(2) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(3) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } + + if (Shared.ElementAtOrDefault(4) is SharedType.PerAssembly) + { + await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId) + .OnLastTestInAssembly(context.Item2); + } }; return ( diff --git a/TUnit.Core/Attributes/TestData/ClassDataSources.cs b/TUnit.Core/Attributes/TestData/ClassDataSources.cs index 01ba28c1cb..24a8fe4a74 100644 --- a/TUnit.Core/Attributes/TestData/ClassDataSources.cs +++ b/TUnit.Core/Attributes/TestData/ClassDataSources.cs @@ -161,20 +161,18 @@ public async ValueTask OnTestEnd(SharedType shared, string key, T? item) } } - public async ValueTask IfLastTestInClass(SharedType shared) + public async ValueTask OnLastTestInClass(TestContext testContext) { - if (shared == SharedType.PerClass) - { - await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(TestDataContainer.GetInstanceForType(typeof(T), () => default(T)!)); - } + var instance = TestDataContainer.GetInstanceForType(testContext.TestDetails.ClassType, () => default(T)!); + + await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(instance); } - public async ValueTask IfLastTestInAssembly(SharedType shared) + public async ValueTask OnLastTestInAssembly(TestContext testContext) { - if (shared == SharedType.PerAssembly) - { - await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(TestDataContainer.GetInstanceForType(typeof(T), () => default(T)!)); - } + var instance = TestDataContainer.GetInstanceForAssembly(testContext.TestDetails.ClassType.Assembly, () => default(T)!); + + await new Disposer(GlobalContext.Current.GlobalLogger).DisposeAsync(instance); } private static T Create() where T : new() diff --git a/TUnit.Core/Hooks/LastTestInAssemblyClassAdapter.cs b/TUnit.Core/Hooks/LastTestInAssemblyClassAdapter.cs index ea1e770161..7a381302a1 100644 --- a/TUnit.Core/Hooks/LastTestInAssemblyClassAdapter.cs +++ b/TUnit.Core/Hooks/LastTestInAssemblyClassAdapter.cs @@ -5,7 +5,7 @@ namespace TUnit.Core.Hooks; public class LastTestInAssemblyAdapter(ILastTestInAssemblyEventReceiver lastTestInAssemblyEventReceiver, TestContext testContext) : IExecutableHook { - public string Name => nameof(lastTestInAssemblyEventReceiver.IfLastTestInAssembly); + public string Name => nameof(lastTestInAssemblyEventReceiver.OnLastTestInAssembly); public MethodInfo MethodInfo => typeof(ILastTestInAssemblyEventReceiver).GetMethod(Name)!; public int Order => 0; @@ -16,7 +16,7 @@ public bool Execute(AssemblyHookContext context, CancellationToken cancellationT public async Task ExecuteAsync(AssemblyHookContext context, CancellationToken cancellationToken) { - await lastTestInAssemblyEventReceiver.IfLastTestInAssembly(context, testContext); + await lastTestInAssemblyEventReceiver.OnLastTestInAssembly(context, testContext); } public bool IsSynchronous => false; diff --git a/TUnit.Core/Hooks/LastTestInClassAdapter.cs b/TUnit.Core/Hooks/LastTestInClassAdapter.cs index c6e5e3e2f2..24954e0d3c 100644 --- a/TUnit.Core/Hooks/LastTestInClassAdapter.cs +++ b/TUnit.Core/Hooks/LastTestInClassAdapter.cs @@ -5,7 +5,7 @@ namespace TUnit.Core.Hooks; public class LastTestInClassAdapter(ILastTestInClassEventReceiver lastTestInClassEventReceiver, TestContext testContext) : IExecutableHook { - public string Name => nameof(lastTestInClassEventReceiver.IfLastTestInClass); + public string Name => nameof(lastTestInClassEventReceiver.OnLastTestInClass); public MethodInfo MethodInfo => typeof(ILastTestInClassEventReceiver).GetMethod(Name)!; public int Order => 0; @@ -16,7 +16,7 @@ public bool Execute(ClassHookContext context, CancellationToken cancellationToke public async Task ExecuteAsync(ClassHookContext context, CancellationToken cancellationToken) { - await lastTestInClassEventReceiver.IfLastTestInClass(context, testContext); + await lastTestInClassEventReceiver.OnLastTestInClass(context, testContext); } public bool IsSynchronous => false; diff --git a/TUnit.Core/Interfaces/ILastTestInAssemblyEventReceiver.cs b/TUnit.Core/Interfaces/ILastTestInAssemblyEventReceiver.cs index fd382af774..4c01c6bdf1 100644 --- a/TUnit.Core/Interfaces/ILastTestInAssemblyEventReceiver.cs +++ b/TUnit.Core/Interfaces/ILastTestInAssemblyEventReceiver.cs @@ -2,5 +2,5 @@ public interface ILastTestInAssemblyEventReceiver : IEventReceiver { - ValueTask IfLastTestInAssembly(AssemblyHookContext context, TestContext testContext); + ValueTask OnLastTestInAssembly(AssemblyHookContext context, TestContext testContext); } \ No newline at end of file diff --git a/TUnit.Core/Interfaces/ILastTestInClassEventReceiver.cs b/TUnit.Core/Interfaces/ILastTestInClassEventReceiver.cs index 1f422f6fc4..bde1a883f0 100644 --- a/TUnit.Core/Interfaces/ILastTestInClassEventReceiver.cs +++ b/TUnit.Core/Interfaces/ILastTestInClassEventReceiver.cs @@ -2,5 +2,5 @@ public interface ILastTestInClassEventReceiver : IEventReceiver { - ValueTask IfLastTestInClass(ClassHookContext context, TestContext testContext); + ValueTask OnLastTestInClass(ClassHookContext context, TestContext testContext); } \ No newline at end of file diff --git a/TUnit.Core/Interfaces/ILastTestInTestSessionEventReceiver.cs b/TUnit.Core/Interfaces/ILastTestInTestSessionEventReceiver.cs index 92c93fdc31..e17cdd5ad8 100644 --- a/TUnit.Core/Interfaces/ILastTestInTestSessionEventReceiver.cs +++ b/TUnit.Core/Interfaces/ILastTestInTestSessionEventReceiver.cs @@ -2,5 +2,5 @@ public interface ILastTestInTestSessionEventReceiver : IEventReceiver { - ValueTask IfLastTestInTestSession(TestSessionContext current, TestContext testContext); + ValueTask OnLastTestInTestSession(TestSessionContext current, TestContext testContext); } \ No newline at end of file diff --git a/TUnit.Core/TestContextEvents.cs b/TUnit.Core/TestContextEvents.cs index e075d79b6c..83cfaeacd2 100644 --- a/TUnit.Core/TestContextEvents.cs +++ b/TUnit.Core/TestContextEvents.cs @@ -43,17 +43,17 @@ ValueTask ITestSkippedEventReceiver.OnTestSkipped(TestContext testContext) return OnTestSkipped?.InvokeAsync(this, testContext) ?? ValueTask.CompletedTask; } - public ValueTask IfLastTestInClass(ClassHookContext context, TestContext testContext) + ValueTask ILastTestInClassEventReceiver.OnLastTestInClass(ClassHookContext context, TestContext testContext) { return OnLastTestInClass?.InvokeAsync(this, (context, testContext)) ?? ValueTask.CompletedTask; } - public ValueTask IfLastTestInAssembly(AssemblyHookContext context, TestContext testContext) + ValueTask ILastTestInAssemblyEventReceiver.OnLastTestInAssembly(AssemblyHookContext context, TestContext testContext) { return OnLastTestInAssembly?.InvokeAsync(this, (context, testContext)) ?? ValueTask.CompletedTask; } - public ValueTask IfLastTestInTestSession(TestSessionContext context, TestContext testContext) + ValueTask ILastTestInTestSessionEventReceiver.OnLastTestInTestSession(TestSessionContext context, TestContext testContext) { return OnLastTestInTestSession?.InvokeAsync(this, (context, testContext)) ?? ValueTask.CompletedTask; } diff --git a/TUnit.Engine/Hooks/AssemblyHookOrchestrator.cs b/TUnit.Engine/Hooks/AssemblyHookOrchestrator.cs index 18f506a5f1..a66559cff9 100644 --- a/TUnit.Engine/Hooks/AssemblyHookOrchestrator.cs +++ b/TUnit.Engine/Hooks/AssemblyHookOrchestrator.cs @@ -56,7 +56,9 @@ Assembly assembly yield break; } - foreach (var testEndEventsObject in testContext.GetLastTestInAssemblyEventObjects()) + var allTestContextsInAssembly = GetContext(assembly).AllTests; + + foreach (var testEndEventsObject in allTestContextsInAssembly.SelectMany(tc => tc.GetLastTestInAssemblyEventObjects())) { yield return new LastTestInAssemblyAdapter(testEndEventsObject, testContext); } diff --git a/TUnit.Engine/Services/SingleTestExecutor.cs b/TUnit.Engine/Services/SingleTestExecutor.cs index a18f4852be..ab1ddc6603 100644 --- a/TUnit.Engine/Services/SingleTestExecutor.cs +++ b/TUnit.Engine/Services/SingleTestExecutor.cs @@ -306,7 +306,7 @@ private async Task ExecuteStaticAfterHooks(DiscoveredTest test, TestContext test foreach (var testEndEventsObject in testContext.GetLastTestInTestSessionEventObjects()) { await RunHelpers.RunValueTaskSafelyAsync( - () => testEndEventsObject.IfLastTestInTestSession(TestSessionContext.Current!, testContext), + () => testEndEventsObject.OnLastTestInTestSession(TestSessionContext.Current!, testContext), cleanUpExceptions); } } diff --git a/TUnit.TestProject/ClassDataSourceDisposal/Repro.cs b/TUnit.TestProject/ClassDataSourceDisposal/Repro.cs new file mode 100644 index 0000000000..d5de86dbad --- /dev/null +++ b/TUnit.TestProject/ClassDataSourceDisposal/Repro.cs @@ -0,0 +1,69 @@ +namespace TUnit.TestProject.ClassDataSourceDisposal; + +public class ReproFixtureBase : IDisposable +{ + public ReproFixtureBase() + { + Console.WriteLine($"Constructing {GetType().Name}"); + } + + public void Dispose() + { + Console.WriteLine($"Disposing {GetType().Name}"); + } +} + +public class SharedTypeNoneFixture : ReproFixtureBase; +[ClassDataSource(Shared = SharedType.None)] +public class SharedTypeNoneTests(SharedTypeNoneFixture fixture) +{ + [Test] + public void Test() + { + Console.WriteLine($"Test using {fixture.GetType().Name}"); + } +} + +public class SharedTypePerClassFixture : ReproFixtureBase; +[ClassDataSource(Shared = SharedType.PerClass)] +public class SharedTypePerClassTests(SharedTypePerClassFixture fixture) +{ + [Test] + public void Test() + { + Console.WriteLine($"Test using {fixture.GetType().Name}"); + } +} + +public class SharedTypeKeyedFixture : ReproFixtureBase; +[ClassDataSource(Shared = SharedType.Keyed, Key = "Key")] +public class SharedTypeKeyedTests(SharedTypeKeyedFixture fixture) +{ + [Test] + public void Test() + { + Console.WriteLine($"Test using {fixture.GetType().Name}"); + } +} + +public class SharedTypePerAssemblyFixture : ReproFixtureBase; +[ClassDataSource(Shared = SharedType.PerAssembly)] +public class SharedTypePerAssemblyTests(SharedTypePerAssemblyFixture fixture) +{ + [Test] + public void Test() + { + Console.WriteLine($"Test using {fixture.GetType().Name}"); + } +} + +public class SharedTypePerPerTestSessionFixture : ReproFixtureBase; +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class SharedTypePerTestSessionTests(SharedTypePerPerTestSessionFixture fixture) +{ + [Test] + public void Test() + { + Console.WriteLine($"Test using {fixture.GetType().Name}"); + } +} \ No newline at end of file