From 0268dedc4935516a5de12df592feb7d344edfc69 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:20:34 -0500 Subject: [PATCH 01/13] Add a failing test for Topping.initialize. --- spec/pizza_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2f30c05..523bc11 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -10,4 +10,11 @@ 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 a7194134a08ab3519cf93483d48fb26a5c4b857d Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:24:59 -0500 Subject: [PATCH 02/13] Add initialize method and attr_accessor to class Topping --- pizza.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pizza.rb b/pizza.rb index 49ff37a..2203c5d 100644 --- a/pizza.rb +++ b/pizza.rb @@ -2,4 +2,8 @@ class Pizza end class Topping + attr_accessor :name + def initialize(name) + @name = name + end end From e653d0d930e143673bcc8c5c2c3dba9e9c25d7b0 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:36:07 -0500 Subject: [PATCH 03/13] Add failing vegetarian test for Topping.initialize --- spec/pizza_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 523bc11..e60e738 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -16,5 +16,11 @@ 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 d2ffd01a721499e16b29cb6ee80674cf94e5e441 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:45:56 -0500 Subject: [PATCH 04/13] Add optional vegetarian parameter to Topping.new and add test to rspec --- pizza.rb | 6 ++++-- spec/pizza_spec.rb | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pizza.rb b/pizza.rb index 2203c5d..f50096d 100644 --- a/pizza.rb +++ b/pizza.rb @@ -2,8 +2,10 @@ class Pizza end class Topping - attr_accessor :name - def initialize(name) + attr_accessor :name, :vegetarian + + def initialize(name, vegetarian: false) @name = name + @vegetarian = vegetarian end end diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index e60e738..f3bda1f 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -19,8 +19,10 @@ it "sets whether or not the topping is vegetarian" do topping = Topping.new 'bell peppers', vegetarian: true + topping2 = Topping.new 'pepperoni' expect(topping.vegetarian).to eq(true) + expect(topping2.vegetarian).to eq(false) end end end From b2ed92a93ed5fd3d9c9a668f8eca8b681de1a665 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:50:04 -0500 Subject: [PATCH 05/13] Add failing test to Pizza.initialize --- spec/pizza_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index f3bda1f..2613fb2 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -4,6 +4,18 @@ 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 describe Topping do From 109d9d5551acd9055846308419e703386f023a6c Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:53:17 -0500 Subject: [PATCH 06/13] Add Pizza.initialize method to pizza.rb --- pizza.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pizza.rb b/pizza.rb index f50096d..18d89bd 100644 --- a/pizza.rb +++ b/pizza.rb @@ -1,4 +1,9 @@ class Pizza + attr_accessor :toppings + + def initialize(toppings) + @toppings = toppings + end end class Topping From 9d2225c44b290b2a33d82ec4cc164cd09d252e0b Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 14:56:22 -0500 Subject: [PATCH 07/13] Add failing test to Pizza.initialize. Tests if default topping is cheese --- spec/pizza_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2613fb2..8e13b81 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -15,6 +15,12 @@ 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 1244aa7decbd97af01fb90a7991e86e755f598b6 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 15:15:23 -0500 Subject: [PATCH 08/13] Set cheese as default topping --- pizza.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pizza.rb b/pizza.rb index 18d89bd..9e34b03 100644 --- a/pizza.rb +++ b/pizza.rb @@ -1,8 +1,8 @@ class Pizza attr_accessor :toppings - def initialize(toppings) - @toppings = toppings + def initialize(toppings=[Topping.new('cheese', vegetarian: true)]) + @toppings = toppings end end From 7c42b83dadb79dad8e66e0322576cd7b19c71e40 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 15:16:24 -0500 Subject: [PATCH 09/13] Remove pry_debugger from spec --- spec/pizza_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 8e13b81..2140f75 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -1,4 +1,5 @@ require './pizza' +#require 'pry-debugger' describe Pizza do it "exists" do From 5d0281ac785627428a6fb7edb3a79878d25e14fe Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 15:38:40 -0500 Subject: [PATCH 10/13] Add vegetarian? test to spec and made it pass in pizza.rb --- pizza.rb | 4 ++++ spec/pizza_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pizza.rb b/pizza.rb index 9e34b03..4bb50aa 100644 --- a/pizza.rb +++ b/pizza.rb @@ -4,6 +4,10 @@ class Pizza def initialize(toppings=[Topping.new('cheese', vegetarian: true)]) @toppings = toppings end + + def vegetarian? + @toppings.all? { |topping| topping.vegetarian } + end end class Topping diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2140f75..a565ee4 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -23,6 +23,20 @@ expect(pizza.toppings.first.name).to eq('cheese') end end + + describe '.vegetarian?' do + it 'checks if a pizza is vegetarian' do + toppings = [ + Topping.new('bell peppers', vegetarian: true), + Topping.new('pepperoni') + ] + pizza1 = Pizza.new(toppings) + pizza2 = Pizza.new + + expect(pizza1.vegetarian?).to eq(false) + expect(pizza2.vegetarian?).to eq(true) + end + end end describe Topping do From 29cd95337249661e93b9e79d6b4b459c1e3c9558 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 15:47:37 -0500 Subject: [PATCH 11/13] Add failing test for Pizza.add_topping --- spec/pizza_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index a565ee4..2acfd38 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -37,6 +37,22 @@ expect(pizza2.vegetarian?).to eq(true) end end + + describe '.add_topping' do + it 'adds topping to existing pizza' do + toppings = [ + Topping.new('bell peppers', vegetarian: true), + Topping.new('pepperoni') + ] + pizza = Pizza.new(toppings) + + #Adds another pizza topping + pizza.add_topping(Topping.new('mushrooms', vegetarian: true)) + + expect(pizza.toppings.size).to eq(3) + expect(pizza.toppings.last.name).to eq('mushrooms') + end + end end describe Topping do From 690a51dfe65b2cf9a2fe56496365b5fefc006d22 Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 15:48:57 -0500 Subject: [PATCH 12/13] Add Pizza.add_topping method --- pizza.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pizza.rb b/pizza.rb index 4bb50aa..ed04e40 100644 --- a/pizza.rb +++ b/pizza.rb @@ -8,6 +8,10 @@ def initialize(toppings=[Topping.new('cheese', vegetarian: true)]) def vegetarian? @toppings.all? { |topping| topping.vegetarian } end + + def add_topping(topping) + @toppings << topping + end end class Topping From 97281cf3d9747abd597776ad52f6690a8a27635e Mon Sep 17 00:00:00 2001 From: Zachary Spector Date: Wed, 23 Apr 2014 16:40:15 -0500 Subject: [PATCH 13/13] Refactor variables with let method --- spec/pizza_spec.rb | 48 ++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2acfd38..a972814 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -7,31 +7,34 @@ end describe '.initialize' do - it 'records all of the toppings' do - toppings = [ + let(:toppings) {[ Topping.new('mushrooms', vegetarian: true), Topping.new('pepperoni') - ] - pizza = Pizza.new(toppings) + ]} + let(:pizza1) { Pizza.new(toppings) } + + let(:pizza2) { Pizza.new } + + it 'records all of the toppings' do - expect(pizza.toppings).to eq(toppings) + expect(pizza1.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') + expect(pizza2.toppings.size).to eq(1) + expect(pizza2.toppings.first.name).to eq('cheese') end end describe '.vegetarian?' do - it 'checks if a pizza is vegetarian' do - toppings = [ + let(:toppings) {[ Topping.new('bell peppers', vegetarian: true), Topping.new('pepperoni') - ] - pizza1 = Pizza.new(toppings) - pizza2 = Pizza.new + ]} + let(:pizza1) { Pizza.new(toppings) } + let(:pizza2) { Pizza.new } + + it 'checks if a pizza is vegetarian' do expect(pizza1.vegetarian?).to eq(false) expect(pizza2.vegetarian?).to eq(true) @@ -39,12 +42,14 @@ end describe '.add_topping' do - it 'adds topping to existing pizza' do - toppings = [ + let(:topping) {[ Topping.new('bell peppers', vegetarian: true), Topping.new('pepperoni') - ] - pizza = Pizza.new(toppings) + ]} + + let(:pizza) {Pizza.new(topping)} + + it 'adds topping to existing pizza' do #Adds another pizza topping pizza.add_topping(Topping.new('mushrooms', vegetarian: true)) @@ -60,17 +65,18 @@ expect(Topping).to be_a(Class) end describe '.initialize' do + let(:topping) { Topping.new('olives') } it "sets the name of the topping" do - topping = Topping.new('olives') expect(topping.name).to eq('olives') end + let(:topping1) { Topping.new('bell peppers', vegetarian: true) } + let(:topping2) { Topping.new('pepperoni') } + it "sets whether or not the topping is vegetarian" do - topping = Topping.new 'bell peppers', vegetarian: true - topping2 = Topping.new 'pepperoni' - expect(topping.vegetarian).to eq(true) + expect(topping1.vegetarian).to eq(true) expect(topping2.vegetarian).to eq(false) end end