-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.rb
179 lines (131 loc) · 4.09 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true
require_relative 'checkout'
require_relative 'promotion_rules/product'
require_relative 'promotion_rules/order'
require_relative 'product'
def assert(left, right)
raise "#{left} is not equal #{right}!" unless left == right
end
heart = Product.new('001', 'Lavender heart', 9.25)
cufflinks = Product.new('002', 'Personalised cufflinks', 45.00)
shirt = Product.new('003', 'Kids T-shirt', 19.95)
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Product.new('001', 2, 8.5)]
# Scenario 1
co = Checkout.new(promotions)
co.scan(heart)
co.scan(cufflinks)
co.scan(shirt)
assert(co.total, '£66.78')
# Scenario 2
co = Checkout.new(promotions)
co.scan(heart)
co.scan(shirt)
co.scan(heart)
assert(co.total, '£36.95')
# Scenario 3
co = Checkout.new(promotions)
co.scan(heart)
co.scan(cufflinks)
co.scan(heart)
co.scan(shirt)
assert(co.total, '£73.76')
# Scenario 4a - more products promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Product.new('001', 2, 8.5),
PromotionRules::Product.new('001', 3, 7.5)]
co = Checkout.new(promotions)
co.scan(heart)
co.scan(heart)
co.scan(heart)
assert(co.total, '£22.50')
# Scenario 4b - more products promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Product.new('001', 2, 8.5),
PromotionRules::Product.new('001', 3, 7.5)]
co = Checkout.new(promotions)
co.scan(heart)
co.scan(heart)
assert(co.total, '£17.00')
# Scenario 4c - more products promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Product.new('001', 2, 8.5),
PromotionRules::Product.new('001', 3, 7.5)]
co = Checkout.new(promotions)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
co.scan(heart)
assert(co.total, '£67.50')
# Scenario 5a - more order promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Order.new(100.00, 0.2)]
co = Checkout.new(promotions)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(cufflinks)
assert(co.total, '£108.00')
# Scenario 5b - more order promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Order.new(100.00, 0.2),
PromotionRules::Product.new('002', 3, 30)]
co = Checkout.new(promotions)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(cufflinks)
assert(co.total, '£81.00')
# Scenario 5c - more order promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Order.new(100.00, 0.2),
PromotionRules::Product.new('002', 3, 35)]
co = Checkout.new(promotions)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(cufflinks)
assert(co.total, '£84.00')
# Scenario 6a - more order promotions and more product promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Order.new(100.00, 0.2),
PromotionRules::Product.new('002', 3, 35),
PromotionRules::Product.new('002', 2, 40)]
co = Checkout.new(promotions)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(shirt)
assert(co.total, '£99.96')
# Scenario 6b - more order promotions and more product promotions
promotions = [PromotionRules::Order.new(60.00, 0.1),
PromotionRules::Order.new(100.00, 0.2),
PromotionRules::Product.new('002', 3, 35),
PromotionRules::Product.new('002', 2, 40),
PromotionRules::Product.new('003', 2, 15),
PromotionRules::Product.new('003', 3, 12),
PromotionRules::Product.new('003', 5, 9)]
co = Checkout.new(promotions)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(cufflinks)
co.scan(shirt)
co.scan(shirt)
co.scan(shirt)
co.scan(shirt)
assert(co.total, '£122.40')
# Scenario 7a - no promotions
co = Checkout.new([])
co.scan(heart)
co.scan(cufflinks)
co.scan(shirt)
assert(co.total, '£74.20')
# Scenario 7a - no promotions
co = Checkout.new(nil)
co.scan(heart)
co.scan(cufflinks)
co.scan(shirt)
assert(co.total, '£74.20')