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

Archie Martin Airport Challenge #2507

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
61 changes: 61 additions & 0 deletions lib/airport.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Airport

DEFAULT_CAPACITY = 10

attr_reader :capacity
attr_reader :plane
attr_reader :weather

def initialize(capacity=DEFAULT_CAPACITY)
@capacity = capacity
@hanger = []
weather = rand(20) + 1
if weather >= 15
@weather = 'STORMY'
else
@weather = 'GOOD'
end
end


def land(plane)
fail 'Landing prevented; hanger is full' if full?
fail 'Landing denied; storms overhead' unless @weather == 'GOOD'
@hanger << plane
end

def take_off(plane)
fail 'No planes ready to take off' if empty?
fail 'Take off denied; storms incoming' if @weather == 'STORMY'
@hanger.delete(plane)
end

def check_hanger
@hanger
end

def check_weather
@weather
end

private


def full?
@hanger.count >= @capacity ? true : false
end

def empty?
@hanger.empty?
end

def set_weather
self.instance_variable_set(:@weather, 'GOOD')
end

end

class Plane


end
103 changes: 103 additions & 0 deletions spec/airport_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'airport.rb'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I appreciate your use of Single Responsibility Principle. It make the code super readable with names that makes sense.


describe Airport do

it 'sets user capacity when creating an airport' do
port = Airport.new(50)
expect(port.capacity).to eq 50
end

it 'capacity is equal to Default Capacity if no argument is given' do
expect(subject.capacity).to eq Airport::DEFAULT_CAPACITY
end


describe '#full?' do
it 'checks if Airport is full' do
port = Airport.new
expect(port.send(:full?)).to eq false
end
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that you described the method within the class before testing it.

describe '#land' do
it 'lands the plane' do
expect(subject).to respond_to :land
end
# THIS GOT MESSY
# it 'raises an error if hanger is full' do
# airport = Airport.new
# airport.send(:set_weather)
# airport.capacity.times {airport.land(Plane.new)}
# expect {subject.land(Plane.new)}.to raise_error 'Landing prevented; hanger is full'
# end
it 'refuses a plane landing if weather is stormy' do
airport = Airport.new
plane = Plane.new
airport.send(:set_weather)
weather = airport.check_weather
if weather == 'STORMY' then expect {airport.land(plane)}.to raise_error 'Landing denied; storms overhead'
end
end
end

it { is_expected.to respond_to(:land).with(1).argument }


describe '#take_off' do
it 'instructs plane to take off' do
expect(subject).to respond_to :take_off
end
it 'takes off correct plane passed as argument' do
airport = Airport.new
plane = Plane.new
airport.send(:set_weather)
airport.land(plane)
3.times {airport.land(Plane.new)}
expect(airport.take_off(plane)).to eq plane
end
it 'raises an error when hanger is empty' do
plane = Plane.new
expect {subject.take_off(plane)}.to raise_error 'No planes ready to take off'
end

it { is_expected.to respond_to(:take_off).with(1).argument }

it 'refuses a plane take off if weather is stormy' do
airport = Airport.new
plane = Plane.new
airport.send(:set_weather)
weather = airport.check_weather
if weather == 'STORMY' then expect {airport.take_off(plane)}.to raise_error 'Take off denied; storms incoming'
end
end
end

describe '#check_hanger' do
it 'returns hanger' do
port = Airport.new
plane = Plane.new
port.send(:set_weather)
port.land(plane)
expect(port.check_hanger).to be_truthy
end
end

describe '#check_weather' do
it 'checks the weather' do
airport = Airport.new
expect(airport.check_weather).to eq('GOOD').or(eq('STORMY'))
end
end

# describe '#weather_randomizer' do
# it 'returns a number from 1..20' do
# expect(subject.send(:weather_randomizer)).to be_between(1,20)
# end
# end


end

describe Plane do

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could have used a separate new doc and spec_helper for the Plane class.

end