Skip to content

Commit

Permalink
more promo_rule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goodviber committed Jun 11, 2018
1 parent 2179ba9 commit af1937c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
15 changes: 15 additions & 0 deletions lib/checkout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Checkout

attr_reader :promotional_rules
attr_accessor :cart

def initialize(promotional_rules = [])
@promotional_rules = promotional_rules
@cart = cart
end

def scan(item)
cart << item
end

end
19 changes: 13 additions & 6 deletions lib/promo_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,30 @@ def initialize(type, &block)
end

def apply(*args)
block.call(args)
case block.arity
when 0
block.call
when 1
block.call(args[0])
when 2
block.call(args[0], args[1])
end
end

def self.discount_rule(minimum_spend, percentage_discount)
PromoRule.new(PromoRule::TYPE[:discount]) do |discount_total|
if discount_total >= minimum_spend
discount_total - discount_total*percentage_discount/100
PromoRule.new(PromoRule::TYPE[:discount]) do |sum_total|
if sum_total >= minimum_spend
sum_total.round(2) - sum_total.round(2)*percentage_discount/100
else
discount_total
sum_total
end
end
end

def self.multi_discount_rule(product_code, qualifying_quantity, discount_price)
PromoRule.new(PromoRule::TYPE[:multi_discount]) do |code, quantity|
if code == product_code && quantity >= qualifying_quantity
discounted_price
discount_price
end
end
end
Expand Down
63 changes: 57 additions & 6 deletions spec/promo_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end
end
end

describe 'creating discount_promo_rule processor' do
describe 'creating discount_promo_rule processor' do
it 'should call block with no arguments' do
value = false
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do
Expand All @@ -26,11 +26,11 @@
expect(promo_rule.apply).to be_truthy
end

it 'should not call block with one argument' do
it 'should call block with one argument' do
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do |value|
value
end
expect(promo_rule.apply('foo')).not_to eq('foo')
expect(promo_rule.apply('foo')).to eq('foo')
end

it 'should call block with two arguments' do
Expand All @@ -40,13 +40,64 @@
expect(promo_rule.apply('foo', 'bar')).to eq('foo-bar')
end

it 'should not call block with three arguments' do
it 'should call block with three arguments' do
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do |value1, value2, value3|
"#{value1}-#{value2}-#{value3}"
end
expect(promo_rule.apply('foo', 'bar', 'baz')).to eq('foo-bar-baz')
expect(promo_rule.apply('foo', 'bar', 'baz')).not_to eq('foo-bar-baz')
end
end

describe 'multi_discount method' do
subject { PromoRule.multi_discount_rule('001', 10, 5.00) }

it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end

it 'should calculate discount when count is equal' do
discount = subject.apply('001', 10)
expect(discount).to eq(5.00)
end

it 'should calculate discount when count is more than' do
discount = subject.apply('001', 11)
expect(discount).to eq(5.00)
end

it 'should not discount when count is less' do
discount = subject.apply('001', 9)
expect(discount).to be_nil
end

it 'should not discount when product code is different' do
discount = subject.apply('002', 10)
expect(discount).to be_nil
end
end

describe 'discount method' do
subject { PromoRule.discount_rule(60, 10) }

it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end

it 'should calculate discount when count is equal' do
discounted = subject.apply(60)
expect(discounted).to eq(54)
end

it 'should calculate discount when count is more than' do
discounted = subject.apply(61)
expect(discounted).to eq(54.9)
end

it 'should not discount when count is less than' do
discounted = subject.apply(59)
expect(discounted).to eq(59)
end
end


end

0 comments on commit af1937c

Please sign in to comment.