Skip to content

Commit 887601e

Browse files
committed
Merge pull request #1505 from peteri/fix-1434
2 parents 632eb63 + 3405f85 commit 887601e

4 files changed

Lines changed: 688 additions & 22 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1634UnitTests.cs

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1414
/// </summary>
1515
public class SA1634UnitTests : FileHeaderTestBase
1616
{
17+
private string multiLineSettings;
18+
1719
/// <summary>
1820
/// Verifies that a file header without a copyright element will produce the expected diagnostic (none for the default case)
1921
/// </summary>
@@ -95,9 +97,191 @@ namespace Bar
9597
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
9698
}
9799

100+
/// <summary>
101+
/// Check multiple line copyright headers can be checked correctly.
102+
/// </summary>
103+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
104+
[Fact]
105+
public async Task TestValidMultiLineFileHeaderWithCopyrightLastAsync()
106+
{
107+
var testCode = @"// <author>
108+
// John Doe
109+
// </author>
110+
// <copyright file=""Test0.cs"" company=""FooCorp"">
111+
// Copyright (c) FooCorp. All rights reserved.
112+
//
113+
// Licence is FooBar MIT.
114+
// </copyright>
115+
116+
namespace Bar
117+
{
118+
}
119+
";
120+
this.multiLineSettings = @"
121+
{
122+
""settings"": {
123+
""documentationRules"": {
124+
""companyName"": ""FooCorp"",
125+
""copyrightText"": ""Copyright (c) FooCorp. All rights reserved.\n\nLicence is FooBar MIT.""
126+
}
127+
}
128+
}
129+
";
130+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
131+
}
132+
133+
/// <summary>
134+
/// Verifies that we keep leading spaces in a file header when adding copyright text.
135+
/// </summary>
136+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
137+
[Fact]
138+
public async Task TestFileHeaderKeepsLeadingWhiteSpaceWhenAddingCopyrightMessageAsync()
139+
{
140+
var testCode = @" // <author>FooCorp</author>
141+
// <summary>
142+
// FooCorp Bar class
143+
// </summary>
144+
145+
namespace Bar
146+
{
147+
}
148+
";
149+
var fixedCode = @" // <copyright file=""Test0.cs"" company=""FooCorp"">
150+
// Copyright (c) FooCorp. All rights reserved.
151+
// </copyright>
152+
// <author>FooCorp</author>
153+
// <summary>
154+
// FooCorp Bar class
155+
// </summary>
156+
157+
namespace Bar
158+
{
159+
}
160+
";
161+
162+
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 5);
163+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
164+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
165+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
166+
}
167+
168+
/// <summary>
169+
/// Verifies that a file header with missing copyright text the fix leaves behind other comments.
170+
/// </summary>
171+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
172+
[Fact]
173+
public async Task TestFileHeaderFixWithReplaceCopyrightTagTextAsync()
174+
{
175+
var testCode = @"// <author>
176+
// John Doe
177+
// </author>
178+
// <summary>This is a test file.</summary>
179+
180+
namespace Bar
181+
{
182+
}
183+
";
184+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
185+
// Copyright (c) FooCorp. All rights reserved.
186+
// </copyright>
187+
// <author>
188+
// John Doe
189+
// </author>
190+
// <summary>This is a test file.</summary>
191+
192+
namespace Bar
193+
{
194+
}
195+
";
196+
197+
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
198+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
199+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
200+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
201+
}
202+
203+
/// <summary>
204+
/// Verifies that a file header with missing copyright text and a multiline comment without leading stars the fix leaves behind other comments.
205+
/// </summary>
206+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
207+
[Fact]
208+
public async Task TestFileHeaderForMultilineCommentWithoutLeadingStarsFixWithReplaceCopyrightTagTextAsync()
209+
{
210+
var testCode = @"/* <author>
211+
John Doe
212+
</author>
213+
<summary>This is a test file.</summary>
214+
*/
215+
216+
namespace Bar
217+
{
218+
}
219+
";
220+
var fixedCode = @"/* <copyright file=""Test0.cs"" company=""FooCorp"">
221+
Copyright (c) FooCorp. All rights reserved.
222+
</copyright>
223+
<author>
224+
John Doe
225+
</author>
226+
<summary>This is a test file.</summary>
227+
*/
228+
229+
namespace Bar
230+
{
231+
}
232+
";
233+
234+
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
235+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
236+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
237+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
238+
}
239+
240+
/// <summary>
241+
/// Verifies that a file header with missing copyright text and a multiline comment with leading stars the fix leaves behind other comments.
242+
/// </summary>
243+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
244+
[Fact]
245+
public async Task TestFileHeaderForMultilineCommentWithLeadingStarsFixWithReplaceCopyrightTagTextAsync()
246+
{
247+
var testCode = @"/* <author>
248+
* John Doe
249+
* </author>
250+
* <summary>This is a test file.</summary>
251+
*/
252+
253+
namespace Bar
254+
{
255+
}
256+
";
257+
var fixedCode = @"/* <copyright file=""Test0.cs"" company=""FooCorp"">
258+
* Copyright (c) FooCorp. All rights reserved.
259+
* </copyright>
260+
* <author>
261+
* John Doe
262+
* </author>
263+
* <summary>This is a test file.</summary>
264+
*/
265+
266+
namespace Bar
267+
{
268+
}
269+
";
270+
271+
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
272+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
273+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
274+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
275+
}
276+
98277
protected override CodeFixProvider GetCSharpCodeFixProvider()
99278
{
100-
throw new System.NotImplementedException();
279+
return new FileHeaderCodeFixProvider();
280+
}
281+
282+
protected override string GetSettings()
283+
{
284+
return this.multiLineSettings ?? base.GetSettings();
101285
}
102286
}
103287
}

0 commit comments

Comments
 (0)