require 'minesweeper' describe Minesweeper do before(:each) do @ms = Minesweeper::Game.new end it 'creates fields from input' do @ms.input '4 4' @ms.fields.length.should eq 1 end it 'sets field row & column size from input' do @ms.input '4 6' f = @ms.fields[0] f.num_rows.should eq 4 f.num_cols.should eq 6 end it 'allows row and column size in the range 1..100' do expect { @ms.input '199 0' }.to raise_error 'num rows not in range' expect { @ms.input '1 0' }.to raise_error 'num cols not in range' end it 'takes row input after field size input' do @ms.input '4 4' expect { @ms.input '4 4' }.to raise_error 'input does not define field row' @ms.input '...*' @ms.input '*.*.' f = @ms.fields[0] f.data[0].join.should eq '...*' f.data[1].join.should eq '*.*.' end it 'expects row input to match defined column size' do @ms.input '4 4' expect { @ms.input '...**' }.to raise_error 'too many columns input, expected 4, received 5' end it 'expects row inou to only contain valid characters' do @ms.input '4 4' expect { @ms.input '...*ABC' }.to raise_error 'input does not define field row' end it 'expects number of row inputs to not be less than defined row size' do @ms.input '2 4' @ms.input '*...' expect { @ms.input '2 4' }.to raise_error 'input does not define field row' end it 'expects number of row inputs to not be greater than defined row size' do @ms.input '2 4' @ms.input '*...' @ms.input '*...' expect { @ms.input '*...' }.to raise_error 'too many rows, expected new field or end of input' end it 'stops accepting input when n=m=0' do @ms.input '2 4' @ms.input '*...' @ms.input '*...' @ms.input '0 0' expect { @ms.input '2 4' }.to raise_error 'input not processed. end-of-input message already received' end it 'outputs field number message and adjacent mine count' do @ms.input '4 4' @ms.input '*...' @ms.input '....' @ms.input '.*..' @ms.input '....' @ms.input '3 5' @ms.input '**...' @ms.input '.....' @ms.input '.*...' @ms.input '0 0' @ms.output.should eq 'Field #1:' @ms.output.should eq '*100' @ms.output.should eq '2210' @ms.output.should eq '1*10' @ms.output.should eq '1110' @ms.output.should eq '' @ms.output.should eq 'Field #2:' @ms.output.should eq '**100' @ms.output.should eq '33200' @ms.output.should eq '1*100' end end