Skip to content

Commit 3df6038

Browse files
authored
fix: enhance enum exclusion handling in MatrixDataSourceAttribute and add related tests (#3322)
1 parent e3e88bf commit 3df6038

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

TUnit.Core/Attributes/TestData/MatrixDataSourceAttribute.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ private bool IsExcluded(object?[] exclusion, IEnumerable<object?> row)
6666
var exclusionValue = exclusion[i];
6767
var rowValue = rowArray[i];
6868

69-
// Handle enum to underlying type conversion
70-
if (exclusionValue != null && exclusionValue.GetType().IsEnum && rowValue != null)
69+
// Handle enum to underlying type conversion for both values
70+
if (exclusionValue != null && exclusionValue.GetType().IsEnum)
7171
{
7272
exclusionValue = Convert.ChangeType(exclusionValue, Enum.GetUnderlyingType(exclusionValue.GetType()));
7373
}
7474

75+
if (rowValue != null && rowValue.GetType().IsEnum)
76+
{
77+
rowValue = Convert.ChangeType(rowValue, Enum.GetUnderlyingType(rowValue.GetType()));
78+
}
79+
7580
if (!Equals(exclusionValue, rowValue))
7681
{
7782
return false;

TUnit.TestProject/MatrixExclusionBugTest.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,65 @@ public async Task Should_Filter_By_Status(Status status)
3232
}
3333
await Task.CompletedTask;
3434
}
35-
35+
3636
[Test]
3737
[MatrixDataSource]
3838
[MatrixExclusion(Status.Draft)] // This should work with the base attribute
3939
public async Task Should_Filter_By_Status_Base(Status status)
4040
{
4141
// Should generate 4 tests (all statuses except Draft)
42-
// If this test runs with Status.Draft, the bug is NOT fixed
42+
// If this test runs with Status.Draft, the bug is NOT fixed
4343
if (status == Status.Draft)
4444
{
4545
throw new InvalidOperationException("Draft status should have been excluded but was not!");
4646
}
4747
await Task.CompletedTask;
4848
}
49+
50+
// Issue #3320: Test Matrix attribute with explicit enum values + MatrixExclusion
51+
[Test]
52+
[MatrixDataSource]
53+
[MatrixExclusion(Status.Draft)]
54+
public async Task Matrix_With_Explicit_Enums_Should_Respect_Exclusion(
55+
[Matrix(Status.Draft, Status.Pending, Status.Published)] Status status)
56+
{
57+
// Should generate 2 tests (Pending and Published only, Draft is excluded)
58+
await Assert.That(status).IsNotEqualTo(Status.Draft);
59+
}
60+
61+
// Issue #3320: Test with multiple enum parameters and exclusion
62+
[Test]
63+
[MatrixDataSource]
64+
[MatrixExclusion(Status.Draft, Status.Pending)]
65+
public async Task Multiple_Enum_Matrix_With_Exclusion(
66+
[Matrix(Status.Draft, Status.Published)] Status status1,
67+
[Matrix(Status.Pending, Status.Archived)] Status status2)
68+
{
69+
// Should exclude (Draft, Pending) combination
70+
// Should generate 3 tests: (Draft,Archived), (Published,Pending), (Published,Archived)
71+
await Assert.That((status1, status2)).IsNotEqualTo((Status.Draft, Status.Pending));
72+
}
73+
74+
// Issue #3320: Test with int cast exclusion (this was already working)
75+
[Test]
76+
[MatrixDataSource]
77+
[MatrixExclusion((int)Status.Draft)]
78+
public async Task Matrix_Enum_With_Int_Exclusion(
79+
[Matrix(Status.Draft, Status.Pending, Status.Published)] Status status)
80+
{
81+
// Should generate 2 tests (Pending and Published only)
82+
await Assert.That(status).IsNotEqualTo(Status.Draft);
83+
}
84+
85+
// Issue #3320: Test mixing auto-generated and explicit Matrix enums
86+
[Test]
87+
[MatrixDataSource]
88+
[MatrixExclusion(Status.Draft, Status.Pending)]
89+
public async Task Mixed_Auto_And_Explicit_Matrix_Enums(
90+
Status autoGenerated,
91+
[Matrix(Status.Pending, Status.Published)] Status explicitMatrix)
92+
{
93+
// Should exclude (Draft, Pending) combination
94+
await Assert.That((autoGenerated, explicitMatrix)).IsNotEqualTo((Status.Draft, Status.Pending));
95+
}
4996
}

0 commit comments

Comments
 (0)