Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anija hemal mars rover practice #17

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
2aeb9a9
Initial test list for mars rover kafta
anijasara Jun 11, 2024
ef35fca
make the first spec initial points passed with minimum code
anijasara Jun 11, 2024
a542549
refactor the constructor to have single coordinates variable
anijasara Jun 11, 2024
aac5ee7
remove unused the x and y variables in constructor
anijasara Jun 11, 2024
3ff29e8
another spec to raise error when coordinates are unknown
anijasara Jun 11, 2024
5fd3bf1
Spec to get a direction with minimal code
anijasara Jun 18, 2024
b92e206
set the inital direction as state variable
anijasara Jun 18, 2024
42ae7ef
receive the direction from the user
anijasara Jun 18, 2024
2d0e1c7
set direction as attr_reader
anijasara Jun 18, 2024
f108660
set coordinates also as an attr_reader
anijasara Jun 18, 2024
aa1a7f0
mars rover should able to face any direction which is given
anijasara Jun 18, 2024
7287771
raise error when get a direction out of the 4 cardinal ones
anijasara Jun 18, 2024
b5b9c74
refactor the code on checking the direction ones
anijasara Jun 18, 2024
b4389a8
homework: move the raise_error in to the constructor
anijasara Jul 2, 2024
de7debe
Pending spec to check error is raised when direction is nil
anijasara Jul 2, 2024
0d58d4b
added the inspect to get more clarity of errors and started with arra…
anijasara Jul 2, 2024
e6e928b
use respond_to to check the method is present in spec
anijasara Jul 2, 2024
cdca585
Implement the commands method for move forward test
anijasara Jul 2, 2024
63b8988
HW: make forwad test passing and add testlist for backward and forward
anijasara Jul 18, 2024
edea40d
Update the coordinates after processing the commands
anijasara Jul 18, 2024
965dc5e
Refactor : use the coordinates variable in returning the new position
anijasara Jul 18, 2024
60603dd
Remove the unnecessary direction varaiable introduced in forward calc
anijasara Jul 18, 2024
6b3200a
Remove the if-statement for forward logic alone
anijasara Jul 18, 2024
46e0fca
HW : add testlist for backward and forward
anijasara Jul 23, 2024
d472bd5
Pass another spec with different starting points in North direction
anijasara Jul 23, 2024
e4c6d76
Spec passing for moving forward in the east direction
anijasara Jul 23, 2024
586e65b
Refactor: extracted common expectations into a separate method
anijasara Jul 23, 2024
210dcdf
Removed the repetitive codes in other blocks as well with the refacto…
anijasara Jul 23, 2024
971a05b
refactor: added a custom matcher to demonstrate designing a testing DSL.
hemalvarambhia Jul 23, 2024
46f85a3
Added new custom matcher for direction
anijasara Jul 23, 2024
21bde62
Remove the extra matchers and use custom matchers alone
anijasara Jul 23, 2024
b718adc
use custom matchers for other existing specs
anijasara Aug 9, 2024
b036d03
remove obsolete code as custom matchers are in place
anijasara Aug 9, 2024
8ac0cff
introduce first failing test for the point class
anijasara Aug 9, 2024
234ec1e
make the test pass for initializing x coordinate
anijasara Aug 9, 2024
8e7eb21
introduce y_coordiante definition and spec
anijasara Aug 9, 2024
cfd1006
remove default testing as both co-ordinates are passing from tests
anijasara Aug 9, 2024
88bff1f
add spec for equating two points and introduce the equal method
anijasara Aug 9, 2024
cf564d6
spec passes for equality check when 2 different x_coordinates
anijasara Aug 9, 2024
017ddcf
points should be equal when both the co-ordinates are equal
anijasara Sep 10, 2024
9be1c7c
spec to check 2 co-ordinates with different types are unequal
anijasara Sep 17, 2024
83bece4
use rspec equal matchers in a spec
anijasara Sep 17, 2024
293926d
use not eq matchers instead of checking to be_false
anijasara Sep 17, 2024
2067416
include point object in expectation and hash in the matcher to test t…
anijasara Sep 17, 2024
1bdfec0
remove obsolete code
anijasara Sep 17, 2024
c161862
parameterised test on checking types are unequal
anijasara Sep 17, 2024
5489e5b
Introduce checking with hash to compare the point objects
anijasara Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Point
attr_reader :x_coordinate, :y_coordinate

def initialize(x_coordinate, y_coordinate)
@x_coordinate = x_coordinate
@y_coordinate = y_coordinate
end

def ==(point)
return false unless point.is_a? Point

point.x_coordinate == x_coordinate && point.y_coordinate == y_coordinate
end

def hash
[x_coordinate, y_coordinate].hash
end
end
26 changes: 26 additions & 0 deletions rover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Rover
attr_reader :coordinates, :direction

def initialize(x, y, direction = 'N')
raise StandardError unless ['N', 'S', 'E', 'W'].include?direction

@coordinates = [x, y]
@direction = direction
end

def inspect
"Rover facing direction: #{@direction } and the coordinates: #{@coordinates}"
end

def commands(commands)
if @direction == 'N'
y_coord = @coordinates[1].to_i + 1
@coordinates = [@coordinates[0], y_coord]
else
x_coord = @coordinates[0].to_i + 1
@coordinates = [x_coord, @coordinates[1]]
end

[@direction] + @coordinates
end
end
41 changes: 41 additions & 0 deletions spec/point_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative '../point'

RSpec.describe 'Point' do
describe "point" do
it "has a x co-ordinate" do
expect(Point.new(1,0).x_coordinate).to eq(1)
end

it "has a y co-ordinate" do
expect(Point.new(0,1).y_coordinate).to eq(1)
end

specify "two co-ordinates with the same x and y co-ordinates are the same" do
expect(Point.new(1,1)).to eq(Point.new(1,1))
end

specify "two co-ordinates with different x co-ordinates are unequal" do
expect(Point.new(1,1)).not_to eq(Point.new(2,1))
end

specify "two co-ordinates with different y co-ordinates are unequal" do
expect(Point.new(1,2)).not_to eq(Point.new(1,1))
end

specify "two co-ordinates with different types are unequal" do
expect(Point.new(1,2)).not_to eq(Hash.new())
end

context "when different types" do
[ Hash.new(), [1,2] ].each do |type|
specify "point and #{type} are unequal" do
expect(Point.new(1,2)).not_to eq(type)
end
end
end

specify "two objects representing the same points have the same hash" do
expect(Point.new(1,1).hash).to eq(Point.new(1,1).hash)
end
end
end
85 changes: 85 additions & 0 deletions spec/rover_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require_relative '../rover'

RSpec.describe 'Rover' do

describe "Setting the rover" do
it 'has an initial point' do
expect(Rover.new(0, 0).coordinates).to eq([0, 0])
end

it 'knows the direction' do
expect(Rover.new(0, 0).direction).to eq('N')
end

it 'faces any direction' do
expect(Rover.new(0, 0, 'S').direction).to eq('S')
end

it 'errors when does not know the coordinates' do
expect{ Rover.new().coordinates }.to raise_error
end

it 'errors when direction is unavailable' do
expect{Rover.new(0, 0, nil)}.to raise_error
end

it 'errors when the direction is not one of the cardinal directions' do
expect{Rover.new(0, 0, 'B')}.to raise_error
end
end

describe "rover receives the commands" do
it "receives an array of commands" do
rover = Rover.new(0, 0)
expect(rover).to respond_to(:commands).with(1)
end

it "moves forward given starting from initial point and the direction set is North" do
rover = Rover.new(0, 0, 'N')
expect(rover.commands(['f'])).to eq(['N', 0, 1])
expect(rover).to be_located_at([0, 1]).and be_directed('N')
end

it "moves forward given starting point is 2,2 and the direction is North" do
rover = Rover.new(2, 2, 'N')
expect(rover.commands(['f'])).to eq(['N', 2, 3])
expect(rover).to be_located_at([2, 3]).and be_directed('N')
end

it "moves forward given starting point is 2,2 and the direction set is South"

it 'moves forward given starting from initial point and the direction set is East' do
rover = Rover.new(0, 0, 'E')
expect(rover.commands(['f'])).to eq(['E', 1, 0])
expect(rover).to be_located_at([1, 0]).and be_directed('E')
end

it 'moves forward given starting from initial point and the direction set is West'

it "moves forward when starting from the point -1,0 and the direction is North"

it "moves backward given starting from initial point and the direction set is North"

it "moves backward given starting from initial point and the direction set is South"

it "moves backward given starting from initial point and the direction set is East"

it "moves backward given starting point is 2,2 and the direction set is West"

it "moves backward given starting point is 2,2 and the direction set is East"

it "moves backward when starting from the point -1,0 and the direction is North"
end

RSpec::Matchers.define :be_located_at do |expected_location|
match do |mars_rover|
mars_rover.coordinates == expected_location
end
end

RSpec::Matchers.define :be_directed do |expected_direction|
match do |mars_rover|
mars_rover.direction == expected_direction
end
end
end