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
37 changes: 37 additions & 0 deletions pizza.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
class Pizza

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

attr_accessor :name, :vegetarian

def initialize(name, vegetarian:false)
@name = name
@vegetarian = vegetarian
end

end
95 changes: 95 additions & 0 deletions spec/pizza_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,108 @@
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

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

# -- 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

# -- BEGINNING OF TOPPING CLASS TESTS

describe Topping do
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

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