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

[Roslynator] Add docs for unit testing #28

Merged
merged 5 commits into from
Oct 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add documentation for Roslynator unit tests ([#PR](https://github.com/josefpihrt/josefpihrt.github.io/pull/28))

## [0.4.4] - 2023-10-02

### Added
Expand Down Expand Up @@ -100,4 +104,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add GitHub icon to navbar ([PR](https://github.com/josefpihrt/josefpihrt.github.io/pull/3)).
- Make sidebar hideable ([PR](https://github.com/josefpihrt/josefpihrt.github.io/pull/3)).
- Add documentation for [Roslynator](https://github.com/josefpihrt/roslynator), [Orang](https://github.com/josefpihrt/orang) and [DotMarkdown](https://github.com/josefpihrt/dotmarkdown) ([PR](https://github.com/josefpihrt/josefpihrt.github.io/pull/1)).
- Add documentation for [Roslynator](https://github.com/dotnet/roslynator), [Orang](https://github.com/josefpihrt/orang) and [DotMarkdown](https://github.com/josefpihrt/dotmarkdown) ([#1](https://github.com/josefpihrt/josefpihrt.github.io/pull/1)).
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This website contains documentation for following projects:

- [Roslynator](https://github.com/josefpihrt/roslynator)
- [Roslynator](https://github.com/dotnet/roslynator)
- [DotMarkdown](https://github.com/josefpihrt/dotmarkdown)
- [Orang](https://github.com/josefpihrt/orang)
- [Snippetica](https://github.com/josefpihrt/snippetica)
Expand All @@ -13,7 +13,7 @@ This website is built using [Docusaurus 2](https://docusaurus.io/), a modern sta

For local development it's necessary to pull following repositories and place them next to this repository:

- [Roslynator](https://github.com/josefpihrt/roslynator)
- [Roslynator](https://github.com/dotnet/roslynator)
- [DotMarkdown](https://github.com/josefpihrt/dotmarkdown)
- [Orang](https://github.com/josefpihrt/orang)
- [Snippetica](https://github.com/josefpihrt/snippetica)
Expand Down
93 changes: 93 additions & 0 deletions docs/roslynator/analyzers-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
sidebar_label: Analyzers Testing
---

# Unit Testing of Analyzers

## NuGet Package

```
dotnet add package roslynator.testing.csharp.xunit
```
or
```
dotnet add package roslynator.testing.csharp.mstest
```

## Example: Test Analyzer With Code Fix

```cs
using Roslynator.Testing.CSharp.Xunit;
using Xunit;

public class AccessibilityModifiersTests
: XunitDiagnosticVerifier<AccessibilityModifiersAnalyzer, AccessibilityModifiersCodeFixProvider>
{
public override DiagnosticDescriptor Descriptor { get; } = AccessibilityModifiersAnalyzer.Descriptor;

[Fact]
public async Task Test()
{
await VerifyDiagnosticAndFixAsync(
source: @"
namespace N
{
class [|C|]
{
}
}
",
expectedSource: @"
namespace N
{
internal class C
{
}
}
",
options: Options.AddConfigOption("roslynator_accessibility_modifiers", "explicit"));
}
}
```

## Example: Test Analyzer Without Code Fix

Use `EmptyCodeFixProvider` in tests of analyzers without a code fix.

```cs
using Roslynator.Testing.CSharp.Xunit;
using Xunit;

public class UnusedParameterTests
: XunitDiagnosticVerifier<UnusedParameterAnalyzer, EmptyCodeFixProvider>
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.UnusedParameter;

[Fact]
public async Task Test()
{
await VerifyDiagnosticAsync(
source: @"
class C
{
void M([|object p|])
{
}
}
"
);
}
}
```

## How to Mark Diagnostic's Location

Diagnostic's location is marked directly in a test code using `[|` and `|]` tokens.

## How to Add Config Options

Each test method has parameter `options` of type `TestOptions`:

```cs
options: Options.AddConfigOption("option_key", "option_value")
```
46 changes: 46 additions & 0 deletions docs/roslynator/compiler-diagnostic-fixes-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
sidebar_label: Compiler Diagnostic Fix Testing
---

# Unit Testing of Compiler Diagnostic Fixes

## NuGet Package

```
dotnet add package roslynator.testing.csharp.xunit
```
or
```
dotnet add package roslynator.testing.csharp.mstest
```

## Example: Test Compiler Diagnostic Fix

```cs
using Roslynator.Testing.CSharp.Xunit;
using Xunit;

public class ModifierIsNotValidForThisItemTests : XunitCompilerDiagnosticFixVerifier<RemoveModifierCodeFixProvider>
{
public override string DiagnosticId { get; } = "CS0106";

[Fact]
public async Task Test()
{
await VerifyFixAsync(
source: @"
struct S
{
public virtual string M() => null;
}
",
expectedSource: @"
struct S
{
public string M() => null;
}
");
}
}
```

71 changes: 71 additions & 0 deletions docs/roslynator/refactorings-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
sidebar_label: Refactorings Testing
---

# Unit Testing of Refactorings

## NuGet Package

```
dotnet add package roslynator.testing.csharp.xunit
```
or
```
dotnet add package roslynator.testing.csharp.mstest
```

## Example: Test Refactoring

```cs
using Roslynator.Testing.CSharp.Xunit;
using Xunit;

public class AddArgumentNameTests : XunitRefactoringVerifier<AddArgumentNameCodeRefactoringProvider>
{
[Fact]
public async Task Test()
{
await VerifyRefactoringAsync(
source: @"
class C
{
void M()
{
var arr = new[]
{
new string(
[|' ',
1|])
};
}
}
",
expectedSource: @"
class C
{
void M()
{
var arr = new[]
{
new string(
c: ' ',
count: 1)
};
}
}
");
}
}
```

## How to Mark Selected Text

Selected text is marked directly in a test code using `[|` and `|]` tokens.

## How to Add Config Options

Each test method has parameter `options` of type `TestOptions`:

```cs
options: Options.AddConfigOption("option_key", "option_value")
```
14 changes: 9 additions & 5 deletions docs/roslynator/testing.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Testing

Roslynator provides packages for unit testing of analyzers, refactoring and code fixes:
- [Unit testing of analyzers](analyzers-testing)
- [Unit testing of refactorings](refactorings-testing)
- [Unit testing of compiler diagnostic fixes](compiler-diagnostic-fixes-testing)

## NuGet Packages

- [MSTest-based package](https://www.nuget.org/packages/Roslynator.Testing.CSharp.MSTest)
- [Xunit-based package](https://www.nuget.org/packages/Roslynator.Testing.CSharp.Xunit)

Roslynator repository contains extensive usage of Roslynator testing framework:
## Unit Testing in Roslynator Repository

- [tests of analyzers](https://github.com/JosefPihrt/Roslynator/tree/main/src/Tests/Analyzers.Tests)
- [tests of refactoring](https://github.com/JosefPihrt/Roslynator/tree/main/src/Tests/Refactorings.Tests)
- [tests of code fixes](https://github.com/JosefPihrt/Roslynator/tree/main/src/Tests/CodeFixes.Tests)
- [Unit testing of analyzers](https://github.com/dotnet/roslynator/tree/main/src/Tests/Analyzers.Tests)
- [Unit testing of refactoring](https://github.com/dotnet/roslynator/tree/main/src/Tests/Refactorings.Tests)
- [Unit testing of code fixes](https://github.com/dotnet/roslynator/tree/main/src/Tests/CodeFixes.Tests)

10 changes: 10 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ const sidebars = {
'roslynator/how-to-use-net-api',
'roslynator/how-to-add-new-analyzer',
'roslynator/how-to-add-new-refactoring',
{
type: 'category',
label: 'Testing',
link: { type: 'doc', id: 'roslynator/testing' },
items: [
'roslynator/analyzers-testing',
'roslynator/refactorings-testing',
'roslynator/compiler-diagnostic-fixes-testing',
],
},
{
type: 'category',
label: 'Metadata Reference',
Expand Down