Permalink
Browse files

Validate the board instead of the seed data, so it can be reused ever…

…ytime a board in reconfigured
  • Loading branch information...
1 parent 433d046 commit 0814e843c9133e88bd1356b1523381dba1aafdd5 @arnab committed Jan 7, 2012
Showing with 22 additions and 28 deletions.
  1. +20 −17 lib/game_of_life/board.rb
  2. +2 −11 spec/game_of_life/board_spec.rb
View
@@ -1,6 +1,10 @@
module GameOfLife
+ # Raised when a {Board} gets into an invalid shape
+ class InvalidBoardError < RuntimeError; end;
+
# The board used in the game. Holds the {Cell}s.
class Board
+
# The {Game} to which this board belongs
attr_reader :game
@@ -18,13 +22,18 @@ class Board
# @param [2D Array<Symbol>] seed_data the data for the {Cell}s in the board, as an 2D array.
# @example seed_data looks like
# the output of SimpleStringInputter#parse
- # @raise [ArgumentError] if the seed_data is not in the shape of a square,
+ # @raise [InvalidBoardError] if the seed_data is not in the shape of a square,
# or if all elements are not present
def initialize(game, seed_data)
- validate_seed_data(seed_data)
@game = game
@cells = Array.new(Array.new)
seed_with!(seed_data)
+ begin
+ validate
+ rescue InvalidBoardError => ex
+ # Add the seed_data into the error message, so the caller gets a clue
+ raise InvalidBoardError, ex.message + " [seed data was: #{seed_data.inspect}]"
+ end
end
def view
@@ -83,30 +92,24 @@ def shed_dead_weight!
end
private
- def validate_seed_data(seed_data)
- rows_found = seed_data.size
-
- columns_in_each_row = seed_data.map(&:size)
+ def validate
+ num_o_rows = @cells.size
+ columns_in_each_row = @cells.map(&:size)
unless columns_in_each_row.uniq.size == 1
msg = "Unequal number of columns, #{columns_in_each_row.inspect} in different rows found"
- fail_with(seed_data, msg)
+ raise InvalidBoardError, msg
end
- columns_found = columns_in_each_row.uniq.first
- elements_found = seed_data.flatten.reject {|d| d.nil? }.size
- unless (rows_found * columns_found) == elements_found
+ num_o_columns = columns_in_each_row.uniq.first
+ num_o_elements = @cells.flatten.reject {|d| d.nil? }.size
+ unless (num_o_rows * num_o_columns) == num_o_elements
msg = "Not a rectangular shape: " +
- "rows(#{rows_found}) x columns(#{columns_found}) != total elements(#{elements_found})]"
+ "rows(#{num_o_rows}) x columns(#{num_o_columns}) != total elements(#{num_o_elements})]"
"Probably not all elements are filled with valid data."
- fail_with(seed_data, msg)
+ raise InvalidBoardError, msg
end
end
- def fail_with(message, seed_data)
- raise ArgumentError, "Cannot create a board with seed_data: #{seed_data.inspect}" +
- "(Reason: #{message})"
- end
-
def seed_with!(data)
data.each_with_index do |row, y|
@cells << []
@@ -16,16 +16,7 @@ module GameOfLife
]
expect {
Board.new(game, seed_data)
- }.to raise_error ArgumentError, /unequal number of columns/i
- end
-
- it "should raise Error when not all elements are present (i.e. some are nil)" do
- seed_data = [
- [:live, :dead], [:dead, nil]
- ]
- expect {
- Board.new(game, seed_data)
- }.to raise_error ArgumentError
+ }.to_not raise_error InvalidBoardError, /unequal number of columns/i
end
it "should NOT raise Error when rows != columns, but is a rectangular shape" do
@@ -35,7 +26,7 @@ module GameOfLife
]
expect {
Board.new(game, seed_data)
- }.to_not raise_error ArgumentError
+ }.to_not raise_error InvalidBoardError
end
end

0 comments on commit 0814e84

Please sign in to comment.