Skip to content

Commit

Permalink
TestCaseSource now ignore all classes derived from TestCaseParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Feb 22, 2023
1 parent da32943 commit 58bfb8f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;

namespace NUnit.Analyzers.Tests.Constants
{
Expand Down Expand Up @@ -140,6 +141,7 @@ public sealed class NUnitFrameworkConstantsTests
(nameof(NUnitFrameworkConstants.FullNameOfTypeISimpleTestBuilder), typeof(ISimpleTestBuilder)),
(nameof(NUnitFrameworkConstants.FullNameOfTypeIParameterDataSource), typeof(IParameterDataSource)),
(nameof(NUnitFrameworkConstants.FullNameOfTypeTestCaseData), typeof(TestCaseData)),
(nameof(NUnitFrameworkConstants.FullNameOfTypeTestCaseParameters), typeof(TestCaseParameters)),

(nameof(NUnitFrameworkConstants.FullNameOfTypeOneTimeSetUpAttribute), typeof(OneTimeSetUpAttribute)),
(nameof(NUnitFrameworkConstants.FullNameOfTypeOneTimeTearDownAttribute), typeof(OneTimeTearDownAttribute)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ static IEnumerable<object> TestData()
[TestCase("IEnumerable", "object", "System.Collections")]
[TestCase("IEnumerable<object>", "object", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseData>", "TestCaseData", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseParameters>", "TestCaseParameters", "System.Collections.Generic; using NUnit.Framework.Internal")]
[TestCase("IEnumerable<int>", "int", "System.Collections.Generic")]
public void NoIssueIsRaisedWhenOneParameterIsExpectedAndTypeCannotBeDetermined(string enumerableType, string testCaseType, string collections)
{
Expand All @@ -681,6 +682,7 @@ public void ShortName(int n)
[TestCase("IEnumerable", "object", "System.Collections")]
[TestCase("IEnumerable<object>", "object", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseData>", "TestCaseData", "System.Collections.Generic")]
[TestCase("IEnumerable<TestCaseParameters>", "TestCaseParameters", "System.Collections.Generic; using NUnit.Framework.Internal")]
public void NoIssueIsRaisedWhenMultipleParameterAreExpectedAndTypeCannotBeDetermined(string enumerableType, string testCaseType, string collections)
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@"
Expand Down
1 change: 1 addition & 0 deletions src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public static class NUnitFrameworkConstants
public const string FullNameOfTypeValueSourceAttribute = "NUnit.Framework.ValueSourceAttribute";
public const string FullNameOfTypeIParameterDataSource = "NUnit.Framework.Interfaces.IParameterDataSource";
public const string FullNameOfTypeTestCaseData = "NUnit.Framework.TestCaseData";
public const string FullNameOfTypeTestCaseParameters = "NUnit.Framework.Internal.TestCaseParameters";

public const string FullNameOfTypeOneTimeSetUpAttribute = "NUnit.Framework.OneTimeSetUpAttribute";
public const string FullNameOfTypeOneTimeTearDownAttribute = "NUnit.Framework.OneTimeTearDownAttribute";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context)
var (methodRequiredParameters, methodOptionalParameters, methodParamsParameters) = testMethod.GetParameterCounts();

if (elementType.SpecialType != SpecialType.System_String && (elementType.SpecialType == SpecialType.System_Object || elementType.IsIEnumerable(out _) ||
SymbolEqualityComparer.Default.Equals(elementType, context.SemanticModel.Compilation.GetTypeByMetadataName(NUnitFrameworkConstants.FullNameOfTypeTestCaseData))))
IsOrDerivesFrom(elementType, context.SemanticModel.Compilation.GetTypeByMetadataName(NUnitFrameworkConstants.FullNameOfTypeTestCaseParameters))))
{
// We only know that there is 1 or (likely) more parameters.
// The object could hide an array, possibly with a variable number of elements: TestCaseData.Argument.
Expand Down Expand Up @@ -306,5 +306,27 @@ private static void ReportIfParametersSupplied(
kind));
}
}

private static bool IsOrDerivesFrom(ITypeSymbol type, ITypeSymbol? baseType)
{
if (baseType is null)
{
return false;
}

ITypeSymbol? typeAtHand = type;
do
{
if (SymbolEqualityComparer.Default.Equals(typeAtHand, baseType))
{
return true;
}

typeAtHand = typeAtHand.BaseType;
}
while (typeAtHand is not null);

return false;
}
}
}

0 comments on commit 58bfb8f

Please sign in to comment.