Files
space_game_server/game/spec/ship_spec.rb
T

96 lines
2.7 KiB
Ruby

# Nathan Hinton 2 Oct 2025
# Test file for the ship object
require './ship'
RSpec.describe Ship do
let(:initial_x) { 0 }
let(:initial_y) { 0 }
subject(:ship) { Ship.new(initial_x, initial_y) }
describe '#initialize' do
it 'stores the given position' do
expect(ship.instance_variable_get(:@position)).to eq([initial_x, initial_y])
end
it 'sets the speed to 0.1' do
expect(ship.instance_variable_get(:@speed)).to eq(0.1)
end
it 'does not set @moving until a direction is chosen' do
expect(ship.instance_variable_get(:@moving)).to be_nil
end
end
describe '#tick' do
it 'does nothing (but does not raise an exception)' do
expect { ship.tick }.not_to raise_error
end
end
describe '#move' do
context 'when coordinates are valid' do
it 'accepts adjacent moves whose sum is even' do
# adjacent positions that satisfy the rules
expect { ship.move(1, 1) }.not_to raise_error # sum = 2
expect { ship.move(2, 0) }.not_to raise_error # sum = 2
expect { ship.move(0, 2) }.not_to raise_error # sum = 2
expect { ship.move(-1, -1) }.not_to raise_error # sum = -2
end
end
context 'when the x + y sum is odd' do
it 'raises an ArgumentError' do
expect { ship.move(0, 1) }.to raise_error(
ArgumentError,
/must add to be even/
)
end
end
context 'when the target is not adjacent' do
it 'raises an ArgumentError' do
# distance > 1: difference of sums != 2
expect { ship.move(3, 3) }.to raise_error(
ArgumentError,
/must be to an adjcent space/
)
expect { ship.move(0, 0) }.to raise_error(
ArgumentError,
/must be to an adjcent space/
)
end
end
end
describe '#move_direction' do
it 'accepts direction values between 0 and 5' do
(0..5).each do |direction|
expect { ship.move_direction(direction) }.not_to raise_error
# verify the @moving vector matches the spec
expected = case direction
when 0 then [2, 0]
when 1 then [1, 1]
when 2 then [-1, 1]
when 3 then [-2, 0]
when 4 then [-1, -1]
when 5 then [1, -1]
end
expect(ship.instance_variable_get(:@moving)).to eq(expected)
end
end
it 'raises an ArgumentError for invalid directions' do
expect { ship.move_direction(-1) }.to raise_error(
ArgumentError,
/must be between \(0..5\)/
)
expect { ship.move_direction(6) }.to raise_error(
ArgumentError,
/must be between \(0..5\)/
)
end
end
end