Skip to content

Commit

Permalink
Fix Nunit1029 for generic test method (nunit#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbersch committed Jan 13, 2025
1 parent f4a5549 commit c87166b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,30 @@ static IEnumerable<object> TestData()
RoslynAssert.Valid(analyzer, testCode);
}

#if NUNIT4
[Test]
public void AnalyzeWhenNumberOfParametersOfGenericTestIsNotEvidentFromTestSource()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
[TestFixture]
public class AnalyzeWhenTestSourceProvidesDataWithSingleTypeArgs
{
[TestCaseSource(nameof(TestData))]
public void Method<T>()
{
Assert.That(typeof(T).IsValueType, Is.True);
}
static IEnumerable<TestCaseData> TestData()
{
yield return new TestCaseData { TypeArgs = new Type[] { typeof(int) } };
}
}", additionalUsings: "using System.Collections.Generic;");

RoslynAssert.Valid(analyzer, testCode);
}
#endif

[TestCase("IEnumerable", "object", "System.Collections")]
[TestCase("IEnumerable<object>", "object", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseData>", "TestCaseData", "System.Collections.Generic")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ private static void AnalyzeAttribute(
var hasCancelAfterAttribute = testMethod.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType)) ||
testMethod.ContainingType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, cancelAfterType));

var (methodRequiredParameters, methodOptionalParameters, methodParamsParameters) = testMethod.GetParameterCounts(hasCancelAfterAttribute, cancellationTokenType);
var (methodRequiredParameters, methodOptionalParameters, _) = testMethod.GetParameterCounts(hasCancelAfterAttribute, cancellationTokenType);

if (elementType.SpecialType != SpecialType.System_String && (elementType.SpecialType == SpecialType.System_Object || elementType.IsIEnumerable(out _) ||
IsOrDerivesFrom(elementType, context.SemanticModel.Compilation.GetTypeByMetadataName(NUnitFrameworkConstants.FullNameOfTypeTestCaseParameters))))
{
// We only know that there is 1 or (likely) more parameters.
// For non-generic methods we only know that there is 1 or (likely) more parameters, for generic methods we even don't know this for sure.
// The object could hide an array, possibly with a variable number of elements: TestCaseData.Argument.
// Potentially we could examine the body of the TestCaseSource to see if we can determine the exact amount.
// For complex method that is certainly beyond the scope of this.
if (methodRequiredParameters + methodOptionalParameters < 1)
if (methodRequiredParameters + methodOptionalParameters < 1 && !testMethod.IsGenericMethod)
{
context.ReportDiagnostic(Diagnostic.Create(
mismatchInNumberOfTestMethodParameters,
Expand Down

0 comments on commit c87166b

Please sign in to comment.