Cleaning up and writing tests
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require './position'
|
||||
|
||||
RSpec.describe Position do
|
||||
let(:even_pair) { [4, 6] } # 4+6 = 10 → even
|
||||
let(:odd_pair) { [3, 4] } # 3+4 = 7 → odd
|
||||
|
||||
describe 'initialisation' do
|
||||
context 'with two arguments' do
|
||||
it 'stores the coordinates when the sum is even' do
|
||||
pos = Position.new(*even_pair)
|
||||
expect(pos.x_pos).to eq(even_pair[0])
|
||||
expect(pos.y_pos).to eq(even_pair[1])
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when the sum is odd' do
|
||||
expect { Position.new(*odd_pair) }
|
||||
.to raise_error(ArgumentError, /must sum to an even number/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with three arguments' do
|
||||
it 'raises NotImplementedError' do
|
||||
expect { Position.new(1, 2, 3) }
|
||||
.to raise_error(NotImplementedError, /positioning not implimented/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with an unsupported number of arguments' do
|
||||
it 'raises ArgumentError when no arguments are supplied' do
|
||||
expect { Position.new }
|
||||
.to raise_error(ArgumentError, /requires two or three integer args/)
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when too many arguments are supplied' do
|
||||
expect { Position.new(1, 2, 3, 4) }
|
||||
.to raise_error(ArgumentError, /requires two or three integer args/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#two_d_cartesian' do
|
||||
it 'returns the original (x, y) pair' do
|
||||
pos = Position.new(*even_pair)
|
||||
expect(pos.two_d_cartesian).to eq(even_pair)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#two_d_hexagonal' do
|
||||
it 'returns an array of nil values for a, b, c' do
|
||||
pos = Position.new(*even_pair)
|
||||
expect(pos.two_d_hexagonal).to eq([nil, nil, nil])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_s' do
|
||||
it 'puts out the coordinates' do
|
||||
expect(Position.new(*even_pair).to_s).to eq('(4, 6)')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#==' do
|
||||
it 'returns false when the positions differ' do
|
||||
expect(Position.new(*even_pair) == Position.new(6, 8)).to eq(false)
|
||||
end
|
||||
|
||||
it 'returns true when the positions are the same' do
|
||||
expect(Position.new(*even_pair) == Position.new(4, 6)).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user