Welcome to your second lesson in this course dedicated to practicing Test Driven Development (TDD) utilizing Swift and XCTest. In this unit, we will continue adding features to our calculateDiscount function. We have five more requirements for you to implement!
In this course, emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to implement code that makes each test pass, simulating a real-world TDD environment. As the course guide, it's akin to being your pair programmer, providing test-driven prompts to hone your skills as you progress.
As a reminder, the Red-Green-Refactor cycle is central to TDD, guiding your development process:
- Red: Start by writing a failing test to define the next step.
- Green: Implement just enough code to pass the test, focusing on functionality.
- Refactor: Optimize the code for clarity and efficiency without changing its behavior.
- Description: The function should validate that the price is a numeric value and raise an error if a non-numeric input is provided.
- Test Case:
Swift
Swift is a statically typed language, so passing a string to a function expecting a Double would typically cause a compile-time error. To simulate this scenario, the calculateDiscount function must accept inputs of type Any or a protocol like Numeric?, then explicitly check the type at runtime and throw errors if the type is invalid. This pattern simulates validation logic found in loosely typed inputs such as user-entered data or external APIs.
