-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/CallInfoAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
internal class CallInfoAnalyzer : AbstractDiagnosticAnalyzer | ||
{ | ||
public CallInfoAnalyzer() | ||
: base(new DiagnosticDescriptorsProvider()) | ||
{ | ||
} | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptorsProvider.CallInfoArgumentOutOfRange); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression); | ||
} | ||
|
||
private void AnalyzeInvocation(SyntaxNodeAnalysisContext syntaxNodeContext) | ||
{ | ||
var invocationExpression = syntaxNodeContext.Node; | ||
var methodSymbolInfo = syntaxNodeContext.SemanticModel.GetSymbolInfo(invocationExpression); | ||
|
||
if (methodSymbolInfo.Symbol?.Kind != SymbolKind.Method) | ||
{ | ||
return; | ||
} | ||
|
||
var methodSymbol = (IMethodSymbol)methodSymbolInfo.Symbol; | ||
if (IsCallInfoMethod(methodSymbol) == false) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
private bool IsCallInfoMethod(IMethodSymbol methodSymbol) | ||
{ | ||
return methodSymbol.ContainingType.ToString() == "NSubstitute.Core.CallInfo"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...yzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; | ||
using NSubstitute.Analyzers.Shared; | ||
using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; | ||
using Xunit; | ||
|
||
namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.CallInfoAnalyzerTests | ||
{ | ||
public class CallInfoAnalyzerTests : CSharpDiagnosticVerifier | ||
{ | ||
[Fact] | ||
public async Task ReportsDiagnostic_WhenAccessingArgumentOutOfBounds() | ||
{ | ||
var source = @"using System; | ||
using NSubstitute; | ||
namespace MyNamespace | ||
{ | ||
public interface Foo | ||
{ | ||
int Bar(int x); | ||
} | ||
public class FooTests | ||
{ | ||
public void Test() | ||
{ | ||
var substitute = NSubstitute.Substitute.For<Foo>(); | ||
substitute.Bar(Arg.Any<int>()).Returns(callInfo => callInfo.ArgAt<int>(1)); | ||
} | ||
} | ||
}"; | ||
var expectedDiagnostic = new DiagnosticResult | ||
{ | ||
Id = DiagnosticIdentifiers.CallInfoArgumentOutOfRange, | ||
Severity = DiagnosticSeverity.Warning, | ||
Message = "There is no argument at position 1", | ||
Locations = new[] | ||
{ | ||
new DiagnosticResultLocation(18, 13) | ||
} | ||
}; | ||
|
||
await VerifyDiagnostic(source, expectedDiagnostic); | ||
} | ||
|
||
protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() | ||
{ | ||
return new CallInfoAnalyzer(); | ||
} | ||
} | ||
} |