From 874e1d0d3704337e8dbbd2ef4f5f2a6dacd0f91b Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Sun, 1 Oct 2023 16:58:04 +0200 Subject: [PATCH 1/4] Roslynator - Add docs for unit testing --- CHANGELOG.md | 2 +- README.md | 4 +- docs/.gitignore | 43 +++++---- docs/roslynator/analyzers-testing.md | 93 +++++++++++++++++++ .../compiler-diagnostic-fixes-testing.md | 46 +++++++++ docs/roslynator/refactorings-testing.md | 71 ++++++++++++++ docs/roslynator/testing.md | 14 ++- sidebars.js | 11 ++- 8 files changed, 253 insertions(+), 31 deletions(-) create mode 100644 docs/roslynator/analyzers-testing.md create mode 100644 docs/roslynator/compiler-diagnostic-fixes-testing.md create mode 100644 docs/roslynator/refactorings-testing.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c31a52c..7f2942e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,4 +90,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add GitHub icon to navbar ([#3](https://github.com/josefpihrt/josefpihrt.github.io/pull/3)). - Make sidebar hideable ([#3](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) ([#1](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)). diff --git a/README.md b/README.md index 9db6bc7d..c37f1fe8 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/docs/.gitignore b/docs/.gitignore index 2fd47038..6967672b 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -1,22 +1,21 @@ -/* -!.gitignore -!/dotmarkdown/docusaurus-markdown.md -!/dotmarkdown/index.md -!/gitalias/commands-aliases.md -!/gitalias/index.md -!/gitalias/most-used-aliases.md -!/orang/index.md -!/orang/cli/how-to.md -!/roslynator/analyzer-metadata.md -!/roslynator/analyzers-vs-refactorings.md -!/roslynator/cli.md -!/roslynator/how-to-add-new-analyzer.md -!/roslynator/how-to-add-new-refactoring.md -!/roslynator/how-to-fix-all-diagnostics.md -!/roslynator/how-to-generate-net-api-docs.md -!/roslynator/how-to-suppress-diagnostic.md -!/roslynator/how-to-update-documentation.md -!/roslynator/index.md -!/roslynator/refactoring-metadata.md -!/roslynator/testing.md -!/snippetica/index.md +dotmarkdown/ref +dotmarkdown/ref.md +orang/cli/commands +orang/cli/commands.md +orang/cli/options-values.md +orang/cli/expression-syntax.md +orang/cli.md +orang/ref +orang/ref.md +roslynator/analyzers +roslynator/cli +roslynator/fixes +roslynator/ref +roslynator/refactorings +roslynator/analyzers.md +roslynator/configuration.md +roslynator/fixes.md +roslynator/ref.md +roslynator/refactorings.md +snippetica/snippets +snippetica/quick-reference-cs-vb.md diff --git a/docs/roslynator/analyzers-testing.md b/docs/roslynator/analyzers-testing.md new file mode 100644 index 00000000..74272208 --- /dev/null +++ b/docs/roslynator/analyzers-testing.md @@ -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 +{ + public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.AddOrRemoveAccessibilityModifiers; + + [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 +{ + 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") +``` diff --git a/docs/roslynator/compiler-diagnostic-fixes-testing.md b/docs/roslynator/compiler-diagnostic-fixes-testing.md new file mode 100644 index 00000000..a63271e2 --- /dev/null +++ b/docs/roslynator/compiler-diagnostic-fixes-testing.md @@ -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 +{ + 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; +} +"); + } +} +``` + diff --git a/docs/roslynator/refactorings-testing.md b/docs/roslynator/refactorings-testing.md new file mode 100644 index 00000000..7871f08f --- /dev/null +++ b/docs/roslynator/refactorings-testing.md @@ -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 +{ + [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") +``` diff --git a/docs/roslynator/testing.md b/docs/roslynator/testing.md index c589fb4a..3ebc790c 100644 --- a/docs/roslynator/testing.md +++ b/docs/roslynator/testing.md @@ -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) diff --git a/sidebars.js b/sidebars.js index e5364bd4..eead37b1 100644 --- a/sidebars.js +++ b/sidebars.js @@ -44,7 +44,16 @@ const sidebars = { items: [ 'roslynator/how-to-add-new-analyzer', 'roslynator/how-to-add-new-refactoring', - 'roslynator/testing', + { + 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', From f39502ef76fd6a915f5914befc1cf2c8b0ef0bed Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Sun, 1 Oct 2023 16:59:29 +0200 Subject: [PATCH 2/4] update --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f2942e0..3faa4fad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Roslynator: Add documentation for unit tests ([#28](https://github.com/josefpihrt/josefpihrt.github.io/pull/28)) + ## [0.4.3] - 2023-09-26 ### Changed From c0c03a0e0569b5d71b62f88cb0a5a6164ccc2841 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Mon, 2 Oct 2023 22:23:52 +0200 Subject: [PATCH 3/4] update --- docs/roslynator/analyzers-testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/roslynator/analyzers-testing.md b/docs/roslynator/analyzers-testing.md index 74272208..6b69a738 100644 --- a/docs/roslynator/analyzers-testing.md +++ b/docs/roslynator/analyzers-testing.md @@ -23,7 +23,7 @@ using Xunit; public class AccessibilityModifiersTests : XunitDiagnosticVerifier { - public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.AddOrRemoveAccessibilityModifiers; + public override DiagnosticDescriptor Descriptor { get; } = AccessibilityModifiersAnalyzer.Descriptor; [Fact] public async Task Test() From 2601fcb33053d8e808ae94a930d3d1dcc89302c9 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 18 Oct 2023 21:03:31 +0200 Subject: [PATCH 4/4] update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adf6b2dc..8e31565c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Roslynator: Add documentation for unit tests ([#28](https://github.com/josefpihrt/josefpihrt.github.io/pull/28)) +- Add documentation for Roslynator unit tests ([#PR](https://github.com/josefpihrt/josefpihrt.github.io/pull/28)) ## [0.4.4] - 2023-10-02