tamaro_skaljic

test-driven-development-prompt-for-ai-agents

Jan 20th, 2026
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | Fixit | 0 0
  1. ### Test Driven Development (TDD)
  2.  
  3. #### Definitions
  4.  
  5. | Term | Meaning |
  6. | ----------------------- | ----------------------------------------------------------------------- |
  7. | **Red** | Test compiles, runs, and fails on an assertion |
  8. | **Green** | Test passes after implementation changes |
  9. | **Refactor** | Code cleanup while tests remain green |
  10. | **Signature change** | Adding function parameters, struct fields, enum variants, or variant fields |
  11. | **Implementation change** | Modifying function or method bodies |
  12. | **Observable behavior** | Outputs, return values, or side effects visible through the public API |
  13.  
  14. #### Execution Steps
  15.  
  16. 1. Write a test calling the desired public API.
  17. 2. Resolve compilation errors by signature changes only. No implementation changes.
  18. 3. Run the test. Confirm assertion failure.
  19. 4. Write minimal implementation code.
  20. 5. Run the test. Confirm it passes.
  21. 6. Refactor. Confirm tests remain green.
  22.  
  23. #### Phase Validation Criteria
  24.  
  25. ##### Red Phase
  26.  
  27. Run `cargo test`. All conditions must be true:
  28.  
  29. | Condition | Validation |
  30. | ----------------- | --------------------------------------------------- |
  31. | Compiles | No compilation errors |
  32. | Runs | Test executes without panic before assertions |
  33. | Fails | Assertion fails (e.g., `assert_eq!` mismatch) |
  34. | Signatures only | Only function signatures, struct fields, enum variants changed |
  35. | Tests behavior | Assertion checks outputs, not internal state |
  36.  
  37. If any condition is false, fix the test or signatures before proceeding.
  38.  
  39. | Symptom | Diagnosis | Action |
  40. | ------------------- | -------------------------------------- | ----------------------------- |
  41. | Compilation error | Missing signature elements | Add parameters, fields, variants |
  42. | Panic before assert | Missing setup or invalid state | Fix test setup |
  43. | Test passes | Behavior already exists or test is bad | Verify test logic |
  44. | Implementation edited | Function body modified | Revert implementation changes |
  45.  
  46. ##### Green Phase
  47.  
  48. Run `cargo test`. All conditions must be true:
  49.  
  50. | Condition | Validation |
  51. | ---------------------- | ----------------------------------- |
  52. | Test passes | Assertion succeeds |
  53. | Implementation minimal | No code beyond what test requires |
  54. | Test unchanged | Test file has no modifications |
  55.  
  56. Modify implementation to pass the test. Keep the test unchanged.
  57.  
  58. ##### Refactor Phase
  59.  
  60. Run `cargo test` after each change. Condition: all tests pass.
  61.  
  62. #### Example
  63.  
  64. Goal: Watcher emits one event per file during rapid saves (currently emits multiple).
  65.  
  66. ##### Red
  67.  
  68. Step 1: Write test calling desired API.
  69.  
  70. ```rust
  71. #[tokio::test]
  72. async fn test_rapid_saves_produce_single_event() {
  73. // Arrange
  74. let config = WatcherConfiguration { debounce_milliseconds: 100 };
  75. let (mut watcher, mut rx) = create_watcher(config).unwrap();
  76. watch_path(&mut watcher, &temp_dir).unwrap();
  77.  
  78. // Act: trigger multiple filesystem writes
  79. for i in 0..10 {
  80. write_file(&path, format!("v{}", i));
  81. }
  82. sleep(Duration::from_millis(200)).await;
  83.  
  84. // Assert: expect 1 event (will fail when impl added)
  85. let events: Vec<_> = collect_all(&mut rx);
  86. assert_eq!(events.len(), 1);
  87. }
  88. ```
  89.  
  90. Step 2: Resolve compilation errors (signature changes only).
  91.  
  92. ```rust
  93. pub struct WatcherConfiguration {
  94. pub debounce_milliseconds: u64,
  95. }
  96.  
  97. pub fn create_watcher(config: WatcherConfiguration) -> Result<(Watcher, Receiver)> {
  98. todo!() // unchanged body
  99. }
  100. ```
  101.  
  102. Step 3: Run test.
  103.  
  104. Run: `cargo test test_rapid_saves` → panics on `todo!()` before assertion → Fix: replace `todo!()` with minimal non-panicking stub → Run again → fails with `assertion failed: 1 != 10` → Red confirmed.
  105.  
  106. ##### Green
  107.  
  108. Implement deduplication using `config.debounce_milliseconds` in `create_watcher`. Run: `cargo test test_rapid_saves` → passes → Green confirmed.
  109.  
  110. ##### Refactor
  111.  
  112. Clean up. Run: `cargo test` → all pass → Refactor complete.
Advertisement
Add Comment
Please, Sign In to add comment