nunit / nunit.analyzers

Roslyn analyzers for writing unit tests with NUnit
MIT License
89 stars 33 forks source link

AD0001 Crash: TestCase contains int instead of enum #165

Closed jcurl closed 5 years ago

jcurl commented 5 years ago

Version: NUnit.Analyzer-0.1.0-dev-00205 Visual Studio 2019 NUnit 2.6.7

When analyzing a project, the following crash occurred by the analyzer:

Severity    Code    Description Project File    Line    Suppression State   Detail Description
Warning AD0001  Analyzer 'NUnit.Analyzers.TestCaseUsage.TestCaseUsageAnalyzer' threw an exception of type 'System.ArgumentNullException' with message 'Value cannot be null.
Parameter name: type'.  HBAS.Automation.Trace.Dlt.V850Test      1   Active  Analyzer 'NUnit.Analyzers.TestCaseUsage.TestCaseUsageAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: HBAS.Automation.Trace.Dlt.V850Test
SyntaxTree: C:\Users\jcurl\Documents\Programming\HELIOS\helios.devfxio\framework\tracereader\TraceReader.Dlt\HBAS.Automation.Trace.Dlt.V850Test\Dlt\TraceLineV850ToDltTest.cs
SyntaxNode: TestCase(0xFF, DltType.LOG_ERROR ... [AttributeSyntax]@[7194..7227) (139,9)-(139,42)

System.ArgumentNullException: Value cannot be null.
Parameter name: type
   at System.ComponentModel.TypeDescriptor.GetDescriptor(Type type, String typeName)
   at System.ComponentModel.TypeDescriptor.GetConverter(Type type)
   at NUnit.Analyzers.Extensions.AttributeArgumentSyntaxExtensions.CanAssignTo(AttributeArgumentSyntax this, ITypeSymbol target, SemanticModel model)
   at NUnit.Analyzers.TestCaseUsage.TestCaseUsageAnalyzer.AnalyzePositionalArgumentsAndParameters(SyntaxNodeAnalysisContext context, ImmutableArray`1 attributePositionalArguments, ImmutableArray`1 methodParameters)
   at NUnit.Analyzers.TestCaseUsage.TestCaseUsageAnalyzer.AnalyzeAttribute(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__53`1.<ExecuteSyntaxNodeAction>b__53_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

The file that caused this issue is attached.

It seems to be related to:

[TestCase(SystemMessageTypes.Normal, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.VersionEvent, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.Reset, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.TimeStamp, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.BufferDump, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.BufferDumpEnd, DltType.LOG_INFO)]
[TestCase(SystemMessageTypes.BufferThreshold, DltType.LOG_WARN)]
[TestCase(SystemMessageTypes.BufferFull, DltType.LOG_WARN)]
[TestCase(SystemMessageTypes.BytesSkipped, DltType.LOG_ERROR)]
[TestCase(SystemMessageTypes.UnknownId, DltType.LOG_ERROR)]
[TestCase(SystemMessageTypes.MessageCorruption, DltType.LOG_ERROR)]
[TestCase(SystemMessageTypes.MemoryMessageCorruption, DltType.LOG_ERROR)]
[TestCase(SystemMessageTypes.MessageConversionError, DltType.LOG_ERROR)]
[TestCase(0xFF, DltType.LOG_ERROR)]
public void CheckDltType(SystemMessageTypes v850Type, DltType dltType)
{
    DltLineConfig config = new DltLineConfig();

    IV850ModuleNameTranslator moduleTranslator = new V850ShortModuleName();
    TraceLineV850ToDlt traceLineConverter = new TraceLineV850ToDlt(moduleTranslator, config);
    TraceLineDlt dltLine = traceLineConverter.CreateDltLine("LogText", "Custom text", TimeSpan.FromSeconds(11),
        "MOD_SYSTEM", DateTimeHelper.UnixStartDate, v850Type);

    Assert.That(dltType, Is.EqualTo(dltLine.Type));
}

in particular the line:

[TestCase(0xFF, DltType.LOG_ERROR)]

Note, this is a valid test case, as .NET treats enums as an int underlying.

TraceLineV850ToDltTest.zip

Dreamescaper commented 5 years ago

Will take a look into that.

stevenaw commented 5 years ago

Thanks @Dreamescaper I think the underlying null issue was probably a result of a recent change of mine

Dreamescaper commented 5 years ago

Null issue - yes. But before your changes it would report warning, which is not correct as well.

stevenaw commented 5 years ago

Fair. In any event, thanks for fixing it