Project 2: Unit Testing Your grep
You will take the code from Project 1 and add
unittestblocks to your functions, creating a self-testing codebase.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | D |
| Alternative Languages | N/A |
| Difficulty | Level 1: Beginner |
| Time Estimate | A few hours. |
| Knowledge Area | Software Engineering / Testing |
| Tooling | dub |
| Prerequisites | Project 1. |
What You Will Build
You will take the code from Project 1 and add unittest blocks to your functions, creating a self-testing codebase.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Writing a
unittestblock → maps to learning the simple syntax and how to useassert - Testing pure functions → maps to refactoring your code to make it more testable
- Running tests with
dub→ maps to simply runningdub test
Key Concepts
- Unit Testing: “Programming in D” - Chapter 8.
Real-World Outcome
d
unittest {
string testContent = "line1\nError on line2\nline3";
auto results = findMatches(testContent, "Error");
assert(results.length == 1);
assert(results[0].lineText == "Error on line2");
}
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
LEARN_D_PROGRAMMING_LANGUAGE.md - “Programming in D” by Ali Çehreli