Skip to content

Commit 811f681

Browse files
committed
Add a regression test for #1829
1 parent f1bf53b commit 811f681

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1649UnitTests.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,39 @@ public async Task VerifyWrongFileNameAsync(string typeKeyword)
8181
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.cs", 3, 13 + typeKeyword.Length);
8282
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.cs").ConfigureAwait(false);
8383
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
84-
await this.VerifyRenameAsync(testCode, "TestType.cs", CancellationToken.None).ConfigureAwait(false);
84+
await this.VerifyRenameAsync(testCode, "WrongFileName.cs", "TestType.cs", CancellationToken.None).ConfigureAwait(false);
85+
}
86+
87+
/// <summary>
88+
/// Verifies that a wrong file name with multiple extensions is correctly reported and fixed. This is a
89+
/// regression test for DotNetAnalyzers/StyleCopAnalyzers#1829.
90+
/// </summary>
91+
/// <param name="typeKeyword">The type keyword to use during the test.</param>
92+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
93+
[Theory]
94+
[MemberData(nameof(TypeKeywords))]
95+
public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
96+
{
97+
var testCode = $@"namespace TestNameSpace
98+
{{
99+
public {typeKeyword} TestType
100+
{{
101+
}}
102+
}}
103+
";
104+
105+
var fixedCode = $@"namespace TestNamespace
106+
{{
107+
public {typeKeyword} TestType
108+
{{
109+
}}
110+
}}
111+
";
112+
113+
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.svc.cs", 3, 13 + typeKeyword.Length);
114+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.svc.cs").ConfigureAwait(false);
115+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.svc.cs").ConfigureAwait(false);
116+
await this.VerifyRenameAsync(testCode, "WrongFileName.svc.cs", "TestType.svc.cs", CancellationToken.None).ConfigureAwait(false);
85117
}
86118

87119
/// <summary>
@@ -171,7 +203,7 @@ public async Task VerifyStyleCopNamingConventionForGenericTypeAsync(string typeK
171203
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
172204
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
173205
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType{T1,T2,T3}.cs").ConfigureAwait(false);
174-
await this.VerifyRenameAsync(testCode, "TestType{T1,T2,T3}.cs", CancellationToken.None).ConfigureAwait(false);
206+
await this.VerifyRenameAsync(testCode, "TestType`3.cs", "TestType{T1,T2,T3}.cs", CancellationToken.None).ConfigureAwait(false);
175207
}
176208

177209
/// <summary>
@@ -199,7 +231,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeAsync(string typeK
199231
expectedDiagnostic = this.CSharpDiagnostic().WithLocation("TestType.cs", 3, 13 + typeKeyword.Length);
200232
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
201233
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
202-
await this.VerifyRenameAsync(testCode, "TestType`3.cs", CancellationToken.None).ConfigureAwait(false);
234+
await this.VerifyRenameAsync(testCode, "TestType.cs", "TestType`3.cs", CancellationToken.None).ConfigureAwait(false);
203235
}
204236

205237
/// <summary>
@@ -235,10 +267,10 @@ protected override string GetSettings()
235267
return this.useMetadataSettings ? MetadataSettings : StyleCopSettings;
236268
}
237269

238-
private async Task VerifyRenameAsync(string source, string expectedFileName, CancellationToken cancellationToken)
270+
private async Task VerifyRenameAsync(string source, string sourceFileName, string expectedFileName, CancellationToken cancellationToken)
239271
{
240272
var analyzers = this.GetCSharpDiagnosticAnalyzers().ToImmutableArray();
241-
var document = this.CreateDocument(source, LanguageNames.CSharp);
273+
var document = this.CreateDocument(source, LanguageNames.CSharp, sourceFileName);
242274
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);
243275

244276
Assert.Equal(1, analyzerDiagnostics.Length);

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
107107
/// <param name="language">The language the source classes are in. Values may be taken from the
108108
/// <see cref="LanguageNames"/> class.</param>
109109
/// <returns>A <see cref="Document"/> created from the source string.</returns>
110-
protected Document CreateDocument(string source, string language = LanguageNames.CSharp)
110+
protected Document CreateDocument(string source, string language = LanguageNames.CSharp, string fileName = null)
111111
{
112-
return this.CreateProject(new[] { source }, language).Documents.Single();
112+
string[] filenames = null;
113+
if (fileName != null)
114+
{
115+
filenames = new[] { fileName };
116+
}
117+
118+
return this.CreateProject(new[] { source }, language, filenames).Documents.Single();
113119
}
114120

115121
/// <summary>

0 commit comments

Comments
 (0)