Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NUnit version 4. #612

Merged
merged 11 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ dotnet_naming_rule.parameters_and_locals.style = camel_case
##################################################################################
# IDE Code Style Analyzers

# IDE0005: Using directive is unnecessary
dotnet_diagnostic.IDE0005.severity = warning

# IDE0041: Use 'is null' check
dotnet_diagnostic.IDE0041.severity = warning

Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ jobs:
uses: actions/setup-dotnet@main
with:
global-json-file: ./global.json
- name: dotnet test
run: dotnet test --configuration=Release ./src/nunit.analyzers.tests/

- name: dotnet test (NUnit4)
run: dotnet test --configuration=Release -p:NUnitVersion=4 ./src/nunit.analyzers.tests/

- name: dotnet test (NUnit3)
run: dotnet test --configuration=Release -p:NUnitVersion=3 ./src/nunit.analyzers.tests/
4 changes: 2 additions & 2 deletions documentation/NUnit1015.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MyTestClass
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}
}

Expand Down Expand Up @@ -59,7 +59,7 @@ public class MyTestClass
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}
}

Expand Down
4 changes: 2 additions & 2 deletions documentation/NUnit1016.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MyTestClass
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}
}

Expand Down Expand Up @@ -61,7 +61,7 @@ public class MyTestClass
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}
}

Expand Down
4 changes: 2 additions & 2 deletions documentation/NUnit1017.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MyTestClass
[TestCaseSource(nameof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}

object[] DivideCases =
Expand Down Expand Up @@ -56,7 +56,7 @@ public class MyTestClass
[TestCaseSource(nameof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}

static object[] DivideCases =
Expand Down
4 changes: 2 additions & 2 deletions documentation/NUnit1020.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MyTestClass
[TestCaseSource(nameof(DivideCases), new object[] { "Testing" })]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}

static object[] DivideCases =
Expand All @@ -54,7 +54,7 @@ public class MyTestClass
[TestCaseSource(nameof(DivideCases), new object[] { "Testing" })]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual(q, n / d);
ClassicAssert.AreEqual(q, n / d);
}

static object[] DivideCases(string input)
Expand Down
4 changes: 2 additions & 2 deletions documentation/NUnit1022.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MyTestClass
[Test]
public void DivideTest([ValueSource(nameof(Numbers))] int n)
{
Assert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
ClassicAssert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
}

object[] Numbers => new int[] { 1, 2, 3 };
Expand All @@ -51,7 +51,7 @@ public class MyTestClass
[Test]
public void DivideTest([ValueSource(nameof(Numbers))] int n)
{
Assert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
ClassicAssert.AreEqual(n, Is.GreaterThanOrEqualTo(0));
}

static object[] Numbers => new int[] { 1, 2, 3 };
Expand Down
20 changes: 10 additions & 10 deletions documentation/NUnit2001.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NUnit2001

## Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)
## Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)

| Topic | Value
| :-- | :--
Expand All @@ -12,24 +12,24 @@

## Description

Consider using the constraint model, `Assert.That(expr, Is.False)`, instead of the classic model, `Assert.False(expr)`.
Consider using the constraint model, `Assert.That(expr, Is.False)`, instead of the classic model, `ClassicAssert.False(expr)`.

## Motivation

The classic Assert model contains less flexibility than the constraint model,
so this analyzer marks usages of `Assert.False` from the classic Assert model.
so this analyzer marks usages of `ClassicAssert.False` from the classic Assert model.

```csharp
[Test]
public void Test()
{
Assert.False(expression);
ClassicAssert.False(expression);
}
```

## How to fix violations

The analyzer comes with a code fix that will replace `Assert.False(expression)` with
The analyzer comes with a code fix that will replace `ClassicAssert.False(expression)` with
`Assert.That(expression, Is.False)`. So the code block above will be changed into.

```csharp
Expand All @@ -50,7 +50,7 @@ Configure the severity per project, for more info see [MSDN](https://learn.micro
### Via .editorconfig file

```ini
# NUnit2001: Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)
# NUnit2001: Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)
dotnet_diagnostic.NUnit2001.severity = chosenSeverity
```

Expand All @@ -59,22 +59,22 @@ where `chosenSeverity` can be one of `none`, `silent`, `suggestion`, `warning`,
### Via #pragma directive

```csharp
#pragma warning disable NUnit2001 // Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)
#pragma warning disable NUnit2001 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)
Code violating the rule here
#pragma warning restore NUnit2001 // Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)
#pragma warning restore NUnit2001 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)
```

Or put this at the top of the file to disable all instances.

```csharp
#pragma warning disable NUnit2001 // Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)
#pragma warning disable NUnit2001 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)
```

### Via attribute `[SuppressMessage]`

```csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2001:Consider using Assert.That(expr, Is.False) instead of Assert.False(expr)",
"NUnit2001:Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr)",
Justification = "Reason...")]
```
<!-- end generated config severity -->
20 changes: 10 additions & 10 deletions documentation/NUnit2002.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NUnit2002

## Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)
## Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)

| Topic | Value
| :-- | :--
Expand All @@ -12,24 +12,24 @@

## Description

Consider using the constraint model, `Assert.That(expr, Is.False)`, instead of the classic model, `Assert.IsFalse(expr)`.
Consider using the constraint model, `Assert.That(expr, Is.False)`, instead of the classic model, `ClassicAssert.IsFalse(expr)`.

## Motivation

The classic Assert model contains less flexibility than the constraint model,
so this analyzer marks usages of `Assert.IsFalse` from the classic Assert model.
so this analyzer marks usages of `ClassicAssert.IsFalse` from the classic Assert model.

```csharp
[Test]
public void Test()
{
Assert.IsFalse(expression);
ClassicAssert.IsFalse(expression);
}
```

## How to fix violations

The analyzer comes with a code fix that will replace `Assert.IsFalse(expression)` with
The analyzer comes with a code fix that will replace `ClassicAssert.IsFalse(expression)` with
`Assert.That(expression, Is.False)`. So the code block above will be changed into.

```csharp
Expand All @@ -50,7 +50,7 @@ Configure the severity per project, for more info see [MSDN](https://learn.micro
### Via .editorconfig file

```ini
# NUnit2002: Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)
# NUnit2002: Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)
dotnet_diagnostic.NUnit2002.severity = chosenSeverity
```

Expand All @@ -59,22 +59,22 @@ where `chosenSeverity` can be one of `none`, `silent`, `suggestion`, `warning`,
### Via #pragma directive

```csharp
#pragma warning disable NUnit2002 // Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)
#pragma warning disable NUnit2002 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)
Code violating the rule here
#pragma warning restore NUnit2002 // Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)
#pragma warning restore NUnit2002 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)
```

Or put this at the top of the file to disable all instances.

```csharp
#pragma warning disable NUnit2002 // Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)
#pragma warning disable NUnit2002 // Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)
```

### Via attribute `[SuppressMessage]`

```csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2002:Consider using Assert.That(expr, Is.False) instead of Assert.IsFalse(expr)",
"NUnit2002:Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr)",
Justification = "Reason...")]
```
<!-- end generated config severity -->
20 changes: 10 additions & 10 deletions documentation/NUnit2003.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NUnit2003

## Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)
## Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)

| Topic | Value
| :-- | :--
Expand All @@ -12,24 +12,24 @@

## Description

Consider using the constraint model, `Assert.That(expr, Is.True)`, instead of the classic model, `Assert.IsTrue(expr)`.
Consider using the constraint model, `Assert.That(expr, Is.True)`, instead of the classic model, `ClassicAssert.IsTrue(expr)`.

## Motivation

The classic Assert model contains less flexibility than the constraint model,
so this analyzer marks usages of `Assert.IsTrue` from the classic Assert model.
so this analyzer marks usages of `ClassicAssert.IsTrue` from the classic Assert model.

```csharp
[Test]
public void Test()
{
Assert.IsTrue(expression);
ClassicAssert.IsTrue(expression);
}
```

## How to fix violations

The analyzer comes with a code fix that will replace `Assert.IsTrue(expression)` with
The analyzer comes with a code fix that will replace `ClassicAssert.IsTrue(expression)` with
`Assert.That(expression, Is.True)`. So the code block above will be changed into.

```csharp
Expand All @@ -50,7 +50,7 @@ Configure the severity per project, for more info see [MSDN](https://learn.micro
### Via .editorconfig file

```ini
# NUnit2003: Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)
# NUnit2003: Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)
dotnet_diagnostic.NUnit2003.severity = chosenSeverity
```

Expand All @@ -59,22 +59,22 @@ where `chosenSeverity` can be one of `none`, `silent`, `suggestion`, `warning`,
### Via #pragma directive

```csharp
#pragma warning disable NUnit2003 // Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)
#pragma warning disable NUnit2003 // Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)
Code violating the rule here
#pragma warning restore NUnit2003 // Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)
#pragma warning restore NUnit2003 // Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)
```

Or put this at the top of the file to disable all instances.

```csharp
#pragma warning disable NUnit2003 // Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)
#pragma warning disable NUnit2003 // Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)
```

### Via attribute `[SuppressMessage]`

```csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
"NUnit2003:Consider using Assert.That(expr, Is.True) instead of Assert.IsTrue(expr)",
"NUnit2003:Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr)",
Justification = "Reason...")]
```
<!-- end generated config severity -->
Loading