diff --git a/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs b/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs index 4d0db535..76205bc7 100644 --- a/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs +++ b/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -7,18 +8,60 @@ namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers { [DiagnosticAnalyzer(LanguageNames.CSharp)] - internal sealed class ArgumentMatcherAnalyzer : AbstractArgumentMatcherAnalyzer + internal sealed class ArgumentMatcherAnalyzer : AbstractArgumentMatcherAnalyzer { + private static ImmutableArray> AllowedPaths { get; } = ImmutableArray.Create( + ImmutableArray.Create( + (int)SyntaxKind.Argument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.Argument, + (int)SyntaxKind.BracketedArgumentList, + (int)SyntaxKind.ElementAccessExpression), + ImmutableArray.Create( + (int)SyntaxKind.CastExpression, + (int)SyntaxKind.Argument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.AsExpression, + (int)SyntaxKind.Argument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.CastExpression, + (int)SyntaxKind.Argument, + (int)SyntaxKind.BracketedArgumentList, + (int)SyntaxKind.ElementAccessExpression), + ImmutableArray.Create( + (int)SyntaxKind.AsExpression, + (int)SyntaxKind.Argument, + (int)SyntaxKind.BracketedArgumentList, + (int)SyntaxKind.ElementAccessExpression)); + + private static ImmutableArray> IgnoredPaths { get; } = ImmutableArray.Create( + ImmutableArray.Create( + (int)SyntaxKind.EqualsValueClause, + (int)SyntaxKind.VariableDeclarator), + ImmutableArray.Create( + (int)SyntaxKind.CastExpression, + (int)SyntaxKind.EqualsValueClause, + (int)SyntaxKind.VariableDeclarator), + ImmutableArray.Create( + (int)SyntaxKind.AsExpression, + (int)SyntaxKind.EqualsValueClause, + (int)SyntaxKind.VariableDeclarator)); + public ArgumentMatcherAnalyzer() - : base(CSharp.DiagnosticDescriptorsProvider.Instance) + : base(NSubstitute.Analyzers.CSharp.DiagnosticDescriptorsProvider.Instance) { } - protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; + protected override ImmutableArray> AllowedAncestorPaths { get; } = AllowedPaths; - protected override AbstractArgumentMatcherCompilationAnalyzer CreateArgumentMatcherCompilationAnalyzer() - { - return new ArgumentMatcherCompilationAnalyzer(SubstitutionNodeFinder.Instance, DiagnosticDescriptorsProvider); - } + protected override ImmutableArray> IgnoredAncestorPaths { get; } = IgnoredPaths; + + protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; } } \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs b/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs deleted file mode 100644 index 1c394a59..00000000 --- a/src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Diagnostics; -using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; - -namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers -{ - internal sealed class ArgumentMatcherCompilationAnalyzer : AbstractArgumentMatcherCompilationAnalyzer - { - private static ImmutableArray> AllowedPaths { get; } = ImmutableArray.Create( - ImmutableArray.Create( - (int)SyntaxKind.Argument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.Argument, - (int)SyntaxKind.BracketedArgumentList, - (int)SyntaxKind.ElementAccessExpression), - ImmutableArray.Create( - (int)SyntaxKind.CastExpression, - (int)SyntaxKind.Argument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.AsExpression, - (int)SyntaxKind.Argument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.CastExpression, - (int)SyntaxKind.Argument, - (int)SyntaxKind.BracketedArgumentList, - (int)SyntaxKind.ElementAccessExpression), - ImmutableArray.Create( - (int)SyntaxKind.AsExpression, - (int)SyntaxKind.Argument, - (int)SyntaxKind.BracketedArgumentList, - (int)SyntaxKind.ElementAccessExpression)); - - private static ImmutableArray> IgnoredPaths { get; } = ImmutableArray.Create( - ImmutableArray.Create( - (int)SyntaxKind.EqualsValueClause, - (int)SyntaxKind.VariableDeclarator)); - - public ArgumentMatcherCompilationAnalyzer( - ISubstitutionNodeFinder substitutionNodeFinder, - IDiagnosticDescriptorsProvider diagnosticDescriptorsProvider) - : base(substitutionNodeFinder, diagnosticDescriptorsProvider) - { - } - - protected override ImmutableArray> AllowedAncestorPaths { get; } = AllowedPaths; - - protected override ImmutableArray> IgnoredAncestorPaths { get; } = IgnoredPaths; - - protected override SyntaxNode GetOperationSyntax(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, ArgumentSyntax argumentExpression) - { - var operation = syntaxNodeAnalysisContext.SemanticModel.GetOperation(argumentExpression); - return operation?.Parent?.Syntax; - } - - protected override IEnumerable TryGetArgumentExpressions(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, SyntaxNode syntaxNode) - { - SeparatedSyntaxList argumentList = default; - switch (syntaxNode) - { - case InvocationExpressionSyntax invocation: - argumentList = invocation.ArgumentList.Arguments; - break; - case ElementAccessExpressionSyntax elementAccessExpression: - argumentList = elementAccessExpression.ArgumentList.Arguments; - break; - } - - return argumentList.Select(node => node.Expression); - } - } -} \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.CSharp/NSubstitute.Analyzers.CSharp.csproj b/src/NSubstitute.Analyzers.CSharp/NSubstitute.Analyzers.CSharp.csproj index 3eeff264..3509968f 100644 --- a/src/NSubstitute.Analyzers.CSharp/NSubstitute.Analyzers.CSharp.csproj +++ b/src/NSubstitute.Analyzers.CSharp/NSubstitute.Analyzers.CSharp.csproj @@ -47,6 +47,6 @@ - + \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherAnalyzer.cs b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherAnalyzer.cs index 3291faad..049e22d7 100644 --- a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherAnalyzer.cs +++ b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherAnalyzer.cs @@ -2,42 +2,99 @@ using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; +using NSubstitute.Analyzers.Shared.Extensions; namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers { - internal abstract class AbstractArgumentMatcherAnalyzer : AbstractDiagnosticAnalyzer - where TSyntaxKind : struct + internal abstract class AbstractArgumentMatcherAnalyzer : AbstractDiagnosticAnalyzer where TInvocationExpressionSyntax : SyntaxNode - where TMemberAccessExpressionSyntax : SyntaxNode - where TArgumentSyntax : SyntaxNode + where TSyntaxKind : struct { - private readonly Action _compilationStartAction; + private readonly Action _analyzeInvocationAction; + + protected abstract ImmutableArray> AllowedAncestorPaths { get; } + + protected abstract ImmutableArray> IgnoredAncestorPaths { get; } protected abstract TSyntaxKind InvocationExpressionKind { get; } protected AbstractArgumentMatcherAnalyzer(IDiagnosticDescriptorsProvider diagnosticDescriptorsProvider) : base(diagnosticDescriptorsProvider) { + _analyzeInvocationAction = AnalyzeInvocation; SupportedDiagnostics = ImmutableArray.Create(DiagnosticDescriptorsProvider.ArgumentMatcherUsedWithoutSpecifyingCall); - _compilationStartAction = AnalyzeCompilation; } public override ImmutableArray SupportedDiagnostics { get; } protected override void InitializeAnalyzer(AnalysisContext context) { - context.RegisterCompilationStartAction(_compilationStartAction); + context.RegisterSyntaxNodeAction(_analyzeInvocationAction, InvocationExpressionKind); } - protected abstract AbstractArgumentMatcherCompilationAnalyzer CreateArgumentMatcherCompilationAnalyzer(); + private void AnalyzeInvocation(SyntaxNodeAnalysisContext syntaxNodeContext) + { + var invocationExpression = (TInvocationExpressionSyntax)syntaxNodeContext.Node; + var methodSymbolInfo = syntaxNodeContext.SemanticModel.GetSymbolInfo(invocationExpression); + + if (methodSymbolInfo.Symbol?.Kind != SymbolKind.Method) + { + return; + } + + var symbol = methodSymbolInfo.Symbol; + + if (symbol.IsArgMatcherLikeMethod() == false) + { + return; + } + + AnalyzeArgLikeMethod(syntaxNodeContext, invocationExpression); + } - private void AnalyzeCompilation(CompilationStartAnalysisContext compilationContext) + private void AnalyzeArgLikeMethod(SyntaxNodeAnalysisContext syntaxNodeContext, TInvocationExpressionSyntax argInvocationExpression) { - var compilationAnalyzer = CreateArgumentMatcherCompilationAnalyzer(); + // find allowed enclosing expression + var enclosingExpression = FindAllowedEnclosingExpression(argInvocationExpression); - compilationContext.RegisterSyntaxNodeAction(compilationAnalyzer.BeginAnalyzeArgMatchers, InvocationExpressionKind); + // if Arg is used with not allowed expression, find if it is used in ignored ones eg. var x = Arg.Any + // as variable might be used later on + if (enclosingExpression == null && FindIgnoredEnclosingExpression(argInvocationExpression) == null) + { + var diagnostic = Diagnostic.Create( + DiagnosticDescriptorsProvider.ArgumentMatcherUsedWithoutSpecifyingCall, + argInvocationExpression.GetLocation()); - compilationContext.RegisterCompilationEndAction(compilationAnalyzer.FinishAnalyzeArgMatchers); + syntaxNodeContext.ReportDiagnostic(diagnostic); + return; + } + + if (enclosingExpression == null) + { + return; + } + + var symbol = syntaxNodeContext.SemanticModel.GetSymbolInfo(enclosingExpression).Symbol; + var canBeSetuped = symbol.CanBeSetuped(); + + if (canBeSetuped == false || symbol.MemberVisibleToProxyGenerator() == false) + { + var diagnostic = Diagnostic.Create( + DiagnosticDescriptorsProvider.ArgumentMatcherUsedWithoutSpecifyingCall, + argInvocationExpression.GetLocation()); + + TryReportDiagnostic(syntaxNodeContext, diagnostic, symbol); + } + } + + private SyntaxNode FindAllowedEnclosingExpression(TInvocationExpressionSyntax invocationExpression) + { + return invocationExpression.GetAncestorNode(AllowedAncestorPaths); + } + + private SyntaxNode FindIgnoredEnclosingExpression(TInvocationExpressionSyntax invocationExpressionSyntax) + { + return invocationExpressionSyntax.GetAncestorNode(IgnoredAncestorPaths); } } } \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherCompilationAnalyzer.cs b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherCompilationAnalyzer.cs deleted file mode 100644 index f6d2d9a1..00000000 --- a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractArgumentMatcherCompilationAnalyzer.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using NSubstitute.Analyzers.Shared.Extensions; - -namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers -{ - internal abstract class AbstractArgumentMatcherCompilationAnalyzer - where TInvocationExpressionSyntax : SyntaxNode - where TMemberAccessExpressionSyntax : SyntaxNode - where TArgumentSyntax : SyntaxNode - { - protected abstract ImmutableArray> AllowedAncestorPaths { get; } - - protected abstract ImmutableArray> IgnoredAncestorPaths { get; } - - private readonly ISubstitutionNodeFinder _substitutionNodeFinder; - - private readonly IDiagnosticDescriptorsProvider _diagnosticDescriptorsProvider; - - private readonly ConcurrentBag _receivedInOrderNodes = new ConcurrentBag(); - - private readonly ConcurrentBag _whenNodes = new ConcurrentBag(); - - private readonly ConcurrentDictionary> _potentialMisusedNodes = new ConcurrentDictionary>(); - - private readonly ConcurrentBag _misusedNodes = new ConcurrentBag(); - - protected AbstractArgumentMatcherCompilationAnalyzer( - ISubstitutionNodeFinder substitutionNodeFinder, - IDiagnosticDescriptorsProvider diagnosticDescriptorsProvider) - { - _substitutionNodeFinder = substitutionNodeFinder; - _diagnosticDescriptorsProvider = diagnosticDescriptorsProvider; - } - - public void BeginAnalyzeArgMatchers(SyntaxNodeAnalysisContext syntaxNodeContext) - { - var invocationExpression = (TInvocationExpressionSyntax)syntaxNodeContext.Node; - var methodSymbolInfo = syntaxNodeContext.SemanticModel.GetSymbolInfo(invocationExpression); - - if (methodSymbolInfo.Symbol?.Kind != SymbolKind.Method) - { - return; - } - - var methodSymbol = (IMethodSymbol)methodSymbolInfo.Symbol; - - switch (methodSymbol) - { - case ISymbol symbol when symbol.IsArgMatcherLikeMethod(): - BeginAnalyzeArgLikeMethod(syntaxNodeContext, invocationExpression); - break; - case ISymbol symbol when symbol.IsWhenLikeMethod(): - BeginAnalyzeWhenLikeMethod(syntaxNodeContext, invocationExpression); - break; - case ISymbol symbol when symbol.IsReceivedInOrderMethod(): - BeginAnalyzeReceivedInOrderMethod(syntaxNodeContext, invocationExpression); - break; - } - } - - public void FinishAnalyzeArgMatchers(CompilationAnalysisContext compilationAnalysisContext) - { - // we dont need thread safety anymore - changing for hashset for faster lookup - var whenNodes = _whenNodes.ToImmutableHashSet(); - var receivedInOrderNodes = _receivedInOrderNodes.ToImmutableHashSet(); - - foreach (var potential in _potentialMisusedNodes) - { - if (whenNodes.Contains(potential.Key) == false && receivedInOrderNodes.Contains(potential.Key) == false) - { - foreach (var arg in potential.Value) - { - var diagnostic = Diagnostic.Create( - _diagnosticDescriptorsProvider.ArgumentMatcherUsedWithoutSpecifyingCall, - arg.GetLocation()); - - compilationAnalysisContext.ReportDiagnostic(diagnostic); - } - } - } - - foreach (var misusedNode in _misusedNodes) - { - var diagnostic = Diagnostic.Create( - _diagnosticDescriptorsProvider.ArgumentMatcherUsedWithoutSpecifyingCall, - misusedNode.GetLocation()); - - compilationAnalysisContext.ReportDiagnostic(diagnostic); - } - } - - protected abstract SyntaxNode GetOperationSyntax(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, TArgumentSyntax argumentExpression); - - protected abstract IEnumerable TryGetArgumentExpressions(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, SyntaxNode syntaxNode); - - private bool IsFollowedBySetupInvocation(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode invocationExpressionSyntax) - { - var parentNote = invocationExpressionSyntax.Parent; - - if (parentNote is TMemberAccessExpressionSyntax) - { - var child = parentNote.ChildNodes().Except(new[] { invocationExpressionSyntax }).FirstOrDefault(); - - return child != null && IsSetupLikeMethod(syntaxNodeContext.SemanticModel.GetSymbolInfo(child).Symbol); - } - - if (parentNote is TArgumentSyntax argumentExpression) - { - var operationSyntax = GetOperationSyntax(syntaxNodeContext, argumentExpression); - return operationSyntax != null && IsSetupLikeMethod(syntaxNodeContext.SemanticModel.GetSymbolInfo(operationSyntax).Symbol); - } - - return false; - } - - private void BeginAnalyzeWhenLikeMethod(SyntaxNodeAnalysisContext syntaxNodeContext, TInvocationExpressionSyntax invocationExpression) - { - foreach (var syntaxNode in _substitutionNodeFinder.FindForWhenExpression(syntaxNodeContext, invocationExpression)) - { - _whenNodes.Add(syntaxNode); - } - } - - private void BeginAnalyzeReceivedInOrderMethod(SyntaxNodeAnalysisContext syntaxNodeContext, TInvocationExpressionSyntax invocationExpression) - { - foreach (var syntaxNode in _substitutionNodeFinder - .FindForReceivedInOrderExpression(syntaxNodeContext, invocationExpression)) - { - _receivedInOrderNodes.Add(syntaxNode); - } - } - - private void BeginAnalyzeArgLikeMethod(SyntaxNodeAnalysisContext syntaxNodeContext, TInvocationExpressionSyntax invocationExpression) - { - // find allowed enclosing expression - var enclosingExpression = FindAllowedEnclosingExpression(invocationExpression); - - // if Arg is used with not allowed expression, find if it is used in ignored ones eg. var x = Arg.Any - // as variable might be used later on - if (enclosingExpression == null && FindIgnoredEnclosingExpression(invocationExpression) == null) - { - _misusedNodes.Add(invocationExpression); - } - - if (enclosingExpression != null && - IsFollowedBySetupInvocation(syntaxNodeContext, enclosingExpression) == false && - IsPrecededByReceivedInvocation(syntaxNodeContext, enclosingExpression) == false && - IsUsedAlongWithArgInvokers(syntaxNodeContext, enclosingExpression) == false) - { - _potentialMisusedNodes.AddOrUpdate( - enclosingExpression, - node => new List { invocationExpression }, - (node, list) => - { - list.Add(invocationExpression); - return list; - }); - } - } - - private bool IsPrecededByReceivedInvocation(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode invocationExpressionSyntax) - { - var syntaxNodes = invocationExpressionSyntax.Parent.DescendantNodes().ToList(); - var index = syntaxNodes.IndexOf(invocationExpressionSyntax.DescendantNodes().First()); - - if (index >= 0 && index + 1 < syntaxNodes.Count - 1) - { - var syntaxNode = syntaxNodes[index + 1]; - return syntaxNodeContext.SemanticModel.GetSymbolInfo(syntaxNode).Symbol.IsReceivedLikeMethod(); - } - - return false; - } - - private SyntaxNode FindAllowedEnclosingExpression(TInvocationExpressionSyntax invocationExpression) - { - return invocationExpression.GetAncestorNode(AllowedAncestorPaths); - } - - private SyntaxNode FindIgnoredEnclosingExpression(TInvocationExpressionSyntax invocationExpressionSyntax) - { - return invocationExpressionSyntax.GetAncestorNode(IgnoredAncestorPaths); - } - - private bool IsUsedAlongWithArgInvokers(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode invocationExpressionSyntax) - { - return TryGetArgumentExpressions(syntaxNodeContext, invocationExpressionSyntax) - .OfType() - .Select(syntaxNode => syntaxNodeContext.SemanticModel.GetSymbolInfo(syntaxNode).Symbol).Any(symbol => symbol.IsArgInvokerLikeMethod()); - } - - private bool IsSetupLikeMethod(ISymbol symbol) - { - return symbol.IsReturnLikeMethod() || symbol.IsThrowLikeMethod(); - } - } -} \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs b/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs index 7b0cdfaf..73d96a12 100644 --- a/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs +++ b/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherAnalyzer.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.VisualBasic; @@ -7,18 +8,55 @@ namespace NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers { [DiagnosticAnalyzer(LanguageNames.VisualBasic)] - internal sealed class ArgumentMatcherAnalyzer : AbstractArgumentMatcherAnalyzer + internal sealed class ArgumentMatcherAnalyzer : AbstractArgumentMatcherAnalyzer { + private static ImmutableArray> AllowedPaths { get; } = ImmutableArray.Create( + ImmutableArray.Create( + (int)SyntaxKind.SimpleArgument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.TryCastExpression, + (int)SyntaxKind.SimpleArgument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.DirectCastExpression, + (int)SyntaxKind.SimpleArgument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression), + ImmutableArray.Create( + (int)SyntaxKind.CTypeExpression, + (int)SyntaxKind.SimpleArgument, + (int)SyntaxKind.ArgumentList, + (int)SyntaxKind.InvocationExpression)); + + private static ImmutableArray> IgnoredPaths { get; } = ImmutableArray.Create( + ImmutableArray.Create( + (int)SyntaxKind.EqualsValue, + (int)SyntaxKind.VariableDeclarator), + ImmutableArray.Create( + (int)SyntaxKind.TryCastExpression, + (int)SyntaxKind.EqualsValue, + (int)SyntaxKind.VariableDeclarator), + ImmutableArray.Create( + (int)SyntaxKind.DirectCastExpression, + (int)SyntaxKind.EqualsValue, + (int)SyntaxKind.VariableDeclarator), + ImmutableArray.Create( + (int)SyntaxKind.CTypeExpression, + (int)SyntaxKind.EqualsValue, + (int)SyntaxKind.VariableDeclarator)); + public ArgumentMatcherAnalyzer() - : base(VisualBasic.DiagnosticDescriptorsProvider.Instance) + : base(NSubstitute.Analyzers.VisualBasic.DiagnosticDescriptorsProvider.Instance) { } - protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; + protected override ImmutableArray> AllowedAncestorPaths { get; } = AllowedPaths; - protected override AbstractArgumentMatcherCompilationAnalyzer CreateArgumentMatcherCompilationAnalyzer() - { - return new ArgumentMatcherCompilationAnalyzer(SubstitutionNodeFinder.Instance, DiagnosticDescriptorsProvider); - } + protected override ImmutableArray> IgnoredAncestorPaths { get; } = IgnoredPaths; + + protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; } } \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs b/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs deleted file mode 100644 index b411f139..00000000 --- a/src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/ArgumentMatcherCompilationAnalyzer.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.VisualBasic; -using Microsoft.CodeAnalysis.VisualBasic.Syntax; -using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; - -namespace NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers -{ - internal sealed class ArgumentMatcherCompilationAnalyzer : AbstractArgumentMatcherCompilationAnalyzer - { - private static ImmutableArray> AllowedPaths { get; } = ImmutableArray.Create( - ImmutableArray.Create( - (int)SyntaxKind.SimpleArgument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.TryCastExpression, - (int)SyntaxKind.SimpleArgument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.DirectCastExpression, - (int)SyntaxKind.SimpleArgument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression), - ImmutableArray.Create( - (int)SyntaxKind.CTypeExpression, - (int)SyntaxKind.SimpleArgument, - (int)SyntaxKind.ArgumentList, - (int)SyntaxKind.InvocationExpression)); - - private static ImmutableArray> IgnoredPaths { get; } = ImmutableArray.Create( - ImmutableArray.Create( - (int)SyntaxKind.EqualsValue, - (int)SyntaxKind.VariableDeclarator)); - - public ArgumentMatcherCompilationAnalyzer(ISubstitutionNodeFinder substitutionNodeFinder, IDiagnosticDescriptorsProvider diagnosticDescriptorsProvider) - : base(substitutionNodeFinder, diagnosticDescriptorsProvider) - { - } - - protected override ImmutableArray> AllowedAncestorPaths { get; } = AllowedPaths; - - protected override ImmutableArray> IgnoredAncestorPaths { get; } = IgnoredPaths; - - protected override SyntaxNode GetOperationSyntax(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, ArgumentSyntax argumentExpression) - { - var operation = syntaxNodeAnalysisContext.SemanticModel.GetOperation(argumentExpression); - return operation?.Parent?.Syntax; - } - - protected override IEnumerable TryGetArgumentExpressions( - SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, SyntaxNode syntaxNode) - { - SeparatedSyntaxList argumentList = default; - switch (syntaxNode) - { - case InvocationExpressionSyntax invocation: - argumentList = invocation.ArgumentList.Arguments; - break; - } - - return argumentList.Select(node => node.GetExpression()); - } - } -} \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.VisualBasic/NSubstitute.Analyzers.VisualBasic.csproj b/src/NSubstitute.Analyzers.VisualBasic/NSubstitute.Analyzers.VisualBasic.csproj index eadadb34..3caa4935 100644 --- a/src/NSubstitute.Analyzers.VisualBasic/NSubstitute.Analyzers.VisualBasic.csproj +++ b/src/NSubstitute.Analyzers.VisualBasic/NSubstitute.Analyzers.VisualBasic.csproj @@ -60,6 +60,6 @@ - + \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs index 6244a1bb..faf6a52e 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs @@ -1,12 +1,11 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.CSharp; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Tests.Shared; using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; using Xunit; namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests @@ -15,46 +14,119 @@ public abstract class ArgumentMatcherDiagnosticVerifier : CSharpDiagnosticVerifi { protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.ArgumentMatcherUsedWithoutSpecifyingCall; - [CombinatoryTheory] - [InlineData("Arg.Any()")] - [InlineData("(int)Arg.Any()")] - [InlineData("Arg.Any() as int?")] - [InlineData("Arg.Compat.Any()")] - [InlineData("(int)Arg.Compat.Any()")] - [InlineData("Arg.Compat.Any() as int?")] - [InlineData("Arg.Is(1)")] - [InlineData("(int)Arg.Is(1)")] - [InlineData("Arg.Is(1) as int?")] - [InlineData("Arg.Compat.Is(1)")] - [InlineData("(int)Arg.Compat.Is(1)")] - [InlineData("Arg.Compat.Is(1) as int?")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg); - - [CombinatoryTheory] - [InlineData("Arg.Any()")] - [InlineData("(int)Arg.Any()")] - [InlineData("Arg.Any() as int?")] - [InlineData("Arg.Compat.Any()")] - [InlineData("(int)Arg.Compat.Any()")] - [InlineData("Arg.Compat.Any() as int?")] - [InlineData("Arg.Is(1)")] - [InlineData("(int)Arg.Is(1)")] - [InlineData("Arg.Is(1) as int?")] - [InlineData("Arg.Compat.Is(1)")] - [InlineData("(int)Arg.Compat.Is(1)")] - [InlineData("Arg.Compat.Is(1) as int?")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg); - - [CombinatoryTheory] - [InlineData("[|Arg.Any()|]")] - [InlineData("[|Arg.Compat.Any()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg); + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInStaticMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInVirtualMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInNonSealedOverrideMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInDelegate(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInSealedOverrideMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInAbstractMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInterfaceMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInGenericInterfaceMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInterfaceIndexer(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInVirtualIndexer(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualIndexer(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedAsStandaloneExpression(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInConstructor(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToNotApplied(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToApplied(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInProtectedInternalVirtualMember(string arg); protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() { return new ArgumentMatcherAnalyzer(); } + + public static IEnumerable MisusedArgTestCases + { + get + { + yield return new object[] { "[|Arg.Any()|]" }; + yield return new object[] { "[|Arg.Compat.Any()|]" }; + yield return new object[] { "[|Arg.Is(1)|]" }; + yield return new object[] { "[|Arg.Compat.Is(1)|]" }; + } + } + + public static IEnumerable CorrectlyUsedArgTestCases + { + get + { + yield return new object[] { "Arg.Any()" }; + yield return new object[] { "(int)Arg.Any()" }; + yield return new object[] { "Arg.Any() as int?" }; + yield return new object[] { "Arg.Compat.Any()" }; + yield return new object[] { "(int)Arg.Compat.Any()" }; + yield return new object[] { "Arg.Compat.Any() as int?" }; + yield return new object[] { "Arg.Is(1)" }; + yield return new object[] { "(int)Arg.Is(1)" }; + yield return new object[] { "Arg.Is(1) as int?" }; + yield return new object[] { "Arg.Compat.Is(1)" }; + yield return new object[] { "(int)Arg.Compat.Is(1)" }; + yield return new object[] { "Arg.Compat.Is(1) as int?" }; + } + } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs deleted file mode 100644 index c2efebbe..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using NSubstitute.Analyzers.CSharp; -using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; -using Xunit; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - public abstract class ArgumentMatcherMisuseDiagnosticVerifier : CSharpDiagnosticVerifier, IArgumentMatcherMisuseDiagnosticVerifier - { - protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.ArgumentMatcherUsedWithoutSpecifyingCall; - - [Theory] - [InlineData("[|Arg.Any()|]")] - [InlineData("[|Arg.Compat.Any()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForMethodCall(string arg); - - [Theory] - [InlineData("[|Arg.Any()|]")] - [InlineData("[|Arg.Compat.Any()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForIndexerCall(string arg); - - [Theory] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedArgMethod(string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Do(1)", "Arg.Compat.Do(1)", "Arg.Invoke(1)", "Arg.Compat.Invoke(1)", "Arg.InvokeDelegate(1)", "Arg.Compat.InvokeDelegate(1)")] - [InlineData("[|NSubstitute.Arg.Any()|]")] - [InlineData("[|NSubstitute.Arg.Compat.Any()|]")] - [InlineData("[|NSubstitute.Arg.Is(1)|]")] - [InlineData("[|NSubstitute.Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUseTogetherWithUnfortunatelyNamedArgDoInvoke(string argDoInvoke, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Do(_ => {})", "Arg.Compat.Do(_ => {})")] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForMethodCall(string argDo, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Do(_ => {})", "Arg.Compat.Do(_ => {})")] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForIndexerCall(string argDo, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Invoke(1)", "Arg.Compat.Invoke(1)", "Arg.InvokeDelegate>(1)", "Arg.Compat.InvokeDelegate>(1)")] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForMethodCall(string argInvoke, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Invoke(1)", "Arg.Compat.Invoke(1)", "Arg.InvokeDelegate>(1)", "Arg.Compat.InvokeDelegate>(1)")] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForIndexerCall(string argInvoke, string arg); - - [Theory] - [InlineData("Arg.Any()")] - [InlineData("Arg.Compat.Any()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ArgumentMatcherAnalyzer(); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs deleted file mode 100644 index 5bd55e1c..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs +++ /dev/null @@ -1,377 +0,0 @@ -using System.Threading.Tasks; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - public class ArgumentMatcherMisuseTests : ArgumentMatcherMisuseDiagnosticVerifier - { - public override async Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForMethodCall(string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x, int y); - }} - - public class Bar - {{ - public int I {{ get; set; }} - public Bar(){{}} - - public Bar(int i){{}} - - public int FooBar(int x, int y) - {{ - return 1; - }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.Bar({arg}, {arg}); - var bar = substitute.Bar({arg}, {arg}); - new Bar().FooBar({arg}, {arg}); - new Bar({arg}); - new Bar {{ I = {arg}}}; - var anonymous = new {{ I = {arg}}}; - substitute.When(x => {{ new Bar().FooBar({arg}, {arg});}}); - }} - }} -}}"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForIndexerCall(string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int x, int y] {{ get; }} - }} - - public class Bar - {{ - public int this[int x, int y] => 1; - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - _ = substitute[{arg}, {arg}]; - _ = new Bar()[{arg}, {arg}]; - substitute.When(x => {{ _ = new Bar()[{arg}, {arg}];}}); - }} - }} -}}"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedArgMethod(string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x, int y); - }} - - public class Bar - {{ - public int FooBar(int x, int y) - {{ - return 1; - }} - }} - - public class Arg - {{ - public static T Any() - {{ - return default(T); - }} - - public static T Is(T value) - {{ - return default(T); - }} - - public static T Invoke(T value) - {{ - return default(T); - }} - - public static T Do(T value) - {{ - return default(T); - }} - - public static class Compat - {{ - public static T Any() - {{ - return default(T); - }} - - public static T Is(T value) - {{ - return default(T); - }} - - public static T Invoke(T value) - {{ - return default(T); - }} - - public static T Do(T value) - {{ - return default(T); - }} - }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.Bar({arg}, {arg}); - var bar = substitute.Bar({arg}, {arg}); - new Bar().FooBar({arg}, {arg}); - substitute.When(x => {{ new Bar().FooBar({arg}, {arg});}}); - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUseTogetherWithUnfortunatelyNamedArgDoInvoke(string argDoInvoke, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x, int y); - }} - - public class Bar - {{ - public int FooBar(int x, int y) - {{ - return 1; - }} - }} - - public class Arg - {{ - public static T Invoke(T value) - {{ - return default(T); - }} - - public static T Do(T value) - {{ - return default(T); - }} - - public static T InvokeDelegate(T value) - {{ - return default(T); - }} - - public static class Compat - {{ - public static T Invoke(T value) - {{ - return default(T); - }} - - public static T InvokeDelegate(T value) - {{ - return default(T); - }} - - public static T Do(T value) - {{ - return default(T); - }} - }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.Bar({arg}, {argDoInvoke}); - var bar = substitute.Bar({argDoInvoke}, {arg}); - new Bar().FooBar({arg}, {argDoInvoke}); - substitute.When(x => {{ new Bar().FooBar({argDoInvoke}, {arg});}}); - }} - }} -}}"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForMethodCall(string argDo, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x, int y); - }} - - public class Bar - {{ - public int FooBar(int x, int y) - {{ - return 1; - }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.Bar({arg}, {argDo}); - var bar = substitute.Bar({argDo}, {arg}); - new Bar().FooBar({arg}, {argDo}); - substitute.When(x => {{ new Bar().FooBar({argDo}, {arg});}}); - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForIndexerCall(string argDo, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int x, int y] {{ get; }} - }} - - public class Bar - {{ - public int this[int x, int y] => 1; - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - _ = substitute[{arg}, {argDo}]; - _ = new Bar()[{argDo}, {arg}]; - substitute.When(x => {{ _ = new Bar()[{argDo}, {arg}];}}); - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForMethodCall(string argInvoke, string arg) - { - var source = $@"using System; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x, Action y); - }} - - public class Bar - {{ - public int FooBar(int x, Action y) - {{ - return 1; - }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.Bar({arg}, {argInvoke}); - var bar = substitute.Bar({arg}, {argInvoke}); - new Bar().FooBar({arg}, {argInvoke}); - substitute.When(x => {{ new Bar().FooBar({arg}, {argInvoke});}}); - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForIndexerCall(string argInvoke, string arg) - { - var source = $@"using System; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int x, Action y] {{ get; }} - }} - - public class Bar - {{ - public int this[int x, Action y] => 1; - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - _ = substitute[{arg}, {argInvoke}]; - _ = new Bar()[{arg}, {argInvoke}]; - substitute.When(x => {{ _ = new Bar()[{arg}, {argInvoke}];}}); - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg) - { - var source = $@"using System; -using NSubstitute; - -namespace MyNamespace -{{ - public class FooTests - {{ - public void Test() - {{ - var arg = {arg}; - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs new file mode 100644 index 00000000..cee9e9dc --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs @@ -0,0 +1,551 @@ +using System.Threading.Tasks; + +namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests +{ + public class ArgumentMatcherTests : ArgumentMatcherDiagnosticVerifier + { + public override async Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public int Bar(int firstArg) + {{ + return 2; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInStaticMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public static int Bar(int firstArg) + {{ + return 2; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + Foo.Bar({arg}); + }} + }} +}}"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInVirtualMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public virtual int Bar(int? firstArg) + {{ + return 2; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInNonSealedOverrideMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public virtual int Bar(int? firstArg) + {{ + return 2; + }} + }} + + public class Foo2 : Foo + {{ + public override int Bar(int? firstArg) => 1; + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInDelegate(string arg) + { + var source = $@"using NSubstitute; +using System; + +namespace MyNamespace +{{ + public class FooTests + {{ + public void Test() + {{ + var substitute = Substitute.For>(); + substitute({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInSealedOverrideMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public virtual int Bar(int firstArg) + {{ + return 2; + }} + }} + + public class Foo2 : Foo + {{ + public sealed override int Bar(int firstArg) => 1; + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInAbstractMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public abstract class Foo + {{ + public abstract int Bar(int? firstArg); + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInterfaceMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public interface IFoo + {{ + int Bar(int? firstArg); + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInGenericInterfaceMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public interface IFoo + {{ + int Bar(int? firstArg); + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For>(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInterfaceIndexer(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public interface IFoo + {{ + int this[int? i] {{ get; }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute[{arg}]; + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInVirtualIndexer(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public virtual int this[int? x] => 0; + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute[{arg}]; + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInNonVirtualIndexer(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public int this[int x] => 0; + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute[{arg}]; + }} + }} +}}"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + public int Bar(int? firstArg) + {{ + return 1; + }} + }} + + public class Arg + {{ + public static T Any() + {{ + return default(T); + }} + + public static T Is(T value) + {{ + return default(T); + }} + + public static T Invoke(T value) + {{ + return default(T); + }} + + public static T Do(T value) + {{ + return default(T); + }} + + public static class Compat + {{ + public static T Any() + {{ + return default(T); + }} + + public static T Is(T value) + {{ + return default(T); + }} + + public static T Invoke(T value) + {{ + return default(T); + }} + + public static T Do(T value) + {{ + return default(T); + }} + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.Bar({arg}); + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg) + { + var source = $@"using System; +using NSubstitute; + +namespace MyNamespace +{{ + public class FooTests + {{ + public void Test() + {{ + var arg = {arg}; + }} + }} +}}"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedAsStandaloneExpression(string arg) + { + var source = $@"using System; +using NSubstitute; + +namespace MyNamespace +{{ + public class FooTests + {{ + public void Test() + {{ + {arg}; + }} + }} +}}"; + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInConstructor(string arg) + { + var source = $@"using System; +using NSubstitute; + +namespace MyNamespace +{{ + public class FooTests + {{ + public FooTests(int firstArg) + {{ + }} + + public void Test() + {{ + var x = new FooTests({arg}); + }} + }} +}}"; + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToNotApplied(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar(int? firstArg) + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.FooBar({arg}); + }} + }} +}}"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToApplied(string arg) + { + var source = $@"using NSubstitute; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo(""OtherFirstAssembly"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""OtherSecondAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar(int? firstArg) + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.FooBar({arg}); + }} + }} +}}"; + + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string arg) + { + var source = $@"using NSubstitute; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar(int? firstArg) + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.FooBar({arg}); + }} + }} +}}"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInProtectedInternalVirtualMember(string arg) + { + var source = $@"using NSubstitute; + +namespace MyNamespace +{{ + public class Foo + {{ + protected internal virtual int FooBar(int? firstArg) + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.FooBar({arg}); + }} + }} +}}"; + + await VerifyNoDiagnostic(source); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs deleted file mode 100644 index 4a06d999..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Received", "Received", "ReceivedWithAnyArgs", "ReceivedWithAnyArgs", "DidNotReceive", "DidNotReceive", "DidNotReceiveWithAnyArgs", "DidNotReceiveWithAnyArgs")] - public class ReceivedAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.{method}().Bar({arg}); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.{method}()[{arg}]; - }} - }} -}}"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.{method}(1m).Bar({arg}); - }} - }} - - public static class SubstituteExtensions - {{ - public static T Received(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T ReceivedWithAnyArgs(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T DidNotReceive(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T DidNotReceiveWithAnyArgs(this T returnValue, decimal x) - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs deleted file mode 100644 index 69634fde..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Diagnostics; -using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; -using Xunit; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData( - "SubstituteExtensions.Received", - "SubstituteExtensions.Received", - "SubstituteExtensions.ReceivedWithAnyArgs", - "SubstituteExtensions.ReceivedWithAnyArgs", - "SubstituteExtensions.DidNotReceive", - "SubstituteExtensions.DidNotReceive", - "SubstituteExtensions.DidNotReceiveWithAnyArgs", - "SubstituteExtensions.DidNotReceiveWithAnyArgs")] - public class ReceivedAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute).Bar({arg}); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - _ = {method}(substitute)[{arg}]; - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute, 1m).Bar({arg}); - }} - }} - - public static class SubstituteExtensions - {{ - public static T Received(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T ReceivedWithAnyArgs(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T DidNotReceive(this T returnValue, decimal x) - {{ - return default(T); - }} - - public static T DidNotReceiveWithAnyArgs(this T returnValue, decimal x) - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs deleted file mode 100644 index fd3318b7..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Received.InOrder")] - public class ReceivedInOrderMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(() => substitute.Bar({arg}) ); - {method}(() => {{ substitute.Bar({arg}); }} ); - {method}(delegate {{ substitute.Bar({arg}); }}); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(() => {{ var x = substitute[{arg}]; }}); - {method}(delegate {{ var x = substitute[{arg}]; }}); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int x] {{ get; }} - - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(() => {{ var x = substitute[{arg}]; }}, 1); - {method}(delegate {{ var x = substitute[{arg}]; }}, 1); - }} - }} - - public class Received - {{ - public static void InOrder(Action calls, int x) - {{ - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs deleted file mode 100644 index f25debb8..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Returns", "Returns", "ReturnsForAnyArgs", "ReturnsForAnyArgs")] - public class ReturnsAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(1); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute[{arg}].{method}(1); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(1); - }} - }} - - public static class SubstituteExtensions - {{ - public static T Returns(this T returnValue, T returnThis) - {{ - return default(T); - }} - - public static T ReturnsForAnyArgs(this T returnValue, T returnThis) - {{ - return default(T); - }} - }} - -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs deleted file mode 100644 index 8b2955c8..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,101 +0,0 @@ -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 NSubstitute.Analyzers.Tests.Shared.Extensibility; -using Xunit; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("SubstituteExtensions.Returns", "SubstituteExtensions.Returns", "SubstituteExtensions.ReturnsForAnyArgs", "SubstituteExtensions.ReturnsForAnyArgs")] - public class ReturnsAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute.Bar({arg}), 1); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute[{arg}], 1); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute.Bar({arg}), 1); - }} - }} - - public static class SubstituteExtensions - {{ - public static T Returns(this T returnValue, T returnThis) - {{ - return default(T); - }} - - public static T ReturnsForAnyArgs(this T returnValue, T returnThis) - {{ - return default(T); - }} - }} - -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs deleted file mode 100644 index 5939aab4..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ReturnsNull", "ReturnsNull", "ReturnsNullForAnyArgs", "ReturnsNullForAnyArgs")] - public class ReturnsNullAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute[{arg}].{method}(); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(); - }} - }} - - public static class ReturnsExtensions - {{ - public static T ReturnsNull(this T returnValue) - {{ - return default(T); - }} - - public static T ReturnsNullForAnyArgs(this T returnValue) - {{ - return default(T); - }} - }} - -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs deleted file mode 100644 index fa1b54ed..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ReturnsExtensions.ReturnsNull", "ReturnsExtensions.ReturnsNull", "ReturnsExtensions.ReturnsNullForAnyArgs", "ReturnsExtensions.ReturnsNullForAnyArgs")] - public class ReturnsNullAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = {method}(substitute.Bar({arg})); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute[{arg}]); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using NSubstitute; -using NSubstitute.ReturnsExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract Foo Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute.Bar({arg})); - }} - }} - - public static class ReturnsExtensions - {{ - public static T ReturnsNull(this T returnValue) - {{ - return default(T); - }} - - public static T ReturnsNullForAnyArgs(this T returnValue) - {{ - return default(T); - }} - }} - -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs deleted file mode 100644 index 64eba860..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Throws", "ThrowsForAnyArgs")] - public class ThrowsAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(new Exception()); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute[{arg}].{method}(new Exception()); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(new Exception()); - }} - }} - - public static class ExceptionExtensions - {{ - public static T Throws(this object value, T ex) where T: Exception - {{ - return default(T); - }} - - public static T ThrowsForAnyArgs(this object value, T ex) where T: Exception - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs deleted file mode 100644 index 8fd8a2ff..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Throws", "ThrowsForAnyArgs")] - public class ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute[{arg}].{method}(); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = substitute.Bar({arg}).{method}(); - }} - }} - - public static class ExceptionExtensions - {{ - public static T Throws(this object value) where T: Exception - {{ - return default(T); - }} - - public static T ThrowsForAnyArgs(this object value) where T: Exception - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs deleted file mode 100644 index 3f022000..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] - public class ThrowsAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = {method}(substitute.Bar({arg}), new Exception()); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute[{arg}], new Exception()); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute.Bar({arg}), new Exception()); - }} - }} - - public static class ExceptionExtensions - {{ - public static T Throws(this object value, T ex) where T: Exception - {{ - return default(T); - }} - - public static T ThrowsForAnyArgs(this object value, T ex) where T: Exception - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs deleted file mode 100644 index 7034848b..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] - public class ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - var x = {method}(substitute.Bar({arg})); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute[{arg}]); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; -using NSubstitute.ExceptionExtensions; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute.Bar({arg})); - }} - }} - - public static class ExceptionExtensions - {{ - public static T Throws(this object value) where T: Exception - {{ - return default(T); - }} - - public static T ThrowsForAnyArgs(this object value) where T: Exception - {{ - return default(T); - }} - }} -}}"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs deleted file mode 100644 index 7eddb07a..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs +++ /dev/null @@ -1,122 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("When", "When", "WhenForAnyArgs", "WhenForAnyArgs")] - public class WhenAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using System.Threading.Tasks; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.{method}(delegate(Foo x) {{ x.Bar({arg}); }}).Do(x => throw new NullReferenceException()); - substitute.{method}(x => x.Bar({arg})).Do(x => throw new NullReferenceException()); - substitute.{method}(SubstituteCall).Do(x => {{ throw new NullReferenceException(); }}); - }} - - private Task SubstituteCall(Foo obj) - {{ - obj.Bar({arg}); - return Task.CompletedTask; - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using System; -using System.Threading.Tasks; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.{method}(delegate(Foo x) {{ var y = x[{arg}]; }}).Do(x => throw new NullReferenceException()); - substitute.{method}(x => {{ var y = x[{arg}]; }}).Do(x => throw new NullReferenceException()); - substitute.{method}(SubstituteCall).Do(x => {{ throw new NullReferenceException(); }}); - }} - - private Task SubstituteCall(Foo obj) - {{ - _ = obj[{arg}]; - return Task.CompletedTask; - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using System.Threading.Tasks; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - substitute.{method}(delegate(Foo x) {{ x.Bar({arg}); }}, 1); - substitute.{method}(x => x.Bar({arg}), 1); - }} - - private Task SubstituteCall(Foo obj) - {{ - obj.Bar({arg}); - return Task.CompletedTask; - }} - }} - - public static class SubstituteExtensions - {{ - public static T When(this T substitute, System.Action substituteCall, int x) - {{ - return default(T); - }} - - public static T WhenForAnyArgs(this T substitute, System.Action substituteCall, int x) - {{ - return default(T); - }} - }} -}}"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs deleted file mode 100644 index 0fecbfdd..00000000 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("SubstituteExtensions.When", "SubstituteExtensions.When", "SubstituteExtensions.WhenForAnyArgs", "SubstituteExtensions.WhenForAnyArgs")] - public class WhenAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"using System; -using System.Threading.Tasks; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int? x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute, delegate(Foo x) {{ x.Bar({arg}); }}).Do(x => throw new NullReferenceException()); - {method}(substitute, x => x.Bar({arg})).Do(x => throw new NullReferenceException()); - {method}(substitute, SubstituteCall).Do(x => {{ throw new NullReferenceException(); }}); - }} - - private Task SubstituteCall(Foo obj) - {{ - obj.Bar({arg}); - return Task.CompletedTask; - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"using NSubstitute; -using System; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int this[int? x] {{ get; }} - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute, delegate(Foo x) {{ var y = x[{arg}]; }}).Do(x => throw new NullReferenceException()); - {method}(substitute, x => {{ var y = x[{arg}]; }}).Do(x => throw new NullReferenceException()); - }} - }} -}}"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"using System; -using NSubstitute; - -namespace MyNamespace -{{ - public abstract class Foo - {{ - public abstract int Bar(int x); - }} - - public class FooTests - {{ - public void Test() - {{ - var substitute = NSubstitute.Substitute.For(); - {method}(substitute, delegate(Foo x) {{ x.Bar({arg}); }}, 1); - {method}(substitute, x => x.Bar({arg}), 1); - {method}(substitute, x => x.Bar({arg}), 1); - }} - }} - - public static class SubstituteExtensions - {{ - public static T When(this T substitute, System.Action substituteCall, int x) - {{ - return default(T); - }} - - public static T WhenForAnyArgs(this T substitute, System.Action substituteCall, int x) - {{ - return default(T); - }} - }} -}}"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherDiagnosticVerifier.cs index a1d7caf4..a8c4abc0 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherDiagnosticVerifier.cs @@ -4,10 +4,44 @@ namespace NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers { public interface IArgumentMatcherDiagnosticVerifier { - Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg); + Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg); - Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg); + Task ReportsDiagnostics_WhenUsedInStaticMethod(string arg); - Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg); + Task ReportsNoDiagnostics_WhenUsedInVirtualMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInNonSealedOverrideMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInDelegate(string arg); + + Task ReportsDiagnostics_WhenUsedInSealedOverrideMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInAbstractMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInInterfaceMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInGenericInterfaceMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedInInterfaceIndexer(string arg); + + Task ReportsNoDiagnostics_WhenUsedInVirtualIndexer(string arg); + + Task ReportsDiagnostics_WhenUsedInNonVirtualIndexer(string arg); + + Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string arg); + + Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); + + Task ReportsDiagnostics_WhenUsedAsStandaloneExpression(string arg); + + Task ReportsDiagnostics_WhenUsedInConstructor(string arg); + + Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToNotApplied(string arg); + + Task ReportsNoDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToApplied(string arg); + + Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string arg); + + Task ReportsNoDiagnostics_WhenUsedInProtectedInternalVirtualMember(string arg); } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherMisuseDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherMisuseDiagnosticVerifier.cs deleted file mode 100644 index 53104703..00000000 --- a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/IArgumentMatcherMisuseDiagnosticVerifier.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Threading.Tasks; - -namespace NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers -{ - public interface IArgumentMatcherMisuseDiagnosticVerifier - { - Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForMethodCall(string arg); - - Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForIndexerCall(string arg); - - Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedArgMethod(string arg); - - Task ReportsDiagnostics_WhenUseTogetherWithUnfortunatelyNamedArgDoInvoke(string argDoInvoke, string arg); - - Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForMethodCall(string argDo, string arg); - - Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForIndexerCall(string argDo, string arg); - - Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForMethodCall(string argInvoke, string arg); - - Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForIndexerCall(string argInvoke, string arg); - - Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs index 0e4bb4fa..5de736ce 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherDiagnosticVerifier.cs @@ -1,9 +1,9 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Shared; using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; using NSubstitute.Analyzers.VisualBasic; using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers; using Xunit; @@ -14,54 +14,126 @@ public abstract class ArgumentMatcherDiagnosticVerifier : VisualBasicDiagnosticV { protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.ArgumentMatcherUsedWithoutSpecifyingCall; - [CombinatoryTheory] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("TryCast(Arg.Any(Of Integer)(), Object)")] - [InlineData("CType(Arg.Any(Of Integer)(), Integer)")] - [InlineData("DirectCast(Arg.Any(Of Integer)(), Integer)")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("TryCast(Arg.Compat.Any(Of Integer)(), Object)")] - [InlineData("CType(Arg.Compat.Any(Of Integer)(), Integer)")] - [InlineData("DirectCast(Arg.Compat.Any(Of Integer)(), Integer)")] - [InlineData("Arg.Is(1)")] - [InlineData("TryCast(Arg.Is(1), Object)")] - [InlineData("CType(Arg.Is(1), Integer)")] - [InlineData("DirectCast(Arg.Is(1), Integer)")] - [InlineData("Arg.Compat.Is(1)")] - [InlineData("TryCast(Arg.Compat.Is(1), Object)")] - [InlineData("CType(Arg.Compat.Is(1), Integer)")] - [InlineData("DirectCast(Arg.Compat.Is(1), Integer)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg); - - [CombinatoryTheory] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("TryCast(Arg.Any(Of Integer)(), Object)")] - [InlineData("CType(Arg.Any(Of Integer)(), Integer)")] - [InlineData("DirectCast(Arg.Any(Of Integer)(), Integer)")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("TryCast(Arg.Compat.Any(Of Integer)(), Object)")] - [InlineData("CType(Arg.Compat.Any(Of Integer)(), Integer)")] - [InlineData("DirectCast(Arg.Compat.Any(Of Integer)(), Integer)")] - [InlineData("Arg.Is(1)")] - [InlineData("TryCast(Arg.Is(1), Object)")] - [InlineData("CType(Arg.Is(1), Integer)")] - [InlineData("DirectCast(Arg.Is(1), Integer)")] - [InlineData("Arg.Compat.Is(1)")] - [InlineData("TryCast(Arg.Compat.Is(1), Object)")] - [InlineData("CType(Arg.Compat.Is(1), Integer)")] - [InlineData("DirectCast(Arg.Compat.Is(1), Integer)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg); - - [CombinatoryTheory] - [InlineData("[|Arg.Any(Of Integer)()|]")] - [InlineData("[|Arg.Compat.Any(Of Integer)()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg); + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInStaticMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInVirtualMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInNonSealedOverrideMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInDelegate(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInSealedOverrideMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInAbstractMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInterfaceMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInGenericInterfaceMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInterfaceIndexer(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInVirtualIndexer(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualIndexer(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedAsStandaloneExpression(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInConstructor(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToNotApplied(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToApplied(string arg); + + [Theory] + [MemberData(nameof(MisusedArgTestCases))] + public abstract Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string arg); + + [Theory] + [MemberData(nameof(CorrectlyUsedArgTestCases))] + public abstract Task ReportsNoDiagnostics_WhenUsedInProtectedInternalVirtualMember(string arg); protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() { return new ArgumentMatcherAnalyzer(); } + + public static IEnumerable MisusedArgTestCases + { + get + { + yield return new object[] { "[|Arg.Any(Of Integer)()|]" }; + yield return new object[] { "[|Arg.Compat.Any(Of Integer)()|]" }; + yield return new object[] { "[|Arg.Is(1)|]" }; + yield return new object[] { "[|Arg.Compat.Is(1)|]" }; + } + } + + public static IEnumerable CorrectlyUsedArgTestCases + { + get + { + yield return new object[] { "Arg.Any(Of Integer)()" }; + yield return new object[] { "TryCast(Arg.Any(Of Integer)(), Object)" }; + yield return new object[] { "CType(Arg.Any(Of Integer)(), Integer)" }; + yield return new object[] { "DirectCast(Arg.Any(Of Integer)(), Integer)" }; + + yield return new object[] { "Arg.Compat.Any(Of Integer)()" }; + yield return new object[] { "TryCast(Arg.Compat.Any(Of Integer)(), Object)" }; + yield return new object[] { "CType(Arg.Compat.Any(Of Integer)(), Integer)" }; + yield return new object[] { "DirectCast(Arg.Compat.Any(Of Integer)(), Integer)" }; + + yield return new object[] { "Arg.Is(1)" }; + yield return new object[] { "TryCast(Arg.Is(1), Object)" }; + yield return new object[] { "CType(Arg.Is(1), Integer)" }; + yield return new object[] { "DirectCast(Arg.Is(1), Integer)" }; + + yield return new object[] { "Arg.Compat.Is(1)" }; + yield return new object[] { "TryCast(Arg.Compat.Is(1), Object)" }; + yield return new object[] { "CType(Arg.Compat.Is(1), Integer)" }; + yield return new object[] { "DirectCast(Arg.Compat.Is(1), Integer)" }; + } + } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs deleted file mode 100644 index 78c55891..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseDiagnosticVerifier.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; -using NSubstitute.Analyzers.VisualBasic; -using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers; -using Xunit; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - public abstract class ArgumentMatcherMisuseDiagnosticVerifier : VisualBasicDiagnosticVerifier, IArgumentMatcherMisuseDiagnosticVerifier - { - protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.ArgumentMatcherUsedWithoutSpecifyingCall; - - [Theory] - [InlineData("[|Arg.Any(Of Integer)()|]")] - [InlineData("[|Arg.Compat.Any(Of Integer)()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForMethodCall(string arg); - - [Theory] - [InlineData("[|Arg.Any(Of Integer)()|]")] - [InlineData("[|Arg.Compat.Any(Of Integer)()|]")] - [InlineData("[|Arg.Is(1)|]")] - [InlineData("[|Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForIndexerCall(string arg); - - [Theory] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedArgMethod(string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Do(Of Integer)(1)", "Arg.Compat.Do(Of Integer)(1)", "Arg.Invoke(Of Integer)(1)", "Arg.Compat.Invoke(Of Integer)(1)", "Arg.InvokeDelegate(Of Integer)(1)", "Arg.Compat.InvokeDelegate(Of Integer)(1)")] - [InlineData("[|NSubstitute.Arg.Any(Of Integer)()|]")] - [InlineData("[|NSubstitute.Arg.Compat.Any(Of Integer)()|]")] - [InlineData("[|NSubstitute.Arg.Is(1)|]")] - [InlineData("[|NSubstitute.Arg.Compat.Is(1)|]")] - public abstract Task ReportsDiagnostics_WhenUseTogetherWithUnfortunatelyNamedArgDoInvoke(string argDoInvoke, string arg); - - [CombinatoryTheory] - [CombinatoryData( - @"Arg.Do(Of Integer)(Function() - End Function)", - @"Arg.Compat.Do(Of Integer)(Function() - End Function)")] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForMethodCall(string argDo, string arg); - - [CombinatoryTheory] - [CombinatoryData( - @"Arg.Do(Of Integer)(Function() - End Function)", - @"Arg.Compat.Do(Of Integer)(Function() - End Function)")] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForIndexerCall(string argDo, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Invoke(Of Integer)(1)", "Arg.Compat.Invoke(Of Integer)(1)", "Arg.InvokeDelegate(Of Action(Of Integer))(1)", "Arg.Compat.InvokeDelegate(Of Action(Of Integer))(1)")] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForMethodCall(string argInvoke, string arg); - - [CombinatoryTheory] - [CombinatoryData("Arg.Invoke(Of Integer)(1)", "Arg.Compat.Invoke(Of Integer)(1)", "Arg.InvokeDelegate(Of Action(Of Integer))(1)", "Arg.Compat.InvokeDelegate(Of Action(Of Integer))(1)")] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForIndexerCall(string argInvoke, string arg); - - [Theory] - [InlineData("Arg.Any(Of Integer)()")] - [InlineData("Arg.Compat.Any(Of Integer)()")] - [InlineData("Arg.Is(1)")] - [InlineData("Arg.Compat.Is(1)")] - public abstract Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ArgumentMatcherAnalyzer(); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs deleted file mode 100644 index d4ebb6d4..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherMisuseTests.cs +++ /dev/null @@ -1,339 +0,0 @@ -using System.Threading.Tasks; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - public class ArgumentMatcherMisuseTests : ArgumentMatcherMisuseDiagnosticVerifier - { - public override async Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForMethodCall(string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Public Property I As Integer - - Public Sub New() - End Sub - - Public Sub New(ByVal x As Integer) - End Sub - - Public Function FooBar(ByVal x As Integer, ByVal y As Integer) As Integer - Return 1 - End Function - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}, {arg}) - Dim bar = substitute.Bar({arg}, {arg}) - Dim newBar = New Bar().FooBar({arg}, {arg}) - Dim otherBar = New Bar({arg}) - Dim yetAnotherBar = New Bar With {{ - .I = {arg} - }} - Dim anonymous = New With {{ - .I = {arg} - }} - substitute.[When](Function(x) - Dim innerNewBar = New Bar().FooBar({arg}, {arg}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsDiagnostics_WhenUsedWithoutSubstituteMethod_ForIndexerCall(string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Default Public ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Integer - Get - Return 1 - End Get - End Property - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim indexer = substitute({arg}, {arg}) - Dim newBar = (New Bar())({arg}, {arg}) - substitute.[When](Function(x) - Dim innerNewBar = (New Bar())({arg}, {arg}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedArgMethod(string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Public Function FooBar(ByVal x As Integer, ByVal y As Integer) As Integer - Return 1 - End Function - End Class - - Public Class Arg - Public Shared Function Any(Of T)() As T - Return Nothing - End Function - - Public Shared Function [Is](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Class Compat - Public Shared Function Any(Of T)() As T - Return Nothing - End Function - - Public Shared Function [Is](Of T)(ByVal value As T) As T - Return Nothing - End Function - End Class - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}, {arg}) - Dim bar = substitute.Bar({arg}, {arg}) - Dim newBar = (New Bar()).FooBar({arg}, {arg}) - substitute.[When](Function(x) - Dim innerBar = (New Bar()).FooBar({arg}, {arg}) - End Function) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUseTogetherWithUnfortunatelyNamedArgDoInvoke(string argDoInvoke, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Public Function FooBar(ByVal x As Integer, ByVal y As Integer) As Integer - Return 1 - End Function - End Class - - Public Class Arg - Public Shared Function [Do](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Public Shared Function [Invoke](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Public Shared Function [InvokeDelegate](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Class Compat - Public Shared Function [Do](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Public Shared Function [Invoke](Of T)(ByVal value As T) As T - Return Nothing - End Function - - Public Shared Function [InvokeDelegate](Of T)(ByVal value As T) As T - Return Nothing - End Function - End Class - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}, {argDoInvoke}) - Dim bar = substitute.Bar({argDoInvoke}, {arg}) - Dim newBar = (New Bar()).FooBar({arg}, {argDoInvoke}) - substitute.[When](Function(x) - Dim innerBar = (New Bar()).FooBar({argDoInvoke}, {arg}) - End Function) - End Sub - End Class -End Namespace -"; - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForMethodCall(string argDo, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Public Function FooBar(ByVal x As Integer, ByVal y As Integer) As Integer - Return 1 - End Function - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}, {argDo}) - Dim bar = substitute.Bar({argDo}, {arg}) - Dim newBar = New Bar().FooBar({arg}, {argDo}) - substitute.[When](Function(x) - Dim innerNewBar = New Bar().FooBar({argDo}, {arg}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgDo_ForIndexerCall(string argDo, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Integer - End Class - - Public Class Bar - Default Public ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Integer - Get - Return 1 - End Get - End Property - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim indexer = substitute({arg}, {argDo}) - Dim newBar = (New Bar())({argDo}, {arg}) - substitute.[When](Function(x) - Dim innerNewBar = (New Bar())({arg}, {argDo}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForMethodCall(string argInvoke, string arg) - { - var source = $@"Imports System -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer, ByVal y As Action(Of Integer)) As Integer - End Class - - Public Class Bar - Public Function FooBar(ByVal x As Integer, ByVal y As Action(Of Integer)) As Integer - Return 1 - End Function - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}, {argInvoke}) - Dim bar = substitute.Bar({arg}, {argInvoke}) - Dim newBar = New Bar().FooBar({arg}, {argInvoke}) - substitute.[When](Function(x) - Dim innerNewBar = New Bar().FooBar({arg}, {argInvoke}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUseTogetherWithArgInvoke_ForIndexerCall(string argInvoke, string arg) - { - var source = $@"Imports System -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Integer, ByVal y As Action(Of Integer)) As Integer - End Class - - Public Class Bar - Default Public ReadOnly Property Item(ByVal x As Integer, ByVal y As Action(Of Integer)) As Integer - Get - Return 1 - End Get - End Property - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim indexer = substitute({arg}, {argInvoke}) - Dim newBar = (New Bar())({arg}, {argInvoke}) - substitute.[When](Function(x) - Dim innerNewBar = (New Bar())({arg}, {argInvoke}) - End Function) - End Sub - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg) - { - var source = $@"Imports System -Imports NSubstitute - -Namespace MyNamespace - Public Class FooTests - Public Sub Test() - Dim arg As Integer = NSubstitute.{arg} - End Sub - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs new file mode 100644 index 00000000..7cd1b3b0 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ArgumentMatcherTests.cs @@ -0,0 +1,488 @@ +using System.Threading.Tasks; + +namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests +{ + public class ArgumentMatcherTests : ArgumentMatcherDiagnosticVerifier + { + public override async Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Function Bar(ByVal firstArg As Integer) As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInStaticMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Shared Function Bar(ByVal firstArg As Integer) As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Foo.Bar({arg}) + End Sub + End Class +End Namespace"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInVirtualMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar(ByVal firstArg As Integer?) As Integer + Return 2 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInNonSealedOverrideMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar(ByVal firstArg As Integer?) As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public Overrides Function Bar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInDelegate(string arg) + { + var source = $@"Imports NSubstitute +Imports System + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer?, Integer))() + Dim x = substitute({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInSealedOverrideMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Public Overridable Function Bar(ByVal firstArg As Integer) As Integer + Return 2 + End Function + End Class + + Public Class Foo2 + Inherits Foo + + Public NotOverridable Overrides Function Bar(ByVal firstArg As Integer) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo2)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInAbstractMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public MustInherit Class Foo + Public MustOverride Function Bar(ByVal firstArg As Integer?) As Integer + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace"; + + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInterfaceMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Function Bar(ByVal firstArg As Integer?) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInGenericInterfaceMethod(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + + Public Interface IFoo(Of T) + Function Bar(Of T)(ByVal firstArg as Integer?) As Integer + End Interface + + Public Class FooTests + + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of IFoo(Of Integer))() + substitute.Bar(Of Integer)({arg}) + End Sub + End Class +End Namespace"; + + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInterfaceIndexer(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Interface IFoo + Default ReadOnly Property Item(ByVal i As Integer?) As Integer + End Interface + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of IFoo)() + Dim x = substitute({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInVirtualIndexer(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Default Public Overridable ReadOnly Property Item(ByVal x As Integer?) As Integer + Get + Return 0 + End Get + End Property + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInNonVirtualIndexer(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Get + Return 0 + End Get + End Property + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string arg) + { + var source = $@"Imports NSubstitute +Imports System.Runtime.CompilerServices + +Namespace MyNamespace + Public Class Foo + Public Function Bar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class Arg + Public Shared Function Any(Of T)() As T + Return Nothing + End Function + + Public Shared Function [Is](Of T)(ByVal value As T) As T + Return Nothing + End Function + + Public Shared Function Invoke(Of T)(ByVal value As T) As T + Return Nothing + End Function + + Public Shared Function [Do](Of T)(ByVal value As T) As T + Return Nothing + End Function + + Public Class Compat + Public Shared Function Any(Of T)() As T + Return Nothing + End Function + + Public Shared Function [Is](Of T)(ByVal value As T) As T + Return Nothing + End Function + + Public Shared Function Invoke(Of T)(ByVal value As T) As T + Return Nothing + End Function + + Public Shared Function [Do](Of T)(ByVal value As T) As T + Return Nothing + End Function + End Class + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.Bar({arg}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsNoDiagnostics_WhenUsedWithPotentiallyValidAssignment(string arg) + { + var source = $@"Imports System +Imports NSubstitute + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + Dim x = {arg} + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedAsStandaloneExpression(string arg) + { + var source = $@"Imports System +Imports NSubstitute + +Namespace MyNamespace + Public Class FooTests + Public Sub Test() + {arg} + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInConstructor(string arg) + { + var source = $@"Imports System +Imports NSubstitute + +Namespace MyNamespace + Public Class FooTests + Public Sub New(ByVal firstArg As Integer) + End Sub + + Public Sub Test() + Dim x = New FooTests({arg}) + End Sub + End Class +End Namespace +"; + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToNotApplied(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.FooBar({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToApplied(string arg) + { + var source = $@"Imports NSubstitute +Imports System.Runtime.CompilerServices + + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.FooBar({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyNoDiagnostic(source); + } + + public override async Task ReportsDiagnostics_WhenUsedInInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string arg) + { + var source = $@"Imports NSubstitute +Imports System.Runtime.CompilerServices + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.FooBar({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); + } + + public override async Task ReportsNoDiagnostics_WhenUsedInProtectedInternalVirtualMember(string arg) + { + var source = $@"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Protected Friend Overridable Function FooBar(ByVal firstArg As Integer?) As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.FooBar({arg}) + End Sub + End Class +End Namespace +"; + + await VerifyNoDiagnostic(source); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs deleted file mode 100644 index 830308ee..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsExtensionMethodTests.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Received", "ReceivedWithAnyArgs", "DidNotReceive", "DidNotReceiveWithAnyArgs")] - public class ReceivedAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.{method}().Bar({arg}) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim x = substitute.{method}()({arg}) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.{method}(1D).Bar({arg}) - End Sub - End Class - - Module SubstituteExtensions - - Function Received(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function ReceivedWithAnyArgs(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function DidNotReceive(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function DidNotReceiveWithAnyArgs(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs deleted file mode 100644 index 21836fdc..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData( - "SubstituteExtensions.Received", - "SubstituteExtensions.Received(Of Foo)", - "SubstituteExtensions.ReceivedWithAnyArgs", - "SubstituteExtensions.ReceivedWithAnyArgs(Of Foo)", - "SubstituteExtensions.DidNotReceive", - "SubstituteExtensions.DidNotReceive(Of Foo)", - "SubstituteExtensions.DidNotReceiveWithAnyArgs", - "SubstituteExtensions.DidNotReceiveWithAnyArgs(Of Foo)")] - public class ReceivedAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute).Bar({arg}) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim x = {method}(substitute)({arg}) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute, 1D).Bar({arg}) - End Sub - End Class - - Module SubstituteExtensions - - Function Received(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function ReceivedWithAnyArgs(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function DidNotReceive(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - - - Function DidNotReceiveWithAnyArgs(Of T)(ByVal returnValue As T, ByVal x As Decimal) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs deleted file mode 100644 index 317ac447..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReceivedInOrderMethodTests.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Received.InOrder")] - public class ReceivedInOrderMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - NSubstitute.{method}(Function() substitute.Bar({arg})) - NSubstitute.{method}(Function() - substitute.Bar({arg}) - End Function) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - NSubstitute.{method}(Function() - Dim x = substitute({arg}) - End Function) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - MyNamespace.{method}(Sub() - Dim x = substitute({arg}) - End Sub, 1) - End Sub - End Class - - Public Class Received - Public Shared Sub InOrder(ByVal calls As Action, ByVal x As Integer) - End Sub - End Class -End Namespace"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs deleted file mode 100644 index 8643d368..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Returns", "ReturnsForAnyArgs")] - public class ReturnsAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}).{method}(1) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute({arg}).{method}(1) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - Dim x = substitute.Bar({arg}).{method}(1) - End Sub - End Class - - Module SubstituteExtensions - - Function Returns(Of T)(ByVal returnValue As T, ByVal returnThis As T) As T - Return Nothing - End Function - - - Function ReturnsForAnyArgs(Of T)(ByVal returnValue As T, ByVal returnThis As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs deleted file mode 100644 index 3a164e42..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData( - "SubstituteExtensions.Returns", - "SubstituteExtensions.Returns(Of Integer)", - "SubstituteExtensions.ReturnsForAnyArgs", - "SubstituteExtensions.ReturnsForAnyArgs(Of Integer)")] - public class ReturnsAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg}), 1) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute({arg}), 1) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg}), 1) - End Sub - End Class - - Module SubstituteExtensions - - Function Returns(Of T)(ByVal returnValue As T, ByVal returnThis As T) As T - Return Nothing - End Function - - - Function ReturnsForAnyArgs(Of T)(ByVal returnValue As T, ByVal returnThis As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs deleted file mode 100644 index 064f83f6..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ReturnsNull", "ReturnsNullForAnyArgs")] - public class ReturnsNullAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports NSubstitute.ReturnsExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}).{method}() - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports NSubstitute.ReturnsExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute({arg}).{method}() - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}).{method}() - End Sub - End Class - - Module ReturnsExtensions - - Function ReturnsNull(Of T)(ByVal returnValue As T) As T - Return Nothing - End Function - - - Function ReturnsNullForAnyArgs(Of T)(ByVal returnValue As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs deleted file mode 100644 index b8122d3b..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData( - "ReturnsExtensions.ReturnsNull", - "ReturnsExtensions.ReturnsNull(Of Object)", - "ReturnsExtensions.ReturnsNullForAnyArgs", - "ReturnsExtensions.ReturnsNullForAnyArgs(Of Object)")] - public class ReturnsNullAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports NSubstitute.ReturnsExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg})) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports NSubstitute.ReturnsExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute({arg})) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg})) - End Sub - End Class - - Module ReturnsExtensions - - Function ReturnsNull(Of T)(ByVal returnValue As T) As T - Return Nothing - End Function - - - Function ReturnsNullForAnyArgs(Of T)(ByVal returnValue As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs deleted file mode 100644 index e9972b9c..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("Throws", "ThrowsForAnyArgs")] - public class ThrowsAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}).{method}(New Exception()) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute({arg}).{method}(New Exception()) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.Bar({arg}).{method}(new Exception()) - End Sub - End Class - - Module ExceptionExtensions - - Function Throws(Of T As Exception)(ByVal value As Object, ByVal ex As T) As T - Return Nothing - End Function - - - Function ThrowsForAnyArgs(Of T As Exception)(ByVal value As Object, ByVal ex As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs deleted file mode 100644 index 7342d56d..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] - public class ThrowsAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg}), New Exception()) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute({arg}), New Exception()) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg}), New Exception()) - End Sub - End Class - - Module ExceptionExtensions - - Function Throws(Of T As Exception)(ByVal value As Object, ByVal ex As T) As T - Return Nothing - End Function - - - Function ThrowsForAnyArgs(Of T As Exception)(ByVal value As Object, ByVal ex As T) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs deleted file mode 100644 index ff9dd575..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("ExceptionExtensions.Throws(Of Exception)", "ExceptionExtensions.ThrowsForAnyArgs(Of Exception)")] - public class ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg})) - End Sub - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports NSubstitute.ExceptionExtensions - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute({arg})) - End Sub - End Class -End Namespace -"; - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Foo - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute.Bar({arg})) - End Sub - End Class - - Module ExceptionExtensions - - Function Throws(Of T As Exception)(ByVal value As Object) As T - Return Nothing - End Function - - - Function ThrowsForAnyArgs(Of T As Exception)(ByVal value As Object) As T - Return Nothing - End Function - End Module -End Namespace -"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs deleted file mode 100644 index 69bcd2ed..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsExtensionMethodTests.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData("When", "WhenForAnyArgs")] - public class WhenAsExtensionMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.{method}(Function(ByVal x As Foo) - x.Bar({arg}) - End Function).[Do](Function(x) - Throw New NullReferenceException() - End Function) - substitute.{method}(AddressOf SubstituteCall).[Do](Function(x) - Throw New NullReferenceException() - End Function) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - obj.Bar({arg}) - Return Task.CompletedTask - End Function - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.{method}(Function(ByVal x As Foo) - Dim y = x({arg}) - End Function).[Do](Function(x) - Throw New NullReferenceException() - End Function) - substitute.{method}(AddressOf SubstituteCall).[Do](Function(x) - Throw New NullReferenceException() - End Function) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - Dim x = obj({arg}) - Return Task.CompletedTask - End Function - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - substitute.{method}(Function(ByVal x As Foo) - x.Bar({arg}) - End Function, 1) - substitute.{method}(Function(x) x.Bar({arg}), 1) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - obj.Bar({arg}) - Return Task.CompletedTask - End Function - End Class - - Module SubstituteExtensions - - Function [When](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T - Return Nothing - End Function - - - Function [WhenForAnyArgs](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T - Return Nothing - End Function - End Module -End Namespace"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs deleted file mode 100644 index aba5e4a3..00000000 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ArgumentMatcherAnalyzerTests/WhenAsOrdinaryMethodTests.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System.Threading.Tasks; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; - -namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.ArgumentMatcherAnalyzerTests -{ - [CombinatoryData( - "SubstituteExtensions.When", - "SubstituteExtensions.When(Of Foo)", - "SubstituteExtensions.WhenForAnyArgs", - "SubstituteExtensions.WhenForAnyArgs(Of Foo)")] - public class WhenAsOrdinaryMethodTests : ArgumentMatcherDiagnosticVerifier - { - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForMethodCall(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute, Function(ByVal x As Foo) - x.Bar({arg}) - End Function).[Do](Function(x) - Throw New NullReferenceException() - End Function) - {method}(substitute, AddressOf SubstituteCall).[Do](Function(x) - Throw New NullReferenceException() - End Function) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - obj.Bar({arg}) - Return Task.CompletedTask - End Function - End Class -End Namespace -"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsNoDiagnostics_WhenUsedWithSubstituteMethod_ForIndexerCall(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute - -Namespace MyNamespace - Public MustInherit Class Foo - Default Public MustOverride ReadOnly Property Item(ByVal x As Object) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute, Function(ByVal x As Foo) - Dim y = x({arg}) - End Function).[Do](Function(x) - Throw New NullReferenceException() - End Function) - {method}(substitute, AddressOf SubstituteCall).[Do](Function(x) - Throw New NullReferenceException() - End Function) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - Dim x = obj({arg}) - Return Task.CompletedTask - End Function - End Class -End Namespace"; - - await VerifyNoDiagnostic(source); - } - - public override async Task ReportsDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method, string arg) - { - var source = $@"Imports System -Imports System.Threading.Tasks -Imports NSubstitute -Imports System.Runtime.CompilerServices - -Namespace MyNamespace - Public MustInherit Class Foo - Public MustOverride Function Bar(ByVal x As Integer) As Integer - End Class - - Public Class FooTests - Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo)() - {method}(substitute, Function(ByVal x As Foo) - x.Bar({arg}) - End Function, 1) - {method}(substitute, Function(x) x.Bar({arg}), 1) - End Sub - - Private Function SubstituteCall(ByVal obj As Foo) As Task - obj.Bar({arg}) - Return Task.CompletedTask - End Function - End Class - - Module SubstituteExtensions - - Function [When](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T - Return Nothing - End Function - - - Function [WhenForAnyArgs](Of T)(ByVal substitute As T, ByVal substituteCall As System.Action(Of T), ByVal x As Integer) As T - Return Nothing - End Function - End Module -End Namespace"; - - await VerifyDiagnostic(source, ArgumentMatcherUsedWithoutSpecifyingCall); - } - } -} \ No newline at end of file