Skip to content

Commit 727aa74

Browse files
authored
tests: add examples for excluding assert methods from analyzer (#376)
1 parent e8a770e commit 727aa74

File tree

11 files changed

+143
-42
lines changed

11 files changed

+143
-42
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ dotnet add package FluentAssertions.Analyzers
3636
- [NUnit3 Analyzer Docs](docs/Nunit3Analyzer.md)
3737
- [Xunit Analyzer Docs](docs/XunitAnalyzer.md)
3838

39+
## Configuration
40+
41+
© Thanks to https://github.com/meziantou/Meziantou.FluentAssertionsAnalyzers
42+
43+
You can exclude assertion methods using the .editorconfig file:
44+
45+
[*.cs]
46+
ffa_excluded_methods=M:NUnit.Framework.Assert.Fail;M:NUnit.Framework.Assert.Fail(System.String)
47+
3948
## Getting Started
4049

4150
### Build

src/FluentAssertions.Analyzers.TestUtils/CsProjectArguments.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
6+
namespace FluentAssertions.Analyzers.TestUtils;
37

48
public class CsProjectArguments
59
{
610
public TargetFramework TargetFramework { get; set; } = TargetFramework.Net8_0;
711
public string[] Sources { get; set; }
812
public PackageReference[] PackageReferences { get; set; } = Array.Empty<PackageReference>();
913
public string Language { get; set; } = LanguageNames.CSharp;
14+
public Dictionary<string, string> AnalyzerConfigOptions { get; } = new();
15+
16+
public AnalyzerConfigOptionsProvider CreateAnalyzerConfigOptionsProvider() => new TestAnalyzerConfigOptionsProvider(AnalyzerConfigOptions);
1017
}
1118

1219
public static class CsProjectArgumentsExtensions
@@ -28,4 +35,10 @@ public static TCsProjectArguments WithPackageReferences<TCsProjectArguments>(thi
2835
arguments.PackageReferences = packageReferences;
2936
return arguments;
3037
}
38+
39+
public static TCsProjectArguments WithAnalyzerConfigOption<TCsProjectArguments>(this TCsProjectArguments arguments, string name, string value) where TCsProjectArguments : CsProjectArguments
40+
{
41+
arguments.AnalyzerConfigOptions[name] = value;
42+
return arguments;
43+
}
3144
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// <copyright file="TestAnalyzerConfigOptionsProvider.cs">
2+
// Copyright (c) 2022 All Rights Reserved
3+
// </copyright>
4+
// <author>Gérald Barré - https://github.com/meziantou</author>
5+
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.Diagnostics;
10+
11+
namespace FluentAssertions.Analyzers.TestUtils;
12+
13+
public sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
14+
{
15+
public static TestAnalyzerConfigOptionsProvider Empty { get; } = new(ImmutableDictionary<string, string>.Empty);
16+
17+
private readonly IDictionary<string, string> _values;
18+
public TestAnalyzerConfigOptionsProvider(IDictionary<string, string> values) => _values = values;
19+
20+
public override AnalyzerConfigOptions GlobalOptions => new TestAnalyzerConfigOptions(_values);
21+
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => new TestAnalyzerConfigOptions(_values);
22+
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => new TestAnalyzerConfigOptions(_values);
23+
24+
private sealed class TestAnalyzerConfigOptions : AnalyzerConfigOptions
25+
{
26+
private readonly IDictionary<string, string> _values;
27+
28+
public TestAnalyzerConfigOptions(IDictionary<string, string> values) => _values = values;
29+
30+
public override bool TryGetValue(string key, out string value)
31+
{
32+
return _values.TryGetValue(key, out value);
33+
}
34+
}
35+
}

src/FluentAssertions.Analyzers.Tests/CodeFixVerifierArguments.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using FluentAssertions.Analyzers.TestUtils;
23
using Microsoft.CodeAnalysis.CodeFixes;
34
using Microsoft.CodeAnalysis.Diagnostics;
45

src/FluentAssertions.Analyzers.Tests/DiagnosticVerifier.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ private static string GetStringFromDocument(Document document)
206206
/// <param name="analyzers">The analyzer to run on the documents</param>
207207
/// <param name="documents">The Documents that the analyzer will be run on</param>
208208
/// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns>
209-
private static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer[] analyzers, Document[] documents)
209+
private static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer[] analyzers, Document[] documents) => GetSortedDiagnosticsFromDocuments(analyzers, TestAnalyzerConfigOptionsProvider.Empty, documents);
210+
211+
private static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer[] analyzers, AnalyzerConfigOptionsProvider analyzerConfigOptions, Document[] documents)
210212
{
211213
var projects = new HashSet<Project>();
212214
foreach (var document in documents)
@@ -228,7 +230,7 @@ private static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer
228230
["CS1705"] = ReportDiagnostic.Suppress,
229231
["CS8019"] = ReportDiagnostic.Suppress // TODO: Unnecessary using directive
230232
}))
231-
.WithAnalyzers(ImmutableArray.Create(analyzers));
233+
.WithAnalyzers(ImmutableArray.Create(analyzers), new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty, analyzerConfigOptions));
232234
var relevantDiagnostics = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result;
233235

234236
var allDiagnostics = compilationWithAnalyzers.GetAllDiagnosticsAsync().Result;
@@ -291,7 +293,7 @@ public static void VerifyDiagnostic(DiagnosticVerifierArguments arguments)
291293
var project = CsProjectGenerator.CreateProject(arguments);
292294
var documents = project.Documents.ToArray();
293295

294-
var diagnostics = GetSortedDiagnosticsFromDocuments(arguments.DiagnosticAnalyzers.ToArray(), documents);
296+
var diagnostics = GetSortedDiagnosticsFromDocuments(arguments.DiagnosticAnalyzers.ToArray(), arguments.CreateAnalyzerConfigOptionsProvider(), documents);
295297
VerifyDiagnosticResults(diagnostics, arguments.DiagnosticAnalyzers.ToArray(), arguments.ExpectedDiagnostics.ToArray());
296298
}
297299

src/FluentAssertions.Analyzers.Tests/Tips/FluentAssertionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using FluentAssertions.Analyzers.TestUtils;
12
using Microsoft.CodeAnalysis.CodeFixes;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34

src/FluentAssertions.Analyzers.Tests/Tips/MsTestTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ namespace FluentAssertions.Analyzers.Tests.Tips
88
[TestClass]
99
public class MsTestTests
1010
{
11+
[TestMethod]
12+
[Implemented]
13+
public void SupportExcludingMethods()
14+
{
15+
var source = GenerateCode.MsTestAssertion("bool actual", "Assert.IsTrue(actual);");
16+
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
17+
.WithAllAnalyzers()
18+
.WithSources(source)
19+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.MSTestTestFramework_3_1_1)
20+
.WithAnalyzerConfigOption("ffa_excluded_methods", "M:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(System.Boolean)")
21+
.WithExpectedDiagnostics()
22+
);
23+
}
24+
1125
[DataTestMethod]
1226
[AssertionDiagnostic("Assert.Inconclusive({0});")]
1327
[AssertionDiagnostic("Assert.Fail({0});")]

src/FluentAssertions.Analyzers.Tests/Tips/NullConditionalAssertionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using FluentAssertions.Analyzers.TestUtils;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using System.Text;
34

45
namespace FluentAssertions.Analyzers.Tests.Tips

src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
42
using FluentAssertions.Analyzers.TestUtils;
53
using Microsoft.CodeAnalysis;
64
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -10,6 +8,20 @@ namespace FluentAssertions.Analyzers.Tests.Tips;
108
[TestClass]
119
public class NunitTests
1210
{
11+
[TestMethod]
12+
[Implemented]
13+
public void SupportExcludingMethods()
14+
{
15+
var source = GenerateCode.Nunit3Assertion("bool actual", "Assert.IsTrue(actual);");
16+
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
17+
.WithAllAnalyzers()
18+
.WithSources(source)
19+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0, PackageReference.Nunit_3_14_0)
20+
.WithAnalyzerConfigOption("ffa_excluded_methods", "M:NUnit.Framework.Assert.IsTrue(System.Boolean)")
21+
.WithExpectedDiagnostics()
22+
);
23+
}
24+
1325
#region Assert.cs
1426

1527
[DataTestMethod]
@@ -1705,7 +1717,7 @@ public void Nunit4_CollectionAssertDoesNotContain_TestCodeFix(string oldAssertio
17051717
Nunit4VerifyFix("object expected, List<object> actual", oldAssertion, newAssertion);
17061718
Nunit4VerifyFix("DateTime expected, DateTime[] actual", oldAssertion, newAssertion);
17071719
Nunit4VerifyFix("DateTime expected, List<DateTime> actual", oldAssertion, newAssertion);
1708-
}
1720+
}
17091721

17101722
[DataTestMethod]
17111723
[AssertionMethodCodeFix(
@@ -1775,7 +1787,7 @@ public void Nunit4_CollectionAssertDoesNotContain_WithCasting_TestCodeFix(string
17751787
[AssertionDiagnostic("Assert.That(actual, Has.All.InstanceOf<int>(){0});")]
17761788
[AssertionDiagnostic("Assert.That(actual, Has.All.InstanceOf(type){0});")]
17771789
[Implemented]
1778-
public void Nunit3_CollectionAssertAllItemsAreInstancesOfType_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable<string> actual, Type type", assertion);
1790+
public void Nunit3_CollectionAssertAllItemsAreInstancesOfType_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable<string> actual, Type type", assertion);
17791791

17801792
[DataTestMethod]
17811793
[AssertionDiagnostic("CollectionAssert.AllItemsAreInstancesOfType(actual, typeof(string){0});")]
@@ -1787,7 +1799,7 @@ public void Nunit4_CollectionAssertDoesNotContain_WithCasting_TestCodeFix(string
17871799
[AssertionDiagnostic("Assert.That(actual, Has.All.InstanceOf<int>());")]
17881800
[AssertionDiagnostic("Assert.That(actual, Has.All.InstanceOf(type));")]
17891801
[Implemented]
1790-
public void Nunit4_CollectionAssertAllItemsAreInstancesOfType_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable<string> actual, Type type", assertion);
1802+
public void Nunit4_CollectionAssertAllItemsAreInstancesOfType_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable<string> actual, Type type", assertion);
17911803

17921804
[DataTestMethod]
17931805
[AssertionCodeFix(

0 commit comments

Comments
 (0)