Skip to content

Commit

Permalink
Merge pull request #524 from manfred-brands/issue523_TestCaseData
Browse files Browse the repository at this point in the history
TestCaseSource now ignore all classes derived from TestCaseParameters
  • Loading branch information
mikkelbu authored Feb 22, 2023
2 parents da32943 + 58bfb8f commit a3b4672
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 a3b4672

Please sign in to comment.