diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cde7c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.rspec \ No newline at end of file diff --git a/README.md b/README.md index 9120405..8dda68d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Panda Level Tiger Level --------------- 1. Create a FoodBarge that can be called like: -2. Test that when the zookeepers gets food for the panda, +2. Test that when the zookeepers gets food for the panda, the panda will eat it ``` diff --git a/zoo.rb b/zoo.rb index d4d906c..d111823 100644 --- a/zoo.rb +++ b/zoo.rb @@ -3,7 +3,6 @@ module Animal def eat(food) - @meals ||= 0 if likes?(food) @meals += 1 true @@ -26,10 +25,22 @@ def full? end +class Human + include Animal + + def acceptable_food + [Bacon.new, Tacos.new] + end +end + class Panda include Animal + def initialize + @meals ||= 0 + end + def acceptable_food [Bamboo.new] end @@ -38,11 +49,19 @@ def full? @meals > 30 end + def feed(food) + acceptable_food == food + end + end class Lion include Animal + def initialize + @meals ||= 0 + end + def acceptable_food [Wildebeests.new, Zeebras.new] end @@ -65,13 +84,23 @@ class Tacos < Food; end class Wildebeests < Food; end class Zeebras < Food; end class Bamboo < Food; end +class Bacon < Food; end class Zookeeper def feed(args={}) food = args.fetch(:food) - panda = args.fetch(:to) - panda.eat(food) + animal = args.fetch(:to) + if animal.full? + puts "The #{animal} is full." + else + animal.eat(food) + end end +end +class Foodbarge + def food_for(animal) + animal.acceptable_food + end end diff --git a/zoo_spec.rb b/zoo_spec.rb index 64203cc..794f2cc 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -28,16 +28,23 @@ class Salad < Food; end it "should be full after eating 30 bamboo" do panda = Panda.new 31.times do - panda.eat(Bamboo.new) + panda.eat(Bamboo.new) end panda.should be_full end it "should not be full after 1" do panda = Panda.new - panda.eat(Bamboo.new) + panda.eat(Bamboo.new) panda.should_not be_full end + + it "should be able to eat panda food from the foodbarge" do + foodbarge = Foodbarge.new + panda = Panda.new + food = foodbarge.food_for(panda) + panda.feed(food).should eq(true) + end end describe Lion do @@ -67,6 +74,18 @@ class Salad < Food; end end end +describe Human do + it "should like bacon" do + Human.new.likes?(Bacon.new).should eq(true) + end + it "should like tacos" do + Human.new.likes?(Tacos.new).should eq(true) + end + it "should not like bamboo" do + Human.new.likes?(Bamboo.new).should eq(false) + end +end + describe Zookeeper do it "should be able to feed bamboo to the pandas" do panda = Panda.new @@ -79,4 +98,20 @@ class Salad < Food; end lion.should_receive(:eat).with(:zeebras) Zookeeper.new.feed(food: :zeebras, to: lion) end + + it "should stop feeding panda when panda is full" do + panda = Panda.new + keeper = Zookeeper.new + 31.times { keeper.feed(food: Bamboo.new, to: panda) } + panda.should_not_receive(:eat) + keeper.feed(food: Bamboo.new, to: panda) + end +end + +describe Foodbarge do + it "should be able to get food for panda" do + foodbarge = Foodbarge.new + panda = Panda.new + foodbarge.food_for(panda).should eq([Bamboo.new]) + end end