Skip to content

Commit 561a24d

Browse files
committed
Feat: Using CodeOfChaos.Testing.TUnit
1 parent 31c8a20 commit 561a24d

File tree

12 files changed

+55
-99
lines changed

12 files changed

+55
-99
lines changed

src/CodeOfChaos.Extensions.DependencyInjection.Generators/ServiceRegistrationGenerator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Immutable;
1212
using System.Linq;
1313
using System.Text;
14+
using System.Text.RegularExpressions;
1415

1516
namespace CodeOfChaos.Extensions.DependencyInjection.Generators;
1617
// ---------------------------------------------------------------------------------------------------------------------
@@ -31,6 +32,8 @@ public class ServiceRegistrationGenerator : IIncrementalGenerator {
3132
PooledInjectableServiceAttributeMetadataName
3233
];
3334

35+
private static Regex RegexSanitizeAssemblyName { get; } = new(@"((?im)[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?)|(\.dll)", RegexOptions.Compiled);
36+
3437
// -----------------------------------------------------------------------------------------------------------------
3538
// Methods
3639
// -----------------------------------------------------------------------------------------------------------------
@@ -60,9 +63,11 @@ private static void GenerateSources(SourceProductionContext context, (Compilatio
6063
.ToArray()
6164
;
6265

63-
string assemblyNameSanitized = assemblyName
64-
.Replace(".dll", "")
65-
.Replace("-", "_");
66+
// This fixes an issue with the testing environment, where we add a guid to the assembly name, to deter conflicts
67+
string assemblyNameSanitized = RegexSanitizeAssemblyName.Replace(assemblyName, string.Empty)
68+
.Replace("-", "_")
69+
.TrimEnd('-', '_');
70+
6671

6772
context.AddSource(
6873
PooledServicesFileName,

src/CodeOfChaos.Extensions.DependencyInjection/CodeOfChaos.Extensions.DependencyInjection.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
32-
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.1" />
31+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
32+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.2" />
3333
<PackageReference Include="System.Buffers" Version="4.6.0" />
3434
</ItemGroup>
3535

src/CodeOfChaos.Extensions.EntityFrameworkCore/CodeOfChaos.Extensions.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
25+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

src/CodeOfChaos.Extensions/StringExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
#if DEBUG
5+
using System.Diagnostics;
6+
#endif
47
using System.Diagnostics.CodeAnalysis;
58
using System.Security.Cryptography;
69
using System.Text;
@@ -28,7 +31,7 @@ public static Guid ToGuid(this string input) {
2831
Debug.Fail("Failed to parse Guid");
2932
return Guid.Empty;
3033
#else
31-
// Because "testing" of the input is done during debug, we can just "blindly" parse during release.
34+
// Because "testing" of the value is handled by analyzer, we can just "blindly" parse during release.
3235
return Guid.Parse(input);
3336
#endif
3437
}

tests/Tests.CodeOfChaos.Extensions.AspNetCore/Tests.CodeOfChaos.Extensions.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1515
<PackageReference Include="Moq" Version="4.20.72"/>
16-
<PackageReference Include="TUnit" Version="0.10.33" />
16+
<PackageReference Include="TUnit" Version="0.12.17" />
1717
<PackageReference Include="Bogus" Version="35.6.1"/>
1818
</ItemGroup>
1919

tests/Tests.CodeOfChaos.Extensions.DependencyInjection.Generators/IncrementalGeneratorTest.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

tests/Tests.CodeOfChaos.Extensions.DependencyInjection.Generators/ServiceRegistrationGeneratorTests.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
// ---------------------------------------------------------------------------------------------------------------------
44
using CodeOfChaos.Extensions.DependencyInjection;
55
using CodeOfChaos.Extensions.DependencyInjection.Generators;
6+
using CodeOfChaos.Testing.TUnit;
67
using JetBrains.Annotations;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.ObjectPool;
1011
using System;
11-
using System.Linq;
1212
using System.Threading.Tasks;
1313

1414
namespace Tests.CodeOfChaos.Extensions.DependencyInjection.Generators;
1515
// ---------------------------------------------------------------------------------------------------------------------
1616
// Code
1717
// ---------------------------------------------------------------------------------------------------------------------
18-
public class ServiceRegistrationGeneratorTests : IncrementalGeneratorTest<ServiceRegistrationGenerator> {
19-
protected override System.Reflection.Assembly[] ReferenceAssemblies { get; } = [
18+
public class ServiceRegistrationGeneratorTests {
19+
private static System.Reflection.Assembly[] ReferenceAssemblies { get; } = [
2020
typeof(object).Assembly,
2121
typeof(FactoryCreatedServiceAttribute<,>).Assembly,
2222
typeof(IFactoryService<>).Assembly,
@@ -35,47 +35,48 @@ public class ServiceRegistrationGeneratorTests : IncrementalGeneratorTest<Servic
3535
[Arguments(InjectableServiceInput, InjectableServiceOutput)]
3636
public async Task TestText(string inputText, string expectedOutput) {
3737
// Arrange
38-
GeneratorDriverRunResult runResult = await RunGeneratorAsync(inputText);
38+
RoslynGeneratorRunner runner = await new RoslynCompilationRunner()
39+
.AddReferences(ReferenceAssemblies)
40+
.AddDocument("Test.cs", inputText)
41+
.GetGeneratorRunnerAsync();
3942

4043
// Act
41-
GeneratedSourceResult? generatedSource = runResult.Results
42-
.SelectMany(result => result.GeneratedSources)
43-
.SingleOrDefault(result => result.HintName.EndsWith("ServiceRegistration.g.cs"));
44+
GeneratorDriverRunResult runResult = runner.AddGenerator<ServiceRegistrationGenerator>();
4445

4546
// Assert
46-
await Assert.That(generatedSource?.SourceText).IsNotNull();
47-
await Assert
48-
.That(generatedSource?.SourceText.ToString())
49-
.IsEqualTo(expectedOutput).IgnoringWhitespace().WithTrimming();
50-
47+
await Assert.That(runResult).HasSourceTextEqualTo(
48+
"ServiceRegistration.g.cs",
49+
expectedOutput,
50+
ignoreWhiteSpace:true,
51+
withTrimming:true
52+
);
5153
}
5254

5355
[Test]
5456
[Arguments(PooledInjectableServiceInput, PooledInjectableServiceOutput, PooledInjectableServiceOutputPooledServices)]
5557
public async Task TestPooledInjectableServiceOutput(string inputText, string expectedOutput, string expectedOutputPooledServices) {
5658
// Arrange
57-
GeneratorDriverRunResult runResult = await RunGeneratorAsync(inputText);
59+
RoslynGeneratorRunner runner = await new RoslynCompilationRunner()
60+
.AddReferences(ReferenceAssemblies)
61+
.AddDocument("Test.cs", inputText)
62+
.GetGeneratorRunnerAsync();
5863

5964
// Act
60-
GeneratedSourceResult? serviceRegistrationResult = runResult.Results
61-
.SelectMany(result => result.GeneratedSources)
62-
.SingleOrDefault(result => result.HintName.EndsWith("ServiceRegistration.g.cs"));
63-
64-
GeneratedSourceResult? pooledServicesResult = runResult.Results
65-
.SelectMany(result => result.GeneratedSources)
66-
.SingleOrDefault(result => result.HintName.EndsWith("AutoPooledServices.g.cs"));
65+
GeneratorDriverRunResult runResult = runner.AddGenerator<ServiceRegistrationGenerator>();
6766

6867
// Assert
69-
await Assert.That(serviceRegistrationResult?.SourceText).IsNotNull();
70-
await Assert
71-
.That(serviceRegistrationResult?.SourceText.ToString())
72-
.IsEqualTo(expectedOutput).IgnoringWhitespace().WithTrimming();
73-
74-
await Assert.That(pooledServicesResult?.SourceText).IsNotNull();
75-
await Assert
76-
.That(pooledServicesResult?.SourceText.ToString())
77-
.IsEqualTo(expectedOutputPooledServices).IgnoringWhitespace().WithTrimming();
78-
68+
await Assert.That(runResult).HasSourceTextEqualTo(
69+
"ServiceRegistration.g.cs",
70+
expectedOutput,
71+
ignoreWhiteSpace:true,
72+
withTrimming:true
73+
);
74+
await Assert.That(runResult).HasSourceTextEqualTo(
75+
"AutoPooledServices.g.cs",
76+
expectedOutputPooledServices,
77+
ignoreWhiteSpace:true,
78+
withTrimming:true
79+
);
7980
}
8081

8182
#region FactoryCreatedService Test

tests/Tests.CodeOfChaos.Extensions.DependencyInjection.Generators/Tests.CodeOfChaos.Extensions.DependencyInjection.Generators.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="CodeOfChaos.Testing.TUnit" Version="0.6.1" />
1011
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1112
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
1213
<PrivateAssets>all</PrivateAssets>
@@ -17,8 +18,8 @@
1718
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.2"/>
1819
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
1920
<PackageReference Include="Moq" Version="4.20.72" />
20-
<PackageReference Include="System.Formats.Asn1" Version="9.0.1" />
21-
<PackageReference Include="TUnit" Version="0.10.33" />
21+
<PackageReference Include="System.Formats.Asn1" Version="9.0.2" />
22+
<PackageReference Include="TUnit" Version="0.12.17" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

tests/Tests.CodeOfChaos.Extensions.DependencyInjection/Tests.CodeOfChaos.Extensions.DependencyInjection.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
16-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
1717
<PackageReference Include="Moq" Version="4.20.72" />
18-
<PackageReference Include="TUnit" Version="0.10.33" />
18+
<PackageReference Include="TUnit" Version="0.12.17" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

tests/Tests.CodeOfChaos.Extensions.EntityFrameworkCore/Tests.CodeOfChaos.Extensions.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
1515
<PackageReference Include="Moq" Version="4.20.72"/>
16-
<PackageReference Include="TUnit" Version="0.10.33" />
16+
<PackageReference Include="TUnit" Version="0.12.17" />
1717
<PackageReference Include="Bogus" Version="35.6.1"/>
1818
</ItemGroup>
1919

0 commit comments

Comments
 (0)