Skip to content

Fix MatrixDataSource failing with ClassConstructor#4838

Merged
thomhurst merged 1 commit intomainfrom
fix/matrix-class-constructor-4836
Feb 17, 2026
Merged

Fix MatrixDataSource failing with ClassConstructor#4838
thomhurst merged 1 commit intomainfrom
fix/matrix-class-constructor-4836

Conversation

@thomhurst
Copy link
Owner

Summary

  • Fixes IndexOutOfRangeException when a test class uses [ClassConstructor<T>] for DI and has [MatrixDataSource] tests ([BUG] Test Classes With Injected Dependencies Cause Matrix Expansion To Fail #4836)
  • The engine's early instance creation for method data source expansion now checks ClassConstructorHelper before falling back to InstanceFactory
  • The source generator now detects assembly-level [assembly: ClassConstructor<T>] and guards all inline InstanceFactory code paths with the ClassConstructor throw-stub

Test plan

  • TUnit.Engine builds successfully (0 errors, 0 warnings)
  • TUnit.Core.SourceGenerator builds successfully (0 errors, 0 warnings)
  • Source generator snapshot tests pass (455/455, no .received.txt changes)
  • Engine tests pass (273/273, pre-existing F#/VB infra failures only)
  • Manual verification with reproduction: class with [ClassConstructor<T>] + [MatrixDataSource] test method

🤖 Generated with Claude Code

When a test class uses [ClassConstructor<T>] for DI and has [MatrixDataSource]
tests, the matrix expansion fails with IndexOutOfRangeException because the
early instance creation bypasses ClassConstructorHelper and calls InstanceFactory
directly, which tries to index into an empty args array.

Fixes both engine and source generator:
- Engine: CreateInstanceForMethodDataSources now checks ClassConstructor first
- Engine: Consolidated duplicated instance creation in BuildTestsFromMetadataAsync
- Source gen: InstanceFactoryGenerator.HasClassConstructorAttribute now checks
  assembly-level [assembly: ClassConstructor<T>] in addition to class attributes
- Source gen: All inline InstanceFactory paths in TestMetadataGenerator now guard
  against ClassConstructor, emitting the throw-stub instead of constructor code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 17, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

This is a well-structured fix that properly addresses the dual-mode compatibility requirements. The changes correctly implement ClassConstructor detection in both the source generator (with throw-stubs) and the engine (with proper fallback logic), ensuring that [ClassConstructor<T>] works correctly with [MatrixDataSource] in both modes.

@thomhurst thomhurst merged commit b0ec373 into main Feb 17, 2026
13 of 14 checks passed
@thomhurst thomhurst deleted the fix/matrix-class-constructor-4836 branch February 17, 2026 21:15
This was referenced Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments