Skip to content
Browse files

refactored to use custom ValidationError

  • Loading branch information...
1 parent 8459081 commit f23122b15d4bc83cc7adc70a9e90bea49986e210 @mspwong committed Aug 28, 2011
Showing with 36 additions and 30 deletions.
  1. +13 −7 game.rb
  2. +23 −23 spec/game_spec.rb
View
20 game.rb
@@ -17,7 +17,8 @@ def move(piece, new_position)
(new_position.first.is_a? Integer) && (VALID_COORDINATES.include? new_position.first) &&
(new_position.last.is_a? Integer) && (VALID_COORDINATES.include? new_position.last) )
- raise RuntimeError if (!immediate_forward_and_diagonal(piece, new_position) || occupied(new_position))
+ validate_immediate_forward_diagonal! piece, new_position
+ validate_not_occupied! new_position
@teams[piece[:team]][piece[:piece_num]] = new_position
end
@@ -30,23 +31,28 @@ def position(piece)
VALID_COORDINATES = (1..8).to_a
- def immediate_forward_and_diagonal(piece, new_position)
+ def validate_immediate_forward_diagonal!(piece, new_position)
p = position(piece)
current_x = p.first
current_y = p.last
if piece[:team] == :white
- (new_position.last == current_y+1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
+ raise ValidationError.new("must only move to a position immediately forward and diagonal") unless (new_position.last == current_y+1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
elsif piece[:team] == :red
- (new_position.last == current_y-1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
+ raise ValidationError.new("must only move to a position immediately forward and diagonal") unless (new_position.last == current_y-1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
else
- raise RuntimeError("unknown team: #{piece[:team]}")
+ raise ArgumentError("unknown team: #{piece[:team]}")
end
end
- def occupied(new_position)
- @teams.values.any? do |pieces|
+ def validate_not_occupied!(new_position)
+ raise ValidationError.new("must not move to a position that is already occupied") if @teams.values.any? do |pieces|
pieces.values.include? new_position
end
end
+end
+
+
+
+class ValidationError < StandardError
end
View
46 spec/game_spec.rb
@@ -1,5 +1,5 @@
require "rspec"
-require File.dirname("__FILE__") + "/game"
+require File.dirname(__FILE__) + "/../game"
describe "Checker game: " do
@@ -12,34 +12,34 @@
describe "to invalid square" do
it "results in exception" do
- lambda { @game.move(@piece, [3.5, @y]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [3.5, 3.5]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [@x, -1]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [-1, @y]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [0, @y]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [@x, 0]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, ["does not matter", @y]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [@x, 9]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [9, @y]) }.should raise_error(ArgumentError)
- lambda { @game.move(@piece, [9, 9]) }.should raise_error(ArgumentError)
+ lambda { @game.move(@piece, [3.5, @y]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [3.5, 3.5]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [@x, -1]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [-1, @y]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [0, @y]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [@x, 0]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, ["does not matter", @y]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [@x, 9]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [9, @y]) }.should raise_error ArgumentError
+ lambda { @game.move(@piece, [9, 9]) }.should raise_error ArgumentError
end
end
describe "to light colored square" do
it "results in exception" do
- lambda { @game.move(@piece, [4, 4]) }.should raise_error(RuntimeError)
+ lambda { @game.move(@piece, [4, 4]) }.should raise_error ValidationError
end
end
describe "to horizontally adjacent (side way) square" do
it "results in exception" do
- lambda { @game.move(@piece, [8, 3]) }.should raise_error(RuntimeError)
+ lambda { @game.move(@piece, [8, 3]) }.should raise_error ValidationError
end
end
describe "to vertically adjacent (up or down) square" do
it "results in exception" do
- lambda { @game.move(@piece, [7, 4]) }.should raise_error(RuntimeError)
+ lambda { @game.move(@piece, [7, 4]) }.should raise_error ValidationError
end
end
@@ -48,35 +48,35 @@
it "results in exception" do
piece = {:team => :red, :piece_num => 4}
x, y = @game.position(piece).first, @game.position(piece).last
- lambda { @game.move(piece, [x-1, y+1]) }.should raise_error(RuntimeError)
+ lambda { @game.move(piece, [x-1, y+1]) }.should raise_error ValidationError
end
end
describe "by a white piece" do
it "results in exception" do
- lambda { @game.move(@piece, [@x, @y-1]) }.should raise_error(RuntimeError)
+ lambda { @game.move(@piece, [@x, @y-1]) }.should raise_error ValidationError
end
end
end
describe "forward by more than 1 diagonal" do
it "results in exception" do
- lambda { @game.move(@piece, [5, 5]) }.should raise_error(RuntimeError)
+ lambda { @game.move(@piece, [5, 5]) }.should raise_error ValidationError
end
end
describe "to square" do
describe "occupied by own team" do
it "results in exception" do
piece = {:team => :white, :piece_num => 7}
- lambda { @game.move(piece, [@x, @y]) }.should raise_error(RuntimeError)
+ lambda { @game.move(piece, [@x, @y]) }.should raise_error ValidationError
end
end
describe "occupied by opponent" do
it "results in exception" do
piece = {:team => :red, :piece_num => 4}
- lambda { @game.move(piece, [@x, @y]) }.should raise_error(RuntimeError)
+ lambda { @game.move(piece, [@x, @y]) }.should raise_error ValidationError
end
end
end
@@ -125,7 +125,7 @@
piece = {:team => :red, :piece_num => 8}
position = @game.position(piece)
x, y = position.first, position.last
- lambda { @game.move(piece, [x, y]) }.should raise_error
+ lambda { @game.move(piece, [x, y]) }.should raise_error ValidationError
piece = {:team => :red, :piece_num => 4}
position = @game.position(piece)
@@ -139,7 +139,7 @@
piece = {:team => :white, :piece_num => 12}
position = @game.position(piece)
x, y = position.first, position.last
- lambda { @game.move(piece, [x+1, y+1]) }.should raise_error
+ lambda { @game.move(piece, [x+1, y+1]) }.should raise_error ValidationError
piece = {:team => :red, :piece_num => 8}
position = @game.position(piece)
@@ -159,7 +159,7 @@
piece = {:team => :red, :piece_num => 2}
position = @game.position(piece)
x, y = position.first, position.last
- lambda { @game.move(piece, [x+1, y-1]) }.should raise_error
+ lambda { @game.move(piece, [x+1, y-1]) }.should raise_error ValidationError
piece = {:team => :white, :piece_num => 8}
position = @game.position(piece)
@@ -171,7 +171,7 @@
position = @game.position(piece)
x, y = position.first, position.last
[x, y].should eq [6, 4]
- lambda { @game.move(piece, [x-1, y+1]) }.should raise_error
+ lambda { @game.move(piece, [x-1, y+1]) }.should raise_error ValidationError
# keep playing
end

0 comments on commit f23122b

Please sign in to comment.
Something went wrong with that request. Please try again.