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

False positive for Nunit1029 when only a type argument is use #832

Closed
cbersch opened this issue Jan 13, 2025 · 1 comment · Fixed by #833
Closed

False positive for Nunit1029 when only a type argument is use #832

cbersch opened this issue Jan 13, 2025 · 1 comment · Fixed by #833

Comments

@cbersch
Copy link
Contributor

cbersch commented Jan 13, 2025

The following code

using NUnit.Framework;

namespace NUnitTesting
{
    public sealed class GenericTestMethod
    {
        public static IEnumerable<TestCaseData> Source
            => [new TestCaseData { TypeArgs = [typeof(int)] }];

        [TestCaseSource(nameof(Source))]
        public void Generic<T>()
        {

        }
    }
}

raises an Nunit1029 error "The TestCaseSource provides '>0' parameter(s), but the Test methods expects '0' parameters", which shouldn't be the case.
Tested with NUnit.Analyzers version 4.6.0.

In my opinion, the check in https://github.com/nunit/nunit.analyzers/blob/master/src/nunit.analyzers/TestCaseSourceUsage/TestCaseSourceUsesStringAnalyzer.cs#L260

if (methodRequiredParameters + methodOptionalParameters < 1)

is valid only for non-generic methods and must be changed to

if (methodRequiredParameters + methodOptionalParameters < 1 && !testMethod.IsGenericMethod)

I could provide a PR for this.

@manfred-brands
Copy link
Member

For TestCaseData the analyzer doesn't know how many parameters are supplied and it assumes there should at least be one.

You are right that for generic methods the count isn't correct.

You are welcome to create a PR.

cbersch added a commit to cbersch/nunit.analyzers that referenced this issue Jan 13, 2025
cbersch added a commit to cbersch/nunit.analyzers that referenced this issue Jan 13, 2025
manfred-brands added a commit that referenced this issue Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants