-
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.
feat: add suppressor of CA1707 for test methods (#97)
enable .NET analyzers
- Loading branch information
Showing
57 changed files
with
1,856 additions
and
109 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
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
101 changes: 101 additions & 0 deletions
101
...mentation/suppressors/CA1707IdentifiersShouldNotContainUnderscoresSuppressor.md
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,101 @@ | ||
# Suppress CA1707 for test methods | ||
|
||
DiagnosticSuppressor: [CA1707IdentifiersShouldNotContainUnderscoresSuppressor.cs](../../source/production/F0.Analyzers/CodeAnalysis/Suppressors/CA1707IdentifiersShouldNotContainUnderscoresSuppressor.cs) | ||
|
||
| | | | ||
|------------|------------------| | ||
| ID | F0CA1707 | | ||
| Suppresses | [CA1707][ca1707] | | ||
| Applies to | `[vNext,)` | | ||
|
||
## Summary | ||
|
||
Suppress warning [CA1707][ca1707] on test methods to allow the best practices naming standard for tests. | ||
|
||
## Remarks | ||
|
||
[CA1707][ca1707] reports a warning on non-field identifiers when containing underscores, i.e., a `_` character. | ||
|
||
The [best practices for writing unit tests in .NET](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices) suggest the following [naming standard](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#naming-your-tests): | ||
|
||
> The name of your test should consist of three parts: | ||
> | ||
> - The name of the method being tested. | ||
> - The scenario under which it's being tested. | ||
> - The expected behavior when the scenario is invoked. | ||
These parts should be separated by the _underscore_ (`_`) character. | ||
|
||
[CA1707][ca1707] is suppressed only for _MSTest_, _NUnit_ and _xUnit.net_ test methods. | ||
For regular methods and other identifiers, [CA1707][ca1707] is not suppressed. | ||
|
||
### MSTest | ||
A method is a _MSTest_ test method, when it has | ||
- either the `TestMethodAttribute` | ||
- or the `DataTestMethodAttribute` | ||
|
||
and the containing _class_ is attributed with | ||
- `TestClassAttribute` | ||
|
||
from the _namespace_ `Microsoft.VisualStudio.TestTools.UnitTesting`. | ||
|
||
### NUnit | ||
A method is an _NUnit_ test method, when it has one of the following _attributes_ (Namespace: `NUnit.Framework`): | ||
- `TestAttribute` | ||
- `TestCaseAttribute` | ||
- `TestCaseSourceAttribute` | ||
- `CombinatorialAttribute` | ||
- `PairwiseAttribute` | ||
- `SequentialAttribute` | ||
- `TheoryAttribute` | ||
|
||
### xUnit.net | ||
A method is a _xUnit.net_ test method, when it has one of the following _attributes_ (Namespace: `Xunit`): | ||
- `FactAttribute` | ||
- `TheoryAttribute` | ||
|
||
## Example | ||
|
||
```cs | ||
using Xunit; | ||
|
||
namespace My_Namespace; // Remove the underscores from namespace name 'My_Namespace' | ||
public class My_Tests // Remove the underscores from type name My_Namespace.My_Tests | ||
{ | ||
[Fact] | ||
public void Given_When_Then() // Warning CA1707 suppressed | ||
{ | ||
Assert.Equal(240, 0x_F0); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0x_F0)] | ||
public void MethodUnderTest_Scenario_ExpectedResult(int value) // Warning CA1707 suppressed | ||
{ | ||
Assert.Equal(240, value); | ||
} | ||
|
||
public int My_Property { get; set; } // Remove the underscores from member name My_Namespace.My_Tests.My_Property | ||
public void My_Method() // Remove the underscores from member name My_Namespace.My_Tests.My_Method() | ||
=> throw null; | ||
|
||
public void MyMethod<Method_TypeParameter>() // On method My_Namespace.My_Tests.MyMethod<Method_TypeParameter>(), remove the underscores from generic type parameter name Method_TypeParameter | ||
=> throw null; | ||
} | ||
``` | ||
|
||
## See also | ||
|
||
- [Warning CA1707][ca1707] | ||
- [MSTest](https://github.com/microsoft/testfx) | ||
- [NUnit](https://github.com/nunit/nunit) | ||
- [xUnit.net](https://github.com/xunit/xunit) | ||
|
||
## History | ||
|
||
- [vNext](../../CHANGELOG.md#vNext) | ||
|
||
|
||
[ca1707]: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1707 |
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
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
4 changes: 4 additions & 0 deletions
4
source/example/F0.Analyzers.Example.CSharp10/Properties/AssemblyInfo.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,4 @@ | ||
using System.Reflection; | ||
|
||
[assembly: AssemblyDescription("F0.Analyzers code sample.")] | ||
[assembly: AssemblyCopyright("Copyright © f[0] 2022")] |
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,4 @@ | ||
is_global = true | ||
|
||
# CA1707: Identifiers should not contain underscores | ||
dotnet_diagnostic.CA1707.severity = none |
35 changes: 35 additions & 0 deletions
35
source/example/F0.Analyzers.Example.Tests/CodeAnalysis/Suppressors/MSTest.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,35 @@ | ||
using F0.Analyzers.Example.Tests.Services; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace F0.Analyzers.Example.Tests.CodeAnalysis.Suppressors | ||
{ | ||
[TestClass] | ||
public class MSTest | ||
{ | ||
private readonly PrimeService primeService; | ||
|
||
public MSTest() | ||
{ | ||
primeService = new PrimeService(); | ||
} | ||
|
||
[TestMethod] | ||
public void IsPrime_InputIs1_ReturnFalse() | ||
{ | ||
var result = primeService.IsPrime(1); | ||
|
||
Assert.IsFalse(result, "1 should not be prime"); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(-1)] | ||
[DataRow(0)] | ||
[DataRow(1)] | ||
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) | ||
{ | ||
var result = primeService.IsPrime(value); | ||
|
||
Assert.IsFalse(result, $"{value} should not be prime"); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
source/example/F0.Analyzers.Example.Tests/CodeAnalysis/Suppressors/NUnit.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,84 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using F0.Analyzers.Example.Tests.Services; | ||
using NUnit.Framework; | ||
|
||
namespace F0.Analyzers.Example.Tests.CodeAnalysis.Suppressors | ||
{ | ||
[TestFixture] | ||
[SuppressMessage("Style", "IDE0022:Use expression body for methods", Justification = "Example")] | ||
public class NUnit | ||
{ | ||
private PrimeService primeService; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
primeService = new PrimeService(); | ||
} | ||
|
||
[Test] | ||
public void IsPrime_InputIs1_ReturnFalse() | ||
{ | ||
var result = primeService.IsPrime(1); | ||
|
||
Assert.IsFalse(result, "1 should not be prime"); | ||
} | ||
|
||
[TestCase(-1)] | ||
[TestCase(0)] | ||
[TestCase(1)] | ||
public void IsPrime_ValuesLessThan2_ReturnFalse(int value) | ||
{ | ||
var result = primeService.IsPrime(value); | ||
|
||
Assert.IsFalse(result, $"{value} should not be prime"); | ||
} | ||
|
||
[TestCaseSource(nameof(TestCaseSource))] | ||
public void TestCaseSource_Attribute(int value) | ||
{ | ||
var result = primeService.IsPrime(value); | ||
|
||
Assert.IsFalse(result, $"{value} should not be prime"); | ||
} | ||
|
||
private static int[] TestCaseSource => new int[] { -2, -3 }; | ||
|
||
[Combinatorial] | ||
public void Combinatorial_Attribute([Values(1, 2)] int number, [Values("A", "B")] string text) | ||
{ | ||
var actual = number + text; | ||
|
||
Assert.That(actual, Is.AnyOf("1A", "1B", "2A", "2B")); | ||
} | ||
|
||
[Pairwise] | ||
public void Pairwise_Attribute([Values("a", "b", "c")] string left, [Values("+", "-")] string middle, [Values("x", "y")] string right) | ||
{ | ||
var actual = left + middle + right; | ||
|
||
Assert.That(actual, Is.AnyOf("a-x", "a+y", "b+x", "b-y", "c-x", "c+y")); | ||
} | ||
|
||
[Sequential] | ||
public void Sequential_Attribute([Values(1, 2, 3)] int number, [Values("A", "B")] string text) | ||
{ | ||
var actual = number + text; | ||
|
||
Assert.That(actual, Is.AnyOf("1A", "2B", "3")); | ||
} | ||
|
||
[Theory] | ||
public void Theory_Attribute(int number) | ||
{ | ||
Assume.That(number != 0); | ||
|
||
var @delegate = () => 0 / number; | ||
|
||
Assert.That(@delegate, Throws.Nothing); | ||
} | ||
|
||
[DatapointSource] | ||
public int[] DatapointSource => new int[] { -1, 0, 1 }; | ||
} | ||
} |
Oops, something went wrong.