Welcome to your fourth unit of this course dedicated to practicing Test Driven Development (TDD). We're going to continue building our ShoppingCart system by adding even more features using Go and Testify.
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 write tests AND implement code that makes each test pass, simulating a real-world TDD environment.
Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm still here to help! Just ask.
- Description: Trying to remove an item that is not present in the cart should return an error, indicating that the item was not found.
- Details:
- Implement the removal through a
RemoveItem(id int) errorfunction. - Ensure the function returns an error with an appropriate message if the item is not found in the cart.
- Implement the removal through a
- Examples: Attempting to remove an item with
Id: 999should return an error with the message"Item not found".
- Description: Applying a percentage discount should adjust the total price of the items in the cart accordingly.
- Details:
- Use the
ApplyDiscount(percentage float64) float64method to apply a percentage discount to the total. - Ensure
GetTotal()returns the adjusted price after applying the discount.
- Use the
- Examples: Applying a 10% discount to a total price of
100should result in a new total of90.
- Description: Automatically apply a 10% discount when the total price of items in the cart exceeds $150.
- Details:
- If the total exceeds 150, the 10% discount should be automatically applied.
- The
GetTotal()method should return the discounted total when the discount is applicable.
- Examples: Adding a
"Book"with a price of200should result in a total of180after applying the bulk discount.
- Description: The cart should have the functionality to remove all items, resetting both the item count and the total price to zero.
- Details:
- Implement a
Clear()method to remove all items from the cart. - Ensure
GetItemCount()returns0after clearing. - Verify
GetTotal()is0after clearing.
- Implement a
- Examples: If there are multiple items in the cart, calling
Clearshould leave the count as0and the total as0.
- Description: When the quantity of an item in the cart is changed, both the item count and total price should accurately reflect the new quantity.
- Details:
- Allow item quantities to be updated using an
UpdateQuantity(id int, quantity int) errorfunction. - Ensure
GetItemCount()returns the correct total item count after the quantity is updated. - Ensure
GetTotal()returns the correct total price after the quantity is updated.
- Allow item quantities to be updated using an
- Examples: Updating the quantity of a
"Book"with a price of10from2to3should result in a count of3and a total of30.
In this unit, you explored the design of test cases for a ShoppingCart struct, focusing on essential features like handling an empty cart, adding items, managing item quantities, and removing items. Now it's your turn to ensure that the described functionality is implemented by writing comprehensive test cases and ensuring that the tests pass with the least amount of code needed.
As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle. Be sure to practice writing tests first, and do not write implementation code unless the test asks for it.
Red! Green! Refactor!
