Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CS-362 Final] Test coverage for fridge simulator, vessel implementation #5

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion lib/chiller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Chiller

ROOM_TEMPERATURE = 70

attr_reader :capacity, :temperature
attr_reader :capacity, :temperature, :power

def initialize(capacity = 100)
@capacity = capacity
Expand Down
2 changes: 1 addition & 1 deletion lib/freezer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Freezer

ROOM_TEMPERATURE = 70

attr_reader :capacity, :temperature
attr_reader :capacity, :temperature, :power

def initialize(capacity = 100)
@capacity = capacity
Expand Down
14 changes: 12 additions & 2 deletions lib/vessel.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class Vessel
attr_reader :volume
attr_reader :volume, :name

def initialize(name, volume)
def initialize(name = 'FAKE', volume = 100)
@name = name
@volume = volume
@current_filling = 0
end

def empty?
@current_filling
end

def fill(vol = current_filling)
@current_filling = @volume if @current_filling + vol > @volume
@current_filling += vol
end

end
3 changes: 2 additions & 1 deletion lib/water_dispenser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def initialize(reservoir)
end

def dispense(vessel)
reservoir.drain(vessel.volume)
vol = reservoir.drain(vessel.volume)
vessel.fill(vol)
end

end
3 changes: 2 additions & 1 deletion lib/water_reservoir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ def empty?
end

def fill
current_water_volume = capacity
@current_water_volume = capacity
end

def drain(volume)
return 0 if self.current_water_volume < volume
self.current_water_volume -= volume
end

Expand Down
31 changes: 31 additions & 0 deletions spec/chiller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
require_relative '../lib/chiller'

describe 'A chiller' do
let(:chiller) { Chiller.new }

it 'has a default capacity of 100' do
expect(chiller.capacity).to eq(100)
end

it 'can turn on the power' do
chiller.turn_on
expect(chiller.power).to eq(:on)
end

it 'can turn off the power' do
chiller.turn_off
expect(chiller.power).to eq(:off)
end

it 'can add an item to the chiller' do
item = double
allow(item).to receive('volume').and_return(5)
chiller.add(item)
expect(chiller.remaining_capacity).to_not eq(chiller.capacity)
end

it 'knows the remaining capacity of the chiller' do
expect(chiller.remaining_capacity).to eq(chiller.capacity)
end

it 'can set the temperature of the chiller' do
chiller.set_level(5)
expect(chiller.temperature).to eq(70 - 5 * 5)
end

end
30 changes: 30 additions & 0 deletions spec/freezer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
require_relative '../lib/freezer'

describe 'A freezer' do
let(:freezer) { Freezer.new }

it 'has a default capacity of 100' do
expect(freezer.capacity).to eq(100)
end

it 'can turn on the power' do
freezer.turn_on
expect(freezer.power).to eq(:on)
end

it 'can turn off the power' do
freezer.turn_off
expect(freezer.power).to eq(:off)
end

it 'can add an item to the freezer' do
item = double
allow(item).to receive('volume').and_return(5)
freezer.add(item)
expect(freezer.remaining_capacity).to_not eq(freezer.capacity)
end

it 'knows the remaining capacity of the freezer' do
expect(freezer.remaining_capacity).to eq(freezer.capacity)
end

it 'can set the temperature of the freezer' do
freezer.set_level(5)
expect(freezer.temperature).to eq(70 - 5 * 10)
end
end
10 changes: 10 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
require_relative '../lib/item'

describe 'An item of food or a beverage' do
name = 'FAKE'
volume = 100
let(:item) { Item.new(name, volume) }
it 'has a name' do
expect(item.name).to eq(name)
end

it 'has a volume' do
expect(item.volume).to eq(volume)
end

end
21 changes: 21 additions & 0 deletions spec/refrigerator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
require_relative '../lib/refrigerator'

describe 'A refrigerator' do
before do
@chiller = instance_double('Chiller')
@freezer = instance_double('Freezer')
@water_dispenser = instance_double('WaterDispenser')
@water_reservoir = instance_double('WaterReservoir')
@item = double
allow(@item).to receive('volume').and_return(5)
end

let(:fridge) { Refrigerator.new(@chiller, @freezer, @water_dispenser, @water_reservoir)}

it 'exists' do
expect(fridge.chiller).to eq(@chiller)
expect(fridge.freezer).to eq(@freezer)
expect(fridge.water_dispenser).to eq(@water_dispenser)
expect(fridge.water_reservoir).to eq(@water_reservoir)
end

it 'can chill an item' do
# fridge.chill(@item)
# expect(remaining_capacity).to_not eq()
end
end
8 changes: 3 additions & 5 deletions spec/vessel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
end

it 'is initially empty' do
skip
vessel = Vessel.new('FAKE', 100)
expect(vessel).to be_empty
expect(vessel.empty?).to be_truthy
end

it 'is no longer empty when we fill it' do
skip
vessel = Vessel.new('FAKE', 100)
vessel.fill
expect(vessel).to_not be_empty
vessel.fill(5)
expect(vessel.empty?).to_not be_falsey
end
end
16 changes: 16 additions & 0 deletions spec/water_dispenser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
require_relative '../lib/water_dispenser'

describe 'A water dispenser' do

it 'has a reservoir' do
reservior = double
dispenser = WaterDispenser.new(reservior)
expect(dispenser.reservoir).to eq(reservior)
end

it 'completely empties a reservior' do
reservior = double('WaterReservior')
vessel = double('Vessel')
allow(vessel).to receive('volume').and_return(100)
allow(vessel).to receive('fill').and_return(0)
allow(reservior).to receive('drain').and_return(0)
dispenser = WaterDispenser.new(reservior)
expect(dispenser.dispense(vessel)).to eq(0)
end

end
33 changes: 33 additions & 0 deletions spec/water_reservoir_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
require_relative '../lib/water_reservoir'

describe 'A water reservoir' do
capacity = 15
initial_water_volume = 0
let(:reservoir) { WaterReservoir.new(capacity, initial_water_volume) }

it 'has a capacity' do
expect(reservoir.capacity).to eq(capacity)
end

it 'has an initial volume' do
expect(reservoir.current_water_volume).to eq(initial_water_volume)
end

it 'knows if the reservoir is not empty' do
reservoir.current_water_volume = 10
expect(reservoir.empty?).to be_falsey
end

it 'knows if the reservoir is empty' do
reservoir.current_water_volume = 0
expect(reservoir.empty?).to be_truthy
end

it 'fills the reservoir to a given capacity' do
reservoir.fill
expect(reservoir.current_water_volume).to eq(reservoir.capacity)
end

it 'drains the reservoir by a given capacity' do
volume = 5
reservoir.fill
reservoir.drain(volume)
expect(reservoir.current_water_volume).to eq(capacity - volume)
end

end