From 4f8db120c48eb61e5ca6d73813d988f7e4c0b4a6 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 7 Dec 2013 18:12:02 -0500 Subject: [PATCH 1/2] add human class --- .travis.yml | 5 +++ Gemfile | 4 ++ Gemfile.lock | 20 ++++++++++ LICENSE | 7 ++++ README.md | 30 +++++++++++++++ rakefile.rb | 14 +++++++ spec_helper.rb | 10 +++++ zoo.rb | 87 ++++++++++++++++++++++++++++++++++++++++++++ zoo_spec.rb | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 276 insertions(+) create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE create mode 100644 README.md create mode 100644 rakefile.rb create mode 100644 spec_helper.rb create mode 100644 zoo.rb create mode 100644 zoo_spec.rb diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8ae7878 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 1.9.3 + - jruby-19mode # JRuby in 1.9 mode + - rbx-19mode diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7cd68e3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'http://rubygems.org' + +gem 'rake' +gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..277fb87 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,20 @@ +GEM + remote: http://rubygems.org/ + specs: + diff-lcs (1.1.3) + rake (0.9.2.2) + rspec (2.9.0) + rspec-core (~> 2.9.0) + rspec-expectations (~> 2.9.0) + rspec-mocks (~> 2.9.0) + rspec-core (2.9.0) + rspec-expectations (2.9.1) + diff-lcs (~> 1.1.3) + rspec-mocks (2.9.0) + +PLATFORMS + ruby + +DEPENDENCIES + rake + rspec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f3e4a68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2012 Jesse Wolgamott + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9120405 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Episode 1 - Lions, Pandas and Zookeepers +======================================== + +An accurate simulation of zookeepers feeding Lions and Pandas + +Panda Level +--------------- + +1. Create a human that likes bacon and tacos, but not bamboo +2. (Using TDD [write tests first]) + +Tiger Level +--------------- +1. Create a FoodBarge that can be called like: +2. Test that when the zookeepers gets food for the panda, +the panda will eat it + +``` +food = foodbarge.food_for(panda) +panda.feed(food) +``` + +Eagle Level +---------- + +1. Extract `Food` into a class, rather than a symbol +2. Create separate Tacos, Wildebeests, etc classes for each food +2. Rather than comparing Tacos.new, implement the `==` method so you can do `Tacos.new == Tacos.new` + +Copyright: Jesse Wolgamott, MIT License (See LICENSE) diff --git a/rakefile.rb b/rakefile.rb new file mode 100644 index 0000000..1e0ebfe --- /dev/null +++ b/rakefile.rb @@ -0,0 +1,14 @@ +require "rubygems" +require "bundler/setup" + +require 'rspec/core/rake_task' + +desc 'Default: run specs.' +task :default => :spec + +desc "Run specs" +RSpec::Core::RakeTask.new do |t| + t.pattern = "**/*_spec.rb" # don't need this, it's default. + # Put spec opts in a file named .rspec in root +end + diff --git a/spec_helper.rb b/spec_helper.rb new file mode 100644 index 0000000..cdd59d3 --- /dev/null +++ b/spec_helper.rb @@ -0,0 +1,10 @@ +RSpec.configure do |config| + # Use color in STDOUT + config.color_enabled = true + + # Use color not only in STDOUT but also in pagers and files + config.tty = true + + # Use the specified formatter + # config.formatter = :documentation # :progress, :html, :textmate +end \ No newline at end of file diff --git a/zoo.rb b/zoo.rb new file mode 100644 index 0000000..a7ebd3b --- /dev/null +++ b/zoo.rb @@ -0,0 +1,87 @@ +#Zoo + +module Animal + + def eat(food) + @meals ||= 0 + if likes?(food) + @meals += 1 + true + else + false + end + end + + def likes?(food) + acceptable_food.include?(food) + end + + def acceptable_food + [] + end + + def full? + false + end + +end + + +class Panda + include Animal + + def acceptable_food + [Bamboo.new] + end + + def full? + @meals > 30 + end + +end + +class Lion + include Animal + + def acceptable_food + [Wildebeests.new, Zeebras.new] + end + + def full? + @meals > 10 + end +end + + +class Food + + def ==(other) + other.is_a? self.class + end + +end + +class Tacos < Food; end +class Bacon < Food; end +class Wildebeests < Food; end +class Zeebras < Food; end +class Bamboo < Food; end + +class Zookeeper + def feed(args={}) + food = args.fetch(:food) + panda = args.fetch(:to) + panda.eat(food) + end + +end + +class Human + include Animal + + def acceptable_food + [Bacon.new, Tacos.new] + end + +end + diff --git a/zoo_spec.rb b/zoo_spec.rb new file mode 100644 index 0000000..ebf4360 --- /dev/null +++ b/zoo_spec.rb @@ -0,0 +1,99 @@ +# Zoo spec file + +require "./zoo" +require "rspec" +require '../../spec_helper' + +class Grasshoppers < Food; end +class Salad < Food; end + +describe Tacos do + it "should know all tacos are equal" do + (Tacos.new == Tacos.new).should be_true + end +end + +describe Panda do + + it "should like bamboo" do + Panda.new.likes?(Bamboo.new).should eq(true) + end + + it "should not like grasshoppers" do + Panda.new.likes?(Grasshoppers.new).should eq(false) + end + + it "should be able to eat the food" do + Panda.new.eat(Bamboo.new).should be_true + end + + it "should be full after eating 30 bamboo" do + panda = Panda.new + 31.times do + 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.should_not be_full + end +end + +describe Lion do + it "should like wildebeests" do + Lion.new.likes?(Wildebeests.new).should eq(true) + end + + it "should like zeebras" do + Lion.new.likes?(Zeebras.new).should eq(true) + end + + it "should not like salad" do + Lion.new.likes?(Salad.new).should eq(false) + end + + it "should take 11 meals to be full" do + lion = Lion.new + lion.eat(Zeebras.new) + lion.should_not be_full + end + it "should take 11 meals to be full" do + lion = Lion.new + 11.times do + lion.eat(Zeebras.new) + 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 + +describe Human do + it "should like bacon" do + expect(Human.new.likes?(Bacon.new)).to eq(true) + end + + it "should like tacos" do + expect(Human.new.likes?(Tacos.new)).to eq(true) + end + + it "should not like bamboo" do + expect(Human.new.likes?(Bamboo.new)).to eq(false) + end +end + From f821d42417f04d433098e7c8f091649c6f780ea4 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 7 Dec 2013 19:41:01 -0500 Subject: [PATCH 2/2] add foodbarge --- zoo.rb | 18 +++++++++++++++--- zoo_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/zoo.rb b/zoo.rb index a7ebd3b..f9b94e7 100644 --- a/zoo.rb +++ b/zoo.rb @@ -7,6 +7,7 @@ def eat(food) if likes?(food) @meals += 1 true + else false end @@ -70,8 +71,12 @@ class Bamboo < Food; end class Zookeeper def feed(args={}) food = args.fetch(:food) - panda = args.fetch(:to) - panda.eat(food) + animal = args.fetch(:to) + animal.eat(food) + end + + def receive(foodBarge) + feed(foodBarge) end end @@ -82,6 +87,13 @@ class Human def acceptable_food [Bacon.new, Tacos.new] end - end +class FoodBarge + def food_for(args={}) + food = args.fetch(:food) + zookeeper = args.fetch(:to) + animal = args.fetch(:animal) + zookeeper.receive(food: food, to: animal) + end +end diff --git a/zoo_spec.rb b/zoo_spec.rb index ebf4360..11d28b1 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -81,6 +81,33 @@ class Salad < Food; end lion.should_receive(:eat).with(:zeebras) Zookeeper.new.feed(food: :zeebras, to: lion) end + + it "should feed the panda the bamboo when foodbarge is received" do + foodbarge = FoodBarge.new + zookeeper = Zookeeper.new + panda = Panda.new + bamboo = Bamboo.new + foodbarge.food_for(food: bamboo, to: zookeeper, animal: panda) + expect(panda.eat(bamboo)).to eq(true) + end + + it "should feed the lion the zeebras when foodbarge is received" do + foodbarge = FoodBarge.new + zookeeper = Zookeeper.new + lion = Lion.new + zeebras = Zeebras.new + foodbarge.food_for(food: zeebras, to: zookeeper, animal: lion) + expect(lion.eat(zeebras)).to eq(true) + end + + it "should not feed the lion the bamboo when foodbarge is received" do + foodbarge = FoodBarge.new + zookeeper = Zookeeper.new + lion = Lion.new + bamboo = Bamboo.new + foodbarge.food_for(food: bamboo, to: zookeeper, animal: lion) + expect(lion.eat(bamboo)).to eq(false) + end end describe Human do @@ -97,3 +124,21 @@ class Salad < Food; end end end +describe FoodBarge do + it "should be sent to zookeeper with bamboo for a panda" do + zookeeper = Zookeeper.new + bamboo = Bamboo.new + panda = Panda.new + expect(zookeeper).to receive(:receive).with(food: bamboo, to: panda) + expect(FoodBarge.new.food_for(food: bamboo, to: zookeeper, animal: panda)) + end + + it "should be sent to zookeeper with zeebras for a lion" do + zookeeper = Zookeeper.new + zeebras = Zeebras.new + lion = Lion.new + expect(zookeeper).to receive(:receive).with(food: zeebras, to: lion) + expect(FoodBarge.new.food_for(food: zeebras, to: zookeeper, animal: lion)) + end +end +