diff --git a/spec/mars_rovers_spec.rb b/spec/mars_rovers_spec.rb index 39b80da..006b672 100644 --- a/spec/mars_rovers_spec.rb +++ b/spec/mars_rovers_spec.rb @@ -4,13 +4,13 @@ it 'has a starting position of (2, 3) and a facing direction of N]' do mars_rover = MarsRover.new(Point.new(2,3, 'N')) - expect(mars_rover.point).to eq Point.new(2, 3,'N') + expect(mars_rover.point).to be_located_at Point.new(2, 3,'N') end it 'has a starting position of (1,2) and a facing direction of S' do mars_rover = MarsRover.new(Point.new(1,2, 'S')) - expect(mars_rover.point).to eq Point.new(1, 2,'S') + expect(mars_rover.point).to be_located_at Point.new(1, 2,'S') end it 'does not process unrecognised commands (e.g. command y)' @@ -20,7 +20,7 @@ mars_rover.execute_commands(%w[f]) - expect(mars_rover.point).to eq Point.new(0, 1,'N') + expect(mars_rover.point).to be_located_at Point.new(0, 1,'N') end it 'moves forwards when facing south' do @@ -28,7 +28,7 @@ mars_rover.execute_commands(%w[f]) - expect(mars_rover.point).to eq Point.new(0, 0,'S') + expect(mars_rover.point).to be_located_at Point.new(0, 0,'S') end it 'moves forwards when facing west' do @@ -36,7 +36,7 @@ mars_rover.execute_commands(%w[f]) - expect(mars_rover.point).to eq Point.new(-1, 0,'W') + expect(mars_rover.point).to be_located_at Point.new(-1, 0,'W') end it 'moves forwards when facing east' do @@ -44,7 +44,7 @@ mars_rover.execute_commands(%w[f]) - expect(mars_rover.point).to eq Point.new(1, 0,'E') + expect(mars_rover.point).to be_located_at Point.new(1, 0,'E') end it 'moves backwards when facing north' do @@ -52,7 +52,7 @@ mars_rover.execute_commands(%w[b]) - expect(mars_rover.point).to eq Point.new(0, 0,'N') + expect(mars_rover.point).to be_located_at Point.new(0, 0,'N') end it 'moves backwards when facing south' do @@ -60,7 +60,7 @@ mars_rover.execute_commands(%w[b]) - expect(mars_rover.point).to eq Point.new(0, 1,'S') + expect(mars_rover.point).to be_located_at Point.new(0, 1,'S') end it 'moves left when facing North' do @@ -68,7 +68,7 @@ mars_rover.execute_commands(%w[l]) - expect(mars_rover.point).to eq Point.new(-1, 0,'N') + expect(mars_rover.point).to be_located_at Point.new(-1, 0,'N') end it 'moves left when facing East' do @@ -76,7 +76,7 @@ mars_rover.execute_commands(%w[l]) - expect(mars_rover.point).to eq Point.new(0, 1,'E') + expect(mars_rover.point).to be_located_at Point.new(0, 1,'E') end it 'moves right when facing South' do @@ -84,7 +84,7 @@ mars_rover.execute_commands(%w[r]) - expect(mars_rover.point).to eq Point.new(-1, 0,'S') + expect(mars_rover.point).to be_located_at Point.new(-1, 0,'S') end it 'moves right when facing West' do @@ -92,6 +92,19 @@ mars_rover.execute_commands(%w[r]) - expect(mars_rover.point).to eq Point.new(0, 1,'W') + expect(mars_rover.point).to be_located_at Point.new(0, 1,'W') + end + + RSpec::Matchers.define :be_located_at do |expected_point| + match do |actual_point| + actual_point == expected_point + end + failure_message do |actual_point| + "expected that #{actual_point} would have the same coordinates as #{expected_point}. Coordinates:\n + ACTUAL | EXPECTED\n + X: #{actual_point.x} | #{expected_point.x}\n + Y: #{actual_point.y} | #{expected_point.y}\n + D: #{actual_point.direction} | #{expected_point.direction}\n" + end end end