Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created February 17, 2013 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trshafer/4970209 to your computer and use it in GitHub Desktop.
Save trshafer/4970209 to your computer and use it in GitHub Desktop.
TDD Bus implementation with Keith
class Bus
def initialize
@parked = true
@number_of_children_in_bus = 0
end
def drive!
@driving = true
@parked = false
end
def driving?
@driving == true
end
def park!
@driving = false
@parked = true
end
def parked?
@parked == true
end
def run_over_children(number_of_children)
if driving?
"I hit #{number_of_children} children."
else
false
end
end
def children
@number_of_children_in_bus
end
def pick_up_children(number_of_children)
if parked?
@number_of_children_in_bus = @number_of_children_in_bus + number_of_children
else
false
end
end
def drop_off_children(number_of_children)
@number_of_children_in_bus = @number_of_children_in_bus - number_of_children
end
end
require 'rspec'
require './bus'
# rspec -fd -c bus_spec.rb
describe Bus do
let(:bus) { Bus.new }
it 'should start off parked' do
bus.parked?.should be_true
end
it 'should not start off driving' do
bus.driving?.should be_false
end
it 'should drive' do
bus.drive!
bus.driving?.should be_true
end
it 'should not be parked and driving at the same time' do
bus.parked?.should be_true
bus.drive!
bus.driving?.should be_true
bus.parked?.should be_false
end
it 'should be able to park' do
bus.drive!
bus.driving?.should be_true
bus.park!
bus.parked?.should be_true
bus.driving?.should be_false
end
it 'should be able to run over x children' do
bus.drive!
bus.run_over_children(3).should == 'I hit 3 children.'
bus.run_over_children(5).should == 'I hit 5 children.'
end
it 'must be driving in order to run over children' do
bus.park!
bus.run_over_children(3).should be_false
end
it 'should be able to pick up children and drop them off' do
bus.children.should == 0
bus.pick_up_children(3)
bus.children.should == 3
bus.pick_up_children(2)
bus.children.should == 5
bus.drop_off_children(4)
bus.children.should == 1
bus.drop_off_children(1)
bus.children.should == 0
end
it 'must be parked in order to pick up children' do
bus.drive!
bus.children.should == 0
bus.pick_up_children(2)
bus.children.should == 0
end
end
gem 'rspec'
gem 'debugger'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment