Skip to content
Prev Previous commit
Next Next commit
Misc test cleanup
  • Loading branch information
steveharter committed Sep 27, 2021
commit a3e6eb1e5515069484da1205b2d8fde15c3ee179
32 changes: 9 additions & 23 deletions src/libraries/Common/tests/SourceGenerators/RoslynTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public static Project CreateTestProject(IEnumerable<Assembly>? references, bool
}

return new AdhocWorkspace()
.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
.AddProject("Test", "test.dll", "C#")
.WithMetadataReferences(refs)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithNullableContextOptions(NullableContextOptions.Enable));
.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
.AddProject("Test", "test.dll", "C#")
.WithMetadataReferences(refs)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithNullableContextOptions(NullableContextOptions.Enable));
}

public static Task CommitChanges(this Project proj, params string[] ignorables)
Expand Down Expand Up @@ -152,25 +152,10 @@ public static TextSpan MakeSpan(string text, int spanNum)
CancellationToken cancellationToken = default)
{
Project proj = CreateTestProject(references, includeBaseReferences);

proj = proj.WithDocuments(sources);

Assert.True(proj.Solution.Workspace.TryApplyChanges(proj.Solution));

Compilation? comp = await proj!.GetCompilationAsync(CancellationToken.None).ConfigureAwait(false);

#if ROSLYN4_0_OR_GREATER
// workaround https://github.com/dotnet/roslyn/pull/55866. We can remove "LangVersion=Preview" when we get a Roslyn build with that change.
CSharpParseOptions options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview);
CSharpGeneratorDriver cgd = CSharpGeneratorDriver.Create(new[] { generator.AsSourceGenerator() }, parseOptions: options);
#else
CSharpGeneratorDriver cgd = CSharpGeneratorDriver.Create(new[] { generator });
#endif

GeneratorDriver gd = cgd.RunGenerators(comp!, cancellationToken);

GeneratorDriverRunResult r = gd.GetRunResult();
return (r.Results[0].Diagnostics, r.Results[0].GeneratedSources);
return RunGenerator(comp!, generator, cancellationToken);
}

/// <summary>
Expand All @@ -179,10 +164,11 @@ public static TextSpan MakeSpan(string text, int spanNum)
public static (ImmutableArray<Diagnostic>, ImmutableArray<GeneratedSourceResult>) RunGenerator(
Compilation compilation,
#if ROSLYN4_0_OR_GREATER
IIncrementalGenerator generator)
IIncrementalGenerator generator,
#else
ISourceGenerator generator)
ISourceGenerator generator,
#endif
CancellationToken cancellationToken = default)
{
#if ROSLYN4_0_OR_GREATER
// workaround https://github.com/dotnet/roslyn/pull/55866. We can remove "LangVersion=Preview" when we get a Roslyn build with that change.
Expand All @@ -192,7 +178,7 @@ public static (ImmutableArray<Diagnostic>, ImmutableArray<GeneratedSourceResult>
CSharpGeneratorDriver cgd = CSharpGeneratorDriver.Create(new[] { generator });
#endif

GeneratorDriver gd = cgd.RunGenerators(compilation);
GeneratorDriver gd = cgd.RunGenerators(compilation, cancellationToken);

GeneratorDriverRunResult r = gd.GetRunResult();
return (r.Results[0].Diagnostics, r.Results[0].GeneratedSources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static Compilation CreateCompilation(
refs.Add(MetadataReference.CreateFromFile(typeof(ILogger).Assembly.Location));
refs.Add(MetadataReference.CreateFromFile(typeof(LoggerMessageAttribute).Assembly.Location));

// Add additional references as needed.
if (additionalReferences != null)
{
foreach (MetadataReference reference in additionalReferences)
Expand All @@ -36,9 +35,7 @@ public static Compilation CreateCompilation(
}
}

CSharpParseOptions options = new CSharpParseOptions(
kind: SourceCodeKind.Regular,
documentationMode: DocumentationMode.Parse);
CSharpParseOptions options = CSharpParseOptions.Default;

#if ROSLYN4_0_OR_GREATER
// workaround https://github.com/dotnet/roslyn/pull/55866. We can remove "LangVersion=Preview" when we get a Roslyn build with that change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -682,7 +681,7 @@ static partial class C
}

[Fact]
public void TestMultipleDefinitions()
public void MultipleTypeDefinitions()
{
// Adding a dependency to an assembly that has internal definitions of public types
// should not result in a collision and break generation.
Expand Down Expand Up @@ -729,7 +728,7 @@ partial class C
RoslynTestUtils.RunGenerator(compilation, generator);

// Make sure compilation was successful.
Assert.Empty(diagnostics.Where(diag => diag.Severity.Equals(DiagnosticSeverity.Error)));
Assert.Empty(diagnostics);
Assert.Equal(1, generatedSources.Length);
Assert.Equal(21, generatedSources[0].SourceText.Lines.Count);
}
Expand Down