-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1277 from vweijsters/issue-1271
- Loading branch information
Showing
10 changed files
with
460 additions
and
90 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/NoXmlFileHeaderUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
namespace StyleCop.Analyzers.Test.DocumentationRules | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Analyzers.DocumentationRules; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Unit tests for file header that do not follow the XML syntax. | ||
/// </summary> | ||
public class NoXmlFileHeaderUnitTests : DiagnosticVerifier | ||
{ | ||
private const string SettingsFileName = "stylecop.json"; | ||
private const string TestSettings = @" | ||
{ | ||
""settings"": { | ||
""documentationRules"": { | ||
""companyName"": ""FooCorp"", | ||
""copyrightText"": ""Copyright (c) {companyName}. All rights reserved.\nLicensed under the {licenseName} license. See {licenseFile} file in the project root for full license information."", | ||
""variables"": { | ||
""licenseName"": ""???"", | ||
""licenseFile"": ""LICENSE"", | ||
}, | ||
""xmlHeader"": false | ||
} | ||
} | ||
} | ||
"; | ||
|
||
/// <summary> | ||
/// Verifies that a file header with an auto-generated comment will not produce a diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestAutoGeneratedSourceFileAsync() | ||
{ | ||
var testCode = @"// <auto-generated/> | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that a valid file header built using single line comments will not produce a diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestValidFileHeaderWithSingleLineCommentsAsync() | ||
{ | ||
var testCode = @"// Copyright (c) FooCorp. All rights reserved. | ||
// Licensed under the ??? license. See LICENSE file in the project root for full license information. | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that a valid file header built using multi line comments will not produce a diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestValidFileHeaderWithMultiLineCommentsAsync() | ||
{ | ||
var testCodeFormat1 = @"/* Copyright (c) FooCorp. All rights reserved. | ||
* Licensed under the ??? license. See LICENSE file in the project root for full license information. | ||
*/ | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
var testCodeFormat2 = @"/* Copyright (c) FooCorp. All rights reserved. | ||
Licensed under the ??? license. See LICENSE file in the project root for full license information. */ | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCodeFormat1, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(testCodeFormat2, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that a file header without text / only whitespace will produce the expected diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestInvalidFileHeaderWithoutTextAsync() | ||
{ | ||
var testCodeFormat1 = @"// | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
var testCodeFormat2 = "// " + @" | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
|
||
var expected = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1635Descriptor).WithLocation(1, 1); | ||
await this.VerifyCSharpDiagnosticAsync(testCodeFormat1, expected, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(testCodeFormat2, expected, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that an invalid file header built using single line comments will produce the expected diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestInvalidFileHeaderWithWrongTextAsync() | ||
{ | ||
var testCode = @"// Copyright (c) BarCorp. All rights reserved. | ||
// Licensed under the ??? license. See LICENSE file in the project root for full license information. | ||
namespace Bar | ||
{ | ||
} | ||
"; | ||
var expected = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1636Descriptor).WithLocation(1, 1); | ||
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override Solution CreateSolution(ProjectId projectId, string language) | ||
{ | ||
var solution = base.CreateSolution(projectId, language); | ||
|
||
var documentId = DocumentId.CreateNewId(projectId); | ||
return solution | ||
.AddAdditionalDocument(documentId, SettingsFileName, TestSettings); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected sealed override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() | ||
{ | ||
yield return new FileHeaderAnalyzers(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/DocumentationResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.