-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rules: - **PosInfoMoq2004**: Check that no arguments is passed to mocked interface (fixes #14). - **PosInfoMoq2005**: Check the arguments passed to mocked abstract classes (fixes #15). - **PosInfoMoq2006**: Check setups with Protected() mocks (fixes #10 and fixes #11). Improvements/fixes: - The **PosInfoMoq2000** rule does not raise an error when the `Returns()`/`ReturnsAsync()` methods is invalided by the compiler (fixes #16). - The **PosInfoMoq2001** rule check that each chained member access in a `Setup()` method is overridable (fixes #17). Internal: - Propagate the cancellation token to allow to cancel analysis if the host (the compiler for example) cancel the analysis process. - Migrates the Azure Pipelines to Github actions (fixes #9).
- Loading branch information
1 parent
6880a50
commit f19dd54
Showing
34 changed files
with
2,001 additions
and
230 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Continuous Integration | ||
|
||
on: | ||
pull_request: | ||
branches: [ "main" ] | ||
push: | ||
branches: [ "releases/**" ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup .NET 8.x | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: '8.x' | ||
|
||
- name: Build | ||
run: dotnet build --property:Configuration=Debug "PosInformatique.Moq.Analyzers.sln" | ||
|
||
- name: Test with the dotnet CLI | ||
run: dotnet test --property:Configuration=Debug "PosInformatique.Moq.Analyzers.sln" |
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,36 @@ | ||
name: Release | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
VersionPrefix: | ||
type: string | ||
description: The version of the library | ||
required: true | ||
default: 1.5.0 | ||
VersionSuffix: | ||
type: string | ||
description: The version suffix of the library (for example rc.1) | ||
|
||
run-name: ${{ inputs.VersionPrefix }}-${{ inputs.VersionSuffix }} | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup .NET 8.x | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: '8.x' | ||
|
||
- name: Build | ||
run: dotnet pack | ||
--property:Configuration=Release | ||
--property:VersionPrefix=${{ github.event.inputs.VersionPrefix }} | ||
--property:VersionSuffix=${{ github.event.inputs.VersionSuffix }} | ||
"src/Moq.Analyzers/Moq.Analyzers.csproj" | ||
|
||
- name: Publish the package to nuget.org | ||
run: dotnet nuget push "src/Moq.Analyzers/bin/Release/*.nupkg" --api-key "${{ secrets.NUGET_APIKEY }}" --source https://api.nuget.org/v3/index.json |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# PosInfoMoq2004: The `Callback()` delegate expression must match the signature of the mocked method | ||
|
||
| Property | Value | | ||
|-------------------------------------|---------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2004 | | ||
| **Title** | Constructor arguments cannot be passed for interface mocks. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
Constructor arguments has been passed to a mocked interface. | ||
|
||
## Rule description | ||
|
||
It is not possible to pass contructor arguments to mocked interface. Is only possible with non-sealed class | ||
or abstract class. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service1 = new Mock<IService>("Argument 1", 2); // No constructor arguments can be passed to mocked interface. | ||
var service2 = new Mock<IService>(MockBehavior.Strict, "Argument 1", 2); // Same if we use the MockBehavior. | ||
} | ||
|
||
public interface IService | ||
{ | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure to pass parameters to mocked abstract class. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `MoqException` | ||
thrown with the *"Constructor arguments cannot be passed for interface mocks."* message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# PosInfoMoq2005: The `Callback()` delegate expression must match the signature of the mocked method | ||
|
||
| Property | Value | | ||
|-------------------------------------|-------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2005 | | ||
| **Title** | Constructor arguments must match the constructors of the mocked class. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
Constructor arguments must match the constructors of the mocked class. | ||
|
||
## Rule description | ||
|
||
When configurate mock, all the arguments must match one of the constructor of the mocked type. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service1 = new Mock<Service>(1, 2, 3); // The arguments does not match one of the Service type constructors. | ||
var service2 = new Mock<Service>("Argument 1", 2); // OK | ||
var service3 = new Mock<Service>(MockBehavior.Strict, "Argument 1", 2); // OK | ||
} | ||
|
||
public abstract class Service | ||
{ | ||
public Service(string a) | ||
{ | ||
} | ||
|
||
public Service(string a, int b) | ||
{ | ||
} | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure to pass right arguments of one of the constructor of the mocked instance. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `MoqException` | ||
thrown with the *"Constructor arguments must match the constructors of the mocked class."* message. |
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,56 @@ | ||
# PosInfoMoq2006: The Protected().Setup() method must be use with overridable protected or internal methods | ||
|
||
| Property | Value | | ||
|-------------------------------------|----------------------------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2006 | | ||
| **Title** | The `Protected().Setup()` method must be use with overridable protected or internal methods. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
A `Protected().Setup()` reference a method in the mocked type which is: | ||
- Not existing in the mocked type. | ||
- Not overridable (`abstract`, `virtual` or `override`, but not `sealed`). | ||
- Not `protected` or `internal`. | ||
|
||
## Rule description | ||
|
||
When using the `Protected().Setup()`, the method mocked must be `protected`, `internal` or `protected internal`, | ||
and must be overridable (`virtual`, `abstract` and `override`, but not `sealed`). | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = new Mock<Service>(1, 2, 3); | ||
service.Protected().Setup("GetData") // The GetData() is public and can be mocked with Protected() feature. | ||
.Returns(10); | ||
service.Protected().Setup("NotExists") // The NotExists() method does not exist. | ||
.Returns(10); | ||
service.Protected().Setup("YouCantOverrideMe") // The YouCantOverrideMe() is not virtual or abstract. | ||
.Returns(10); | ||
} | ||
|
||
public abstract class Service | ||
{ | ||
public abstract int GetData(); | ||
|
||
protected void YouCantOverrideMe() { }; | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, use the `Protected().Setup()` to mock method which are: | ||
- `protected` | ||
- `internal` | ||
- `protected internal` | ||
- Overridable (`virtual`, `abstract` or `override`, but not `sealed`). | ||
|
||
Else use the standard mocking feature without the `Protected()` method. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `MoqException` | ||
thrown with the *"Method X.xxxx is public. Use strong-typed."* message. |
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
Oops, something went wrong.