Cleaning up and writing tests
This commit is contained in:
+24
-31
@@ -1,17 +1,13 @@
|
||||
# Nathan Hinton 2 Oct 2025
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Test file for the ship object
|
||||
|
||||
require './ship'
|
||||
require_relative '../ship' # <-- adjust to the actual path of ship.rb
|
||||
|
||||
RSpec.describe Ship do
|
||||
let(:initial_x) { 0 }
|
||||
let(:initial_y) { 0 }
|
||||
subject(:ship) { Ship.new(initial_x, initial_y) }
|
||||
let(:ship) { Ship.new(0, 0) }
|
||||
|
||||
describe '#initialize' do
|
||||
it 'stores the given position' do
|
||||
expect(ship.instance_variable_get(:@position)).to eq([initial_x, initial_y])
|
||||
expect(ship.instance_variable_get(:@position)).to eq([0, 0])
|
||||
end
|
||||
|
||||
it 'sets the speed to 0.1' do
|
||||
@@ -24,23 +20,22 @@ RSpec.describe Ship do
|
||||
end
|
||||
|
||||
describe '#tick' do
|
||||
it 'does nothing (but does not raise an exception)' do
|
||||
it 'does nothing (no exception) when called' do
|
||||
expect { ship.tick }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe '#move' do
|
||||
context 'when coordinates are valid' do
|
||||
it 'accepts adjacent moves whose sum is even' do
|
||||
# adjacent positions that satisfy the rules
|
||||
expect { ship.move(1, 1) }.not_to raise_error # sum = 2
|
||||
expect { ship.move(2, 0) }.not_to raise_error # sum = 2
|
||||
expect { ship.move(0, 2) }.not_to raise_error # sum = 2
|
||||
expect { ship.move(-1, -1) }.not_to raise_error # sum = -2
|
||||
context 'when coordinates are valid (even sum, adjacent)' do
|
||||
it 'accepts positions that satisfy the rules' do
|
||||
expect { ship.move(1, 1) }.not_to raise_error # sum 2
|
||||
expect { ship.move(2, 0) }.not_to raise_error # sum 2
|
||||
expect { ship.move(0, 2) }.not_to raise_error # sum 2
|
||||
expect { ship.move(-1, -1) }.not_to raise_error # sum -2
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the x + y sum is odd' do
|
||||
context 'when the sum is odd' do
|
||||
it 'raises an ArgumentError' do
|
||||
expect { ship.move(0, 1) }.to raise_error(
|
||||
ArgumentError,
|
||||
@@ -51,7 +46,6 @@ RSpec.describe Ship do
|
||||
|
||||
context 'when the target is not adjacent' do
|
||||
it 'raises an ArgumentError' do
|
||||
# distance > 1: difference of sums != 2
|
||||
expect { ship.move(3, 3) }.to raise_error(
|
||||
ArgumentError,
|
||||
/must be to an adjcent space/
|
||||
@@ -65,23 +59,22 @@ RSpec.describe Ship do
|
||||
end
|
||||
|
||||
describe '#move_direction' do
|
||||
it 'accepts direction values between 0 and 5' do
|
||||
(0..5).each do |direction|
|
||||
expect { ship.move_direction(direction) }.not_to raise_error
|
||||
# verify the @moving vector matches the spec
|
||||
expected = case direction
|
||||
when 0 then [2, 0]
|
||||
when 1 then [1, 1]
|
||||
when 2 then [-1, 1]
|
||||
when 3 then [-2, 0]
|
||||
when 4 then [-1, -1]
|
||||
when 5 then [1, -1]
|
||||
end
|
||||
it 'accepts direction values 0..5 and sets @moving correctly' do
|
||||
mapping = {
|
||||
0 => [2, 0],
|
||||
1 => [1, 1],
|
||||
2 => [-1, 1],
|
||||
3 => [-2, 0],
|
||||
4 => [-1, -1],
|
||||
5 => [1, -1]
|
||||
}
|
||||
mapping.each do |dir, expected|
|
||||
expect { ship.move_direction(dir) }.not_to raise_error
|
||||
expect(ship.instance_variable_get(:@moving)).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
it 'raises an ArgumentError for invalid directions' do
|
||||
it 'raises ArgumentError for invalid directions' do
|
||||
expect { ship.move_direction(-1) }.to raise_error(
|
||||
ArgumentError,
|
||||
/must be between \(0..5\)/
|
||||
|
||||
Reference in New Issue
Block a user