Skip to content

Add dual generic parameter methods to CdkTestHelper for exact type matching#5

Merged
ncipollina merged 1 commit into
mainfrom
feature/dual-generic-stack-props
Jul 4, 2025
Merged

Add dual generic parameter methods to CdkTestHelper for exact type matching#5
ncipollina merged 1 commit into
mainfrom
feature/dual-generic-stack-props

Conversation

@ncipollina

Copy link
Copy Markdown
Contributor

Summary

  • Add dual generic parameter methods CreateTestStack<TStack, TProps> and CreateTestStackMinimal<TStack, TProps> to CdkTestHelper
  • Solve Activator.CreateInstance type matching issues when custom stack constructors expect specific props interfaces (not just IStackProps)
  • Add comprehensive unit tests covering all dual generic method scenarios
  • Update README.md with detailed "Testing with Custom Stack Props Types" section

Technical Details

The new dual generic methods enable exact type matching for custom stack constructors that expect specific props interfaces. This solves issues where Activator.CreateInstance fails when trying to pass IStackProps to constructors expecting concrete interface types like ILightsaberStackProps or IInfraStackProps.

New Methods Added:

  • CreateTestStack<TStack, TProps>(string stackName, TProps stackProps)
  • CreateTestStackMinimal<TStack, TProps>(string stackName, TProps stackProps)

When to Use:

  • Custom stack constructor expects specific props interface (e.g., ILightsaberStackProps)
  • Need exact type matching for Activator.CreateInstance
  • Want full IntelliSense support for custom props in tests

Test Coverage

  • Added comprehensive unit tests for all dual generic methods
  • Created test helper classes: TestAdvancedStack, ITestCustomStackProps, TestCustomStackProps
  • Integration tests showing end-to-end workflows with constructs
  • Maintains backward compatibility with existing single generic methods

Documentation Updates

  • Added "Testing with Custom Stack Props Types" section to README.md
  • Updated CLAUDE.md with new method signatures and usage examples
  • Clear guidance on when to use dual generics vs single generics

🤖 Generated with Claude Code

…tching

- Add CreateTestStack<TStack, TProps> and CreateTestStackMinimal<TStack, TProps> methods
- Solve Activator.CreateInstance type matching issues when custom stack constructors expect specific props interfaces
- Add comprehensive unit tests for dual generic methods with TestAdvancedStack and TestCustomStackProps
- Update README.md with "Testing with Custom Stack Props Types" section explaining when to use dual generics
- Maintain backward compatibility with existing single generic methods

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ncipollina
ncipollina requested a review from Copilot July 4, 2025 00:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds dual-generic overloads to the CdkTestHelper for exact props type matching, along with comprehensive tests and documentation updates.

  • Introduces CreateTestStack<TStack, TProps> and CreateTestStackMinimal<TStack, TProps> for custom props interfaces
  • Adds unit tests and helper interfaces/classes to cover dual-generic scenarios
  • Updates README with a "Testing with Custom Stack Props Types" example section

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs Added and refactored tests to exercise the new dual-generic methods
src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs Implemented dual-generic CreateTestStack and CreateTestStackMinimal
README.md Documented dual-generic usage with a full example
Comments suppressed due to low confidence (2)

src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs:85

  • Consider adding error handling or a descriptive exception when reflection fails to instantiate TStack, since Activator.CreateInstance will throw a generic exception if no matching constructor is found.
        var stack = (TStack)Activator.CreateInstance(typeof(TStack), app, stackName, stackProps)!;

test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs:437

  • The test sets custom tags in TestCustomStackProps but doesn't assert that those tags are applied to the stack; consider adding assertions against stack.Tags to verify propagation.
                { "TestMethod", "DualGeneric" },

Comment on lines +70 to +79
/// <summary>
/// Creates a test CDK app and custom stack type with the specified custom props type.
/// This method uses reflection to instantiate the custom stack type with custom props.
/// Note: You must create the Template.FromStack(stack) after adding constructs to the stack.
/// </summary>
/// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam>
/// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam>
/// <param name="stackName">The name of the test stack</param>
/// <param name="stackProps">Custom stack props of type TProps</param>
/// <returns>Tuple containing the app and the custom stack instance</returns>

Copilot AI Jul 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The XML doc comments for the dual-generic overloads largely duplicate those of the single-generic methods; consider refactoring or sharing common documentation to reduce maintenance overhead.

Suggested change
/// <summary>
/// Creates a test CDK app and custom stack type with the specified custom props type.
/// This method uses reflection to instantiate the custom stack type with custom props.
/// Note: You must create the Template.FromStack(stack) after adding constructs to the stack.
/// </summary>
/// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam>
/// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam>
/// <param name="stackName">The name of the test stack</param>
/// <param name="stackProps">Custom stack props of type TProps</param>
/// <returns>Tuple containing the app and the custom stack instance</returns>
/// <inheritdoc cref="CreateTestStack(string, IStackProps)" />
/// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam>
/// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam>

Copilot uses AI. Check for mistakes.
Comment on lines +319 to 320
var customStack = stack;
customStack.CustomProperty.Should().Be("CustomValue");

Copilot AI Jul 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The alias customStack is redundant since stack is already the correct type; you can assert directly on stack to simplify the test.

Suggested change
var customStack = stack;
customStack.CustomProperty.Should().Be("CustomValue");
stack.CustomProperty.Should().Be("CustomValue");

Copilot uses AI. Check for mistakes.
@ncipollina
ncipollina merged commit 36252b4 into main Jul 4, 2025
1 check passed
@ncipollina
ncipollina deleted the feature/dual-generic-stack-props branch July 4, 2025 00:32
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.

2 participants