diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs index cdeefeae3d441..a5c4df1d4b389 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToRegexGeneratorCodeFixer.cs @@ -193,13 +193,13 @@ private static async Task ConvertToSourceGenerator(Document document, // Try to get the pattern and RegexOptions values out from the diagnostic's property bag. if (operation is IObjectCreationOperation objectCreationOperation) // When using the Regex constructors { - patternValue = GetNode((objectCreationOperation).Arguments, properties, UpgradeToRegexGeneratorAnalyzer.PatternIndexName, generator, useOptionsMemberExpression: false, compilation); - regexOptionsValue = GetNode((objectCreationOperation).Arguments, properties, UpgradeToRegexGeneratorAnalyzer.RegexOptionsIndexName, generator, useOptionsMemberExpression: true, compilation); + patternValue = GetNode((objectCreationOperation).Arguments, properties, UpgradeToRegexGeneratorAnalyzer.PatternIndexName, generator, useOptionsMemberExpression: false, compilation, cancellationToken); + regexOptionsValue = GetNode((objectCreationOperation).Arguments, properties, UpgradeToRegexGeneratorAnalyzer.RegexOptionsIndexName, generator, useOptionsMemberExpression: true, compilation, cancellationToken); } else if (operation is IInvocationOperation invocation) // When using the Regex static methods. { - patternValue = GetNode(invocation.Arguments, properties, UpgradeToRegexGeneratorAnalyzer.PatternIndexName, generator, useOptionsMemberExpression: false, compilation); - regexOptionsValue = GetNode(invocation.Arguments, properties, UpgradeToRegexGeneratorAnalyzer.RegexOptionsIndexName, generator, useOptionsMemberExpression: true, compilation); + patternValue = GetNode(invocation.Arguments, properties, UpgradeToRegexGeneratorAnalyzer.PatternIndexName, generator, useOptionsMemberExpression: false, compilation, cancellationToken); + regexOptionsValue = GetNode(invocation.Arguments, properties, UpgradeToRegexGeneratorAnalyzer.RegexOptionsIndexName, generator, useOptionsMemberExpression: true, compilation, cancellationToken); } // Generate the new static partial method @@ -260,7 +260,7 @@ static IEnumerable GetAllMembers(ITypeSymbol? symbol) } // Helper method that looks int the properties bag for the index of the passed in propertyname, and then returns that index from the args parameter. - static SyntaxNode? GetNode(ImmutableArray args, ImmutableDictionary properties, string propertyName, SyntaxGenerator generator, bool useOptionsMemberExpression, Compilation compilation) + static SyntaxNode? GetNode(ImmutableArray args, ImmutableDictionary properties, string propertyName, SyntaxGenerator generator, bool useOptionsMemberExpression, Compilation compilation, CancellationToken cancellationToken) { int? index = TryParseInt32(properties, propertyName); if (index == null) @@ -276,7 +276,7 @@ static IEnumerable GetAllMembers(ITypeSymbol? symbol) { RegexOptions options = (RegexOptions)(int)args[index.Value].Value.ConstantValue.Value; string optionsLiteral = Literal(options); - return SyntaxFactory.ParseExpression(optionsLiteral).SyntaxTree.GetRoot(); + return SyntaxFactory.ParseExpression(optionsLiteral).SyntaxTree.GetRoot(cancellationToken); } } diff --git a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/CSharpCodeFixVerifier`2.cs b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/CSharpCodeFixVerifier`2.cs index 0cbda942d8cc0..83ce1218adb7c 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/CSharpCodeFixVerifier`2.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/CSharpCodeFixVerifier`2.cs @@ -34,10 +34,10 @@ public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) /// public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) - => await VerifyAnalyzerAsync(source, ReferenceAssemblies.Net.Net70, usePreviewLanguageVersion: true, expected); + => await VerifyAnalyzerAsync(source, null, usePreviewLanguageVersion: true, expected); /// - public static async Task VerifyAnalyzerAsync(string source, ReferenceAssemblies references, bool usePreviewLanguageVersion, params DiagnosticResult[] expected) + public static async Task VerifyAnalyzerAsync(string source, ReferenceAssemblies? references, bool usePreviewLanguageVersion, params DiagnosticResult[] expected) { Test test = new Test(references, usePreviewLanguageVersion, numberOfIterations: 1) { @@ -59,7 +59,7 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expe /// public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource, int numberOfIterations = 1) { - Test test = new Test(ReferenceAssemblies.Net.Net70, usePreviewLanguageVersion: true, numberOfIterations) + Test test = new Test(null, usePreviewLanguageVersion: true, numberOfIterations) { TestCode = source, FixedCode = fixedSource, @@ -71,12 +71,22 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] ex public class Test : CSharpCodeFixTest { - public Test(ReferenceAssemblies references, bool usePreviewLanguageVersion, int numberOfIterations) + public Test(ReferenceAssemblies? references, bool usePreviewLanguageVersion, int numberOfIterations) { // Code Fixer generates partial methods that will need to use the source generator to be filled. this.CompilerDiagnostics = CompilerDiagnostics.None; - ReferenceAssemblies = references; + if (references != null) + { + ReferenceAssemblies = references; + } + else + { + // Clear out the default reference assemblies. We explicitly add references from the live ref pack, + // so we don't want the Roslyn test infrastructure to resolve/add any default reference assemblies + ReferenceAssemblies = new ReferenceAssemblies(string.Empty); + TestState.AdditionalReferences.AddRange(SourceGenerators.Tests.LiveReferencePack.GetMetadataReferences()); + } NumberOfFixAllIterations = numberOfIterations; diff --git a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj index 6129eab9b8e1a..77f1ad9eb8ade 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj +++ b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -9,6 +9,7 @@ true true $(DefineConstants);DEBUG + true @@ -48,6 +49,7 @@ + @@ -55,6 +57,6 @@ - + diff --git a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/UpgradeToRegexGeneratorAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/UpgradeToRegexGeneratorAnalyzerTests.cs index 757b70d229bad..dc123275c84f5 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/UpgradeToRegexGeneratorAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/UpgradeToRegexGeneratorAnalyzerTests.cs @@ -119,7 +119,7 @@ public static void Main(string[] args) } }"; - await VerifyCS.VerifyAnalyzerAsync(test, ReferenceAssemblies.Net.Net70, usePreviewLanguageVersion: false); + await VerifyCS.VerifyAnalyzerAsync(test, null, usePreviewLanguageVersion: false); } public static IEnumerable ConstantPatternTestData() @@ -503,8 +503,7 @@ public static void Main(string[] args) foreach (bool includeRegexOptions in new[] { true, false }) { - // Can't test EnumerateMatches yet since the reference assemblies used by the test infrastructure doesn't have that API yet. - foreach (string methodName in new[] { "Count", /* "EnumerateMatches" ,*/ "IsMatch", "Match", "Matches", "Split" }) + foreach (string methodName in new[] { "Count", "EnumerateMatches" , "IsMatch", "Match", "Matches", "Split" }) { if (includeRegexOptions) {