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 ShoppingCart is created, it should start without any items, and the total price should be zero.
  • Details
    • Initialize a new shopping cart using the ShoppingCart struct.
    • Ensure the function GetItemCount() returns 0 for an empty cart.
    • Verify the function GetTotal() returns 0 for the initial state.
  • Examples: A newly created cart should have an item count of 0 and a total price of 0.

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.
  • Examples: Adding a product { ID: "1", Name: "Book", Price: 10 } should result in an item count of 1 and a total of 10.

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.
  • Examples: Adding items { ID: "1", Name: "Book", Price: 10 } and { ID: "2", Name: "Pen", Price: 5 } should lead to an item count of 2 and a total price of 15.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal