From 43cbb56b76fcb4d8a1dd18310a91f3b82b479447 Mon Sep 17 00:00:00 2001 From: mo k Date: Sun, 12 Feb 2012 15:23:58 -0700 Subject: add boundary checks when facing north and moving forward. --- lib/north.rb | 6 ++++-- spec/north_specs.rb | 20 ++++++++++++++++++++ spec/rover_specs.rb | 1 + spec/ts_all.rb | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 spec/north_specs.rb diff --git a/lib/north.rb b/lib/north.rb index 438d52d..9fe6aa8 100644 --- a/lib/north.rb +++ b/lib/north.rb @@ -5,7 +5,9 @@ class North def turn_left West.new end - def forward(current_location, terrain) - current_location[:y] = current_location[:y]+1 + def forward(location, terrain) + unless terrain.is_out_of_bounds({:x =>location[:x],:y => location[:y]+1}) + location[:y] = location[:y]+1 + end end end diff --git a/spec/north_specs.rb b/spec/north_specs.rb new file mode 100644 index 0000000..aeb1308 --- /dev/null +++ b/spec/north_specs.rb @@ -0,0 +1,20 @@ +require 'north' + +describe North do + before(:each) do + @sut = North.new + end + describe "when at the edge of the terrain" do + describe "when moving forward" do + it "should just stay at the current position" do + @location[:y].must_equal 3 + end + before do + terrain = fake + terrain.stub(:is_out_of_bounds).with({:x => 0, :y => 4}).and_return(true) + @location = {:x => 0, :y => 3} + @sut.forward(@location, terrain) + end + end + end +end diff --git a/spec/rover_specs.rb b/spec/rover_specs.rb index 5e99495..52b45c3 100644 --- a/spec/rover_specs.rb +++ b/spec/rover_specs.rb @@ -28,6 +28,7 @@ describe Rover do end describe "when driving forward" do before do + @terrain = fake @sut.move_forward(@terrain) end it "should increment the y coordinate on the terrain" do diff --git a/spec/ts_all.rb b/spec/ts_all.rb index 9bf43ab..b783a1e 100644 --- a/spec/ts_all.rb +++ b/spec/ts_all.rb @@ -7,4 +7,5 @@ require "book_specs" require "library_specs" require "stack_specs" require "rover_specs" +require "north_specs" require "specifications/find_all_books_by_author_specs" -- cgit v1.2.3