
test-driven-development-tdd
by takoeight0821
SKILL.md
name: Test-Driven Development (TDD) description: This skill should be used when the user asks to "use TDD", "write tests first", "test-driven", "add a test for", "fix bug with TDD", "implement using TDD", "red-green-refactor", or mentions developing features test-first. Provides guidance for TDD workflow in Ziku with golden tests. version: 1.1.0
Test-Driven Development for Ziku
Guide Claude through Test-Driven Development (TDD) workflows in the Ziku programming language project. TDD emphasizes writing tests before implementation, following the red-green-refactor cycle to ensure code correctness and maintainability.
Core Principles
Test-First Development: Write failing tests before implementing features.
Red-Green-Refactor Cycle:
- Red: Write a failing test that specifies desired behavior
- Green: Write minimal code to make the test pass
- Refactor: Improve code quality while keeping tests passing
Fast Feedback: Run tests frequently (after each small change) to catch issues early.
Regression Protection: Every bug fix includes a test that would have caught the bug.
Ziku Test Infrastructure
Golden Tests
Ziku uses golden tests with automatic baseline generation:
Test Categories:
- parser (
tests/golden/parser/): Verify syntax parsing produces correct AST - infer (
tests/golden/infer/): Verify type inference produces correct types - ir-eval (
tests/golden/ir-eval/): Verify end-to-end execution (parse → elaborate → translate → evaluate)
Test Workflow:
- Create
.zikutest file in appropriate category directory - Add test name to list in
tests/GoldenTest.lean(e.g.,parserTests,inferTests,irEvalTests) - Run
lake testto auto-generate.goldenbaseline file - Subsequent runs compare actual output against
.goldenbaseline
Key Files:
tests/GoldenTest.lean: Test definitions and test name liststests/golden/{category}/{name}.ziku: Test input filestests/golden/{category}/{name}.golden: Expected output (auto-generated)
Red-Green-Refactor Workflow
Red Phase: Write Failing Test
Objective: Create a test that fails for the right reason.
Steps:
- Identify the smallest next behavior to implement
- Create test file:
tests/golden/{category}/{test_name}.ziku - Add
{test_name}to appropriate list intests/GoldenTest.lean - Run
lake testto verify the test fails - Confirm failure message indicates missing implementation (not a test setup error)
Using Scripts:
# Create test file with template
.claude/skills/tdd/scripts/create_test_file.sh parser lambda_curry lambda
# Add test to GoldenTest.lean
.claude/skills/tdd/scripts/add_golden_test.sh parser lambda_curry
# Verify test fails (red)
.claude/skills/tdd/scripts/check_test_status.sh
Red Phase Success Criteria:
- Test fails with expected error (parse error, type error, wrong value)
- Failure is due to missing implementation, not test bugs
- Test is minimal and focused on one behavior
Green Phase: Make Test Pass
Objective: Write the simplest code to make the test pass.
Steps:
- Implement minimal code in relevant module:
- Parser tests → Edit
Ziku/Parser.lean,Ziku/Lexer.lean, orZiku/Surface/Syntax.lean - Inference tests → Edit
Ziku/Infer.leanorZiku/Type.lean - IR evaluation tests → Edit
Ziku/Translate.lean,Ziku/IR/Eval.lean, orZiku/IR/Syntax.lean
- Parser tests → Edit
- Run
lake testto verify the test now passes - Confirm all existing tests still pass (no regressions)
Implementation Guidelines:
- Write only enough code to pass the current test
- Resist adding extra features or premature optimization
- If implementation path is unclear, use triangulation (write multiple tests to force the right abstraction)
Green Phase Success Criteria:
- New test passes (golden file created if first run)
- All existing tests still pass
- Code compiles without errors
Refactor Phase: Improve Code Quality
Objective: Enhance code structure while maintaining green tests.
Steps:
- Identify code smells: duplication, unclear names, complex logic
- Refactor incrementally (small changes)
- Run
lake testafter each refactoring step - Ensure all tests remain green throughout
- Stop when code is clear, simple, and well-structured
Refactoring Targets:
- Extract duplicated code into helper functions
- Rename variables/functions for clarity
- Simplify conditional logic
- Improve error messages
- Add documentation for non-obvious behavior
Refactor Phase Success Criteria:
- Code is more readable and maintainable
- No duplication or code smells
- All tests still passing
- No new functionality added
Test-First Feature Development
Multi-Phase Implementation
For new language features, implement in phases with tests at each level:
Phase 1: Parser Tests
- Write parser tests for new syntax
- Implement parsing
- Verify AST structure is correct
Phase 2: Type Inference Tests
- Write inference tests for type checking
- Implement type inference rules
- Verify types are inferred correctly
Phase 3: IR Evaluation Tests
- Write end-to-end evaluation tests
- Implement translation to IR and evaluation
- Verify execution produces correct results
Example - Adding New Operator:
# Phase 1: Parser
create_test_file.sh parser power_operator
# Add "1 ** 2" to test file
add_golden_test.sh parser power_operator
lake test # Red
# Implement parsing
lake test # Green
# Phase 2: Inference
create_test_file.sh infer power_operator
# Add "1 ** 2" with type checking
add_golden_test.sh infer power_operator
lake test # Red
# Implement type inference
lake test # Green
# Phase 3: Evaluation
create_test_file.sh ir-eval power_operator
# Add "2 ** 8" expecting 256
add_golden_test.sh ir-eval power_operator
lake test # Red
# Implement evaluation
lake test # Green
# Refactor across all phases
Regression Testing for Bugs
Bug Fix Workflow
Always follow this sequence:
- Reproduce: Create test that demonstrates the bug
- Red: Verify test fails with the bug
- Fix: Implement minimal fix
- Green: Verify test passes
- Verify: Check all existing tests still pass
- Refactor: Clean up if needed
Example:
# Bug report: Shadowed variables return wrong value
# 1. Reproduce
cat > tests/golden/ir-eval/shadow_variable_bug.ziku <<'EOF'
let x = 1 in
let x = 2 in
x
EOF
# 2. Add test and verify failure (red)
add_golden_test.sh ir-eval shadow_variable_bug
lake test # Should output wrong value (1 instead of 2)
# 3. Fix implementation
# Debug and fix in Translate.lean or IR/Eval.lean
# 4. Verify fix (green)
lake test # Should now output correct value (2)
# 5. Verify no regressions
lake test # All tests should pass
# 6. Refactor if needed
Test Naming for Bugs: Use descriptive names that capture the bug:
shadow_variable_bug- variable shadowing issuenested_label_scope- label scoping problemcodata_hash_reference- hash (#) binding issue
Utility Scripts
Scripts in .claude/skills/tdd/scripts/ automate common TDD operations:
create_test_file.sh
Create new test file with optional template.
./create_test_file.sh <category> <test_name> [template]
# Examples:
create_test_file.sh parser lambda_simple lambda
create_test_file.sh infer type_annotation empty
create_test_file.sh ir-eval factorial_letrec empty
# Templates: empty, lambda, let, match, codata, label
add_golden_test.sh
Add test name to appropriate list in GoldenTest.lean.
./add_golden_test.sh <category> <test_name>
# Examples:
add_golden_test.sh parser lambda_nested
add_golden_test.sh infer let_polymorphic
add_golden_test.sh ir-eval codata_lazy
check_test_status.sh
Check test status (red/green state) with clear visual feedback.
./check_test_status.sh [category] [timeout_seconds]
# Examples:
check_test_status.sh # All tests, 30s timeout
check_test_status.sh parser # Just parser tests
check_test_status.sh infer # Just inference tests
check_test_status.sh all 60 # All tests, 60s timeout
# Output:
# 🔴 RED: Tests failing (exit code 1)
# 🟢 GREEN: All tests passing (exit code 0)
# ⏱️ TIMEOUT: Tests exceeded timeout (exit code 2)
Test Organization Best Practices
Test Granularity
Atomic Tests (Preferred):
- Test one behavior per test
- Small, focused input
- Clear failure messages
- Fast execution
Example: Instead of lambda_all_cases.ziku, create:
lambda_simple.ziku- Basic lambdalambda_multi_param.ziku- Multiple parameterslambda_nested.ziku- Nested lambdaslambda_higher_order.ziku- Lambda returning lambda
Test Coverage
Essential Test Types:
- Happy path: Normal, expected usage
- Edge cases: Boundary values (zero, empty, maximum)
- Error cases: Invalid input, type mismatches, unbound variables
For Each Feature, Test:
- Basic usage (happy path)
- With edge case inputs
- Error conditions
- Integration with other features
Test Naming
Use descriptive names that clearly indicate what is being tested:
Good Names:
lambda_closure- Tests closure behaviorlet_shadowing- Tests variable shadowingcodata_hash_binding- Tests hash (#) in codatalabel_nested_goto- Tests nested label/goto
Bad Names:
test1,test2,test3- Non-descriptivecomplex_case- Unclear what complexitylambda- Too generic
Workflow Integration
Daily TDD Practice
Start of Day: Ensure tests are green
lake test # Fix any failures before new work
During Development: Red-Green-Refactor for each feature
# For each small feature:
# 1. Red: Write failing test
# 2. Green: Make it pass
# 3. Refactor: Improve code
# 4. Repeat
Before Commit: Verify all tests pass
lake test # Must be green
git commit -m "feat: add feature_name"
Running Tests
Full Test Suite:
lake test # Run all tests (parser + infer + ir-eval)
Quick Feedback:
# Use check_test_status.sh for quick pass/fail
.claude/skills/tdd/scripts/check_test_status.sh
# Or build without running full test suite
lake build
Timeout Handling:
- Tests have 30-second timeout in
check_test_status.sh - For longer-running tests, use
lake testdirectly - Consider adding
timeout 10 lake testfor faster feedback
Common TDD Mistakes to Avoid
Mistake 1: Skipping Red Phase
Problem: Test passes immediately, not testing new behavior.
Solution: Always verify test fails before implementing. The failure confirms the test actually tests new code.
Mistake 2: Tests Too Large
Problem: Test exercises multiple behaviors, unclear what failed.
Solution: Split into atomic tests, each focused on one behavior.
Mistake 3: Ignoring Failing Tests
Problem: Accumulates test debt, suite becomes unreliable.
Solution: Fix or update failing tests immediately. Never commit with failing tests.
Mistake 4: No Refactoring
Problem: Code becomes messy, hard to maintain.
Solution: Refactor after each green phase. Keep code clean continuously.
Mistake 5: Testing Implementation Details
Problem: Tests break during refactoring even though behavior unchanged.
Solution: Test observable behavior (outputs, types, values), not internal structure.
Additional Resources
Reference Files
For detailed patterns and advanced techniques:
references/tdd-patterns.md- Comprehensive TDD patterns, triangulation, test-driven debugging, property-based testing ideas, and Ziku-specific testing strategies
Example Workflows
Working examples of complete TDD workflows:
examples/workflow-new-feature.md- Adding a newforloop construct using TDD across all three phases (parser, infer, ir-eval)examples/workflow-bug-fix.md- Fixing a nested label/goto bug with regression test creation
Scripts
Automation utilities for common TDD operations:
scripts/create_test_file.sh- Create test files with templatesscripts/add_golden_test.sh- Add tests to GoldenTest.leanscripts/check_test_status.sh- Quick test status check
Summary
TDD in Ziku follows these core practices:
- Write Tests First: Before implementation, write the test
- Red-Green-Refactor: Follow the cycle strictly
- Fast Feedback: Run tests frequently (after each small change)
- Atomic Tests: One test per behavior
- Golden Tests: Leverage automatic baseline generation
- Three Phases: Parser → Inference → Evaluation for language features
- Regression Protection: Every bug gets a test
- Keep Suite Green: Never commit failing tests
- Continuous Refactoring: Clean code while tests pass
- Use Scripts: Automate repetitive operations
TDD provides confidence in code correctness, prevents regressions, and improves design through test-first thinking. The red-green-refactor cycle creates a sustainable development rhythm with continuous validation.
Quick Reference
TDD Cycle
Red (failing test) → Green (minimal implementation) → Refactor (improve code) → Repeat
Test Creation Workflow
# 1. Create test file
create_test_file.sh <category> <name> [template]
# 2. Edit test file with test case
# Edit tests/golden/<category>/<name>.ziku
# 3. Add to test list
add_golden_test.sh <category> <name>
# 4. Run tests (should fail - red)
lake test
# 5. Implement feature
# Edit relevant source files
# 6. Run tests (should pass - green)
lake test
# 7. Refactor
# Improve code while keeping tests green
# 8. Final verification
lake test
Bug Fix Workflow
# 1. Create reproduction test
create_test_file.sh <category> <bug_name>
# Edit test file to demonstrate bug
# 2. Verify failure (red)
lake test
# 3. Fix bug
# Edit implementation
# 4. Verify fix (green)
lake test
# 5. Commit with test
git commit -m "fix: <bug_name>"
Use this skill to maintain high code quality, prevent regressions, and develop features systematically through test-first development in Ziku.
Score
Total Score
Based on repository quality metrics
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
3ヶ月以内に更新がある
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
Reviews
Reviews coming soon