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

Use string syntax attribute to highlight tests #72924

Merged
merged 3 commits into from
Apr 11, 2024
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 @@ -24,6 +24,7 @@
<Compile Include="..\..\..\..\Features\DiagnosticsTestUtilities\CodeActions\VisualBasicCodeRefactoringVerifier`1.cs" Link="VisualBasicCodeRefactoringVerifier`1.cs" />
<Compile Include="..\..\..\..\Features\DiagnosticsTestUtilities\Diagnostics\ParenthesesOptionsProvider.cs" Link="ParenthesesOptionsProvider.cs" />
<Compile Include="..\..\..\..\Features\DiagnosticsTestUtilities\NamingStyles\NamingStylesTestOptionSets.cs" Link="NamingStyles\NamingStylesTestOptionSets.cs" />
<Compile Include="..\..\..\..\Features\DiagnosticsTestUtilities\Utilities\StringSyntaxAttribute.cs" Link="Utilities\StringSyntaxAttribute.cs" />
<Compile Include="..\..\..\..\Workspaces\CoreTestUtilities\OptionsCollection.cs" Link="OptionsCollection.cs" />
</ItemGroup>
<ItemGroup Label="Project References">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,57 @@ public class ConvertConcatenationToInterpolatedStringTests
[Fact]
public async Task TestMissingOnSimpleString()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = [||]"string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
public async Task TestMissingOnConcatenatedStrings1()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = [||]"string" + "string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
public async Task TestMissingOnConcatenatedStrings2()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = "string" + [||]"string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
public async Task TestMissingOnConcatenatedStrings3()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = "string" + '.' + [||]"string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
Expand Down Expand Up @@ -305,49 +297,43 @@ void M()
[Fact]
public async Task TestMissingWithMixedStringTypes1()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = 1 + [||]@"string" + 2 + "string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
public async Task TestMissingWithMixedStringTypes2()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = 1 + @"string" + 2 + [||]"string";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
public async Task TestMissingWithMixedStringTypes3()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = 1 + @"string" + 2 + [||]'\n';
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact]
Expand Down Expand Up @@ -391,7 +377,7 @@ void M()
[Fact]
public async Task TestWithOverloadedOperator2()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class D
{
public static int operator +(D d, string s) => 0;
Expand All @@ -406,9 +392,7 @@ void M()
var v = d + [||]"string" + 1;
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/16820")]
Expand Down Expand Up @@ -486,23 +470,21 @@ void M()
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/16820")]
public async Task TestWithMultipleStringConcatenations4()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = "A" + 1 + [||]"B" + @"C";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20943")]
public async Task TestMissingWithDynamic1()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
class C
{
void M()
Expand All @@ -511,15 +493,13 @@ void M()
string c = [||]"d" + a + "e";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20943")]
public async Task TestMissingWithDynamic2()
{
var code = """
await VerifyCS.VerifyRefactoringAsync("""
class C
{
void M()
Expand All @@ -528,9 +508,7 @@ void M()
var x = dynamic.someVal + [||]" $";
}
}
""";

await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23536")]
Expand Down Expand Up @@ -713,54 +691,48 @@ void M()
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/16981")]
public async Task TestMissingWithSelectionOnPartOfToBeInterpolatedStringPrefix()
{
var code = """
// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = [|"string" + 1|] + "string";
}
}
""";

// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")]
[WorkItem("https://github.com/dotnet/roslyn/issues/16981")]
public async Task TestMissingWithSelectionOnPartOfToBeInterpolatedStringSuffix()
{
var code = """
// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = "string" + [|1 + "string"|];
}
}
""";

// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")]
[WorkItem("https://github.com/dotnet/roslyn/issues/16981")]
public async Task TestMissingWithSelectionOnMiddlePartOfToBeInterpolatedString()
{
var code = """
// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync("""
public class C
{
void M()
{
var v = "a" + [|1 + "string"|] + "b";
}
}
""";

// see comment in AbstractConvertConcatenationToInterpolatedStringRefactoringProvider:ComputeRefactoringsAsync
await VerifyCS.VerifyRefactoringAsync(code, code);
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/16981")]
Expand Down Expand Up @@ -1036,8 +1008,9 @@ void M()
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40413")]
public async Task TestConcatenationWithConstMember()
public async Task TestConcatenationWithConstMemberCSharp9()
{
// lang=c#-test
var code = """
class C
{
Expand All @@ -1046,27 +1019,36 @@ class C
const string Message = Hello + " " + [||]World;
}
""";
var fixedCode = """
class C
{
const string Hello = "Hello";
const string World = "World";
const string Message = $"{Hello} {World}";
}
""";

await new VerifyCS.Test
{
LanguageVersion = LanguageVersion.CSharp9,
TestCode = code,
FixedCode = code,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40413")]
public async Task TestConcatenationWithConstMember()
{
await new VerifyCS.Test
{
LanguageVersion = LanguageVersion.Preview,
TestCode = code,
FixedCode = fixedCode,
TestCode = """
class C
{
const string Hello = "Hello";
const string World = "World";
const string Message = Hello + " " + [||]World;
}
""",
FixedCode = """
class C
{
const string Hello = "Hello";
const string World = "World";
const string Message = $"{Hello} {World}";
}
""",
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
Expand All @@ -13,19 +14,27 @@ public static partial class CSharpCodeRefactoringVerifier<TCodeRefactoring>
where TCodeRefactoring : CodeRefactoringProvider, new()
{
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, string)"/>
public static Task VerifyRefactoringAsync(string source, string fixedSource)
public static Task VerifyRefactoringAsync(
[StringSyntax("C#-Test")] string source)
{
return VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, source);
}

/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, string)"/>
public static Task VerifyRefactoringAsync(
[StringSyntax("C#-Test")] string source, [StringSyntax("C#-Test")] string fixedSource)
{
return VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource);
}

/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult, string)"/>
public static Task VerifyRefactoringAsync(string source, DiagnosticResult expected, string fixedSource)
public static Task VerifyRefactoringAsync([StringSyntax("C#-Test")] string source, DiagnosticResult expected, [StringSyntax("C#-Test")] string fixedSource)
{
return VerifyRefactoringAsync(source, [expected], fixedSource);
}

/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult[], string)"/>
public static Task VerifyRefactoringAsync(string source, DiagnosticResult[] expected, string fixedSource)
public static Task VerifyRefactoringAsync([StringSyntax("C#-Test")] string source, DiagnosticResult[] expected, [StringSyntax("C#-Test")] string fixedSource)
{
var test = new Test
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if !NET

namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class StringSyntaxAttribute : Attribute
{
public StringSyntaxAttribute(string syntax) => Syntax = syntax;
public string Syntax { get; }
}
}

#endif
Loading