Skip to content
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
33 changes: 33 additions & 0 deletions pizza.rb
Original file line number Diff line number Diff line change
@@ -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


149 changes: 147 additions & 2 deletions spec/pizza_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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