Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class FileHeaderTestBase : CodeFixVerifier
""settings"": {
""documentationRules"": {
""companyName"": ""FooCorp"",
""copyrightText"": ""Copyright (c) FooCorp. All rights reserved.""
""copyrightText"": ""{copyright} {companyName}. All rights reserved.""
}
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ namespace Bar
public async Task TestValidFileHeaderAsync()
{
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
// Copyright © 2015 FooCorp. All rights reserved.
// </copyright>
// <summary>This is a test file.</summary>

Expand All @@ -74,7 +74,7 @@ public async Task TestValidFileHeaderWithBordersAsync()
{
var testCode = @"//----------------------------------------
// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
// Copyright © 2015 FooCorp. All rights reserved.
// </copyright>
// <summary>This is a test file.</summary>
//----------------------------------------
Expand All @@ -87,6 +87,79 @@ namespace Bar
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that the literal use of {copyright} in a header does not match the configured token {copyright}.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestLiteralCopyrightAsync()
{
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// {copyright} FooCorp. All rights reserved.
// </copyright>
// <summary>This is a test file.</summary>

namespace Bar
{
}
";

var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1636Descriptor).WithLocation(1, 4);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that {copyright} correctly requires "Copyright ©" followed by at least a 4-digit year.
/// </summary>
/// <param name="copyright">The (malformed) string to use in the {copyright} position.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("© 1000")]
[InlineData("Copyright 1000")]
[InlineData("Copyright ©")]
[InlineData("Copyright © 93")]
[InlineData("Copyright © 1000-93")]
[InlineData("Copyright © 1000, 93")]
public async Task TestMalformedCopyrightAsync(string copyright)
{
var testCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// {copyright} FooCorp. All rights reserved.
// </copyright>
// <summary>This is a test file.</summary>

namespace Bar
{{
}}
";

var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1636Descriptor).WithLocation(1, 4);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that {copyright} correctly accepts multiple years (lists, spans, and mixtures of the two).
/// </summary>
/// <param name="copyright">The string to use in the {copyright} position.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("Copyright © 1000, 1003")]
[InlineData("Copyright © 1000-1003")]
[InlineData("Copyright © 1000-1003, 1005, 1007-1010")]
public async Task TestMultipleCopyrightYearsAsync(string copyright)
{
var testCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// {copyright} FooCorp. All rights reserved.
// </copyright>
// <summary>This is a test file.</summary>

namespace Bar
{{
}}
";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override IEnumerable<string> GetDisabledDiagnostics()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace StyleCop.Analyzers.Test.DocumentationRules
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -38,8 +39,8 @@ public virtual async Task TestNoFileHeaderAsync()
[Fact]
public async Task TestValidFileHeaderNoContentAsync()
{
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var testCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>
";

Expand All @@ -53,26 +54,26 @@ public async Task TestValidFileHeaderNoContentAsync()
[Fact]
public async Task TestValidMultilineCommentFileHeadersAsync()
{
var testCode1 = @"/* <copyright file=""Test0.cs"" company=""FooCorp"">
Copyright (c) FooCorp. All rights reserved.
var testCode1 = $@"/* <copyright file=""Test0.cs"" company=""FooCorp"">
Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
</copyright> */
";

var testCode2 = @"/*
var testCode2 = $@"/*
<copyright file=""Test1.cs"" company=""FooCorp"">
Copyright (c) FooCorp. All rights reserved.
Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
</copyright>
*/
";

var testCode3 = @"/*<copyright file=""Test2.cs"" company=""FooCorp"">
Copyright (c) FooCorp. All rights reserved.
var testCode3 = $@"/*<copyright file=""Test2.cs"" company=""FooCorp"">
Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
</copyright>*/
";

var testCode4 = @"/*
var testCode4 = $@"/*
* <copyright file=""Test3.cs"" company=""FooCorp"">
* Copyright (c) FooCorp. All rights reserved.
* Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
* </copyright>
*/
";
Expand All @@ -87,17 +88,17 @@ public async Task TestValidMultilineCommentFileHeadersAsync()
[Fact]
public async Task TestValidFileHeaderWithDirectivesAsync()
{
var testCode = @"#define MYDEFINE
var testCode = $@"#define MYDEFINE
#if (IGNORE_FILE_HEADERS)
#pragma warning disable SA1633
#endif
// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

namespace Bar
{
}
{{
}}
";

var expected = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1);
Expand All @@ -111,13 +112,13 @@ namespace Bar
[Fact]
public async Task TestValidFileHeaderWithWhitespaceAsync()
{
var testCode = @" // <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var testCode = $@" // <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

namespace Bar
{
}
{{
}}
";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -136,15 +137,15 @@ namespace Foo
{
}
";
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var fixedCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

#define MYDEFINE

namespace Foo
{
}
{{
}}
";

var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMissing).WithLocation(1, 1);
Expand All @@ -160,19 +161,19 @@ namespace Foo
[Fact]
public async Task TestNonXmlFileHeaderAsync()
{
var testCode = @"// Copyright (c) FooCorp. All rights reserved.
var testCode = $@"// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.

namespace Foo
{
}
{{
}}
";
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var fixedCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

namespace Foo
{
}
{{
}}
";

var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMalformed).WithLocation(1, 1);
Expand All @@ -188,20 +189,20 @@ namespace Foo
[Fact]
public async Task TestInvalidXmlFileHeaderAsync()
{
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var testCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.

namespace Foo
{
}
{{
}}
";
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var fixedCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

namespace Foo
{
}
{{
}}
";

var expected = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMalformed).WithLocation(1, 1);
Expand All @@ -217,22 +218,22 @@ namespace Foo
[Fact]
public async Task TestMalformedHeaderAsync()
{
var testCode = @"// <copyright test0.cs company=""FooCorp"">
var testCode = $@"// <copyright test0.cs company=""FooCorp"">
#define MYDEFINE

namespace Foo
{
}
{{
}}
";
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright (c) FooCorp. All rights reserved.
var fixedCode = $@"// <copyright file=""Test0.cs"" company=""FooCorp"">
// Copyright © {DateTime.Now.Year} FooCorp. All rights reserved.
// </copyright>

#define MYDEFINE

namespace Foo
{
}
{{
}}
";

var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1633DescriptorMalformed).WithLocation(1, 1);
Expand Down
Loading