Skip to content

Commit a76a0b1

Browse files
committed
Convert SettingsFileCodeFixProviderUnitTests to the new test helpers
1 parent c89bc23 commit a76a0b1

3 files changed

Lines changed: 116 additions & 98 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsFileCodeFixProviderUnitTests.cs

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33

44
namespace StyleCop.Analyzers.Test.Settings
55
{
6-
using System.Collections.Generic;
76
using System.Threading;
87
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis;
10-
using Microsoft.CodeAnalysis.CodeFixes;
11-
using Microsoft.CodeAnalysis.Diagnostics;
128
using StyleCop.Analyzers.DocumentationRules;
139
using StyleCop.Analyzers.Settings;
14-
using TestHelper;
1510
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.DocumentationRules.FileHeaderAnalyzers,
13+
StyleCop.Analyzers.Settings.SettingsFileCodeFixProvider>;
1614

1715
/// <summary>
1816
/// Unit tests for the <see cref="SettingsFileCodeFixProvider"/>.
1917
/// </summary>
20-
public class SettingsFileCodeFixProviderUnitTests : CodeFixVerifier
18+
public class SettingsFileCodeFixProviderUnitTests
2119
{
2220
private const string TestCode = @"
2321
namespace NamespaceName
2422
{
2523
}
2624
";
2725

28-
private bool createSettingsFile;
29-
private string settingsFileName = SettingsHelper.SettingsFileName;
30-
3126
/// <summary>
3227
/// Verifies that a file without a header, but with leading trivia will produce the correct diagnostic message.
3328
/// </summary>
3429
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
3530
[Fact]
3631
public async Task TestMissingFileHeaderWithLeadingTriviaAsync()
3732
{
38-
this.createSettingsFile = false;
39-
40-
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1);
41-
await this.VerifyCSharpDiagnosticAsync(TestCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
42-
43-
// verify that the code fix does not alter the document
44-
await this.VerifyCSharpFixAsync(TestCode, TestCode).ConfigureAwait(false);
33+
await new CSharpTest
34+
{
35+
TestCode = TestCode,
36+
ExpectedDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
37+
FixedCode = TestCode,
38+
RemainingDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
39+
FixedAdditionalFiles =
40+
{
41+
(SettingsHelper.SettingsFileName, SettingsFileCodeFixProvider.DefaultSettingsFileContent),
42+
},
43+
Settings = null,
44+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
4545
}
4646

4747
/// <summary>
@@ -51,10 +51,18 @@ public async Task TestMissingFileHeaderWithLeadingTriviaAsync()
5151
[Fact]
5252
public async Task TestSettingsFileDoesNotExistAsync()
5353
{
54-
this.createSettingsFile = false;
55-
56-
var offeredFixes = await this.GetOfferedCSharpFixesAsync(TestCode).ConfigureAwait(false);
57-
Assert.Single(offeredFixes);
54+
await new CSharpTest
55+
{
56+
TestCode = TestCode,
57+
ExpectedDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
58+
FixedCode = TestCode,
59+
RemainingDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
60+
FixedAdditionalFiles =
61+
{
62+
(SettingsHelper.SettingsFileName, SettingsFileCodeFixProvider.DefaultSettingsFileContent),
63+
},
64+
Settings = null,
65+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5866
}
5967

6068
/// <summary>
@@ -64,11 +72,14 @@ public async Task TestSettingsFileDoesNotExistAsync()
6472
[Fact]
6573
public async Task TestSettingsFileAlreadyExistsAsync()
6674
{
67-
this.createSettingsFile = true;
68-
this.settingsFileName = SettingsHelper.SettingsFileName;
69-
70-
var offeredFixes = await this.GetOfferedCSharpFixesAsync(TestCode).ConfigureAwait(false);
71-
Assert.Empty(offeredFixes);
75+
await new CSharpTest
76+
{
77+
TestCode = TestCode,
78+
ExpectedDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
79+
FixedCode = TestCode,
80+
Settings = "{}",
81+
SettingsFileName = SettingsHelper.SettingsFileName,
82+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
7283
}
7384

7485
/// <summary>
@@ -78,40 +89,14 @@ public async Task TestSettingsFileAlreadyExistsAsync()
7889
[Fact]
7990
public async Task TestDotPrefixedSettingsFileAlreadyExistsAsync()
8091
{
81-
this.createSettingsFile = true;
82-
this.settingsFileName = SettingsHelper.AltSettingsFileName;
83-
84-
var offeredFixes = await this.GetOfferedCSharpFixesAsync(TestCode).ConfigureAwait(false);
85-
Assert.Empty(offeredFixes);
86-
}
87-
88-
/// <inheritdoc/>
89-
protected override string GetSettings()
90-
{
91-
if (this.createSettingsFile)
92+
await new CSharpTest
9293
{
93-
return "{}";
94-
}
95-
96-
return null;
97-
}
98-
99-
/// <inheritdoc/>
100-
protected override string GetSettingsFileName()
101-
{
102-
return this.settingsFileName;
103-
}
104-
105-
/// <inheritdoc/>
106-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
107-
{
108-
yield return new FileHeaderAnalyzers();
109-
}
110-
111-
/// <inheritdoc/>
112-
protected override CodeFixProvider GetCSharpCodeFixProvider()
113-
{
114-
return new SettingsFileCodeFixProvider();
94+
TestCode = TestCode,
95+
ExpectedDiagnostics = { Diagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1) },
96+
FixedCode = TestCode,
97+
Settings = "{}",
98+
SettingsFileName = SettingsHelper.AltSettingsFileName,
99+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
115100
}
116101
}
117102
}

0 commit comments

Comments
 (0)