From 32f8a2c203ca9afcc225b7ac98ccc13baa6aa01d Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 20 Jul 2022 19:27:04 +0200 Subject: [PATCH 1/4] Delete reachable Debug.Fail in regex generator codefix --- .../gen/UpgradeToRegexGeneratorCodeFixer.cs | 1 - .../UpgradeToRegexGeneratorAnalyzerTests.cs | 100 ++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs index 32ba674fccb5d7..f9c888a11c1a19 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs @@ -289,7 +289,6 @@ static string Literal(RegexOptions options) { // The options were formatted as an int, which means the runtime couldn't // produce a textual representation. So just output casting the value as an int. - Debug.Fail("This shouldn't happen, as we should only get to the point of emitting code if RegexOptions was valid."); return $"(RegexOptions)({(int)options})"; } diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs index 4e8b8c171fcd27..aaa059e85d53f0 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -815,6 +815,106 @@ public partial class C await VerifyCS.VerifyCodeFixAsync(test, fixedCode); } + [Fact] + public async Task InvalidRegexOptions() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public partial class B + { + public class C + { + public partial class D + { + public void Foo() + { + Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|]; + } + } + } + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public partial class B + { + public partial class C + { + public partial class D + { + public void Foo() + { + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)2048)] + private static partial Regex MyRegex(); + } + } + } +} +"; + + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); + } + + [Fact] + public async Task InvalidRegexOptions_Negative() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public partial class B + { + public class C + { + public partial class D + { + public void Foo() + { + Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|]; + } + } + } + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public partial class B + { + public partial class C + { + public partial class D + { + public void Foo() + { + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)(-10000))] + private static partial Regex MyRegex(); + } + } + } +} +"; + await new VerifyCS.Test(null, usePreviewLanguageVersion: true, 1) + { + TestCode = test, + FixedCode = fixedSource, + CodeActionValidationMode = CodeActionValidationMode.None, + }.RunAsync(); + } + #region Test helpers private static string ConstructRegexInvocation(InvocationType invocationType, string pattern, string? options = null) From 8f88f0d2c83a57de213309b00fadfd32db11fca9 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Wed, 20 Jul 2022 20:45:57 +0200 Subject: [PATCH 2/4] Simplify test --- .../UpgradeToRegexGeneratorAnalyzerTests.cs | 64 ++++--------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs index aaa059e85d53f0..beaddae451fa67 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -822,18 +822,9 @@ public async Task InvalidRegexOptions() public class A { - public partial class B + public void Foo() { - public class C - { - public partial class D - { - public void Foo() - { - Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|]; - } - } - } + Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|]; } } "; @@ -841,22 +832,13 @@ public void Foo() public partial class A { - public partial class B + public void Foo() { - public partial class C - { - public partial class D - { - public void Foo() - { - Regex regex = MyRegex(); - } - - [RegexGenerator(""pattern"", (RegexOptions)2048)] - private static partial Regex MyRegex(); - } - } + Regex regex = MyRegex(); } + + [RegexGenerator(""pattern"", (RegexOptions)2048)] + private static partial Regex MyRegex(); } "; @@ -870,18 +852,9 @@ public async Task InvalidRegexOptions_Negative() public class A { - public partial class B + public void Foo() { - public class C - { - public partial class D - { - public void Foo() - { - Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|]; - } - } - } + Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|]; } } "; @@ -889,22 +862,13 @@ public void Foo() public partial class A { - public partial class B + public void Foo() { - public partial class C - { - public partial class D - { - public void Foo() - { - Regex regex = MyRegex(); - } - - [RegexGenerator(""pattern"", (RegexOptions)(-10000))] - private static partial Regex MyRegex(); - } - } + Regex regex = MyRegex(); } + + [RegexGenerator(""pattern"", (RegexOptions)(-10000))] + private static partial Regex MyRegex(); } "; await new VerifyCS.Test(null, usePreviewLanguageVersion: true, 1) From 09e4ee817ba443559e6516ff81bfbddc665e94c0 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 20 Jul 2022 20:49:36 +0200 Subject: [PATCH 3/4] Add test for local constant --- .../UpgradeToRegexGeneratorAnalyzerTests.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs index beaddae451fa67..9ed18bdfa15c0d 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -845,6 +845,38 @@ public void Foo() await VerifyCS.VerifyCodeFixAsync(test, fixedSource); } + [Fact] + public async Task InvalidRegexOptions_LocalConstant() + { + string test = @"using System.Text.RegularExpressions; + +public class A +{ + public void Foo() + { + const RegexOptions MyOptions = (RegexOptions)0x0800; + Regex regex = [|new Regex(""pattern"", MyOptions)|]; + } +} +"; + string fixedSource = @"using System.Text.RegularExpressions; + +public partial class A +{ + public void Foo() + { + const RegexOptions MyOptions = (RegexOptions)0x0800; + Regex regex = MyRegex(); + } + + [RegexGenerator(""pattern"", (RegexOptions)2048)] + private static partial Regex MyRegex(); +} +"; + + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); + } + [Fact] public async Task InvalidRegexOptions_Negative() { From f56efce63c57270af6154cd011a599ef45dd888f Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 20 Jul 2022 21:11:56 +0200 Subject: [PATCH 4/4] Address feedback --- .../UpgradeToRegexGeneratorAnalyzerTests.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs index 9ed18bdfa15c0d..1a278912c62cc6 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -903,12 +903,7 @@ public void Foo() private static partial Regex MyRegex(); } "; - await new VerifyCS.Test(null, usePreviewLanguageVersion: true, 1) - { - TestCode = test, - FixedCode = fixedSource, - CodeActionValidationMode = CodeActionValidationMode.None, - }.RunAsync(); + await VerifyCS.VerifyCodeFixAsync(test, fixedSource); } #region Test helpers