Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options for tests to change the C# language version. #15

Merged
merged 1 commit into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tests/CSGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ public class CSGeneratorTest : CSharpSourceGeneratorTest<IDisposibleGenerator.ID
{
public List<(string, string)> GlobalOptions { get; } = new();

public LanguageVersion LanguageVersion { get; set; }

protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators)
=> CSharpGeneratorDriver.Create(
sourceGenerators,
project.AnalyzerOptions.AdditionalFiles,
(CSharpParseOptions)project.ParseOptions!,
(CSharpParseOptions)this.CreateParseOptions(),
new OptionsProvider(project.AnalyzerOptions.AnalyzerConfigOptionsProvider, this.GlobalOptions));

protected override ParseOptions CreateParseOptions()
=> ((CSharpParseOptions)base.CreateParseOptions()).WithLanguageVersion(this.LanguageVersion);
}
13 changes: 9 additions & 4 deletions tests/IDisposableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,15 @@ public async Task TestGeneratingNoInput()
await RunTest<CSGeneratorTest>(string.Empty, string.Empty).ConfigureAwait(false);
}).ConfigureAwait(false);

private static async Task RunTest<TestType>(string generatedSource, string testSource)
private static async Task RunTest<TestType>(
string generatedSource,
string testSource,
LanguageVersion languageVersion = LanguageVersion.CSharp9)
where TestType : SourceGeneratorTest<XUnitVerifier>, IGeneratorTestBase, new()
{
var test = new TestType
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
TestState =
{
Sources =
Expand All @@ -334,9 +337,11 @@ private static async Task RunTest<TestType>(string generatedSource, string testS
},
};

if (!string.IsNullOrEmpty(testSource))
if (!string.IsNullOrEmpty(testSource) && test is CSGeneratorTest tst)
{
// do not duplicate the code to Elskom.GitInformation here.
tst.LanguageVersion = languageVersion;

// do not duplicate the code to the attributes here.
test.TestState.Sources.Add(
await File.ReadAllTextAsync(
"../../../../content/cs/any/CallOnDisposeAttribute.cs").ConfigureAwait(false));
Expand Down