diff --git a/pizza.rb b/pizza.rb index 49ff37a..887318c 100644 --- a/pizza.rb +++ b/pizza.rb @@ -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 diff --git a/spec/pizza_spec.rb b/spec/pizza_spec.rb index 2f30c05..1f4b139 100644 --- a/spec/pizza_spec.rb +++ b/spec/pizza_spec.rb @@ -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