Skip to content

Commit

Permalink
add more tests and fix extern alias
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubLinhart committed Nov 3, 2022
1 parent edfbcf1 commit 1bcb856
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ private static SyntaxNode GetRelevantNode(SyntaxNode innerNode)
return currentNode;
}

if (currentNode is ExternAliasDirectiveSyntax)
{
return currentNode;
}

currentNode = currentNode.Parent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,202 @@ namespace StyleCop.Analyzers.Test.CSharp10.LayoutRules

public class SA1516CSharp10UnitTests : SA1516CSharp9UnitTests
{
private const string CorrectCode = @"extern alias corlib;
/// <summary>
/// Verifies that SA1516 is reported for usings and extern alias outside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestThatDiagnosticIIsReportedOnUsingsAndExternAliasOutsideFileScopedNamespaceAsync()
{
var testCode = @"extern alias corlib;
[|using|] System;
using System.Linq;
using a = System.Collections;
[|namespace|] Foo;
";

var fixedCode = @"extern alias corlib;
using System;
using System.Linq;
using a = System.Collections.Generic;
using a = System.Collections;
namespace Foo;
";

public class Bar
{
public string Test1;
public string Test2;
public string Test3;
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

public string TestProperty1 { get; set; }
/// <summary>
/// Verifies that SA1516 is reported for usings inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestThatDiagnosticIIsReportedOnSpacingWithUsingsInsideFileScopedNamespaceAsync()
{
var testCode = @"namespace Foo;
[|using|] System;
using System.Linq;
using a = System.Collections;
";

public string TestProperty2 { get; set; }
/// <summary>
/// A summary.
/// </summary>
public string TestProperty3 { get; set; }
var fixedCode = @"namespace Foo;
public string TestProperty4
{
get
{
return Test1;
using System;
using System.Linq;
using a = System.Collections;
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

set
/// <summary>
/// Verifies that SA1516 is reported for member declarations inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestThatDiagnosticIIsReportedOnMemberDeclarationsInsideFileScopedNamespaceAsync()
{
Test1 = value;
}
}
var testCode = @"namespace Foo;
[|public|] class Bar
{
}
[|public|] enum Foobar
{
}
";

public string FooValue, BarValue;
var fixedCode = @"namespace Foo;
[Obsolete]
public enum TestEnum
{
Value1,
Value2
}
public class Bar
{
}
public enum Foobar
{
}
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is reported for usings and member declarations inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestThatDiagnosticIIsReportedOnUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync()
{
var testCode = @"namespace Foo;
[|using|] System;
using System.Linq;
using a = System.Collections;
[|public|] class Bar
{
}
[|public|] enum Foobar
{
}
";

var fixedCode = @"namespace Foo;
using System;
using System.Linq;
using a = System.Collections;
public class Bar
{
}
public enum Foobar
{
}
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is not reported for code with correct blank lines.
/// Verifies that SA1516 is reported extern alias inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestFileScopedNamespaceCorrectSpacingAsync()
public async Task TestThatDiagnosticIIsReportedOnExternAliasInsideFileScopedNamespaceAsync()
{
await VerifyCSharpDiagnosticAsync(CorrectCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
var testCode = @"namespace Foo;
[|extern|] alias corlib;
";

var fixedCode = @"namespace Foo;
extern alias corlib;
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is reported for code with missing correct blank lines.
/// Verifies that SA1516 is reported extern alias and usings inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestFileScopedNamespaceWrongSpacingAsync()
public async Task TestThatDiagnosticIIsReportedOnExternAliasAndUsingsInsideFileScopedNamespaceAsync()
{
var testCode = @"extern alias corlib;
{|#0:using|} System;
var testCode = @"namespace Foo;
[|extern|] alias corlib;
[|using|] System;
using System.Linq;
using a = System.Collections.Generic;
{|#1:namespace|} Foo;
{|#2:public|} class Bar
using a = System.Collections;
";

var fixedCode = @"namespace Foo;
extern alias corlib;
using System;
using System.Linq;
using a = System.Collections;
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

/// <summary>
/// Verifies that SA1516 is reported extern alias, usings and member declarations
/// inside a file scoped namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
public async Task TestThatDiagnosticIIsReportedOnExternAliasUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync()
{
var testCode = @"namespace Foo;
[|extern|] alias corlib;
[|using|] System;
using System.Linq;
using a = System.Collections;
[|public|] class Bar
{
}
{|#3:public|} enum Foobar
[|public|] enum Foobar
{
}
";

var fixedCode = @"extern alias corlib;
var fixedCode = @"namespace Foo;
extern alias corlib;
using System;
using System.Linq;
using a = System.Collections.Generic;
namespace Foo;
using a = System.Collections;
public class Bar
{
Expand All @@ -116,6 +222,11 @@ public enum Foobar
}
";

await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

private static Task VerifyCSharpFixAsync(string testCode, string fixedCode)
{
var test = new CSharpTest(LanguageVersion.CSharp10)
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
Expand All @@ -126,15 +237,8 @@ public enum Foobar
},
FixedCode = fixedCode,
};
var expectedDiagnostic = new[]
{
Diagnostic().WithLocation(0),
Diagnostic().WithLocation(1),
Diagnostic().WithLocation(2),
Diagnostic().WithLocation(3),
};
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostic);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);

return test.RunAsync(CancellationToken.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,35 @@ private static void HandleFileScopedNamespaceDeclaration(SyntaxNodeAnalysisConte
{
var namespaceDeclaration = (BaseNamespaceDeclarationSyntaxWrapper)context.Node;

var usings = namespaceDeclaration.Usings;
var members = namespaceDeclaration.Members;

HandleUsings(context, usings, settings);
HandleMemberList(context, members);

if (namespaceDeclaration.Externs.Count > 0)
{
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, namespaceDeclaration.Externs[0]);
}

if (namespaceDeclaration.Usings.Count > 0)
{
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, namespaceDeclaration.Usings[0]);

if (namespaceDeclaration.Externs.Count > 0)
{
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Externs[namespaceDeclaration.Externs.Count - 1], namespaceDeclaration.Usings[0]);
}
}

if (members.Count > 0)
{
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, members[0]);

if (namespaceDeclaration.Usings.Count > 0)
{
ReportIfThereIsNoBlankLine(context, usings[usings.Count - 1], members[0]);
}
}
}

Expand Down

0 comments on commit 1bcb856

Please sign in to comment.