Repository: dotnet/runtime
File: src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/RegexGeneratorHelper.netcoreapp.cs (C#)

Code before:
            var code = new StringBuilder();
            code.AppendLine("using System.Text.RegularExpressions;");
            code.AppendLine("/// <summary>Container for generated regex method.</summary>");
            code.AppendLine("public partial class C {");

            // Build up the code for all of the regexes
            int count = 0;
            foreach (var regex in regexes)
            {
                Assert.True(regex.options is not null || regex.matchTimeout is null);
                code.AppendLine("    /// <summary>RegexGenerator method</summary>");
                code.Append($"    [GeneratedRegex({SymbolDisplay.FormatLiteral(regex.pattern, quote: true)}");
                if (regex.options is not null)
                {
                    code.Append($", {string.Join(" | ", regex.options.ToString().Split(',').Select(o => $"RegexOptions.{o.Trim()}"))}");
                    if (regex.matchTimeout is not null)
                    {
                        code.Append(string.Create(CultureInfo.InvariantCulture, $", {(int)regex.matchTimeout.Value.TotalMilliseconds}"));
                    }
                }
                if (regex.culture is not null)
                {
                    code.Append($", {SymbolDisplay.FormatLiteral(regex.culture.Name, quote: true)}");
                }

                code.AppendLine($")] public static partial Regex Get{count} {{ get; }}");

                count++;
            }

            code.AppendLine("}");

            // Use a cached compilation to save a little time.  Rather than creating an entirely new workspace
            // for each test, just create a single compilation, cache it, and then replace its syntax tree
            // on each test.
            if (s_compilation is not Compilation comp)
            {
                // Create the project containing the source.
                var proj = new AdhocWorkspace()
                    .AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
                    .AddProject("Test", "test.dll", "C#")
                    .WithMetadataReferences(References)
                    .WithCompilationOptions(
                        new CSharpCompilationOptions(
                            OutputKind.DynamicallyLinkedLibrary,
                            checkOverflow: true,
                            warningLevel: 9999, // docs recommend using "9999" to catch all warnings now and in the future
                            specificDiagnosticOptions: ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("SYSLIB1044", ReportDiagnostic.Hidden)) // regex with limited support
                            .WithNullableContextOptions(NullableContextOptions.Enable))
                            .WithParseOptions(s_previewParseOptions)
                    .AddDocument("RegexGenerator.g.cs", SourceText.From("// Empty", Encoding.UTF8)).Project;

Review comment (refactor):
@copilot, these changes to RegexGeneratorHelper.netcoreapp.cs and RegexGeneratorOutputTests.cs should be reverted. We still need to test that method support works, and these are the tests for that.

Code after:
            var code = new StringBuilder();
            code.AppendLine("using System.Text.RegularExpressions;");
            code.AppendLine("/// <summary>Container for generated regex method.</summary>");
            code.AppendLine("public partial class C {");

            // Build up the code for all of the regexes
            int count = 0;
            foreach (var regex in regexes)
            {
                Assert.True(regex.options is not null || regex.matchTimeout is null);
                code.AppendLine("    /// <summary>RegexGenerator method</summary>");
                code.Append($"    [GeneratedRegex({SymbolDisplay.FormatLiteral(regex.pattern, quote: true)}");
                if (regex.options is not null)
                {
                    code.Append($", {string.Join(" | ", regex.options.ToString().Split(',').Select(o => $"RegexOptions.{o.Trim()}"))}");
                    if (regex.matchTimeout is not null)
                    {
                        code.Append(string.Create(CultureInfo.InvariantCulture, $", {(int)regex.matchTimeout.Value.TotalMilliseconds}"));
                    }
                }
                if (regex.culture is not null)
                {
                    code.Append($", {SymbolDisplay.FormatLiteral(regex.culture.Name, quote: true)}");
                }

                bool useProp = count % 2 == 0; // validate both methods and properties by alternating between them
                code.AppendLine($")] public static partial Regex Get{count}{(useProp ? " { get; }" : "();")}");

                count++;
            }

            code.AppendLine("}");

            // Use a cached compilation to save a little time.  Rather than creating an entirely new workspace
            // for each test, just create a single compilation, cache it, and then replace its syntax tree
            // on each test.
            if (s_compilation is not Compilation comp)
            {
                // Create the project containing the source.
                var proj = new AdhocWorkspace()
                    .AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
                    .AddProject("Test", "test.dll", "C#")
                    .WithMetadataReferences(References)
                    .WithCompilationOptions(
                        new CSharpCompilationOptions(
                            OutputKind.DynamicallyLinkedLibrary,
                            checkOverflow: true,
                            warningLevel: 9999, // docs recommend using "9999" to catch all warnings now and in the future
                            specificDiagnosticOptions: ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("SYSLIB1044", ReportDiagnostic.Hidden)) // regex with limited support
                            .WithNullableContextOptions(NullableContextOptions.Enable))
                            .WithParseOptions(s_previewParseOptions)