Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Nunit1029 for generic test method (#832) #833

Merged
Merged
Show file tree
Hide file tree
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
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
Loading