Files
space_game_server/core/spec/game_spec.rb
T

88 lines
2.4 KiB
Ruby

# Nathan Hinton 19 Dec 2025
require './game'
require './player'
RSpec.describe Game do
describe '#initialize' do
it 'Creates an empty list of players' do
expect(Game.new().players).to eq []
end
it 'sets the width when passed in' do
expect(Game.new(width: 10).width).to eq 10
end
it 'sets the height when passed in' do
expect(Game.new(height: 11).height).to eq 11
end
it 'creates the specified number of planets' do
expect(Game.new(planets: 5).planets.length).to eq 5
end
end
describe '#add_player' do
it 'adds a player to the game' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
expect(game.players).to eq [player]
end
it 'fails when there are no planets available for the player' do
game = Game.new(planets: 0)
player = Player.new()
expect{game.add_player(player)}.to raise_error GameError, "No available planets to add player to"
expect(game.players).to eq []
end
end
describe '#remove_player' do
it 'removes a player from the game' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
expect(game.players).to eq [player]
expect(game.remove_player(player)).to eq player
expect(game.players).to eq []
end
it 'returns an error when the player is not in the game' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
player2 = Player.new()
expect{game.remove_player(player2)}.to raise_error GameError, 'Player not found to remove!'
end
end
describe '#check_id' do
it 'returns true when the player id is valid for a player' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
expect(game.check_id(player.get_id)).to eq player
end
it 'returns false when the player id does not match a player' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
expect{game.check_id('a')}.to raise_error GameError, 'Invalid player id!'
end
end
describe '#get_captured_planets' do
it 'returns the captured planets of the player' do
game = Game.new(planets: 1)
player = Player.new()
game.add_player(player)
expect(game.get_captured_planets(player.get_id)).to eq game.instance_variable_get(:@planets)
end
end
end