109 lines
3.3 KiB
Ruby
109 lines
3.3 KiB
Ruby
# Nathan Hinton 30 Dec 2025
|
|
# Test file for ships
|
|
|
|
require 'game/ship'
|
|
|
|
RSpec.describe Ship do
|
|
let(:ship) {Ship.new('nathan', 0, 2, 4, 'Starlight')}
|
|
describe '#initialize' do
|
|
it 'Creates a ship with a player and a position' do
|
|
expect(ship.owner).to eq 'nathan'
|
|
expect(ship.position).to eq [0, 2, 4]
|
|
end
|
|
end
|
|
|
|
describe '#validate_creation' do
|
|
it 'Fails to create a ship when the name is invalid' do
|
|
expect{Ship.new('nathan', 0, 2, 4, 'billy bob joe')}.to raise_error ShipError, "invalid ship name 'billy bob joe'"
|
|
end
|
|
|
|
it 'Fails when the player does not have the required tech' do
|
|
nathan = instance_double('Player', researched_tech: ['Light Shield', 'Basic Engine'])
|
|
expect{Ship.new(nathan, 0, 2, 4, 'Nebula Dominion')}.to raise_error ShipError, "Player needs the tech of 'Heavy Shield'"
|
|
end
|
|
|
|
it 'Fails when the player does not have the required buildings' do
|
|
nathan = instance_double('Player', researched_tech: ['Heavy Shield', 'Large Engine', 'Laser Cannons', 'Neoplated Armor'], buildings: [])
|
|
expect{Ship.new(nathan, 0, 2, 4, 'Nebula Dominion')}.to raise_error ShipError, "Player needs the building of 'Space Construction Station' to build this ship"
|
|
end
|
|
end
|
|
|
|
describe '#teleport' do
|
|
it 'Changes the coordinates of the ship immediately' do
|
|
expect(ship.position).to eq [0, 2, 4]
|
|
ship.teleport(4, 2, 0)
|
|
expect(ship.position).to eq [4, 2, 0]
|
|
end
|
|
end
|
|
|
|
describe '#move' do
|
|
it 'Sets the target of the ship movement' do
|
|
ship.move(1, 3, 5)
|
|
expect(ship.instance_variable_get(:@target)).to eq [1, 3, 5]
|
|
end
|
|
end
|
|
|
|
describe '#update' do
|
|
it 'Does not move when target is eq to position' do
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [0, 2, 4]
|
|
end
|
|
|
|
describe 'Moves a single ship at the correct speed when target is different from position' do
|
|
it 'in positive x' do
|
|
ship.move(1, 2, 4)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [1.0, 2.0, 4.0]
|
|
end
|
|
|
|
it 'in negative x' do
|
|
ship.move(-1, 2, 4)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [-1.0, 2.0, 4.0]
|
|
end
|
|
|
|
it 'in positive y' do
|
|
ship.move(0, 3, 4)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [0.0, 3.0, 4.0]
|
|
end
|
|
|
|
it 'in negative y' do
|
|
ship.move(0, 1, 4)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [0.0, 1.0, 4.0]
|
|
end
|
|
|
|
it 'in positive z' do
|
|
ship.move(0, 2, 5)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [0.0, 2.0, 5.0]
|
|
end
|
|
|
|
it 'in negative z' do
|
|
ship.move(0, 2, 3)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [0.0, 2.0, 3.0]
|
|
end
|
|
|
|
it 'in multiple dimensions' do
|
|
ship.move(-1, 3, 3)
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [-0.5773502691896258, 2.5773502691896257, 3.4226497308103743]
|
|
expect{ship.update(1)}.to_not raise_error
|
|
expect(ship.position).to eq [-1, 3, 3]
|
|
end
|
|
|
|
end
|
|
|
|
it 'Moves a group of ships at the slowest ship speed' do
|
|
end
|
|
end
|
|
|
|
describe '#save_to_file' do
|
|
end
|
|
|
|
describe '#load_from_save' do
|
|
end
|
|
end
|