-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [GH-108] - detecting non-virtual calls in Received.InOrder blocks
- Loading branch information
Showing
48 changed files
with
2,924 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...Source.CSharp/DiagnosticsSources/NonSubstitutableMemberReceivedInOrderDiagnosticSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using NSubstitute.Analyzers.Benchmarks.Source.CSharp.Models; | ||
|
||
namespace NSubstitute.Analyzers.Benchmarks.Source.CSharp.DiagnosticsSources | ||
{ | ||
public class NonSubstitutableMemberReceivedInOrderDiagnosticSource | ||
{ | ||
public void NS1005_NonVirtualSetupSpecification() | ||
{ | ||
var substitute = Substitute.For<Foo>(); | ||
|
||
Received.InOrder(() => | ||
{ | ||
substitute.ObjectReturningMethod(); | ||
var methodItem = substitute.ObjectReturningMethod(); | ||
substitute.ObjectReturningMethodWithArguments(substitute.IntReturningProperty, 1, 1m); | ||
_ = substitute.IntReturningProperty; | ||
var propertyItem = substitute.IntReturningProperty; | ||
_ = substitute[0]; | ||
var indexerItem = substitute[0]; | ||
var otherIndexerItem = substitute[0]; | ||
var usedIndexerItem = otherIndexerItem; | ||
}); | ||
} | ||
|
||
public void NS1003_InternalVirtualSetupSpecification() | ||
{ | ||
var substitute = Substitute.For<Foo>(); | ||
|
||
substitute.InternalObjectReturningMethod(); | ||
var methodItem = substitute.InternalObjectReturningMethod(); | ||
substitute.InternalObjectReturningMethodWithArguments(substitute.IntReturningProperty); | ||
_ = substitute.InternalObjectReturningProperty; | ||
var propertyItem = substitute.InternalObjectReturningProperty; | ||
_ = substitute[0]; | ||
var indexerItem = substitute[0, 0, 0]; | ||
var otherIndexerItem = substitute[0, 0, 0]; | ||
var usedIndexerItem = otherIndexerItem; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# NS1005 | ||
|
||
<table> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>NS1005</td> | ||
</tr> | ||
<tr> | ||
<td>Category</td> | ||
<td>Non-substitutable member</td> | ||
</tr> | ||
</table> | ||
|
||
## Cause | ||
|
||
Checking call order for non-virtual member of a class. | ||
|
||
## Rule description | ||
|
||
A violation of this rule occurs when `Received.InOrder` checks call order for non-virtual member of a class. | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, make the member of your class virtual or substitute for interface. | ||
|
||
## How to suppress violations | ||
|
||
This warning can be suppressed by disabling the warning in the **ruleset** file for the project or by suppressing it (for selected members) in `nsubstitute.json` file. See the [configuration](../Configuration.md) section for details on how to set this up. | ||
The warning can also be suppressed programmatically for an assembly: | ||
````c# | ||
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Non-substitutable member", "NS1005:Non-virtual setup specification.", Justification = "Reviewed")] | ||
```` | ||
|
||
Or for a specific code block: | ||
````c# | ||
#pragma warning disable NS1005 // Non-virtual setup specification. | ||
// the code which produces warning | ||
#pragma warning restore NS1005 // Non-virtual setup specification. | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/NonSubstitutableMemberAnalysis.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers | ||
{ | ||
internal class NonSubstitutableMemberAnalysis : AbstractNonSubstitutableMemberAnalysis | ||
{ | ||
public static NonSubstitutableMemberAnalysis Instance { get; } = new NonSubstitutableMemberAnalysis(); | ||
|
||
private NonSubstitutableMemberAnalysis() | ||
{ | ||
} | ||
|
||
protected override ImmutableHashSet<Type> KnownNonVirtualSyntaxKinds { get; } = ImmutableHashSet.Create( | ||
typeof(LiteralExpressionSyntax)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ute.Analyzers.CSharp/DiagnosticAnalyzers/NonSubstitutableMemberReceivedInOrderAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
internal sealed class NonSubstitutableMemberReceivedInOrderAnalyzer : AbstractNonSubstitutableMemberReceivedInOrderAnalyzer<SyntaxKind, InvocationExpressionSyntax, MemberAccessExpressionSyntax, BlockSyntax> | ||
{ | ||
private static ImmutableArray<int> IgnoredPaths { get; } = | ||
ImmutableArray.Create((int)SyntaxKind.Argument, (int)SyntaxKind.VariableDeclarator, (int)SyntaxKind.AddAssignmentExpression); | ||
|
||
public NonSubstitutableMemberReceivedInOrderAnalyzer() | ||
: base(SubstitutionNodeFinder.Instance, NonSubstitutableMemberAnalysis.Instance, NSubstitute.Analyzers.CSharp.DiagnosticDescriptorsProvider.Instance) | ||
{ | ||
} | ||
|
||
protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; | ||
|
||
protected override ImmutableArray<int> IgnoredAncestorPaths { get; } = IgnoredPaths; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.