Item removal
This commit is contained in:
@@ -32,7 +32,6 @@ Discounts are simple calculations done in-line, again for simplicity
|
|||||||
## Future Work
|
## Future Work
|
||||||
|
|
||||||
Some basic stuff is missing.
|
Some basic stuff is missing.
|
||||||
* Items cannot be individually removed from the checkout
|
|
||||||
* Checkout items are not grouped when listed
|
* Checkout items are not grouped when listed
|
||||||
* Subtotals are not listed anywhere
|
* Subtotals are not listed anywhere
|
||||||
* Discount totals are not assoiated with the items they apply to (or available to the user at all, they're just applied to the total silently)
|
* Discount totals are not assoiated with the items they apply to (or available to the user at all, they're just applied to the total silently)
|
||||||
|
|||||||
@@ -16,6 +16,14 @@ class Checkout
|
|||||||
@items.push item
|
@items.push item
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove(item)
|
||||||
|
if index = @items.index(item)
|
||||||
|
@items.delete_at(index)
|
||||||
|
end
|
||||||
|
|
||||||
|
@items
|
||||||
|
end
|
||||||
|
|
||||||
def items
|
def items
|
||||||
@items
|
@items
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -108,6 +108,41 @@ class TestCheckout < Minitest::Test
|
|||||||
assert_equal (30.00 + 10.50), @checkout.total
|
assert_equal (30.00 + 10.50), @checkout.total
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Item removal
|
||||||
|
|
||||||
|
def test_that_removing_an_item_not_in_the_checkout_removes_nothing
|
||||||
|
@checkout.add(:PICO2)
|
||||||
|
@checkout.remove(:PI5)
|
||||||
|
|
||||||
|
assert_equal [:PICO2], @checkout.items
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_that_items_are_removed_one_by_one
|
||||||
|
4.times do
|
||||||
|
@checkout.add(:ZERO2W)
|
||||||
|
end
|
||||||
|
|
||||||
|
3.times do
|
||||||
|
@checkout.add(:PICO2)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2, :PICO2].sort, @checkout.items.sort
|
||||||
|
|
||||||
|
@checkout.remove(:ZERO2W)
|
||||||
|
|
||||||
|
assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2, :PICO2].sort, @checkout.items.sort
|
||||||
|
|
||||||
|
@checkout.remove(:PICO2)
|
||||||
|
|
||||||
|
assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2].sort, @checkout.items.sort
|
||||||
|
|
||||||
|
2.times do
|
||||||
|
@checkout.remove(:PICO2)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal [:ZERO2W, :ZERO2W, :ZERO2W].sort, @checkout.items.sort
|
||||||
|
end
|
||||||
|
|
||||||
def test_that_total_is_correctly_recalculated_when_items_are_added
|
def test_that_total_is_correctly_recalculated_when_items_are_added
|
||||||
2.times do
|
2.times do
|
||||||
@checkout.add(:PICO2)
|
@checkout.add(:PICO2)
|
||||||
@@ -125,4 +160,16 @@ class TestCheckout < Minitest::Test
|
|||||||
|
|
||||||
assert_equal 40.50, @checkout.total
|
assert_equal 40.50, @checkout.total
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_that_total_is_correctly_recalculated_when_items_are_removed
|
||||||
|
3.times do
|
||||||
|
@checkout.add(:PICO2) # Bulk discount applies
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 10.50, @checkout.total
|
||||||
|
|
||||||
|
@checkout.remove(:PICO2) # Bulk discount no longer applies
|
||||||
|
|
||||||
|
assert_equal 8.00, @checkout.total
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user