-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GameSetup class for game configuration validation
- Loading branch information
Showing
6 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class GameSetup | ||
attr_reader :errors | ||
|
||
def initialize(setup) | ||
@setup = setup | ||
@errors = {} | ||
end | ||
|
||
def valid? | ||
run_validations | ||
errors.empty? | ||
end | ||
|
||
def invalid? | ||
!valid? | ||
end | ||
|
||
private | ||
|
||
def run_validations | ||
validate_player | ||
validate_opponent | ||
validate_player_order | ||
end | ||
|
||
def validate_player_mark | ||
validate(:player_mark) | ||
end | ||
|
||
def validate_opponent | ||
validate(:opponent) | ||
end | ||
|
||
def validate_player_order | ||
validate(:player_order) | ||
end | ||
|
||
def validate(key) | ||
if valid_options[key].include?(@setup[key]) | ||
true | ||
else | ||
self.errors[key] = message_options[key] | ||
false | ||
end | ||
end | ||
|
||
def valid_options | ||
{ | ||
player_mark: ["X", "O"], | ||
opponent: ["yes", "no"], | ||
player_order: ["first", "second"] | ||
} | ||
end | ||
|
||
def message_options | ||
{ | ||
player_mark: "Please select a mark to continue", | ||
opponent: "Please indicate if you would like to play against the computer", | ||
player_order: "Please select if you would like to go first or second" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'spec_helper' | ||
require 'game_setup' | ||
|
||
describe 'GameSetup' do | ||
valid_params = {player_mark: 'X', opponent: 'yes', player_order: 'first'} | ||
invalid_params = {opponent: 'yes', player_order: 'third'} | ||
|
||
let(:valid_game_setup) {GameSetup.new(valid_params)} | ||
let(:invalid_game_setup) {GameSetup.new(invalid_params)} | ||
|
||
context 'valid game setup' do | ||
it 'validates the game configurations' do | ||
expect(valid_game_setup.valid?).to eq true | ||
end | ||
|
||
it 'does not produce error messages' do | ||
expect(valid_game_setup.errors.empty?).to eq true | ||
end | ||
end | ||
|
||
context 'invalid_game_setup' do | ||
it 'validates the game configurations' do | ||
expect(invalid_game_setup.valid?).to eq false | ||
end | ||
|
||
it 'does not produce error messages' do | ||
expect(invalid_game_setup.errors.empty?).to eq false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
require 'rspec' | ||
require 'rack/test' | ||
|
||
root = File.expand_path(File.dirname(__FILE__) + '/../') | ||
|
||
lib = root + '/lib' | ||
spec = root + '/spec' | ||
|
||
$:.unshift(lib) unless $:.include?(lib) | ||
$:.unshift(spec) unless $:.include?(spec) | ||
|
||
|
||
RSpec.configure do |conf| | ||
conf.include Rack::Test::Methods | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
ENV['RACK_ENV'] = 'test' | ||
|
||
|
||
require './../tic_tac_toe_controller' | ||
require 'rspec' | ||
require 'rack/test' | ||
|
||
describe 'The HelloWorld App' do | ||
include Rack::Test::Methods | ||
|
||
def app | ||
TicTacToeController | ||
end | ||
|
||
it "says hello" do | ||
get '/' | ||
expect(last_response).to be_ok | ||
expect(last_response.status).to eq 200 | ||
expect(last_response.body).to include "Player 1, select your mark" | ||
end | ||
|
||
it "starts the game" do | ||
post "/setup", player_mark: "X", opponent: "yes" | ||
expect(last_response).to be_redirect | ||
|
||
#expect(session[:mark]).to eq "O" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters