From 482d28cb7f45bbd54d6a00e59dda91159126717b Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Mar 2014 16:40:36 +0000 Subject: [PATCH 1/6] Add a failing test for Topping.initialize --- spec/pizza_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2f30c05..1399909 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -10,4 +10,13 @@ it "exists" do expect(Topping).to be_a(Class) end + + describe ".initialize" do + it "sets the name of the topping" do + topping = Topping.new('olives') + + expect(topping.name).to eq('olives') + end + end + end From 6adf80c93d786ea89887d0c219cfeea1485fe462 Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Mar 2014 16:57:11 +0000 Subject: [PATCH 2/6] Adds failing test for whether topping is true --- spec/pizza_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 1399909..3e9eecd 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -12,11 +12,17 @@ end describe ".initialize" do + it "sets the name of the topping" do topping = Topping.new('olives') - expect(topping.name).to eq('olives') end + + it "sets whether or not the topping is vegetarian" do + topping = Topping.new('bell peppers', vegetarian: true) + expect(topping.vegetarian).to eq(true) + end + end end From efd85f6be0b1c82ba84d1f6d634ecb0fc58400fe Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Mar 2014 17:07:50 +0000 Subject: [PATCH 3/6] Adds a failing test for new toppings within Pizza class --- spec/pizza_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 3e9eecd..ba3cbc0 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -1,10 +1,27 @@ require './pizza' describe Pizza do + it "exists" do expect(Pizza).to be_a(Class) end + + describe ".initialize" do + it 'records all of the toppings' do + toppings = [ + Topping.new('mushrooms', vegetarian:true), + Topping.new('pepperoni') + ] + pizza = Pizza.new(toppings) + + expect(pizza.toppings).to eq (toppings) + end + end + end +# -- END OF PIZZA CLASS TESTS + +# -- BEGINNING OF TOPPING CLASS TESTS describe Topping do it "exists" do From ab34a900d19cafd7c88e655a134d3b3dd3b5df19 Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Mar 2014 17:10:55 +0000 Subject: [PATCH 4/6] Edits pizza.rb to pass test for adding new pizza toppings --- pizza.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pizza.rb b/pizza.rb index 49ff37a..8c2e527 100644 --- a/pizza.rb +++ b/pizza.rb @@ -1,5 +1,20 @@ class Pizza + + attr_reader :toppings + + def initialize(toppings) + @toppings = toppings + end + end class Topping + + attr_accessor :name, :vegetarian + + def initialize(name, vegetarian:false) + @name = name + @vegetarian = vegetarian + end + end From cc4eca451e1df09de7269d98a5caab823f917d6f Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Mar 2014 17:19:20 +0000 Subject: [PATCH 5/6] All green --- pizza.rb | 2 +- spec/pizza_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pizza.rb b/pizza.rb index 8c2e527..aac6719 100644 --- a/pizza.rb +++ b/pizza.rb @@ -2,7 +2,7 @@ class Pizza attr_reader :toppings - def initialize(toppings) + def initialize(toppings=[Topping.new('cheese', vegetarian:true)]) @toppings = toppings end diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index ba3cbc0..1fbb6c4 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -16,6 +16,13 @@ expect(pizza.toppings).to eq (toppings) end + + it 'defaults the toppings to cheese only, if the pizza has no toppings' do + pizza = Pizza.new + expect(pizza.toppings.size).to eq(1) + expect(pizza.toppings.first.name).to eq('cheese') + end + end end From 3b082339c596e643d3d488bd17efa7066665474a Mon Sep 17 00:00:00 2001 From: vagrant Date: Sat, 8 Mar 2014 19:21:54 +0000 Subject: [PATCH 6/6] Complete extension exercises with relevant tests --- pizza.rb | 24 +++++++++++++++++++- spec/pizza_spec.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/pizza.rb b/pizza.rb index aac6719..887318c 100644 --- a/pizza.rb +++ b/pizza.rb @@ -1,11 +1,33 @@ class Pizza - attr_reader :toppings + attr_reader :toppings, :delivery_time def initialize(toppings=[Topping.new('cheese', vegetarian:true)]) @toppings = toppings end + def vegetarian? + @toppings.all? do |topping| + topping.vegetarian == true + end + end + + def add_topping(new_topping) + @toppings << new_topping + end + + def deliver! + @delivery_time = Time.now + 30*60 + end + + def late? + if Time.now > @delivery_time + true + else + false + end + end + end class Topping diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 1fbb6c4..1f4b139 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -25,6 +25,62 @@ end +# -- BEGINNING OF EXTENSION EXERCISE TESTS + + describe ".vegetarian?" do + it "reports if the pizza is vegetarian" do + toppings = [ + Topping.new('mushrooms', vegetarian:true), + Topping.new('peppers', vegetarian:true) + ] + pizza = Pizza.new(toppings) + expect(pizza.vegetarian?).to eq(true) + end + end + + describe ".add_toppings" do + it "keeps track of topping names and types" do + toppings = [ + Topping.new('mushrooms', vegetarian:true), + Topping.new('pepperoni') + ] + pizza = Pizza.new(toppings) + topping = Topping.new("sausage") + pizza.add_topping(topping) + expect(pizza.toppings.count).to eq(3) + end + end + + context "delivery times" do + it "sets time in which pizza should be delivered" do + toppings = [ + Topping.new('mushrooms', vegetarian:true), + Topping.new('peppers', vegetarian:true) + ] + pizza = Pizza.new(toppings) + start_time = Time.now + Time.stub(:now).and_return(start_time) + expect(pizza.deliver!).to eq(start_time + 30*60) + end + + it "reports if pizza delivered late" do + toppings = [ + Topping.new('mushrooms', vegetarian:true), + Topping.new('peppers', vegetarian:true) + ] + pizza = Pizza.new(toppings) + start_time = Time.now - 30*60 + Time.stub(:now).and_return(start_time) + pizza.deliver! + expect(pizza.late?).to eq(false) + + late_pizza = pizza.delivery_time + 10*60 + Time.stub(:now).and_return(late_pizza) + expect(pizza.late?).to eq(true) + end + + end + # -- END OF EXTENSION EXERCISE TESTS end # -- END OF PIZZA CLASS TESTS