96 lines
3.0 KiB
Ruby
96 lines
3.0 KiB
Ruby
# Nathan Hinton 19 Dec 2025
|
|
#
|
|
# Defines tests for the planets
|
|
|
|
|
|
require './planet'
|
|
require './building'
|
|
|
|
RSpec.describe Planet do
|
|
describe '#initialize' do
|
|
it 'Starts in a position' do
|
|
expect(Planet.new().instance_variable_get(:@position)).to eq [0, 0, 0]
|
|
end
|
|
|
|
it 'Creates a buildings list' do
|
|
expect(Planet.new().instance_variable_get(:@buildings)).to eq []
|
|
end
|
|
end
|
|
|
|
|
|
describe '#delete' do
|
|
end
|
|
|
|
|
|
describe '#building_build' do
|
|
it 'Adds a building to the planet building list' do
|
|
planet = Planet.new()
|
|
test_building = Building.new(Player.new(), planet)
|
|
expect{planet.building_build(test_building)}.to_not raise_error
|
|
expect(planet.instance_variable_get(:@buildings)).to eq [test_building]
|
|
end
|
|
end
|
|
|
|
|
|
describe '#building_destroy' do
|
|
it 'removes a building from the list' do
|
|
planet = Planet.new()
|
|
test_building = Building.new(Player.new(), planet)
|
|
planet.building_build(test_building)
|
|
expect(planet.instance_variable_get(:@buildings)).to eq [test_building]
|
|
expect(planet.building_destroy(test_building)).to eq test_building
|
|
expect(planet.instance_variable_get(:@buildings)).to eq []
|
|
end
|
|
|
|
it 'raises an error if the building can not be removed' do
|
|
planet = Planet.new()
|
|
test_building = Building.new(Player.new(), planet)
|
|
planet.building_build(test_building)
|
|
test_building2 = Building.new(Player.new(), planet)
|
|
expect{planet.building_destroy(test_building2)}.to raise_error PlanetError, 'Building not found to delete'
|
|
end
|
|
end
|
|
|
|
|
|
describe '#capture' do
|
|
it 'sets the planet owner' do
|
|
planet = Planet.new()
|
|
player = Player.new(name: 'test0')
|
|
expect(planet.capture(player)).to be player
|
|
expect(planet.instance_variable_get(:@owner)).to be player
|
|
end
|
|
|
|
it 'removes all buildings from the planet' do
|
|
planet = Planet.new()
|
|
player = Player.new(name: 'test0')
|
|
planet.capture(player)
|
|
test_building = Building.new(player, planet)
|
|
planet.building_build(test_building)
|
|
|
|
player1 = Player.new(name: 'test1')
|
|
expect(planet.instance_variable_get(:@buildings)).to eq [test_building]
|
|
expect(planet.capture(player1)).to be player1
|
|
expect(planet.instance_variable_get(:@owner)).to be player1
|
|
expect(planet.instance_variable_get(:@buildings)).to eq []
|
|
end
|
|
end
|
|
|
|
|
|
describe '#tick' do
|
|
it 'Sends the tick event to all buildings on the planet' do
|
|
planet = Planet.new()
|
|
test_building1 = Building.new(Player.new(), planet)
|
|
test_building2 = Building.new(Player.new(), planet)
|
|
test_building3 = Building.new(Player.new(), planet)
|
|
planet.building_build(test_building1)
|
|
planet.building_build(test_building2)
|
|
planet.building_build(test_building3)
|
|
|
|
expect(test_building1).to receive(:tick)
|
|
expect(test_building2).to receive(:tick)
|
|
expect(test_building3).to receive(:tick)
|
|
expect(planet.tick).to eq planet.instance_variable_get(:@buildings)
|
|
end
|
|
end
|
|
end
|