Skip to content

Commit e117ffd

Browse files
authored
Merge pull request #511 from manfred-brands/issue509_TestCaseTypeMismatch
Do not report errors on TestCase parameters types that are invalid.
2 parents 7e40c37 + 0426e90 commit e117ffd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ public void Test(char a) { }
355355
RoslynAssert.Diagnostics(this.analyzer, expectedDiagnostic, testCode);
356356
}
357357

358+
[Test]
359+
public void AnalyzeArgumentHasTypeNotAllowedInAttributes()
360+
{
361+
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
362+
class AnalyzeArgumentHasTypeNotAllowedInAttributes
363+
{
364+
[TestCase(1.0m)]
365+
public void Test(decimal d) { }
366+
}");
367+
RoslynAssert.Valid(this.analyzer, testCode, Settings.Default.WithAllowedCompilerDiagnostics(AllowedCompilerDiagnostics.WarningsAndErrors));
368+
}
369+
358370
[Test]
359371
public void AnalyzeWhenArgumentPassesNullToValueType()
360372
{

src/nunit.analyzers/TestCaseUsage/TestCaseUsageAnalyzer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ private static void AnalyzePositionalArgumentsAndParameters(
179179
for (var i = 0; i < attributePositionalArguments.Length; i++)
180180
{
181181
var attributeArgument = attributePositionalArguments[i];
182+
183+
// If the compiler detects an illegal constant, we shouldn't check it.
184+
// Unfortunately the constant 'null' is marked as Error with a null type.
185+
if (attributeArgument.Kind == TypedConstantKind.Error && attributeArgument.Type is not null)
186+
continue;
187+
182188
var (methodParameterType, methodParameterName, methodParameterParamsType) =
183189
TestCaseUsageAnalyzer.GetParameterType(methodParameters, i);
184190

0 commit comments

Comments
 (0)