-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RCS1140 - Don't require user to document exception types if caught in…
… same method (#1524)
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 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
154 changes: 154 additions & 0 deletions
154
src/Tests/Analyzers.Tests/RCS1140AddExceptionToDocumentationCommentTests.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,154 @@ | ||
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Roslynator.CSharp.CodeFixes; | ||
using Roslynator.Testing.CSharp; | ||
using Xunit; | ||
|
||
namespace Roslynator.CSharp.Analysis.Tests; | ||
|
||
public class RCS1140AddExceptionToDocumentationCommentTests : AbstractCSharpDiagnosticVerifier<AddExceptionToDocumentationCommentAnalyzer, AddExceptionToDocumentationCommentCodeFixProvider> | ||
{ | ||
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.AddExceptionToDocumentationComment; | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddExceptionToDocumentationComment)] | ||
public async Task Test_Example_From_Documentation() | ||
{ | ||
await VerifyDiagnosticAndFixAsync(""" | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
public void Foo(object parameter) | ||
{ | ||
if (parameter == null) | ||
[|throw new ArgumentNullException(nameof(parameter));|] | ||
} | ||
} | ||
""", """ | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
/// <exception cref="ArgumentNullException"><paramref name="parameter"/> is <c>null</c>.</exception> | ||
public void Foo(object parameter) | ||
{ | ||
if (parameter == null) | ||
throw new ArgumentNullException(nameof(parameter)); | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddExceptionToDocumentationComment)] | ||
public async Task Test_No_Diagnostic_If_Exception_Is_Caught_In_Method() | ||
{ | ||
await VerifyNoDiagnosticAsync(""" | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
public void Foo(object parameter) | ||
{ | ||
try | ||
{ | ||
if (parameter == null) | ||
throw new ArgumentNullException(nameof(parameter)); | ||
} | ||
catch (ArgumentNullException) {} | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddExceptionToDocumentationComment)] | ||
public async Task Test_No_Diagnostic_If_Exception_Is_Caught_In_Method_Nested() | ||
{ | ||
await VerifyNoDiagnosticAsync(""" | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
public void Foo(object parameter) | ||
{ | ||
try | ||
{ | ||
try | ||
{ | ||
if (parameter == null) | ||
throw new ArgumentNullException(nameof(parameter)); | ||
} | ||
catch (InvalidOperationException) {} | ||
} | ||
catch (ArgumentNullException) {} | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddExceptionToDocumentationComment)] | ||
public async Task Test_Diagnostic_If_Not_Correct_Exception_Is_Caught_In_Method() | ||
{ | ||
await VerifyDiagnosticAndFixAsync(""" | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
public void Foo(object parameter) | ||
{ | ||
try | ||
{ | ||
if (parameter == null) | ||
[|throw new ArgumentNullException(nameof(parameter));|] | ||
} | ||
catch (InvalidOperationException) {} | ||
} | ||
} | ||
""", """ | ||
using System; | ||
class C | ||
{ | ||
/// <summary> | ||
/// ... | ||
/// </summary> | ||
/// <param name="parameter"></param> | ||
/// <exception cref="ArgumentNullException"><paramref name="parameter"/> is <c>null</c>.</exception> | ||
public void Foo(object parameter) | ||
{ | ||
try | ||
{ | ||
if (parameter == null) | ||
throw new ArgumentNullException(nameof(parameter)); | ||
} | ||
catch (InvalidOperationException) {} | ||
} | ||
} | ||
"""); | ||
} | ||
} |