From 306de48faff150b72cefef8494853b61e44748c0 Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Sun, 24 Apr 2022 17:06:58 +0100 Subject: [PATCH 1/5] First user story - land a plane at the airport --- lib/airport.rb | 9 +++++++++ lib/plane.rb | 2 ++ spec/airport_spec.rb | 8 ++++++++ spec/plane_spec.rb | 5 +++++ 4 files changed, 24 insertions(+) create mode 100644 lib/airport.rb create mode 100644 lib/plane.rb create mode 100644 spec/airport_spec.rb create mode 100644 spec/plane_spec.rb diff --git a/lib/airport.rb b/lib/airport.rb new file mode 100644 index 0000000000..8630fc7c8e --- /dev/null +++ b/lib/airport.rb @@ -0,0 +1,9 @@ +require_relative 'plane' + +class Airport + attr_reader :stored_planes + + def land(plane) + @stored_planes = plane + end +end diff --git a/lib/plane.rb b/lib/plane.rb new file mode 100644 index 0000000000..fa2a84f30c --- /dev/null +++ b/lib/plane.rb @@ -0,0 +1,2 @@ +class Plane +end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb new file mode 100644 index 0000000000..68f4c821a2 --- /dev/null +++ b/spec/airport_spec.rb @@ -0,0 +1,8 @@ +require 'airport' + +describe Airport do + it 'Can land a plane at the airport' do + plane = Plane.new + expect(subject.land(plane)).to eq(plane) + end +end diff --git a/spec/plane_spec.rb b/spec/plane_spec.rb new file mode 100644 index 0000000000..f8dd714a06 --- /dev/null +++ b/spec/plane_spec.rb @@ -0,0 +1,5 @@ +require 'plane' + +describe Plane do + +end From 9def9468625df7a830701f908641ba03acc8fe01 Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Sun, 24 Apr 2022 17:34:48 +0100 Subject: [PATCH 2/5] Second user story, plane to take off from airport --- lib/airport.rb | 11 ++++++++++- spec/airport_spec.rb | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/airport.rb b/lib/airport.rb index 8630fc7c8e..6388db654c 100644 --- a/lib/airport.rb +++ b/lib/airport.rb @@ -3,7 +3,16 @@ class Airport attr_reader :stored_planes + def initialize + @stored_planes = [] + end + def land(plane) - @stored_planes = plane + @stored_planes << plane end + + def depart(plane) + @stored_planes.delete(plane) + end + end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb index 68f4c821a2..589db2d848 100644 --- a/spec/airport_spec.rb +++ b/spec/airport_spec.rb @@ -3,6 +3,13 @@ describe Airport do it 'Can land a plane at the airport' do plane = Plane.new - expect(subject.land(plane)).to eq(plane) + expect(subject.land(plane)).to eq([plane]) + end + + it 'Can instruct a stored plane to take off from the airport' do + plane = Plane.new + subject.land(plane) + subject.depart(plane) + expect(subject.stored_planes).to eq [] end end From 25b4e973d08d652177c99017575133e7f2425276 Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Sun, 24 Apr 2022 17:52:21 +0100 Subject: [PATCH 3/5] User story 3 - no landing when the airport is full --- lib/airport.rb | 13 +++++++++++-- spec/airport_spec.rb | 15 +++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/airport.rb b/lib/airport.rb index 6388db654c..f78fa3a786 100644 --- a/lib/airport.rb +++ b/lib/airport.rb @@ -1,13 +1,16 @@ require_relative 'plane' class Airport - attr_reader :stored_planes + attr_reader :stored_planes, :capacity + CAPACITY = 5 - def initialize + def initialize @stored_planes = [] + @capacity = CAPACITY end def land(plane) + fail 'Airport full!' if full? @stored_planes << plane end @@ -15,4 +18,10 @@ def depart(plane) @stored_planes.delete(plane) end + private + + def full? + @stored_planes.length >= @capacity + end + end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb index 589db2d848..3df60d2605 100644 --- a/spec/airport_spec.rb +++ b/spec/airport_spec.rb @@ -1,11 +1,18 @@ require 'airport' describe Airport do - it 'Can land a plane at the airport' do - plane = Plane.new - expect(subject.land(plane)).to eq([plane]) - end + describe '#land' do + it 'Can do so' do + plane = Plane.new + expect(subject.land(plane)).to eq([plane]) + end + it 'Will not do so if the airport is full' do + Airport::CAPACITY.times { subject.land(Plane.new) } + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + end + it 'Can instruct a stored plane to take off from the airport' do plane = Plane.new subject.land(plane) From 778872de6f9779c5817771d199cb5d4186ba02ad Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Sun, 24 Apr 2022 18:05:52 +0100 Subject: [PATCH 4/5] Fourth user story - overrideable default capacity (5) --- lib/airport.rb | 8 +++++++- spec/airport_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/airport.rb b/lib/airport.rb index f78fa3a786..bdfbcb7d9d 100644 --- a/lib/airport.rb +++ b/lib/airport.rb @@ -1,7 +1,7 @@ require_relative 'plane' class Airport - attr_reader :stored_planes, :capacity + attr_reader :stored_planes CAPACITY = 5 def initialize @@ -18,8 +18,14 @@ def depart(plane) @stored_planes.delete(plane) end + def adjust_capacity(number) + @capacity = number + end + private + attr_accessor :capacity + def full? @stored_planes.length >= @capacity end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb index 3df60d2605..cb5e3f1a90 100644 --- a/spec/airport_spec.rb +++ b/spec/airport_spec.rb @@ -19,4 +19,16 @@ subject.depart(plane) expect(subject.stored_planes).to eq [] end + + it 'can have an airport capacity that can be overwritten as appropriate' do + subject.adjust_capacity(1) + subject.land(Plane.new) + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + + it 'has a default capacity' do + Airport::CAPACITY.times { subject.land(Plane.new) } + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + end From 0cd734733043b1f55d691655409a45f2f6e78ba7 Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Mon, 25 Apr 2022 09:58:27 +0100 Subject: [PATCH 5/5] all user stories, still can't rspec stormy weather --- lib/airport.rb | 15 ++++++++++++++- spec/airport_spec.rb | 33 +++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/airport.rb b/lib/airport.rb index bdfbcb7d9d..30fd6893aa 100644 --- a/lib/airport.rb +++ b/lib/airport.rb @@ -1,20 +1,24 @@ require_relative 'plane' class Airport - attr_reader :stored_planes + attr_reader :stored_planes, :weather CAPACITY = 5 def initialize @stored_planes = [] @capacity = CAPACITY + @weather = self.set_weather end def land(plane) fail 'Airport full!' if full? + fail 'Cannot land - stormy weather!' if @weather == 'Stormy' @stored_planes << plane end def depart(plane) + fail 'No planes' if empty? + fail 'Plane not in airport' if !@stored_planes.include?(plane) @stored_planes.delete(plane) end @@ -22,6 +26,11 @@ def adjust_capacity(number) @capacity = number end + def set_weather + die = rand(100) + die > 100 ? 'Stormy' : 'Sunny' + end + private attr_accessor :capacity @@ -30,4 +39,8 @@ def full? @stored_planes.length >= @capacity end + def empty? + @stored_planes == [] + end + end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb index cb5e3f1a90..55c3f82a87 100644 --- a/spec/airport_spec.rb +++ b/spec/airport_spec.rb @@ -1,6 +1,7 @@ require 'airport' describe Airport do + describe '#land' do it 'Can do so' do plane = Plane.new @@ -11,14 +12,26 @@ Airport::CAPACITY.times { subject.land(Plane.new) } expect { subject.land(Plane.new) }.to raise_error 'Airport full!' end + end - it 'Can instruct a stored plane to take off from the airport' do - plane = Plane.new - subject.land(plane) - subject.depart(plane) - expect(subject.stored_planes).to eq [] - end + describe '#depart' do + it 'Can instruct a stored plane to take off from the airport' do + plane = Plane.new + subject.land(plane) + subject.depart(plane) + expect(subject.stored_planes).to eq [] + end + + it 'Will not do so if there are no planes in the airport' do + expect { subject.depart(Plane.new) }.to raise_error 'No planes' + end + + it 'Will not take off if the plane is not in the airport' do + subject.land(Plane.new) + expect{subject.depart(Plane.new)}.to raise_error 'Plane not in airport' + end + end it 'can have an airport capacity that can be overwritten as appropriate' do subject.adjust_capacity(1) @@ -31,4 +44,12 @@ expect { subject.land(Plane.new) }.to raise_error 'Airport full!' end + describe 'initialize' do + it 'will be stormy weather and the plane will not land' do + allow(subject).to receive(:set_weather).and_return('Stormy') + expect { subject.land(Plane.new) }.to raise_error 'Cannot land - stormy weather!' + end + end + + end