Add generic stack creation methods to CdkTestHelper#4
Merged
Conversation
## Summary
- Added generic methods to create custom stack types directly without workarounds
- Enhanced testing flexibility for projects with custom Stack implementations
- Updated documentation with comprehensive examples and usage patterns
## Changes Made
### ✨ New Generic Methods
- **`CreateTestStack<TStack>(string, IStackProps)`**: Creates custom stack types using reflection
- **`CreateTestStackMinimal<TStack>(string, IStackProps)`**: Creates custom stack types with app created internally
### 🔧 Technical Implementation
- Uses `Activator.CreateInstance` with reflection for stack instantiation
- Generic constraint: `where TStack : Stack` ensures type safety
- Returns strongly-typed stack instances for full IntelliSense support
### 🧪 Comprehensive Test Coverage
- **`CdkTestHelper_ShouldCreateGenericStackType`**: Tests full generic method functionality
- **`CdkTestHelper_ShouldCreateGenericStackTypeMinimal`**: Tests minimal generic method
- **`CdkTestHelper_ShouldWorkWithGenericStackAndConstructs`**: End-to-end integration test
- **`TestCustomStack`**: Test helper class demonstrating custom stack implementation
### 📚 Documentation Updates
- **README.md**: Added new section "Testing Custom Stack Types" with real-world examples
- **CLAUDE.md**: Updated testing helpers documentation and added generic stack creation guidance
## Problem Solved
**Before** (workaround required):
```csharp
var (app, _) = CdkTestHelper.CreateTestStack("unused-name", props);
var infraStack = new InfraStack(app, "real-name", props); // Manual instantiation
```
**After** (direct custom stack creation):
```csharp
var infraStack = CdkTestHelper.CreateTestStackMinimal<InfraStack>("test-stack", props);
```
## Benefits
✅ **No Workarounds**: Create custom stack types directly
✅ **Type Safety**: Strongly-typed stack instances with full IntelliSense
✅ **Clean Code**: Eliminates boilerplate while maintaining flexibility
✅ **Real-World Ready**: Works with any custom stack implementation (LightsaberStackProps, etc.)
✅ **Backward Compatible**: All existing code continues to work unchanged
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
Enhanced testing utilities to allow direct creation of custom CDK stack types via generics, accompanied by new tests and documentation updates.
- Added two generic methods (
CreateTestStack<TStack>andCreateTestStackMinimal<TStack>) toCdkTestHelper - Introduced unit and integration tests covering the new generic helpers
- Updated README and CLAUDE documentation to explain and demonstrate generic stack creation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/.../TestingHelpersTests.cs | Added tests for the generic stack helpers and included a TestCustomStack helper class |
| src/.../CdkTestHelper.cs | Implemented CreateTestStack<TStack> and CreateTestStackMinimal<TStack> methods |
| README.md | Documented generic stack creation with examples |
| CLAUDE.md | Updated testing helpers list to include generic methods |
Comments suppressed due to low confidence (1)
test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs:394
- [nitpick] Since
TestCustomStackis a helper for tests, consider suffixing withTestsor placing it in a dedicatedHelpersnamespace/file to clearly distinguish it from production stack implementations.
public class TestCustomStack : Amazon.CDK.Stack
| } | ||
|
|
||
| // Test helper class for generic stack testing | ||
| public class TestCustomStack : Amazon.CDK.Stack |
There was a problem hiding this comment.
The TestCustomStack helper class is declared at the top level without a namespace and shares the test file with your test cases. Consider moving it into its own file and/or adding an appropriate namespace to avoid potential naming collisions and improve clarity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enhanced
CdkTestHelperwith generic methods that enable direct creation of custom stack types, eliminating the need for workarounds when testing custom Stack implementations.Problem Solved
Previously, users with custom stack types (like
InfraStack,LightsaberStackProps, etc.) had to use workarounds:Now they can create custom stacks directly:
Changes Made
✨ New Generic Methods
CreateTestStack<TStack>(string stackName, IStackProps stackProps)Activator.CreateInstancewith reflection(App app, TStack stack)for advanced scenarioswhere TStack : StackCreateTestStackMinimal<TStack>(string stackName, IStackProps stackProps)TStack stackfor simplified usage🔧 Technical Implementation
Activator.CreateInstanceto create stack types(App, string, IStackProps)constructor🧪 Comprehensive Test Coverage
CdkTestHelper_ShouldCreateGenericStackTypeCdkTestHelper_ShouldCreateGenericStackTypeMinimalCdkTestHelper_ShouldWorkWithGenericStackAndConstructsTestCustomStackhelper class📚 Documentation Updates
README.md
CLAUDE.md
Real-World Usage Examples
Custom Infrastructure Stack
LightsaberStackProps Example
Benefits
✅ Eliminates Workarounds: No more unused base stacks
✅ Type Safety: Full IntelliSense and compile-time checking
✅ Clean Testing Code: Reduces boilerplate while maintaining power
✅ Real-World Ready: Works with any custom Stack implementation
✅ Backward Compatible: All existing code continues to work
✅ Well Tested: Comprehensive test coverage including integration scenarios
Test Plan
This enhancement makes the testing helpers significantly more practical for real-world CDK projects with custom stack implementations!
🤖 Generated with Claude Code