Skip to content

Commit a07e6e3

Browse files
committed
Merge branch 'master' into SA1132
2 parents 0d23b74 + 51cab26 commit a07e6e3

25 files changed

Lines changed: 502 additions & 196 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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
5+
{
6+
using Xunit;
7+
8+
public class AttributeTests
9+
{
10+
[Fact]
11+
public void TestNoCodeFixAttributeReason()
12+
{
13+
string reason = "Reason";
14+
var attribute = new NoCodeFixAttribute(reason);
15+
Assert.Same(reason, attribute.Reason);
16+
}
17+
18+
[Fact]
19+
public void TestNoDiagnosticAttributeReason()
20+
{
21+
string reason = "Reason";
22+
var attribute = new NoDiagnosticAttribute(reason);
23+
Assert.Same(reason, attribute.Reason);
24+
}
25+
}
26+
}

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,27 @@ public async Task TestFileHeaderWithMissingCopyrightTagAsync()
2828
// </author>
2929
// <summary>This is a test file.</summary>
3030
31+
namespace Bar
32+
{
33+
}
34+
";
35+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
36+
// Copyright (c) FooCorp. All rights reserved.
37+
// </copyright>
38+
// <author>
39+
// John Doe
40+
// </author>
41+
// <summary>This is a test file.</summary>
42+
3143
namespace Bar
3244
{
3345
}
3446
";
3547

3648
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
3749
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
50+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
51+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3852
}
3953

4054
/// <summary>
@@ -68,13 +82,23 @@ public async Task TestValidFileHeaderWithShorthandCopyrightAsync()
6882
{
6983
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp""/>
7084
85+
namespace Bar
86+
{
87+
}
88+
";
89+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
90+
// Copyright (c) FooCorp. All rights reserved.
91+
// </copyright>
92+
7193
namespace Bar
7294
{
7395
}
7496
";
7597

7698
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1635Descriptor).WithLocation(1, 4);
7799
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
100+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
101+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
78102
}
79103

80104
/// <summary>
@@ -88,13 +112,26 @@ public async Task TestInvalidFileHeaderWithCopyrightInWrongCaseAsync()
88112
// Copyright (c) FooCorp. All rights reserved.
89113
// </Copyright>
90114
115+
namespace Bar
116+
{
117+
}
118+
";
119+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
120+
// Copyright (c) FooCorp. All rights reserved.
121+
// </copyright>
122+
// <Copyright file=""Test0.cs"" company=""FooCorp"">
123+
// Copyright (c) FooCorp. All rights reserved.
124+
// </Copyright>
125+
91126
namespace Bar
92127
{
93128
}
94129
";
95130

96131
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
97132
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
133+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
134+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
98135
}
99136

100137
/// <summary>
@@ -162,7 +199,7 @@ namespace Bar
162199
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 5);
163200
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
164201
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
165-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
202+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
166203
}
167204

168205
/// <summary>
@@ -197,7 +234,7 @@ namespace Bar
197234
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
198235
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
199236
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
200-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
237+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
201238
}
202239

203240
/// <summary>
@@ -234,7 +271,7 @@ namespace Bar
234271
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
235272
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
236273
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
237-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
274+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
238275
}
239276

240277
/// <summary>
@@ -271,7 +308,7 @@ namespace Bar
271308
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
272309
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
273310
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
274-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
311+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
275312
}
276313

277314
/// <summary>
@@ -316,7 +353,7 @@ namespace Bar
316353
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
317354
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
318355
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
319-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
356+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
320357
}
321358

322359
protected override CodeFixProvider GetCSharpCodeFixProvider()

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1637UnitTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ public async Task TestCopyrightElementWithoutFileAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1637Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
protected override CodeFixProvider GetCSharpCodeFixProvider()
3848
{
39-
throw new System.NotImplementedException();
49+
return new FileHeaderCodeFixProvider();
4050
}
4151
}
4252
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1638UnitTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ public async Task TestCopyrightElementWithMismatchingFileAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1638Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
protected override CodeFixProvider GetCSharpCodeFixProvider()
3848
{
39-
throw new System.NotImplementedException();
49+
return new FileHeaderCodeFixProvider();
4050
}
4151
}
4252
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1640UnitTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public async Task TestCopyrightElementWithoutCompanyAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
/// <summary>
@@ -45,13 +55,23 @@ public async Task TestCopyrightElementWithEmptyCompanyAttributeAsync()
4555
// Copyright (c) FooCorp. All rights reserved.
4656
// </copyright>
4757
58+
namespace Bar
59+
{
60+
}
61+
";
62+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
63+
// Copyright (c) FooCorp. All rights reserved.
64+
// </copyright>
65+
4866
namespace Bar
4967
{
5068
}
5169
";
5270

5371
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
5472
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
73+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5575
}
5676

5777
/// <summary>
@@ -65,18 +85,28 @@ public async Task TestCopyrightElementWithWhitespaceOnlyCompanyAttributeAsync()
6585
// Copyright (c) FooCorp. All rights reserved.
6686
// </copyright>
6787
88+
namespace Bar
89+
{
90+
}
91+
";
92+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
93+
// Copyright (c) FooCorp. All rights reserved.
94+
// </copyright>
95+
6896
namespace Bar
6997
{
7098
}
7199
";
72100

73101
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
74102
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
103+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
104+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
75105
}
76106

77107
protected override CodeFixProvider GetCSharpCodeFixProvider()
78108
{
79-
throw new System.NotImplementedException();
109+
return new FileHeaderCodeFixProvider();
80110
}
81111
}
82112
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1641UnitTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public async Task TestCopyrightElementWithWrongCompanyAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1641Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
/// <summary>
@@ -45,18 +55,28 @@ public async Task TestCopyrightElementWithInvalidCaseCompanyAttributeAsync()
4555
// Copyright (c) FooCorp. All rights reserved.
4656
// </copyright>
4757
58+
namespace Bar
59+
{
60+
}
61+
";
62+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
63+
// Copyright (c) FooCorp. All rights reserved.
64+
// </copyright>
65+
4866
namespace Bar
4967
{
5068
}
5169
";
5270

5371
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1641Descriptor).WithLocation(1, 4);
5472
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
73+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5575
}
5676

5777
protected override CodeFixProvider GetCSharpCodeFixProvider()
5878
{
59-
throw new System.NotImplementedException();
79+
return new FileHeaderCodeFixProvider();
6080
}
6181
}
6282
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1500/SA1500UnitTests.Properties.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,51 @@ public bool Property11
415415
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
416416
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
417417
}
418+
419+
/// <summary>
420+
/// Verifies that a single line accessor with an embedded block will be handled correctly.
421+
/// </summary>
422+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
423+
[Fact]
424+
public async Task TestSingleLineAccessorWithEmbeddedBlockAsync()
425+
{
426+
var testCode = @"
427+
public class TestClass
428+
{
429+
public int[] TestProperty
430+
{
431+
get {
432+
{
433+
return new[] { 1, 2, 3 }; } }
434+
}
435+
}
436+
";
437+
438+
var fixedTestCode = @"
439+
public class TestClass
440+
{
441+
public int[] TestProperty
442+
{
443+
get
444+
{
445+
{
446+
return new[] { 1, 2, 3 };
447+
}
448+
}
449+
}
450+
}
451+
";
452+
453+
DiagnosticResult[] expected =
454+
{
455+
this.CSharpDiagnostic().WithLocation(6, 13),
456+
this.CSharpDiagnostic().WithLocation(8, 43),
457+
this.CSharpDiagnostic().WithLocation(8, 45)
458+
};
459+
460+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
461+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
462+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
463+
}
418464
}
419465
}

0 commit comments

Comments
 (0)