160 lines
5.0 KiB
Ruby
160 lines
5.0 KiB
Ruby
# frozen_string_literal: true
|
|
# Nathan Hinton 2 Oct 2025
|
|
|
|
# This is for testing the API that the clients can use.
|
|
|
|
|
|
# Most of the api is here as it relates to interacting with the universe
|
|
require './universe'
|
|
require './player'
|
|
require './position'
|
|
require './ship'
|
|
|
|
RSpec.describe 'API' do
|
|
|
|
# Universe setup
|
|
let(:name) { 'Test Universe' }
|
|
let(:universe_size) { 3 }
|
|
let(:seed) { 12345 }
|
|
|
|
subject(:universe) do
|
|
Universe.new(
|
|
name,
|
|
universe_size: universe_size,
|
|
seed: seed,
|
|
create_chances: { planet: 1 } # force a planet at every valid spot
|
|
)
|
|
end
|
|
|
|
before(:each) do
|
|
universe.create_universe
|
|
end
|
|
|
|
|
|
# Player setup
|
|
let(:player_1) { Player.new('test player 01') }
|
|
let(:player_2) { Player.new('test player 02') }
|
|
|
|
# Positions setup
|
|
let(:center_position) { Position.new(5, 3) }
|
|
describe 'UNIVERSE API' do
|
|
describe '#get_position' do
|
|
it 'returns a position in the universe' do
|
|
expect(universe.get_position(player_1, Position.new(0, 0))).to be nil
|
|
expect(universe.get_position(player_1, Position.new(3, 5))).to be_a(Planet)
|
|
expect(universe.get_position(player_1, center_position)).to be_a(Star)
|
|
end
|
|
end # describe Universe#get_position
|
|
end # describe 'UNIVERSE API'
|
|
|
|
|
|
describe 'SHIPS API' do
|
|
describe '@create_ship' do
|
|
it 'returns false when the player is invalid' do
|
|
expect(universe.create_ship(player_1, Position.new(3, 5))).to eq false
|
|
end
|
|
end # describe '@create_ship'
|
|
|
|
describe '@move_ship' do
|
|
it 'returns false when the player is invalid' do
|
|
expect(universe.move_ship(player_1, Ship.new(2, 4, player_1), Position.new(3, 5))).to eq false
|
|
end
|
|
end # describe '@move_ship'
|
|
|
|
describe '@remove_ship' do
|
|
it 'returns false when the player is invalid' do
|
|
expect(universe.remove_ship(player_1, Position.new(3, 5))).to eq false
|
|
end
|
|
end # describe '@remove_ship'
|
|
end # describe 'SHIPS API'
|
|
|
|
|
|
|
|
describe 'PLANETS API' do
|
|
describe '@remove_planet' do
|
|
end # describe '@remove_planet'
|
|
end # describe 'PLANETS API'
|
|
|
|
|
|
describe 'PLAYER API' do
|
|
describe '#add_player' do
|
|
it 'adds with free planets' do
|
|
expect(universe.add_player(player_1)).to be(true)
|
|
expect(universe.instance_variable_get(:@player_list)).to eq [player_1]
|
|
end
|
|
|
|
it 'fails to add when all planets are occopuied' do
|
|
# Make all the planets owned by player_2
|
|
universe.planets.each do |planet|
|
|
planet.change_owner(player_2)
|
|
end
|
|
expect(universe.add_player(player_1)).to be(false)
|
|
expect(universe.instance_variable_get(:@player_list)).to eq []
|
|
end
|
|
|
|
it 'raises an error if a planet can not be taken' do
|
|
# This is an exceptional case. This is actually proper behavior when
|
|
# not trying to deal out planets.
|
|
end
|
|
end # describe 'add_player'
|
|
|
|
describe '#remove_player' do
|
|
it 'can remove an existing player' do
|
|
universe.add_player(player_1)
|
|
expect(universe.remove_player(player_1)).to eq true
|
|
expect(universe.remove_player(player_2)).to eq true
|
|
expect(universe.instance_variable_get(:@player_list)).to eq []
|
|
end
|
|
|
|
it 'can remove a non existing player' do
|
|
expect(universe.remove_player(player_2)).to eq true
|
|
end
|
|
end # describe 'remove_player'
|
|
|
|
describe '#validate_player' do
|
|
it 'returns true when a player is valid' do
|
|
universe.add_player(player_1)
|
|
expect(universe.validate_player(player_1)).to eq true
|
|
end
|
|
|
|
it 'returns false when the player is not in the player list' do
|
|
expect(universe.validate_player(player_2)).to eq false
|
|
end
|
|
end # describe 'validate_player'
|
|
|
|
|
|
describe '#get_player_ships' do
|
|
it 'lists all ships owned by a player' do
|
|
universe.add_player(player_1)
|
|
universe.add_player(player_2)
|
|
expect(universe.get_player_ships(player_1)).to eq []
|
|
ship_1 = Ship.new(2, 4, player_1)
|
|
ship_2 = Ship.new(2, 4, player_2)
|
|
ship_3 = Ship.new(3, 5, player_1)
|
|
universe.instance_variable_set(:@ship_list, [ship_1, ship_2, ship_3])
|
|
expect(universe.get_player_ships(player_1)).to eq [ship_1, ship_3]
|
|
expect(universe.get_player_ships(player_2)).to eq [ship_2]
|
|
end
|
|
|
|
it 'returns an empty list when a player that is not registered gets ships' do
|
|
ship_2 = Ship.new(2, 4, player_2)
|
|
universe.instance_variable_set(:@ship_list, [ship_2])
|
|
expect(universe.get_player_ships(player_2)).to eq []
|
|
end
|
|
end # describe 'get_player_ships'
|
|
|
|
describe '#get_player_planets' do
|
|
it 'returns the planets owned by a player' do
|
|
universe.add_player(player_1)
|
|
expect(universe.get_player_planets(player_2)).to eq []
|
|
expect(universe.get_player_planets(player_1).length).to eq 1
|
|
end
|
|
|
|
it 'returns an empty list for a unregistered player' do
|
|
expect(universe.get_player_planets(player_2)).to eq []
|
|
end
|
|
|
|
end # describe 'get_player_planets'
|
|
end # describe 'PLAYER API'
|
|
end
|