Skip to content

Commit 8f0db9e

Browse files
committed
Added tests to verify tuple expressions as well
1 parent 91e2fab commit 8f0db9e

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1008CSharp7UnitTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,5 +946,21 @@ public void TestMethod()
946946
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
947947
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
948948
}
949+
950+
[Fact]
951+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
952+
public async Task TestSingleLineIfStatementWithTupleExpressionAsync()
953+
{
954+
var testCode = @"public class TestClass
955+
{
956+
public void TestMethod()
957+
{
958+
if (true) (1, 2).ToString();
959+
}
960+
}
961+
";
962+
963+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
964+
}
949965
}
950966
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1009CSharp7UnitTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,5 +1025,21 @@ public void TestMethod()
10251025
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
10261026
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
10271027
}
1028+
1029+
[Fact]
1030+
[WorkItem(2475, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2475")]
1031+
public async Task TestSingleLineIfStatementWithTupleExpressionAsync()
1032+
{
1033+
var testCode = @"public class TestClass
1034+
{
1035+
public void TestMethod()
1036+
{
1037+
if (true) (1, 2).ToString();
1038+
}
1039+
}
1040+
";
1041+
1042+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
1043+
}
10281044
}
10291045
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/MetadataReferences.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Test.Helpers
55
{
66
using System;
77
using System.Collections.Immutable;
8+
using System.IO;
89
using System.Linq;
910
using System.Reflection;
1011
using Microsoft.CodeAnalysis;
@@ -15,9 +16,9 @@ namespace StyleCop.Analyzers.Test.Helpers
1516
/// </summary>
1617
internal static class MetadataReferences
1718
{
18-
internal static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location).WithAliases(ImmutableArray.Create("global", "corlib"));
19-
internal static readonly MetadataReference SystemReference = MetadataReference.CreateFromFile(typeof(System.Diagnostics.Debug).Assembly.Location).WithAliases(ImmutableArray.Create("global", "system"));
20-
internal static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
19+
internal static readonly MetadataReference CorlibReference = CreateDotNetFrameworkMetadataReference("mscorlib").WithAliases(ImmutableArray.Create("global", "corlib"));
20+
internal static readonly MetadataReference SystemReference = CreateDotNetFrameworkMetadataReference("System").WithAliases(ImmutableArray.Create("global", "system"));
21+
internal static readonly MetadataReference SystemCoreReference = CreateDotNetFrameworkMetadataReference("System.Core");
2122
internal static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
2223
internal static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
2324

@@ -47,5 +48,13 @@ static MetadataReferences()
4748
}
4849
}
4950
}
51+
52+
private static MetadataReference CreateDotNetFrameworkMetadataReference(string name)
53+
{
54+
var referenceAssemblyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Reference Assemblies", "Microsoft", "Framework", ".NETFramework", "v4.6");
55+
var assemblyFilePath = Path.Combine(referenceAssemblyPath, $"{name}.dll");
56+
57+
return MetadataReference.CreateFromFile(assemblyFilePath);
58+
}
5059
}
5160
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
1212
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Lightup;
1314

1415
/// <summary>
1516
/// A closing parenthesis within a C# statement is not spaced correctly.
@@ -90,8 +91,11 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
9091
// Allow a space between an open and a close paren when:
9192
// - they are part of an if statement
9293
// - they are on the same line
93-
// - the open paren is part of a parenthesized expression
94-
precedesStickyCharacter = !(token.Parent.IsKind(SyntaxKind.IfStatement) && nextToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression) && (token.GetLine() == nextToken.GetLine()));
94+
// - the open paren is part of a parenthesized expression or a tuple expression.
95+
precedesStickyCharacter =
96+
!(token.Parent.IsKind(SyntaxKind.IfStatement)
97+
&& (token.GetLine() == nextToken.GetLine())
98+
&& (nextToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression) || nextToken.Parent.IsKind(SyntaxKindEx.TupleExpression)));
9599
break;
96100

97101
case SyntaxKind.CloseParenToken:

0 commit comments

Comments
 (0)