Files
space_game_server/game/spec/ship_spec.rb
T

91 lines
2.4 KiB
Ruby

# frozen_string_literal: true
require './ship'
require './player'
RSpec.describe Ship do
let(:ship) { Ship.new(0, 0, Player.new('test player 01')) }
describe '#initialize' do
it 'stores the given position' do
expect(ship.instance_variable_get(:@position)).to eq [0, 0]
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 (no exception) when called' do
expect { ship.tick }.not_to raise_error
end
end
describe '#move' do
context 'when coordinates are valid (even sum, adjacent)' do
it 'accepts positions that satisfy the rules' do
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 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
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 0..5 and sets @moving correctly' do
mapping = {
0 => [2, 0],
1 => [1, 1],
2 => [-1, 1],
3 => [-2, 0],
4 => [-1, -1],
5 => [1, -1]
}
mapping.each do |dir, expected|
expect { ship.move_direction(dir) }.not_to raise_error
expect(ship.instance_variable_get(:@moving)).to eq expected
end
end
it 'raises 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