Files

87 lines
2.3 KiB
Ruby

# Nathan Hinton
# Test file for the planets
require 'game/planet'
RSpec.describe Planet do
let(:planet) {Planet.new(5, 5, 5)}
before(:all) do
Dir.mkdir('game_data') unless Dir.exist?('game_data')
end
after(:each) do
# Clean up any save file that might have been created
save_file = File.join('game_data', "#{planet.planet_id}.save")
File.delete(save_file) if File.exist?(save_file)
end
describe '#initialize' do
it 'starts with an initial position inside the game board' do
planet.instance_variable_get(:@position).each do |pos|
expect(pos < 5).to be true
end
end
end
describe '#claimable?' do
it 'is claimable when no owner' do
expect(planet.claimable?).to be true
end
it 'is claimable if owned by another player and has no buildings' do
planet.instance_variable_set(:@owner, 'nathan')
expect(planet.claimable?).to be true
end
it 'is not claimable if owned by another player and has buildings' do
planet.instance_variable_set(:@owner, 'nathan')
planet.instance_variable_set(:@buildings, ['command post'])
expect(planet.claimable?).to be false
end
end
describe '#claim' do
it 'sets the owner of the planet to a given player' do
planet.claim('nathan')
expect(planet.owner).to eq 'nathan'
end
it 'overwrites the player even if it was already set' do
planet.claim('nathan')
expect(planet.owner).to eq 'nathan'
planet.claim('leah')
expect(planet.owner).to eq 'leah'
end
end
describe '#update' do
it 'process updates for the planet' do
end
end
describe '#info_update' do
it 'returns the correct data about the planet to the player' do
data = planet.info_update
expect(data.keys).to eq(['id', 'position'])
end
end
describe '#save_to_file' do
it 'saves the planet to a file' do
expect{planet.save_to_file}.to_not raise_error
# Check that save file was created.
expect(File.exist?(File.join('game_data', "#{planet.planet_id}_planet_.save"))).to be true
end
end
describe '#load_from_save' do
it 'can load a planet from a save' do
planet.save_to_file
expect(File.exist?(File.join('game_data', "#{planet.planet_id}_planet_.save"))).to be true
expect(Planet.load_from_save(planet.planet_id)).to be_kind_of(Planet)
end
end
end