Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated unit tests for SA1402 and SA1649 to verify both file content and file names #2237

Merged
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 @@ -4,12 +4,8 @@
namespace StyleCop.Analyzers.Test.DocumentationRules
{
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.DocumentationRules;
Expand Down Expand Up @@ -62,7 +58,7 @@ public static IEnumerable<object[]> TypeKeywords
[MemberData(nameof(TypeKeywords))]
public async Task VerifyWrongFileNameAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType
{{
Expand All @@ -81,7 +77,7 @@ public async Task VerifyWrongFileNameAsync(string typeKeyword)
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.cs", 3, 13 + typeKeyword.Length);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "WrongFileName.cs", "TestType.cs", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName.cs", newFileName: "TestType.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -94,7 +90,7 @@ public async Task VerifyWrongFileNameAsync(string typeKeyword)
[MemberData(nameof(TypeKeywords))]
public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType
{{
Expand All @@ -113,7 +109,7 @@ public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.svc.cs", 3, 13 + typeKeyword.Length);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.svc.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.svc.cs").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "WrongFileName.svc.cs", "TestType.svc.cs", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName.svc.cs", newFileName: "TestType.svc.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -126,7 +122,7 @@ public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
[MemberData(nameof(TypeKeywords))]
public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType
{{
Expand All @@ -145,7 +141,7 @@ public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName", 3, 13 + typeKeyword.Length);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "WrongFileName", "TestType", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName", newFileName: "TestType", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -157,7 +153,7 @@ public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
[MemberData(nameof(TypeKeywords))]
public async Task VerifyCaseInsensitivityAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType
{{
Expand All @@ -177,7 +173,7 @@ public async Task VerifyCaseInsensitivityAsync(string typeKeyword)
[MemberData(nameof(TypeKeywords))]
public async Task VerifyFirstTypeIsUsedAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType
{{
Expand All @@ -201,7 +197,7 @@ public async Task VerifyFirstTypeIsUsedAsync(string typeKeyword)
[MemberData(nameof(TypeKeywords))]
public async Task VerifyThatPartialTypesAreIgnoredAsync(string typeKeyword)
{
var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public partial {typeKeyword} TestType
{{
Expand All @@ -223,7 +219,7 @@ public async Task VerifyStyleCopNamingConventionForGenericTypeAsync(string typeK
{
this.useMetadataSettings = false;

var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType<T1, T2, T3>
{{
Expand All @@ -235,7 +231,7 @@ public async Task VerifyStyleCopNamingConventionForGenericTypeAsync(string typeK
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType{T1,T2,T3}.cs").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "TestType`3.cs", "TestType{T1,T2,T3}.cs", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, testCode, oldFileName: "TestType`3.cs", newFileName: "TestType{T1,T2,T3}.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -249,7 +245,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeAsync(string typeK
{
this.useMetadataSettings = true;

var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType<T1, T2, T3>
{{
Expand All @@ -263,7 +259,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeAsync(string typeK
expectedDiagnostic = this.CSharpDiagnostic().WithLocation("TestType.cs", 3, 13 + typeKeyword.Length);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "TestType.cs", "TestType`3.cs", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, testCode, oldFileName: "TestType.cs", newFileName: "TestType`3.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -278,7 +274,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
{
this.useMetadataSettings = true;

var testCode = $@"namespace TestNameSpace
var testCode = $@"namespace TestNamespace
{{
public {typeKeyword} TestType<T>
{{
Expand All @@ -297,7 +293,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("TestType.svc.cs", 3, 13 + typeKeyword.Length);
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType.svc.cs").ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType`1.svc.cs").ConfigureAwait(false);
await this.VerifyRenameAsync(testCode, "TestType.svc.cs", "TestType`1.svc.cs", CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "TestType.svc.cs", newFileName: "TestType`1.svc.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -307,7 +303,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
[Fact]
public async Task VerifyWithoutFirstTypeAsync()
{
var testCode = @"namespace TestNameSpace
var testCode = @"namespace TestNamespace
{
}
";
Expand All @@ -332,39 +328,5 @@ protected override string GetSettings()
{
return this.useMetadataSettings ? MetadataSettings : StyleCopSettings;
}

private async Task VerifyRenameAsync(string source, string sourceFileName, string expectedFileName, CancellationToken cancellationToken)
{
var analyzers = this.GetCSharpDiagnosticAnalyzers().ToImmutableArray();
var document = this.CreateDocument(source, LanguageNames.CSharp, sourceFileName);
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);

Assert.Equal(1, analyzerDiagnostics.Length);

var actions = new List<CodeAction>();
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), cancellationToken);
await this.GetCSharpCodeFixProvider().RegisterCodeFixesAsync(context).ConfigureAwait(false);

Assert.Equal(1, actions.Count);

var operations = await actions[0].GetOperationsAsync(cancellationToken).ConfigureAwait(false);

var changedSolution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;

var solutionChanges = changedSolution.GetChanges(document.Project.Solution);
var projectChanges = solutionChanges.GetProjectChanges().ToArray();

Assert.Equal(1, projectChanges.Length);

var removedDocuments = projectChanges[0].GetRemovedDocuments().ToArray();
Assert.Equal(1, removedDocuments.Length);
Assert.Equal(document.Id, removedDocuments[0]);

var addedDocuments = projectChanges[0].GetAddedDocuments().ToArray();
Assert.Equal(1, addedDocuments.Length);

var newDocument = changedSolution.GetDocument(addedDocuments[0]);
Assert.Equal(expectedFileName, newDocument.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public partial {this.Keyword} Bar

}}";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
var fixedCode = new[]
{
$@"public partial {this.Keyword} Foo
Expand All @@ -150,7 +151,7 @@ public partial {this.Keyword} Bar

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -163,6 +164,7 @@ public async Task TestPreferFilenameTypeAsync()
{{
}}";

var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
var fixedCode = new[]
{
$@"public {this.Keyword} Test0
Expand All @@ -178,7 +180,7 @@ public async Task TestPreferFilenameTypeAsync()

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async Task TestTwoElementsAsync()
public delegate void Bar();
";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
var fixedCode = new[]
{
@"public delegate void Foo();
Expand All @@ -39,7 +40,7 @@ public async Task TestTwoElementsAsync()

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -49,6 +50,7 @@ public async Task TestTwoGenericElementsAsync()
public delegate void Bar<T1, T2, T3>(T1 x, T2 y, T3 z);
";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
var fixedCode = new[]
{
@"public delegate void Foo();
Expand All @@ -61,7 +63,7 @@ public async Task TestTwoGenericElementsAsync()

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand Down Expand Up @@ -96,6 +98,7 @@ public async Task TestThreeElementsAsync()
public delegate void FooBar();
";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs", "FooBar.cs" };
var fixedCode = new[]
{
@"public delegate void Foo();
Expand All @@ -114,7 +117,7 @@ public async Task TestThreeElementsAsync()

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -124,6 +127,7 @@ public async Task TestPreferFilenameTypeAsync()
public delegate void Test0();
";

var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
var fixedCode = new[]
{
$@"public delegate void Test0();
Expand All @@ -136,7 +140,7 @@ public async Task TestPreferFilenameTypeAsync()

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum Bar
}
";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
var fixedCode = new[]
{
@"enum Foo
Expand All @@ -55,7 +56,7 @@ enum Bar

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand Down Expand Up @@ -111,6 +112,7 @@ enum FooBar
}
";

var fixedFileNames = new[] { "Test0.cs", "Bar.cs", "FooBar.cs" };
var fixedCode = new[]
{
@"enum Foo
Expand Down Expand Up @@ -138,7 +140,7 @@ enum FooBar

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -154,6 +156,7 @@ enum Test0
}
";

var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
var fixedCode = new[]
{
@"enum Test0
Expand All @@ -172,7 +175,7 @@ enum Test0

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}
}
}
Loading