Skip to content

Commit

Permalink
Delete reachable Debug.Fail in regex generator codefix (dotnet#72534)
Browse files Browse the repository at this point in the history
* Delete reachable Debug.Fail in regex generator codefix

* Simplify test

* Add test for local constant

* Address feedback
  • Loading branch information
Youssef1313 committed Jul 20, 2022
1 parent 72da8ac commit bc64ffe
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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})";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,97 @@ public partial class C
await VerifyCS.VerifyCodeFixAsync(test, fixedCode);
}

[Fact]
public async Task InvalidRegexOptions()
{
string test = @"using System.Text.RegularExpressions;
public class A
{
public void Foo()
{
Regex regex = [|new Regex(""pattern"", (RegexOptions)0x0800)|];
}
}
";
string fixedSource = @"using System.Text.RegularExpressions;
public partial class A
{
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_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()
{
string test = @"using System.Text.RegularExpressions;
public class A
{
public void Foo()
{
Regex regex = [|new Regex(""pattern"", (RegexOptions)(-10000))|];
}
}
";
string fixedSource = @"using System.Text.RegularExpressions;
public partial class A
{
public void Foo()
{
Regex regex = MyRegex();
}
[RegexGenerator(""pattern"", (RegexOptions)(-10000))]
private static partial Regex MyRegex();
}
";
await VerifyCS.VerifyCodeFixAsync(test, fixedSource);
}

#region Test helpers

private static string ConstructRegexInvocation(InvocationType invocationType, string pattern, string? options = null)
Expand Down

0 comments on commit bc64ffe

Please sign in to comment.