在应对代码挑战的过程中,我必须向“注册”添加支持。我添加了以下新支持的项目:
require 'register'
require 'timecop'
describe Register do
subject { described_class.new }
describe 'Frozen Pizza' do
it 'should be $5.00 each' do
subject.ring_up('Frozen Pizza', quantity: 1)
expect(subject.total).to eq 5.00
subject.ring_up('Frozen Pizza', quantity: 1)
expect(subject.total).to eq 10.00
end
end
describe 'Corn' do
it 'should be $0.50 each' do
subject.ring_up('Corn', quantity: 1)
expect(subject.total).to eq 0.50
end
it 'should be 5 for $2.00' do
subject.ring_up('Corn', quantity: 5)
expect(subject.total).to eq 2.00
subject.ring_up('Corn', quantity: 1)
expect(subject.total).to eq 2.50
end
end
describe 'Ground Beef' do
it 'should be $4.99/lb' do
subject.ring_up('Ground Beef', quantity: 1)
expect(subject.total).to eq 4.99
end
it 'should allow partial pounds' do
subject.ring_up('Ground Beef', quantity: 0.5)
expect(subject.total).to eq 2.50
end
end
describe 'Chocolate Mayfield Ice Cream' do
it 'should be $5.99' do
subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 1)
expect(subject.total).to eq 5.99
end
context 'on Wednesdays' do
before { Timecop.freeze(Time.parse('2014-06-18 13:00:00')) }
after { Timecop.return }
it 'should be BOGO' do
subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 2)
expect(subject.total).to eq 5.99
subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 1)
expect(subject.total).to eq 11.98
end
end
context 'on Non-Wednesdays' do
before { Timecop.freeze(Time.parse('2014-06-19 13:00:00')) }
after { Timecop.return }
it 'should be NOT be BOGO' do
subject.ring_up('Chocolate Mayfield Ice Cream', quantity: 2)
expect(subject.total).to eq 11.98
end
end
end
describe 'Vanilla Mayfield Ice Cream' do
it 'should be $5.99' do
subject.ring_up('Vanilla Mayfield Ice Cream', quantity: 1)
expect(subject.total).to eq 5.99
subject.ring_up('Vanilla Mayfield Ice Cream', quantity: 1)
expect(subject.total).to eq 11.98
end
end
describe 'Mango' do
context 'on Mondays' do
before { Timecop.freeze(Time.parse('2014-06-16 13:00:00')) }
after { Timecop.return }
it 'should be $0.50 each' do
subject.ring_up('Mango', quantity: 1)
expect(subject.total).to eq 0.50
subject.ring_up('Mango', quantity: 1)
expect(subject.total).to eq 1.00
end
end
end
context 'on Non-Mondays' do
before { Timecop.freeze(Time.parse('2014-06-17 13:00:00')) }
after { Timecop.return }
it 'should be $1.00 each' do
subject.ring_up('Mango', quantity: 1)
expect(subject.total).to eq 1.00
subject.ring_up('Mango', quantity: 1)
expect(subject.total).to eq 2.00
end
end
# Adding Additional Support To the Following Items
describe 'Mahi Fillet' do
it 'should be $7.99' do
subject.ring_up('Mahi Fillet', quantity: 1)
expect(subject.total).to eq 7.99
subject.ring_up('Mahi Fillet', quantity: 1)
expect(subject.total).to eq 15.98
end
end
describe 'Mac & Cheese' do
it 'should be $1.29 each' do
subject.ring_up('Mac & Cheese', quantity: 1)
expect(subject.total).to eq 1.29
end
it 'should be 5 for $10.00' do
subject.ring_up('Corn', quantity: 10)
expect(subject.total).to eq 10.00
subject.ring_up('Corn', quantity: 1)
expect(subject.total).to eq 1.00
end
end
describe 'Super Ripe Avacados' do
it 'should be $1,10' do
subject.ring_up('Super Ripe Avacados', quantity: 1)
expect(subject.total).to eq 1.10
end
it 'should be BOGO' do
subject.ring_up('Super Ripe Avacados', quantity: 3)
expect(subject.total).to eq 1.10
subject.ring_up('Super Ripe Avacados', quantity: 1)
expect(subject.total).to eq 0.36666666666667
end
end
end
现在,我正在创建一些要通过Rake运行的测试,并将其注释为#Additions,仅添加一个后,我就会看到“语法错误”,而我在这里完全迷住了。
class Register
attr :items
def initialize
@items = Hash.new(0)
end
def ring_up(item, args)
@items[item] += args[:quantity]
end
def total
total = 0
items.each do |item, quantity|
case item
when 'Frozen Pizza'
total += quantity * 5
when 'Corn'
if (quantity % 5 == 0)
total += quantity / 5 * 2.00
else
bundles = quantity / 5
singles = quantity % 5
total += bundles * 2.00 + singles * 0.50
end
when 'Ground Beef'
total += (quantity * 4.99).round(2)
when /Mayfield Ice Cream/
if (quantity % 2 == 0)
if item =~ /^Chocolate/
if Time.now.strftime("%A") == 'Wednesday'
total += quantity / 2 * 5.99
else
total += quantity * 5.99
end
else
total += quantity * 5.99
end
else
if item =~ /^Chocolate/
if Time.now.strftime("%A") == 'Wednesday'
total += (quantity / 2) * 5.99 + (quantity % 2) * 5.99
else
total += quantity * 5.99
end
else
total += quantity * 5.99
end
end
when 'Mango'
if Time.now.strftime("%A") == 'Monday'
total += quantity * 0.50
else
total += quantity * 1.00
end
end
#Additions
when 'Mahi Fillet'
total += quantity * 7.99
end
end
total
endNow
end
完整的错误返回如下:
syntax error, unexpected keyword_when, expecting keyword_end (SyntaxError)
when 'Mahi Fillet'
^
这end
:
end <<<<=====
#Additions
when 'Mahi Fillet'
关闭整个case
块,因此when
下一行的解析器没有意义。只需删除它end
即可解决您的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句