diff --git a/pizza.rb b/pizza.rb index 49ff37a..89ec590 100644 --- a/pizza.rb +++ b/pizza.rb @@ -1,5 +1,38 @@ class Pizza + + attr_accessor :toppings, :delivery_time + + def initialize(toppings = [Topping.new('cheese', vegetarian: true)]) + @toppings = toppings + end + + def vegetarian? + toppings.all? {|topping| topping.vegetarian == true} + end + + def add_topping(new_toppings) + new_toppings.each {|topping| @toppings << topping} + end + + def deliver!(now) + @delivery_time = now + end + + def late?(now) + return true if now > @delivery_time + end + end class Topping + + 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 2f30c05..ae50c96 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -7,7 +7,152 @@ end describe Topping do - it "exists" do - expect(Topping).to be_a(Class) + + it "exists" do + 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(:vegetarian_topping) { Topping.new 'olives', vegetarian: true } + it "sets wether or not the topping is vegetarian" do + # topping = Topping.new 'olives', vegetarian: true + + expect(vegetarian_topping.vegetarian).to eq(true) + end + end +end + +describe Pizza do + + describe '.initialize' do + + it 'recoreds 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 + + 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 + + describe "vegetarian?" do + + context 'when there are all non vegeterian toppings' do + + let(:vegeterian_toppings) {[ + Topping.new('mushrooms', vegetarian: true), + Topping.new('olives', vegetarian: true) + ]} + + let(:vegeterian_pizza) {Pizza.new(vegeterian_toppings)} + + it 'returns true' do + expect(vegeterian_pizza.vegetarian?).to eq(true) + end + end + + context 'when there is a non vegeterian topping' do + + let(:non_vegetarian_toppings) {[ + Topping.new('pepperoni'), + Topping.new('mushrooms', vegetarian: true), + Topping.new('bell peppers', vegetarian: true ) + ]} + + let(:non_vegetarian_pizza) { Pizza.new(non_vegetarian_toppings)} + + it 'returns false' do + expect(non_vegetarian_pizza.vegetarian?).to eq(false) + end + end + end + + describe "add_topping" do + + context 'when a new topping is added' do + + let(:toppings) {[ + Topping.new('mushrooms', vegetarian: true), + ]} + + let(:topping) {[Topping.new('pepperoni')]} + + let(:pizza) {Pizza.new(toppings)} + + it 'increases toppings count by 1' do + pizza.add_topping(topping) + + expect(pizza.toppings.count).to eq(2) + end + end + + context 'when multiple toppings are added' do + + let(:new_toppings) {[ + Topping.new('mushrooms', vegetarian: true), + Topping.new('pepperoni') + ]} + + let(:new_topping) {[Topping.new('bacon')]} + + let(:new_pizza) {Pizza.new(new_topping)} + + it 'increases toppings count by count of toppings passed' do + new_pizza.add_topping(new_toppings) + + expect(new_pizza.toppings.count).to eq(3) + end + end + end + + describe "deliver!" do + + context 'when a new delivery is called' do + + let(:pizza) {Pizza.new} + let(:time) {Time.now + 30 * 60} + + it 'creates a delivery time' do + pizza.deliver!(time) + + expect(pizza.delivery_time).to eql(time) + end + end + end + + describe "late?" do + + context 'when a delivery is late' do + + let(:pizza) { Pizza.new } + let(:time) { Time.now + 30 * 60 } + let(:future_time) { Time.now + 30 * 70 } + + it 'returns true' do + pizza.deliver!(time) + + expect(pizza.late?(future_time)).to eq(true) + end + end end end