Skip to content
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
103 changes: 102 additions & 1 deletion TUnit.AOT.Tests/SimpleAotTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using TUnit.Core;
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using System.Diagnostics.CodeAnalysis;
using TUnit.Core.Interfaces;

namespace TUnit.AOT.Tests;

Expand Down Expand Up @@ -43,7 +46,7 @@ public static IEnumerable<string> GetTestData()

[Test]
[ClassDataSource<SimpleDataClass>]
public void ClassDataSourceTest_ShouldWork(string data)
public void ClassDataSourceTest_ShouldWork(SimpleDataClass data)
{
// Test class data sources work in AOT (using source-generated factories)
Console.WriteLine($"Class data source test with: {data}");
Expand Down Expand Up @@ -110,6 +113,104 @@ public class TestObject
}
}

/// <summary>
/// AOT compatibility tests for nested property injection
/// </summary>
public class NestedPropertyInjectionAotTests
{
[CustomDataSource<CustomService>]
public required CustomService? Service { get; set; }

[Test]
public async Task NestedPropertyInjection_ShouldWorkInAot()
{
// Test that nested property injection works correctly in AOT
await Assert.That(Service).IsNotNull();
await Assert.That(Service!.IsInitialized).IsTrue();
await Assert.That(Service.GetMessage()).IsEqualTo("Custom service initialized");

// Test nested service
await Assert.That(Service.NestedService).IsNotNull();
await Assert.That(Service.NestedService!.IsInitialized).IsTrue();
await Assert.That(Service.NestedService.GetData()).IsEqualTo("Nested service initialized");

// Test deeply nested service
await Assert.That(Service.NestedService.DeeplyNestedService).IsNotNull();
await Assert.That(Service.NestedService.DeeplyNestedService!.IsInitialized).IsTrue();
await Assert.That(Service.NestedService.DeeplyNestedService.GetDeepData()).IsEqualTo("Deeply nested service initialized");
}
}

// Custom data source attribute for AOT testing
public class CustomDataSourceAttribute<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T> : AsyncDataSourceGeneratorAttribute<T>
{
protected override async IAsyncEnumerable<Func<Task<T>>> GenerateDataSourcesAsync(DataGeneratorMetadata dataGeneratorMetadata)
{
yield return () =>
{
// Simple creation - framework should handle init properties and nested injection
return Task.FromResult((T)Activator.CreateInstance(typeof(T))!);
};
await Task.CompletedTask;
}
}

public class CustomService : IAsyncInitializer
{
public bool IsInitialized { get; private set; }

// Nested property with its own data source
[CustomDataSource<NestedService>]
public required NestedService? NestedService { get; set; }

public async Task InitializeAsync()
{
await Task.Delay(1);
IsInitialized = true;
}

public string GetMessage()
{
return IsInitialized ? "Custom service initialized" : "Not initialized";
}
}

public class NestedService : IAsyncInitializer
{
public bool IsInitialized { get; private set; }

// Deeply nested property with its own data source
[CustomDataSource<DeeplyNestedService>]
public required DeeplyNestedService? DeeplyNestedService { get; set; }

public async Task InitializeAsync()
{
await Task.Delay(1);
IsInitialized = true;
}

public string GetData()
{
return IsInitialized ? "Nested service initialized" : "Nested not initialized";
}
}

public class DeeplyNestedService : IAsyncInitializer
{
public bool IsInitialized { get; private set; }

public async Task InitializeAsync()
{
await Task.Delay(1);
IsInitialized = true;
}

public string GetDeepData()
{
return IsInitialized ? "Deeply nested service initialized" : "Deeply nested not initialized";
}
}

/// <summary>
/// Hook tests for AOT
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions TUnit.AOT.Tests/TUnit.AOT.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\TestProject.props" />

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>


<!-- Enable AOT publishing for validation -->
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>

<!-- Source generation mode -->
<TUNIT_EXECUTION_MODE>SourceGeneration</TUNIT_EXECUTION_MODE>


<!-- Enable all AOT analysis warnings -->
<PublishTrimmed>true</PublishTrimmed>
<PublishTrimmed>false</PublishTrimmed>
<TrimMode>full</TrimMode>
<EnableAotAnalyzer>true</EnableAotAnalyzer>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsNotAsErrors>IL2104;IL2026</WarningsNotAsErrors>
</PropertyGroup>

Expand All @@ -27,4 +25,6 @@
<ProjectReference Include="..\TUnit.Assertions\TUnit.Assertions.csproj" />
</ItemGroup>

</Project>
<Import Project="..\TestProject.targets" />

</Project>
Loading
Loading