Building a Shopping Cart with Many Requirements through TDD
Introduction to the Shopping Cart Module
Welcome to the third unit of this course dedicated to practicing Test Driven Development (TDD) utilizing Go and Testify. We will begin building a new system, focusing on creating a shopping cart module with a variety of features.
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. Unlike previous exercises, where tests were supplied, this time you're in charge!
Remember to utilize the core concepts of the Red-Green-Refactor cycle while working through these exercises. Assistance is available, so don't hesitate to ask for help.
Requirements for `ShoppingCart` Struct
1. Starting with an Empty Cart
- Description: When a new
ShoppingCartis created, it should start without any items, and the total price should be zero. - Details
- Initialize a new shopping cart using the
ShoppingCartstruct. - Ensure the function
GetItemCount()returns0for an empty cart. - Verify the function
GetTotal()returns0for the initial state.
- Initialize a new shopping cart using the
- Examples: A newly created cart should have an item count of
0and a total price of0.
2. Adding a Single Item
- Description: Verify that adding a single item to the cart increases the item count and adjusts the total price to include the price of the added item.
- Details
- Add an item to the cart using the
AddItem(item Item)method. - Confirm that
GetItemCount()reflects the change in the number of items. - Ensure
GetTotal()accurately accounts for the total price of items in the cart.
- Add an item to the cart using the
- Examples: Adding a product
{ ID: "1", Name: "Book", Price: 10 }should result in an item count of1and a total of10.
3. Adding Multiple Items
- Description: Adding multiple distinct items to the shopping cart should update the item count to reflect the total number of unique items, and the total price should equal the sum of the prices of all items added.
- Details
- Utilize the
AddItem()function to insert different items into the shopping cart. - Confirm that
GetItemCount()correctly represents the total number of unique items added to the cart. - Verify that
GetTotal()accurately calculates the sum of the prices for all individual items added.
- Utilize the
- Examples: Adding items
{ ID: "1", Name: "Book", Price: 10 }and{ ID: "2", Name: "Pen", Price: 5 }should lead to an item count of2and a total price of15.
