Skip to content

Commit

Permalink
Test when multiple Disposable source files are generated in C# 10. (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: AraHaan <[email protected]>
  • Loading branch information
AraHaan authored Apr 15, 2022
1 parent f1f6031 commit aebc620
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/IDisposableGenerator/DisposableCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ namespace {workItem.Namespace};
// inject the created sources into the users compilation.
sourceBuilder.ToString().ToSourceFile($@"Disposables{(
workItemCollection.GetWorkItems().Count > 1
? $"{workItemCollection.GetWorkItems().IndexOf(workItem)}" :
? $".{workItemCollection.GetWorkItems().IndexOf(workItem)}" :
string.Empty)}.g.cs", ref context);
}
}
Expand Down
95 changes: 95 additions & 0 deletions tests/IDisposableGeneratorTests.CSharp10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,99 @@ internal partial class TestDisposable
public string? test { get; set; } = ""stuff here."";
}
", LanguageVersion.CSharp10).ConfigureAwait(false);

[Fact]
public async Task TestGeneratingDisposableWithMultipleNamespacesCSharp10()
{
var testSources = new List<string>
{
@"namespace MyApp.AnotherNamespace;
[GenerateDispose(false)]
internal partial class AnotherDisposable
{
}
",
};
var generatedSources = new Dictionary<string, string>
{
{"Disposables.0.g.cs", @"// <autogenerated/>
namespace MyApp;
internal partial class TestDisposable : IDisposable
{
private bool isDisposed;
/// <summary>
/// Cleans up the resources used by <see cref=""TestDisposable""/>.
/// </summary>
public void Dispose() => this.Dispose(true);
private void Dispose(bool disposing)
{
if (!this.isDisposed && disposing)
{
this.test = null;
this.isDisposed = true;
}
}
}
"},
{"Disposables.1.g.cs", @"// <autogenerated/>
namespace MyApp.AnotherNamespace;
internal partial class AnotherDisposable : IDisposable
{
private bool isDisposed;
/// <summary>
/// Cleans up the resources used by <see cref=""AnotherDisposable""/>.
/// </summary>
public void Dispose() => this.Dispose(true);
private void Dispose(bool disposing)
{
if (!this.isDisposed && disposing)
{
this.isDisposed = true;
}
}
}
"}
};
await RunTest<CSGeneratorTest>(@"// <autogenerated/>
namespace MyApp;
internal partial class TestDisposable : IDisposable
{
private bool isDisposed;
/// <summary>
/// Cleans up the resources used by <see cref=""TestDisposable""/>.
/// </summary>
public void Dispose() => this.Dispose(true);
private void Dispose(bool disposing)
{
if (!this.isDisposed && disposing)
{
this.test = null;
this.isDisposed = true;
}
}
}
", @"global using System;
global using System.ComponentModel.DataAnnotations;
global using IDisposableGenerator;
namespace MyApp;
[GenerateDispose(false)]
internal partial class TestDisposable
{
[NullOnDispose]
public string? test { get; set; } = ""stuff here."";
}
", LanguageVersion.CSharp10, testSources, generatedSources).ConfigureAwait(false);
}
}
26 changes: 23 additions & 3 deletions tests/IDisposableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public async Task TestGeneratingNoInput()
private static async Task RunTest<TestType>(
string generatedSource,
string testSource,
LanguageVersion? languageVersion = LanguageVersion.CSharp9)
LanguageVersion? languageVersion = LanguageVersion.CSharp9,
List<string>? testSources = null,
Dictionary<string, string>? generatedSources = null)
where TestType : SourceGeneratorTest<XUnitVerifier>, IGeneratorTestBase, new()
{
var test = new TestType
Expand All @@ -35,8 +37,26 @@ private static async Task RunTest<TestType>(
var generatedAttributeSource = Properties.Resources.AttributeCodeCSharp!;
test.TestState.GeneratedSources.Add(
(typeof(IDisposableGenerator), "GeneratedAttributes.g.cs", generatedAttributeSource));
test.TestState.GeneratedSources.Add(
(typeof(IDisposableGenerator), "Disposables.g.cs", generatedSource));
if (generatedSources is not null
&& languageVersion == LanguageVersion.CSharp10)
{
foreach (var source in testSources!)
{
test.TestState.Sources.Add(source);
}

foreach (var (key, value) in generatedSources)
{
test.TestState.GeneratedSources.Add(
(typeof(IDisposableGenerator), key, value));
}
}
else
{
test.TestState.GeneratedSources.Add(
(typeof(IDisposableGenerator), "Disposables.g.cs", generatedSource));
}

break;
}
case false when test is VBGeneratorTest:
Expand Down

0 comments on commit aebc620

Please sign in to comment.