-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for DiagnosticSuppressors
- Loading branch information
1 parent
ff76c4f
commit 9521341
Showing
8 changed files
with
494 additions
and
1 deletion.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Suppressed.Fail.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,100 @@ | ||
// ReSharper disable RedundantNameQualifier | ||
namespace Gu.Roslyn.Asserts.Tests.RoslynAssertTests | ||
{ | ||
using Gu.Roslyn.Asserts.Tests.TestHelpers.Suppressors; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
public static partial class VisibleMagicFieldIsAllowed | ||
{ | ||
public static class Fail | ||
{ | ||
private static readonly DiagnosticSuppressor Suppressor = new AllowUnassignedMagicMembers(); | ||
|
||
[Test] | ||
public static void FailsINothingToSuppress() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string? F; | ||
} | ||
}"; | ||
var exception = Assert.Throws<AssertException>(() => RoslynAssert.Suppressed(Suppressor, code)); | ||
CodeAssert.AreEqual(exception.Message, "Found no errors to suppress"); | ||
} | ||
|
||
[Test] | ||
public static void FailsIfDiagnosticIsNotSuppressableBySuppressor() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string M() | ||
{ | ||
string Magic; | ||
return Magic; | ||
} | ||
} | ||
}"; | ||
var exception = Assert.Throws<AssertException>(() => RoslynAssert.Suppressed(Suppressor, code)); | ||
Assert.That(exception.Message, Does.Contain("CS0165 Use of unassigned local variable 'Magic'")); | ||
} | ||
|
||
[Test] | ||
public static void DoesNotSuppressNonMagicField() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string F; | ||
} | ||
}"; | ||
var exception = Assert.Throws<AssertException>(() => RoslynAssert.Suppressed(Suppressor, code)); | ||
Assert.That(exception.Message, Does.Contain(AllowUnassignedMagicMembers.FieldNameIsMagic.SuppressedDiagnosticId)); | ||
} | ||
|
||
[Test] | ||
public static void DoesFailIfSuppressedByWrongSuppressionDescriptor() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic; | ||
} | ||
}"; | ||
var expected = "Expected diagnostic to be suppressed by AllowUnassignedMagicMembers:Property is called Magic but was:\r\n" + | ||
"AllowUnassignedMagicMembers:Field is called Magic\r\n"; | ||
var exception = Assert.Throws<AssertException>(() => | ||
RoslynAssert.SuppressedBy(Suppressor, AllowUnassignedMagicMembers.PropertyNameIsMagic, code)); | ||
CodeAssert.AreEqual(expected, exception.Message); | ||
} | ||
|
||
[Test] | ||
public static void DoesSuppressDiagnosticAboutMagicPropertiesWithWrongSuppressionDescriptor() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic { get; set; } | ||
} | ||
}"; | ||
var expected = "Expected diagnostic to be suppressed by AllowUnassignedMagicMembers:Field is called Magic but was:\r\n" + | ||
"AllowUnassignedMagicMembers:Property is called Magic\r\n"; | ||
var exception = Assert.Throws<AssertException>(() => | ||
RoslynAssert.SuppressedBy(Suppressor, AllowUnassignedMagicMembers.FieldNameIsMagic, code)); | ||
CodeAssert.AreEqual(expected, exception.Message); | ||
} | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Gu.Roslyn.Asserts.Tests/RoslynAssertTests/Suppressed.Success.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,85 @@ | ||
// ReSharper disable RedundantNameQualifier | ||
namespace Gu.Roslyn.Asserts.Tests.RoslynAssertTests | ||
{ | ||
using Gu.Roslyn.Asserts.Tests.TestHelpers.Suppressors; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
public static partial class VisibleMagicFieldIsAllowed | ||
{ | ||
public static class Success | ||
{ | ||
private static readonly DiagnosticSuppressor Suppressor = new AllowUnassignedMagicMembers(); | ||
|
||
[Test] | ||
public static void DoesNotSuppressAllDiagnostics() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string F; | ||
} | ||
}"; | ||
RoslynAssert.NotSuppressed(Suppressor, code); | ||
} | ||
|
||
[Test] | ||
public static void DoesSuppressDiagnosticAboutMagicField() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic; | ||
} | ||
}"; | ||
RoslynAssert.Suppressed(Suppressor, code); | ||
} | ||
|
||
[Test] | ||
public static void DoesSuppressDiagnosticAboutMagicFieldWithSuppressionDescriptor() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic; | ||
} | ||
}"; | ||
RoslynAssert.SuppressedBy(Suppressor, AllowUnassignedMagicMembers.FieldNameIsMagic, code); | ||
} | ||
|
||
[Test] | ||
public static void DoesSuppressDiagnosticAboutMagicProperties() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic { get; set; } | ||
} | ||
}"; | ||
RoslynAssert.Suppressed(Suppressor, code); | ||
} | ||
|
||
[Test] | ||
public static void DoesSuppressDiagnosticAboutMagicPropertiesWithSuppressionDescriptor() | ||
{ | ||
const string code = @" | ||
namespace N | ||
{ | ||
public class C | ||
{ | ||
public string Magic { get; set; } | ||
} | ||
}"; | ||
RoslynAssert.SuppressedBy(Suppressor, AllowUnassignedMagicMembers.PropertyNameIsMagic, code); | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
Gu.Roslyn.Asserts.Tests/TestHelpers/Suppressors/AllowUnassignedMagicMembers.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,56 @@ | ||
namespace Gu.Roslyn.Asserts.Tests.TestHelpers.Suppressors | ||
{ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class AllowUnassignedMagicMembers : DiagnosticSuppressor | ||
{ | ||
public const string MagicFieldName = "Magic"; | ||
|
||
public static readonly SuppressionDescriptor FieldNameIsMagic = new( | ||
id: nameof(AllowUnassignedMagicMembers), | ||
suppressedDiagnosticId: "CS8618", | ||
justification: "Field is called " + MagicFieldName); | ||
|
||
public static readonly SuppressionDescriptor PropertyNameIsMagic = new( | ||
id: nameof(AllowUnassignedMagicMembers), | ||
suppressedDiagnosticId: "CS8618", | ||
justification: "Property is called " + MagicFieldName); | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = | ||
ImmutableArray.Create(FieldNameIsMagic, PropertyNameIsMagic); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
var sourceTree = diagnostic.Location.SourceTree; | ||
|
||
if (sourceTree is null) | ||
{ | ||
continue; | ||
} | ||
|
||
var node = sourceTree.GetRoot(context.CancellationToken) | ||
.FindNode(diagnostic.Location.SourceSpan); | ||
|
||
if (node.ToString().Contains(MagicFieldName)) | ||
{ | ||
if (node is PropertyDeclarationSyntax) | ||
{ | ||
context.ReportSuppression(Suppression.Create(PropertyNameIsMagic, diagnostic)); | ||
} | ||
else if (node is VariableDeclaratorSyntax && | ||
node.Parent is VariableDeclarationSyntax && | ||
node.Parent.Parent is FieldDeclarationSyntax) | ||
{ | ||
context.ReportSuppression(Suppression.Create(FieldNameIsMagic, diagnostic)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
| ||
static Gu.Roslyn.Asserts.Analyze.GetAllDiagnostics(Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor! suppressor, Microsoft.CodeAnalysis.Solution! solution) -> System.Collections.Generic.IReadOnlyList<Microsoft.CodeAnalysis.Diagnostic!>! | ||
static Gu.Roslyn.Asserts.CsharpCompilationOptionsExtensions.WithSuppressableAsError(this Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions! options, params Microsoft.CodeAnalysis.SuppressionDescriptor![]! descriptors) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions! | ||
static Gu.Roslyn.Asserts.CsharpCompilationOptionsExtensions.WithSuppressableAsError(this Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions! options, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SuppressionDescriptor!>! descriptors) -> Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions! | ||
static Gu.Roslyn.Asserts.RoslynAssert.NotSuppressed(Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor! suppressor, string! code, Gu.Roslyn.Asserts.Settings? settings = null) -> void | ||
static Gu.Roslyn.Asserts.RoslynAssert.Suppressed(Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor! suppressor, string! code, Gu.Roslyn.Asserts.Settings? settings = null) -> void | ||
static Gu.Roslyn.Asserts.RoslynAssert.SuppressedBy(Microsoft.CodeAnalysis.Diagnostics.DiagnosticSuppressor! suppressor, Microsoft.CodeAnalysis.SuppressionDescriptor! suppressionDescriptor, string! code, Gu.Roslyn.Asserts.Settings? settings = null) -> void |
Oops, something went wrong.