-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout_tests.rb
103 lines (87 loc) · 2.71 KB
/
checkout_tests.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
$:.unshift File.dirname(__FILE__)
require "test/unit"
require "lib/transaction"
class CheckoutTests < Test::Unit::TestCase
def setup
@price_rules = [
PriceRule.new("Apple") {|qty| qty * 0.5},
PriceRule.new("Apple") { |qty| (qty/3) * -0.2 },
PriceRule.new("Orange") { |qty| qty * 0.8},
PriceRule.new("Orange") { |qty| (qty/2) * -0.8 },
PriceRule.new("Banana") { |qty| qty * 1.0},
PriceRule.new("Banana") { |qty| (qty/2) * -0.5 }
]
@transaction = Transaction.new(formatter: NullFormatter.new, price_rules: @price_rules)
end
def test_can_create_transaction_with_price_rule
t = Transaction.new(price_rules:[PriceRule.new("")])
assert(t.price_rules.count > 0, "Expected price rules")
end
def test_can_add_item
@transaction.add_item("Apple")
assert(@transaction.items.count > 0, "Expected items")
end
def test_price_for_one_apple
@transaction.add_item("Apple")
@transaction.checkout
assert_equal(0.5,@transaction.total)
end
def test_price_for_three_apples
@transaction.add_item("Apple")
@transaction.add_item("Apple")
@transaction.add_item("Apple")
@transaction.checkout
assert_equal(1.30,@transaction.total)
end
def test_buy_one_get_one_free
@transaction.add_item("Orange")
@transaction.add_item("Orange")
@transaction.checkout
assert_equal(0.8,@transaction.total)
end
def test_buy_one_get_one_half_off
@transaction.add_item("Banana")
@transaction.add_item("Banana")
@transaction.checkout
assert_equal(1.5,@transaction.total)
end
def test_money_format
assert_equal("$10.99",Checkout.money_format(10.994))
end
def test_formatter_prints_price_quantity_and_name
f = Formatter.new
name = "TestName"
quantity = 5
price = 10.0
line_item = f.line_item(name,quantity,price)
assert_match(name,line_item)
assert_match("#{quantity}",line_item)
assert_match(Checkout.money_format(price),line_item)
end
def test_formatter_prints_discount_for_negative_values
f = Formatter.new
name = "TestName"
quantity = 5
price = -10.0
line_item = f.line_item(name,quantity,price)
assert_match("-$10.00",line_item)
end
def test_formatter_ignores_lines_with_no_value
# This accounts for discounts evaluated but do not apply any amount.
f = Formatter.new
f.line_item("Test",2,0)
assert_equal("",f.output)
end
def test_transaction_any_rules_returns_true_when_any_rule_applies
assert(@transaction.any_rules?("Apple"))
end
def test_transaction_any_rules_returns_false_when_nothing_applies
assert(false === @transaction.any_rules?("Melon"))
end
end
class NullFormatter
def total (total)
end
def line_item (name,qty,price)
end
end