From 206673376e836a6b6d548fc54b74f8cf9197268c Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sun, 7 Apr 2024 13:30:17 -0700 Subject: [PATCH 1/3] Use string syntax attribute to highlight tests --- ...tConcatenationToInterpolatedStringTests.cs | 121 +++++++----------- .../CSharpCodeRefactoringVerifier`1.cs | 15 ++- .../Utilities/StringSyntaxAttribute.cs | 17 +++ 3 files changed, 72 insertions(+), 81 deletions(-) create mode 100644 src/Features/DiagnosticsTestUtilities/Utilities/StringSyntaxAttribute.cs diff --git a/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs b/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs index f691d31a81708..ce584ea75dd6a 100644 --- a/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs +++ b/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs @@ -21,7 +21,7 @@ public class ConvertConcatenationToInterpolatedStringTests [Fact] public async Task TestMissingOnSimpleString() { - var code = """ + await VerifyCS.VerifyRefactoringAsync(""" public class C { void M() @@ -29,15 +29,13 @@ 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() @@ -45,15 +43,13 @@ 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() @@ -61,15 +57,13 @@ 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() @@ -77,9 +71,7 @@ void M() var v = "string" + '.' + [||]"string"; } } - """; - - await VerifyCS.VerifyRefactoringAsync(code, code); + """); } [Fact] @@ -305,7 +297,7 @@ void M() [Fact] public async Task TestMissingWithMixedStringTypes1() { - var code = """ + await VerifyCS.VerifyRefactoringAsync(""" public class C { void M() @@ -313,15 +305,13 @@ 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() @@ -329,15 +319,13 @@ 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() @@ -345,9 +333,7 @@ void M() var v = 1 + @"string" + 2 + [||]'\n'; } } - """; - - await VerifyCS.VerifyRefactoringAsync(code, code); + """); } [Fact] @@ -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; @@ -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")] @@ -486,7 +470,7 @@ 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() @@ -494,15 +478,13 @@ 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() @@ -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() @@ -528,9 +508,7 @@ void M() var x = dynamic.someVal + [||]" $"; } } - """; - - await VerifyCS.VerifyRefactoringAsync(code, code); + """); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23536")] @@ -713,7 +691,8 @@ 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() @@ -721,17 +700,15 @@ 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() @@ -739,17 +716,15 @@ 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() @@ -757,10 +732,7 @@ 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")] @@ -1035,38 +1007,31 @@ void M() """); } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40413")] - public async Task TestConcatenationWithConstMember() + [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/40413")] + [InlineData(LanguageVersion.CSharp9)] + [InlineData(LanguageVersion.Preview)] + public async Task TestConcatenationWithConstMember( + LanguageVersion languageVersion) { - var code = """ + await new VerifyCS.Test + { + LanguageVersion = languageVersion, + TestCode = """ class C { const string Hello = "Hello"; const string World = "World"; const string Message = Hello + " " + [||]World; } - """; - var fixedCode = """ + """, + 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(); - - await new VerifyCS.Test - { - LanguageVersion = LanguageVersion.Preview, - TestCode = code, - FixedCode = fixedCode, + """, }.RunAsync(); } diff --git a/src/Features/DiagnosticsTestUtilities/CodeActions/CSharpCodeRefactoringVerifier`1.cs b/src/Features/DiagnosticsTestUtilities/CodeActions/CSharpCodeRefactoringVerifier`1.cs index 549a0b00746e9..0e27129adc0bc 100644 --- a/src/Features/DiagnosticsTestUtilities/CodeActions/CSharpCodeRefactoringVerifier`1.cs +++ b/src/Features/DiagnosticsTestUtilities/CodeActions/CSharpCodeRefactoringVerifier`1.cs @@ -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; @@ -13,19 +14,27 @@ public static partial class CSharpCodeRefactoringVerifier where TCodeRefactoring : CodeRefactoringProvider, new() { /// - public static Task VerifyRefactoringAsync(string source, string fixedSource) + public static Task VerifyRefactoringAsync( + [StringSyntax("C#-Test")] string source) + { + return VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, source); + } + + /// + public static Task VerifyRefactoringAsync( + [StringSyntax("C#-Test")] string source, [StringSyntax("C#-Test")] string fixedSource) { return VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); } /// - 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); } /// - 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 { diff --git a/src/Features/DiagnosticsTestUtilities/Utilities/StringSyntaxAttribute.cs b/src/Features/DiagnosticsTestUtilities/Utilities/StringSyntaxAttribute.cs new file mode 100644 index 0000000000000..4c54aa7019884 --- /dev/null +++ b/src/Features/DiagnosticsTestUtilities/Utilities/StringSyntaxAttribute.cs @@ -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 From ac08d3c891de41a6700bebf083498eebe8fa76cc Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sun, 7 Apr 2024 13:33:20 -0700 Subject: [PATCH 2/3] link file --- .../Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CodeStyle/Core/Tests/UnitTestUtilities/Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj b/src/CodeStyle/Core/Tests/UnitTestUtilities/Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj index 1bcec3fcd1e39..69807d0f912fa 100644 --- a/src/CodeStyle/Core/Tests/UnitTestUtilities/Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj +++ b/src/CodeStyle/Core/Tests/UnitTestUtilities/Microsoft.CodeAnalysis.CodeStyle.UnitTestUtilities.csproj @@ -24,6 +24,7 @@ + From ac85d51307ac90046cba2392e7491905f7782c09 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sun, 7 Apr 2024 14:27:48 -0700 Subject: [PATCH 3/3] fix --- ...tConcatenationToInterpolatedStringTests.cs | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs b/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs index ce584ea75dd6a..6a59132c0fd36 100644 --- a/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs +++ b/src/Features/CSharpTest/ConvertToInterpolatedString/ConvertConcatenationToInterpolatedStringTests.cs @@ -1007,31 +1007,48 @@ void M() """); } - [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/40413")] - [InlineData(LanguageVersion.CSharp9)] - [InlineData(LanguageVersion.Preview)] - public async Task TestConcatenationWithConstMember( - LanguageVersion languageVersion) + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40413")] + public async Task TestConcatenationWithConstMemberCSharp9() { - await new VerifyCS.Test - { - LanguageVersion = languageVersion, - TestCode = """ + // lang=c#-test + var code = """ 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 = """ + 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}"; - } - """, + class C + { + const string Hello = "Hello"; + const string World = "World"; + const string Message = $"{Hello} {World}"; + } + """, }.RunAsync(); }