Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Feature: zookeeper can feed animals

  • Loading branch information...
commit 57f536fead3cf35da563e7780ae90565f7ea921b 1 parent eb2014c
@jwo jwo authored
Showing with 149 additions and 0 deletions.
  1. +64 −0 zoo.rb
  2. +85 −0 zoo_spec.rb
View
64 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
+
View
85 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
Please sign in to comment.
Something went wrong with that request. Please try again.