Commit 03915bd

mo k <mo@mokhan.ca>
2012-08-10 04:00:34
create rover.
1 parent 9262e90
lib/rover.rb
@@ -0,0 +1,20 @@
+class Rover
+  attr_reader :location
+  def initialize(heading, coordinates)
+    @heading = heading
+    @location = coordinates
+  end
+  def heading
+    @heading.class.name.downcase.to_sym
+  end
+  def turn_right
+    @heading = @heading.turn_right
+  end
+  def turn_left
+    @heading = @heading.turn_left
+  end
+  def move_forward(terrain)
+    terrain.move_forward(@heading, @location)
+  end
+end
+
spec/rover_spec.rb
@@ -0,0 +1,91 @@
+require 'spec_helper'
+
+describe Rover do
+  def create_sut(heading, x = 0, y = 0)
+    directions = {:north => North.new, :east => East.new, :west => West.new, :south => South.new}
+    Rover.new(directions[heading],{ :x =>x,:y => y })
+  end
+
+  context "when facing north" do
+    before do
+      @sut = create_sut :north, 0, 0
+    end
+    context "when turning right" do
+      it "should face east" do
+        @sut.turn_right
+        @sut.heading.should == :east
+      end
+    end
+    context "when turning left" do
+      it "should face west" do
+        @sut.turn_left
+        @sut.heading.should == :west
+      end
+    end
+  end
+
+  context "when facing south" do
+    before do
+      @sut = create_sut :south, 0, 3
+    end
+    context "when turning right" do
+      it "should face west" do
+        @sut.turn_right
+        @sut.heading.should == :west
+      end
+    end
+    context "when turning left" do
+      it "should face east" do
+        @sut.turn_left
+        @sut.heading.should == :east
+      end
+    end
+  end
+
+  context "when facing east" do
+    before do
+      @sut = create_sut :east
+    end
+    context "when turning right" do
+      it "should face south" do
+        @sut.turn_right
+        @sut.heading.should == :south
+      end
+    end
+    context "when turning left" do
+      it "should face north" do
+        @sut.turn_left
+        @sut.heading.should == :north
+      end
+    end
+  end
+
+  context "when facing west" do
+    before do
+      @sut = create_sut :west, 1, 0
+    end
+    context "when turning right" do
+      it "should face north" do
+        @sut.turn_right
+        @sut.heading.should == :north
+      end
+    end
+    context "when turning left" do
+      it "should face south" do
+        @sut.turn_left
+        @sut.heading.should == :south
+      end
+    end
+  end
+
+  context "when driving forward" do
+    it "should move forward along the terrain" do
+      @terrain.should have_received(:move_forward, @sut.instance_variable_get(:@heading), @sut.location )
+    end
+    before do
+      @terrain = fake
+      @sut = create_sut(:north)
+      @sut.move_forward(@terrain)
+    end
+  end
+end
spec/spec_helper.rb
@@ -6,3 +6,4 @@ require_relative '../lib/north'
 require_relative '../lib/east'
 require_relative '../lib/south'
 require_relative '../lib/west'
+require_relative '../lib/rover'