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:
2. Zero Discount Handling
- Description: The function should return the original price when the discount percentage is 0.
- Test Case:
3. Decimal Discount Precision
- Description: The function must accurately handle decimal percentages in discount calculations and round the result to two decimal places.
- Test Case:
4. Negative Price Handling
- Description: The function should not accept negative prices and must return an error if encountered.
- Test Case:
5. Discount Percentage Greater Than 100%
- Description: The function should not accept discount percentages greater than 100% and must return an error in such cases.
- Test Case:
Summary and Preparation for Practice Exercises
Looking ahead to the practice exercises, you will get the opportunity to ensure all tests pass while practicing the Red-Green-Refactor cycle. Your solutions may vary from the provided ones, and that’s entirely normal. Each practice session will start from a foundational solution, allowing you to compare your approach with the guided solution and develop your features to ensure test success.
Engage in the Red-Green-Refactor cycle throughout these exercises. Each test offered in this guide serves as the "Red" phase, demarcating the next step to accomplish. Your task is to transition into the "Green" phase by making the tests pass and then proceed into the "Refactor" phase to enhance the code's clarity and maintainability while ensuring all tests continue to succeed.
