Skip to content

Commit

Permalink
Feature: zookeeper can feed animals
Browse files Browse the repository at this point in the history
  • Loading branch information
jwo committed Apr 26, 2012
1 parent eb2014c commit 57f536f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
64 changes: 64 additions & 0 deletions zoo.rb
@@ -0,0 +1,64 @@
#Zoo

class Animal

def initialize
@meals = 0
end

def eat(food)
if likes?(food)
@meals += 1
true
else
false
end
end

def likes?(food)
acceptable_food.include?(food.to_sym)
end

def acceptable_food
[]
end

def full?
false
end

end


class Panda < Animal

def acceptable_food
[:bamboo]
end

def full?
@meals > 30
end

end

class Lion < Animal

def acceptable_food
[:wildebeests, :zeebras]
end

def full?
@meals > 10
end
end

class Zookeeper
def feed(args={})
food = args.fetch(:food)
panda = args.fetch(:to)
panda.eat(food)
end

end

85 changes: 85 additions & 0 deletions zoo_spec.rb
@@ -0,0 +1,85 @@
# Zoo spec file
require "./zoo"

describe Animal do
it "should not raise an exception when we try to like" do
expect {
Animal.new.likes?(:bacon)
}.to_not raise_error
end

end

describe Panda do

it "should like bamboo" do
Panda.new.likes?(:bamboo).should eq(true)
end

it "should like bamboo as a string" do
Panda.new.likes?("bamboo").should eq(true)
end

it "should not like grasshoppers" do
Panda.new.likes?(:grasshoppers).should eq(false)
end

it "should be able to eat the food" do
Panda.new.eat(:bamboo).should be_true
end

it "should be full after eating 30 bamboo" do
panda = Panda.new
31.times do
panda.eat(:bamboo)
end
panda.should be_full
end

it "should not be full after 1" do
panda = Panda.new
panda.eat(:bamboo)
panda.should_not be_full
end
end

describe Lion do
it "should like wildebeests" do
Lion.new.likes?(:wildebeests).should eq(true)
end

it "should like zeebras" do
Lion.new.likes?(:zeebras).should eq(true)
end

it "should not like salad" do
Lion.new.likes?(:salad).should eq(false)
end

it "should take 11 meals to be full" do
lion = Lion.new
lion.eat(:zeebras)
lion.should_not be_full
end
it "should take 11 meals to be full" do
lion = Lion.new
11.times do
lion.eat(:zeebras)
end
lion.should be_full
end
end

describe Zookeeper do
it "should be able to feed bamboo to the pandas" do
panda = Panda.new
panda.should_receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end

it "should be able to feed zeebras to the lions" do
lion = Lion.new
lion.should_receive(:eat).with(:zeebras)
Zookeeper.new.feed(food: :zeebras, to: lion)
end
end

0 comments on commit 57f536f

Please sign in to comment.