Practice Red-Green-Refactor: Discount Calculation
Introduction to these practices
Welcome to your first lesson in this course dedicated to practicing Test Driven Development (TDD) with a focus on Rust. Test Driven Development is an effective methodology that emphasizes writing tests before the development of actual application code. This approach ensures that your application components function as expected and improves overall code quality. During this lesson, you will learn the fundamentals of TDD and the Red-Green-Refactor cycle, which are essential for creating consistent and maintainable code.
This course prioritizes hands-on practice, where you will receive requirements through predefined tests, one at a time. Your challenge is to write the code necessary to make each test pass, simulating a practical TDD experience. Think of this course guide as your pair programmer, providing test-driven prompts to enhance your skills as you continue.
A note on code organization
Throughout Units 1 and 2, we will keep calculate_discount and its helper functions at the crate root in src/lib.rs instead of moving them into a separate module. This keeps the focus on the Red-Green-Refactor cycle and on evolving the behavior of the code step by step. Later, you can always group related functions into modules, but for now we’ll use a flat structure to reduce distractions while practicing TDD.
Understanding the Red-Green-Refactor Cycle
The Red-Green-Refactor cycle is central to TDD and guides your development process:
- Red: Begin by writing a failing test to define the next functionality to be implemented.
- Green: Develop just enough code to pass the test, concentrating on completing the functionality.
- Refactor: Optimize the code for clarity and efficiency while ensuring the behavior remains unchanged.
1. Correct Discount Application
- Description: The function must correctly apply a percentage-based discount to an original price.
- Test Case:
Rust
