Skip to content

Commit

Permalink
Fix build and reference live ref pack
Browse files Browse the repository at this point in the history
  • Loading branch information
joperezr committed May 20, 2022
1 parent 4458f6b commit 4263164
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ private static async Task<Document> 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
Expand Down Expand Up @@ -260,7 +260,7 @@ static IEnumerable<ISymbol> 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<IArgumentOperation> args, ImmutableDictionary<string, string?> properties, string propertyName, SyntaxGenerator generator, bool useOptionsMemberExpression, Compilation compilation)
static SyntaxNode? GetNode(ImmutableArray<IArgumentOperation> args, ImmutableDictionary<string, string?> properties, string propertyName, SyntaxGenerator generator, bool useOptionsMemberExpression, Compilation compilation, CancellationToken cancellationToken)
{
int? index = TryParseInt32(properties, propertyName);
if (index == null)
Expand All @@ -276,7 +276,7 @@ static IEnumerable<ISymbol> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)

/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/>
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);

/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/>
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)
{
Expand All @@ -59,7 +59,7 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expe
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult[], string)"/>
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,
Expand All @@ -71,12 +71,22 @@ public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] ex

public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier>
{
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- xUnit2008 is about regexes and isn't appropriate in the test project for regexes -->
<!-- SYSLIB0036 is about obsoletion of regex members -->
Expand All @@ -9,6 +9,7 @@
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants> <!-- always define debug, even in release builds -->
<TestRunRequiresLiveRefPack>true</TestRunRequiresLiveRefPack>
</PropertyGroup>
<ItemGroup>
<DefaultReferenceExclusion Include="System.Text.RegularExpressions" />
Expand Down Expand Up @@ -48,13 +49,14 @@
<Compile Include="..\..\src\System\Collections\HashtableExtensions.cs" Link="Production\HashtableExtensions.cs" />
<Compile Include="..\..\gen\UpgradeToRegexGeneratorAnalyzer.cs" Link="Production\UpgradeToRegexGeneratorAnalyzer.cs" />
<Compile Include="..\..\gen\UpgradeToRegexGeneratorCodeFixer.cs" Link="Production\UpgradeToRegexGeneratorCodeFixer.cs" />
<Compile Include="$(CommonTestPath)SourceGenerators\LiveReferencePack.cs" Link="Common\SourceGenerators\LiveReferencePack.cs" />

<!-- Code used as stubs to avoid pulling in further code from System.Text.RegularExpressions -->
<Compile Include="Stubs.cs" />
<Compile Include="UpgradeToRegexGeneratorAnalyzerTests.cs" />

<!-- References required for the Analyzer tests -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_0)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.2-beta1.22255.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]> ConstantPatternTestData()
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 4263164

Please sign in to comment.