Skip to content

Completed Homework #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions zoo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def eat(food)
end

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

def acceptable_food
Expand All @@ -24,14 +24,36 @@ def full?
false
end

def feed(foods)
foods.each do |food|
eat(food)
end
end
end

class Food

attr_accessor :name

def initialize(name)
@name = name
end
end

class FoodBarge

@foodbarge = FoodBarge.new

def food_for(animal)
animal.acceptable_food
end
end

class Panda
include Animal

def acceptable_food
[:bamboo]
[Food.new("bamboo").name]
end

def full?
Expand All @@ -44,7 +66,7 @@ class Lion
include Animal

def acceptable_food
[:wildebeests, :zeebras]
[Food.new("wildebeests").name, Food.new("zebras").name]
end

def full?
Expand All @@ -58,6 +80,13 @@ def feed(args={})
panda = args.fetch(:to)
panda.eat(food)
end
end

class Human
include Animal

def acceptable_food
[Food.new("bacon").name, Food.new("tacos").name]
end
end

73 changes: 55 additions & 18 deletions zoo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,60 @@
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)
Panda.new.likes?(Food.new("bamboo")).should eq(true)
end

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

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

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

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

it "should be able to get fed" do
panda = Panda.new
panda.should respond_to(:feed)
panda.feed([Food.new("bamboo")])
end
end

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

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

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

it "should take 11 meals to be full" do
lion = Lion.new
lion.eat(:zeebras)
lion.eat(Food.new("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)
lion.eat(Food.new("zebras"))
end
lion.should be_full
end
Expand All @@ -65,13 +67,48 @@
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)
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)
lion.should_receive(:eat).with("zeebras")
Zookeeper.new.feed(food: "zeebras", to: lion)
end
end

describe Human do

it "should like bacon" do
Human.new.likes?(Food.new("bacon")).should be_true
end

it "should like tacos" do
Human.new.likes?(Food.new("tacos")).should be_true
end

it "should not like bamboo" do
Human.new.likes?(Food.new("bamboo")).should_not be_true
end

it "should be able to eat" do
Human.new.eat(Food.new("bacon")).should be_true
end
end

describe FoodBarge do

it "should be able to check what foods are acceptable to the animal" do
foodbarge = FoodBarge.new
panda = Panda.new
foodbarge.should_receive(:food_for).with(panda)
foodbarge.food_for(panda)
end

it "should have bacon and tacos ready for the human to eat" do
animal = Human.new
barge = FoodBarge.new
barge.food_for(animal).should eq([Food.new("bacon").name, Food.new("tacos").name])
end
end