Files
ruby-checkout/tests.rb
T
2026-07-15 11:08:17 -04:00

92 lines
1.9 KiB
Ruby

require "minitest/autorun"
require "./checkout.rb"
class TestCheckout < Minitest::Test
def setup
@checkout = Checkout.new
end
def test_that_unknown_items_cannot_be_added
assert_raises Checkout::InvalidItem do
@checkout.add(:foo)
end
end
def test_that_allowed_items_are_added
@checkout.add(:PI5)
@checkout.add(:ZERO2W)
@checkout.add(:PICO2)
assert_equal [:PI5, :ZERO2W, :PICO2].sort, @checkout.items.sort
end
def test_that_total_is_the_sum_of_all_items_when_no_discounts_apply
@checkout.add(:PI5) # 60.00
@checkout.add(:ZERO2W) # 15.00
@checkout.add(:PICO2) # 4.00
# -----
# 79.00 total
assert_equal 79.00, @checkout.total
end
def test_that_an_empty_checkout_total_is_zero
assert_equal 0.00, @checkout.total
end
# Discounts
def test_that_bogof_applied
4.times do
@checkout.add(:ZERO2W) # Should remove 2 Zeros
end
assert_equal 30.00, @checkout.total
end
def test_that_bogof_applied_with_odd_number
5.times do
@checkout.add(:ZERO2W) # Should still remove 2 Zeros, 5th is full price
end
assert_equal 45.00, @checkout.total
end
def test_that_price_is_reduced_for_3_picos
3.times do
@checkout.add(:PICO2) # 3.50 * 3 = 10.50
end
assert_equal 10.50, @checkout.total
end
def test_that_price_is_reduced_for_more_than_3_picos
4.times do
@checkout.add(:PICO2) # 3.50 * 4 = 14
end
assert_equal 14.00, @checkout.total
end
def test_that_price_is_not_reduced_for_less_than_3_picos
2.times do
@checkout.add(:PICO2)
end
assert_equal 8.00, @checkout.total
end
def test_that_both_discounts_apply_at_the_same_time
4.times do
@checkout.add(:ZERO2W) # 30.00 total
end
3.times do
@checkout.add(:PICO2) # 10.50 total
end
assert_equal (30.00 + 10.50), @checkout.total
end
end