Skip to content

Commit

Permalink
[GH-30] - Additional set of tests for csharp CallInfoAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Aug 24, 2018
1 parent cb6974e commit 887b494
Show file tree
Hide file tree
Showing 12 changed files with 5,517 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ protected AbstractCallInfoAnalyzer(IDiagnosticDescriptorsProvider diagnosticDesc
{
}

private static readonly ImmutableHashSet<string> MethodNames = ImmutableHashSet.Create(
MetadataNames.NSubstituteReturnsMethod,
MetadataNames.NSubstituteReturnsForAnyArgsMethod);
private static readonly ImmutableDictionary<string, string> MethodNames = new Dictionary<string, string>()
{
[MetadataNames.NSubstituteReturnsMethod] = MetadataNames.NSubstituteSubstituteExtensionsFullTypeName,
[MetadataNames.NSubstituteReturnsForAnyArgsMethod] = MetadataNames.NSubstituteSubstituteExtensionsFullTypeName,
[MetadataNames.NSubstituteThrowsMethod] = MetadataNames.NSubstituteExceptionExtensionsFullTypeName,
[MetadataNames.NSubstituteThrowsForAnyArgsMethod] = MetadataNames.NSubstituteExceptionExtensionsFullTypeName
}.ToImmutableDictionary();

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
DiagnosticDescriptorsProvider.CallInfoArgumentOutOfRange,
Expand Down Expand Up @@ -223,7 +227,8 @@ private bool SupportsCallInfo(SyntaxNodeAnalysisContext syntaxNodeContext, TInvo
var argumentsForAnalysis = methodSymbol.MethodKind == MethodKind.ReducedExtension
? allArguments
: allArguments.Skip(1);
if (MethodNames.Contains(methodSymbol.Name) == false)

if (MethodNames.TryGetValue(methodSymbol.Name, out var typeName) == false)
{
return false;
}
Expand All @@ -232,7 +237,7 @@ private bool SupportsCallInfo(SyntaxNodeAnalysisContext syntaxNodeContext, TInvo

var supportsCallInfo =
symbol.Symbol?.ContainingAssembly?.Name.Equals(MetadataNames.NSubstituteAssemblyName, StringComparison.OrdinalIgnoreCase) == true &&
symbol.Symbol?.ContainingType?.ToString().Equals(MetadataNames.NSubstituteSubstituteExtensionsFullTypeName, StringComparison.OrdinalIgnoreCase) == true;
symbol.Symbol?.ContainingType?.ToString().Equals(typeName, StringComparison.OrdinalIgnoreCase) == true;

return supportsCallInfo && IsCalledViaDelegate(syntaxNodeContext.SemanticModel, syntaxNodeContext.SemanticModel.GetTypeInfo(argumentsForAnalysis.First()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/NSubstitute.Analyzers.Shared/MetadataNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ internal class MetadataNames
{
public const string NSubstituteAssemblyName = "NSubstitute";
public const string NSubstituteSubstituteExtensionsFullTypeName = "NSubstitute.SubstituteExtensions";
public const string NSubstituteExceptionExtensionsFullTypeName = "NSubstitute.ExceptionExtensions.ExceptionExtensions";
public const string NSubstituteCoreFullTypeName = "NSubstitute.Core.CallInfo";
public const string NSubstituteSubstituteFullTypeName = "NSubstitute.Substitute";
public const string NSubstituteReturnsMethod = "Returns";
public const string NSubstituteReturnsForAnyArgsMethod = "ReturnsForAnyArgs";
public const string NSubstituteThrowsMethod = "Throws";
public const string NSubstituteThrowsForAnyArgsMethod = "ThrowsForAnyArgs";
public const string NSubstituteDoMethod = "Do";
public const string NSubstituteReceivedMethod = "Received";
public const string NSubstituteReceivedWithAnyArgsMethod = "ReceivedWithAnyArgs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.CallInfoAnalyzerTests
{
public class ReturnsAsExtensionMethodsTests : CallInfoDiagnosticVerifier
public class ReturnsAsExtensionMethodTests : CallInfoDiagnosticVerifier
{
[Theory]
[InlineData("substitute[Arg.Any<int>()]", "callInfo.ArgAt<int>(1);", 22, 17)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.CallInfoAnalyzerTests
{
public class ReturnsAsExtensionMethodsWithGenericTypeSpecifiedTests : CallInfoDiagnosticVerifier
public class ReturnsAsExtensionMethodWithGenericTypeSpecifiedTests : CallInfoDiagnosticVerifier
{
[Theory]
[InlineData("substitute[Arg.Any<int>()]", "callInfo.ArgAt<int>(1);", 22, 17)]
Expand Down
Loading

0 comments on commit 887b494

Please sign in to comment.