Skip to content

Commit a4888af

Browse files
committed
Implement unit tests for SA0002
1 parent ac58f45 commit a4888af

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Settings/SettingsFileCodeFixProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace StyleCop.Analyzers.Settings
2323
[Shared]
2424
internal class SettingsFileCodeFixProvider : CodeFixProvider
2525
{
26-
private const string StyleCopSettingsFileName = "stylecop.json";
27-
private const string DefaultSettingsFileContent = @"{
26+
internal const string DefaultSettingsFileContent = @"{
2827
// ACTION REQUIRED: This file was automatically added to your project, but it
2928
// will not take effect until additional steps are taken to enable it. See the
3029
// following page for additional information:
@@ -40,6 +39,8 @@ internal class SettingsFileCodeFixProvider : CodeFixProvider
4039
}
4140
";
4241

42+
private const string StyleCopSettingsFileName = "stylecop.json";
43+
4344
/// <inheritdoc/>
4445
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
4546
ImmutableArray.Create(
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.SpecialRules
5+
{
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Analyzers.Settings;
10+
using Analyzers.SpecialRules;
11+
using Microsoft.CodeAnalysis.Diagnostics;
12+
using TestHelper;
13+
using Xunit;
14+
15+
/// <summary>
16+
/// Unit tests for <see cref="SA0002InvalidSettingsFile"/>.
17+
/// </summary>
18+
public class SA0002UnitTests : DiagnosticVerifier
19+
{
20+
private const string TestCode = @"
21+
namespace NamespaceName { }
22+
";
23+
24+
private string settings;
25+
26+
[Fact]
27+
public async Task TestMissingSettingsAsync()
28+
{
29+
await this.VerifyCSharpDiagnosticAsync(TestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
30+
}
31+
32+
[Fact]
33+
public async Task TestValidSettingsAsync()
34+
{
35+
this.settings = SettingsFileCodeFixProvider.DefaultSettingsFileContent;
36+
37+
await this.VerifyCSharpDiagnosticAsync(TestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
}
39+
40+
[Fact]
41+
public async Task TestInvalidSettingsAsync()
42+
{
43+
this.settings = @"
44+
{
45+
""$schema"": ""https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json""
46+
""settings"": {
47+
""documentationRules"": {
48+
""companyName"": ""ACME, Inc"",
49+
""copyrightText"": ""Copyright 2015 {companyName}. All rights reserved.""
50+
}
51+
}
52+
}
53+
";
54+
55+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(null, 0, 0);
56+
57+
await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false);
58+
}
59+
60+
/// <inheritdoc/>
61+
protected override string GetSettings()
62+
{
63+
return this.settings;
64+
}
65+
66+
/// <inheritdoc/>
67+
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
68+
{
69+
yield return new SA0002InvalidSettingsFile();
70+
}
71+
}
72+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@
360360
<Compile Include="SpacingRules\SA1027UnitTests.cs" />
361361
<Compile Include="SpacingRules\SA1028UnitTests.cs" />
362362
<Compile Include="SpecialRules\SA0001UnitTests.cs" />
363+
<Compile Include="SpecialRules\SA0002UnitTests.cs" />
363364
<Compile Include="Verifiers\CodeFixVerifier.cs" />
364365
<Compile Include="Verifiers\DiagnosticVerifier.cs" />
365366
</ItemGroup>

0 commit comments

Comments
 (0)