From f712068ee8651572b24506bae17ec0d80a17077e Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 12:05:31 +0100 Subject: [PATCH 1/9] Add notes file --- notes.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 notes.txt diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000..aafa97b4 --- /dev/null +++ b/notes.txt @@ -0,0 +1,3 @@ +Next steps: +- review existing codebase +- set up test infrastructure \ No newline at end of file From e3e2763c9aecd118d79e8e86ae0df4215782b928 Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 12:36:50 +0100 Subject: [PATCH 2/9] Reformat files to pass Rubocop --- Gemfile.lock | 73 +++++++++++++++++++++++++--------------------------- notes.txt | 4 +-- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b5673894..763b3150 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,51 +5,49 @@ GEM public_suffix (>= 2.0.2, < 5.0) ansi (1.5.0) ast (2.4.2) - capybara (3.35.3) + capybara (3.37.1) addressable + matrix mini_mime (>= 0.1.3) nokogiri (~> 1.8) rack (>= 1.6.0) rack-test (>= 0.6.3) regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) - diff-lcs (1.4.4) + diff-lcs (1.5.0) docile (1.4.0) - mini_mime (1.1.1) - mini_portile2 (2.6.1) + matrix (0.4.2) + mini_mime (1.1.2) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) - nokogiri (1.12.3) - mini_portile2 (~> 2.6.1) + nokogiri (1.13.6-arm64-darwin) racc (~> 1.4) - nokogiri (1.12.3-x86_64-darwin) - racc (~> 1.4) - parallel (1.20.1) - parser (3.0.2.0) + parallel (1.22.1) + parser (3.1.2.0) ast (~> 2.4.1) - public_suffix (4.0.6) - racc (1.5.2) - rack (2.2.3) - rack-protection (2.1.0) + public_suffix (4.0.7) + racc (1.6.0) + rack (2.2.4) + rack-protection (2.2.0) rack - rack-test (1.1.0) - rack (>= 1.0, < 3) - rainbow (3.0.0) - regexp_parser (2.1.1) + rack-test (2.0.2) + rack (>= 1.3) + rainbow (3.1.1) + regexp_parser (2.5.0) rexml (3.2.5) - rspec (3.10.0) - rspec-core (~> 3.10.0) - rspec-expectations (~> 3.10.0) - rspec-mocks (~> 3.10.0) - rspec-core (3.10.1) - rspec-support (~> 3.10.0) - rspec-expectations (3.10.1) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) - rspec-mocks (3.10.2) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) - rspec-support (3.10.2) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) rubocop (1.20.0) parallel (~> 1.10) parser (>= 3.0.0.0) @@ -59,8 +57,8 @@ GEM rubocop-ast (>= 1.9.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.11.0) - parser (>= 3.0.1.1) + rubocop-ast (1.18.0) + parser (>= 3.1.1.0) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) simplecov (0.21.2) @@ -72,22 +70,21 @@ GEM simplecov terminal-table simplecov-html (0.12.3) - simplecov_json_formatter (0.1.3) - sinatra (2.1.0) + simplecov_json_formatter (0.1.4) + sinatra (2.2.0) mustermann (~> 1.0) rack (~> 2.2) - rack-protection (= 2.1.0) + rack-protection (= 2.2.0) tilt (~> 2.0) - terminal-table (3.0.1) + terminal-table (3.0.2) unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) - unicode-display_width (1.6.1) + unicode-display_width (2.2.0) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS - ruby - x86_64-darwin-20 + arm64-darwin-21 DEPENDENCIES capybara diff --git a/notes.txt b/notes.txt index aafa97b4..51435aa4 100644 --- a/notes.txt +++ b/notes.txt @@ -1,3 +1,3 @@ Next steps: -- review existing codebase -- set up test infrastructure \ No newline at end of file + +- [x] review existing codebase \ No newline at end of file From 3f9ea76d64a09587fe010b18579ebee634fe51f8 Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 14:00:26 +0100 Subject: [PATCH 3/9] Display player name --- app.rb | 8 -------- lib/app.rb | 25 +++++++++++++++++++++++++ lib/views/display_name.erb | 1 + lib/views/name_form.erb | 4 ++++ notes.txt | 3 --- spec/features/player_name_spec.rb | 8 ++++++++ spec/spec_helper.rb | 2 +- 7 files changed, 39 insertions(+), 12 deletions(-) delete mode 100644 app.rb create mode 100644 lib/app.rb create mode 100644 lib/views/display_name.erb create mode 100644 lib/views/name_form.erb delete mode 100644 notes.txt create mode 100644 spec/features/player_name_spec.rb diff --git a/app.rb b/app.rb deleted file mode 100644 index 4abb71ec..00000000 --- a/app.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'sinatra/base' -class RockPaperScissors < Sinatra::Base - get '/test' do - 'test page' - end - - run! if app_file == $0 -end diff --git a/lib/app.rb b/lib/app.rb new file mode 100644 index 00000000..693a7531 --- /dev/null +++ b/lib/app.rb @@ -0,0 +1,25 @@ +require 'sinatra/base' + +class RockPaperScissors < Sinatra::Base + enable :sessions + + get '/test' do + 'test page' + end + + get '/name-form' do + erb :name_form + end + + post '/register-name' do + session[:player_name] = params[:player_name] + redirect '/display-name' + end + + get '/display-name' do + @player_name = session[:player_name] + erb :display_name + end + + run! if app_file == $0 +end diff --git a/lib/views/display_name.erb b/lib/views/display_name.erb new file mode 100644 index 00000000..e4496275 --- /dev/null +++ b/lib/views/display_name.erb @@ -0,0 +1 @@ +<%= @player_name %> \ No newline at end of file diff --git a/lib/views/name_form.erb b/lib/views/name_form.erb new file mode 100644 index 00000000..acf71fcb --- /dev/null +++ b/lib/views/name_form.erb @@ -0,0 +1,4 @@ +
+ Name: + +
diff --git a/notes.txt b/notes.txt deleted file mode 100644 index 51435aa4..00000000 --- a/notes.txt +++ /dev/null @@ -1,3 +0,0 @@ -Next steps: - -- [x] review existing codebase \ No newline at end of file diff --git a/spec/features/player_name_spec.rb b/spec/features/player_name_spec.rb new file mode 100644 index 00000000..4d42e004 --- /dev/null +++ b/spec/features/player_name_spec.rb @@ -0,0 +1,8 @@ +feature 'player name' do + scenario 'diplay name entered by user' do + visit '/name-form' + fill_in 'player_name', with: 'Ollie' + click_button 'Submit' + expect(page).to have_content('Ollie') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2177ec6a..edc62399 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,7 +13,7 @@ ENV['RACK_ENV'] = 'test' -require File.join(File.dirname(__FILE__), '..', 'app.rb') +require File.join(File.dirname(__FILE__), '../lib', 'app.rb') Capybara.app = RockPaperScissors RSpec.configure do |config| From a5609ea163108f5e9ea6b4c08dcaca4cfa900618 Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 14:59:30 +0100 Subject: [PATCH 4/9] Allow player to select action --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config.ru | 2 +- lib/app.rb | 18 +++++++++--- lib/views/display_action.erb | 1 + lib/views/display_name.erb | 1 - lib/views/name_form.erb | 4 +-- lib/views/select_action.erb | 18 ++++++++++++ spec/features/player_name_spec.rb | 8 ------ spec/features/register_player_name_spec.rb | 6 ++++ spec/features/select_action_spec.rb | 32 ++++++++++++++++++++++ spec/features/web_helpers.rb | 5 ++++ spec/spec_helper.rb | 2 ++ 13 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 lib/views/display_action.erb delete mode 100644 lib/views/display_name.erb create mode 100644 lib/views/select_action.erb delete mode 100644 spec/features/player_name_spec.rb create mode 100644 spec/features/register_player_name_spec.rb create mode 100644 spec/features/select_action_spec.rb create mode 100644 spec/features/web_helpers.rb diff --git a/Gemfile b/Gemfile index 63415039..2dce9c2e 100644 --- a/Gemfile +++ b/Gemfile @@ -14,3 +14,5 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "webrick", "~> 1.7" diff --git a/Gemfile.lock b/Gemfile.lock index 763b3150..ba0206a3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,7 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (2.2.0) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -93,6 +94,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + webrick (~> 1.7) RUBY VERSION ruby 3.0.2p107 diff --git a/config.ru b/config.ru index a33b6bb0..d00c163c 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,2 @@ -require_relative './app' +require_relative './lib/app' run RockPaperScissors diff --git a/lib/app.rb b/lib/app.rb index 693a7531..a316dd29 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -7,18 +7,28 @@ class RockPaperScissors < Sinatra::Base 'test page' end - get '/name-form' do + get '/' do erb :name_form end post '/register-name' do session[:player_name] = params[:player_name] - redirect '/display-name' + redirect '/select-action' end - get '/display-name' do + get '/select-action' do @player_name = session[:player_name] - erb :display_name + erb :select_action + end + + post '/record-action' do + session[:player_action] = params[:action] + redirect '/display-action' + end + + get '/display-action' do + @player_action = session[:player_action] + erb :display_action end run! if app_file == $0 diff --git a/lib/views/display_action.erb b/lib/views/display_action.erb new file mode 100644 index 00000000..6daa8c3c --- /dev/null +++ b/lib/views/display_action.erb @@ -0,0 +1 @@ +<%= @player_action %> diff --git a/lib/views/display_name.erb b/lib/views/display_name.erb deleted file mode 100644 index e4496275..00000000 --- a/lib/views/display_name.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @player_name %> \ No newline at end of file diff --git a/lib/views/name_form.erb b/lib/views/name_form.erb index acf71fcb..630bcfde 100644 --- a/lib/views/name_form.erb +++ b/lib/views/name_form.erb @@ -1,4 +1,4 @@
- Name: - + Name: +
diff --git a/lib/views/select_action.erb b/lib/views/select_action.erb new file mode 100644 index 00000000..438f13b5 --- /dev/null +++ b/lib/views/select_action.erb @@ -0,0 +1,18 @@ +

Greetings, <%= @player_name %>!

+ +
Choose Rock, Paper or Scissors

+ +
+ + +
+ +
+ + +
+ +
+ + +
diff --git a/spec/features/player_name_spec.rb b/spec/features/player_name_spec.rb deleted file mode 100644 index 4d42e004..00000000 --- a/spec/features/player_name_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -feature 'player name' do - scenario 'diplay name entered by user' do - visit '/name-form' - fill_in 'player_name', with: 'Ollie' - click_button 'Submit' - expect(page).to have_content('Ollie') - end -end diff --git a/spec/features/register_player_name_spec.rb b/spec/features/register_player_name_spec.rb new file mode 100644 index 00000000..fc947807 --- /dev/null +++ b/spec/features/register_player_name_spec.rb @@ -0,0 +1,6 @@ +feature 'register player name' do + scenario 'diplay name entered by user' do + enter_name + expect(page).to have_content('Ollie') + end +end diff --git a/spec/features/select_action_spec.rb b/spec/features/select_action_spec.rb new file mode 100644 index 00000000..b2fb8f4e --- /dev/null +++ b/spec/features/select_action_spec.rb @@ -0,0 +1,32 @@ +feature 'select action' do + scenario 'display available actions to user' do + enter_name + expect(page).to have_content('Rock') + expect(page).to have_content('Paper') + expect(page).to have_content('Scissors') + end + + context 'when user selects Rock' do + scenario 'display selected action' do + enter_name + click_button 'Rock' + expect(page).to have_content('Rock') + end + end + + context 'when user selects Paper' do + scenario 'display selected action' do + enter_name + click_button 'Paper' + expect(page).to have_content('Paper') + end + end + + context 'when user selects Scissors' do + scenario 'display selected action' do + enter_name + click_button 'Scissors' + expect(page).to have_content('Scissors') + end + end +end diff --git a/spec/features/web_helpers.rb b/spec/features/web_helpers.rb new file mode 100644 index 00000000..30b586bf --- /dev/null +++ b/spec/features/web_helpers.rb @@ -0,0 +1,5 @@ +def enter_name + visit '/' + fill_in 'player_name', with: 'Ollie' + click_button 'Submit' +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index edc62399..ec815c3f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,6 +16,8 @@ require File.join(File.dirname(__FILE__), '../lib', 'app.rb') Capybara.app = RockPaperScissors +require './spec/features/web_helpers' + RSpec.configure do |config| config.after(:suite) do puts From 615e73689e62e3ce7edbd9cf86176b30de8b403b Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 15:35:03 +0100 Subject: [PATCH 5/9] Refactor to use Player class --- config.ru | 1 + lib/app.rb | 8 ++++---- lib/player.rb | 18 ++++++++++++++++++ lib/views/display_action.erb | 2 +- lib/views/select_action.erb | 6 +++--- spec/units/player_spec.rb | 18 ++++++++++++++++++ 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 lib/player.rb create mode 100644 spec/units/player_spec.rb diff --git a/config.ru b/config.ru index d00c163c..c641b6ed 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,3 @@ require_relative './lib/app' +require_relative './lib/player' run RockPaperScissors diff --git a/lib/app.rb b/lib/app.rb index a316dd29..02afde8a 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -12,22 +12,22 @@ class RockPaperScissors < Sinatra::Base end post '/register-name' do - session[:player_name] = params[:player_name] + session[:player] = Player.new(params[:player_name]) redirect '/select-action' end get '/select-action' do - @player_name = session[:player_name] + @player_name = session[:player].name erb :select_action end post '/record-action' do - session[:player_action] = params[:action] + session[:player].choose_action(params[:action]) redirect '/display-action' end get '/display-action' do - @player_action = session[:player_action] + @player_action = session[:player].action erb :display_action end diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..e70eddc8 --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,18 @@ +class Player + def initialize(name) + @name = name + @action = nil + end + + def name + return @name + end + + def choose_action(action) + @action = action + end + + def action + return @action + end +end diff --git a/lib/views/display_action.erb b/lib/views/display_action.erb index 6daa8c3c..d55d28b9 100644 --- a/lib/views/display_action.erb +++ b/lib/views/display_action.erb @@ -1 +1 @@ -<%= @player_action %> +<%= @player_action.capitalize %> diff --git a/lib/views/select_action.erb b/lib/views/select_action.erb index 438f13b5..b27c0177 100644 --- a/lib/views/select_action.erb +++ b/lib/views/select_action.erb @@ -3,16 +3,16 @@
Choose Rock, Paper or Scissors

- +
- +
- +
diff --git a/spec/units/player_spec.rb b/spec/units/player_spec.rb new file mode 100644 index 00000000..aae2e4cc --- /dev/null +++ b/spec/units/player_spec.rb @@ -0,0 +1,18 @@ +require './lib/player' + +RSpec.describe Player do + before(:example) do + @player = Player.new("Ollie") + end + + it "returns player name" do + expect(@player.name).to eq "Ollie" + end + + context "when player has chosen an action" do + it "returns chosen action" do + @player.choose_action("rock") + expect(@player.action).to eq "rock" + end + end +end From 86d45a4ebd249d11effb4694402b4b412678d5cd Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 16:35:58 +0100 Subject: [PATCH 6/9] Choose random action for computer --- Gemfile | 2 ++ Gemfile.lock | 1 + config.ru | 1 + lib/app.rb | 7 +++--- lib/random_action.rb | 15 +++++++++++++ lib/views/display_action.erb | 1 - lib/views/result.erb | 1 + lib/views/select_action.erb | 2 +- spec/features/register_player_name_spec.rb | 2 +- ...n_spec.rb => select_player_action_spec.rb} | 12 +++++----- spec/features/select_random_action_spec.rb | 7 ++++++ spec/spec_helper.rb | 1 + spec/units/random_action_spec.rb | 22 +++++++++++++++++++ 13 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 lib/random_action.rb delete mode 100644 lib/views/display_action.erb create mode 100644 lib/views/result.erb rename spec/features/{select_action_spec.rb => select_player_action_spec.rb} (64%) create mode 100644 spec/features/select_random_action_spec.rb create mode 100644 spec/units/random_action_spec.rb diff --git a/Gemfile b/Gemfile index 2dce9c2e..4e6aa402 100644 --- a/Gemfile +++ b/Gemfile @@ -16,3 +16,5 @@ group :development, :test do end gem "webrick", "~> 1.7" + +gem "rspec-expectations", "~> 3.11" diff --git a/Gemfile.lock b/Gemfile.lock index ba0206a3..a4c1a24f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,7 @@ PLATFORMS DEPENDENCIES capybara rspec + rspec-expectations (~> 3.11) rubocop (= 1.20) simplecov simplecov-console diff --git a/config.ru b/config.ru index c641b6ed..ea1332a2 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative './lib/app' require_relative './lib/player' +require_relative './lib/random_action' run RockPaperScissors diff --git a/lib/app.rb b/lib/app.rb index 02afde8a..785be204 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -23,12 +23,13 @@ class RockPaperScissors < Sinatra::Base post '/record-action' do session[:player].choose_action(params[:action]) - redirect '/display-action' + redirect '/result' end - get '/display-action' do + get '/result' do @player_action = session[:player].action - erb :display_action + @computer_action = RandomAction.new.action + erb :result end run! if app_file == $0 diff --git a/lib/random_action.rb b/lib/random_action.rb new file mode 100644 index 00000000..35b5def7 --- /dev/null +++ b/lib/random_action.rb @@ -0,0 +1,15 @@ +class RandomAction + def initialize + @actions = ["rock", "paper", "scissors"] + end + + def action + return @actions[random_index] + end + + private + + def random_index + return [0, 1, 2].sample + end +end diff --git a/lib/views/display_action.erb b/lib/views/display_action.erb deleted file mode 100644 index d55d28b9..00000000 --- a/lib/views/display_action.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @player_action.capitalize %> diff --git a/lib/views/result.erb b/lib/views/result.erb new file mode 100644 index 00000000..eb0b12dc --- /dev/null +++ b/lib/views/result.erb @@ -0,0 +1 @@ +
<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>
diff --git a/lib/views/select_action.erb b/lib/views/select_action.erb index b27c0177..ce254546 100644 --- a/lib/views/select_action.erb +++ b/lib/views/select_action.erb @@ -1,4 +1,4 @@ -

Greetings, <%= @player_name %>!

+

Greetings, <%= @player_name %>!

Choose Rock, Paper or Scissors

diff --git a/spec/features/register_player_name_spec.rb b/spec/features/register_player_name_spec.rb index fc947807..a2795894 100644 --- a/spec/features/register_player_name_spec.rb +++ b/spec/features/register_player_name_spec.rb @@ -1,5 +1,5 @@ feature 'register player name' do - scenario 'diplay name entered by user' do + scenario 'display name entered by user' do enter_name expect(page).to have_content('Ollie') end diff --git a/spec/features/select_action_spec.rb b/spec/features/select_player_action_spec.rb similarity index 64% rename from spec/features/select_action_spec.rb rename to spec/features/select_player_action_spec.rb index b2fb8f4e..dc4ded17 100644 --- a/spec/features/select_action_spec.rb +++ b/spec/features/select_player_action_spec.rb @@ -1,13 +1,11 @@ -feature 'select action' do +feature 'select player action' do scenario 'display available actions to user' do enter_name - expect(page).to have_content('Rock') - expect(page).to have_content('Paper') - expect(page).to have_content('Scissors') + expect(page).to have_content('Rock').and have_content('Paper').and have_content('Scissors') end context 'when user selects Rock' do - scenario 'display selected action' do + scenario 'display Rock as selected action' do enter_name click_button 'Rock' expect(page).to have_content('Rock') @@ -15,7 +13,7 @@ end context 'when user selects Paper' do - scenario 'display selected action' do + scenario 'display Paper as selected action' do enter_name click_button 'Paper' expect(page).to have_content('Paper') @@ -23,7 +21,7 @@ end context 'when user selects Scissors' do - scenario 'display selected action' do + scenario 'display Scissors as selected action' do enter_name click_button 'Scissors' expect(page).to have_content('Scissors') diff --git a/spec/features/select_random_action_spec.rb b/spec/features/select_random_action_spec.rb new file mode 100644 index 00000000..ac45790d --- /dev/null +++ b/spec/features/select_random_action_spec.rb @@ -0,0 +1,7 @@ +feature 'select random action' do + scenario 'display random action for computer' do + enter_name + click_button 'Rock' + expect(page).to have_content('Rock vs. Rock').or have_content('Rock vs. Scissors').or have_content('Rock vs. Paper') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ec815c3f..44a07dc4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +require 'rspec/expectations' require 'capybara/rspec' require 'simplecov' require 'simplecov-console' diff --git a/spec/units/random_action_spec.rb b/spec/units/random_action_spec.rb new file mode 100644 index 00000000..46894a2c --- /dev/null +++ b/spec/units/random_action_spec.rb @@ -0,0 +1,22 @@ +require './lib/random_action' + +RSpec.describe RandomAction do + before(:example) do + @random_action = RandomAction.new + end + + it "returns Rock as an action" do + allow(@random_action).to receive(:random_index).and_return(0) + expect(@random_action.action).to eq "rock" + end + + it "returns Paper as an action" do + allow(@random_action).to receive(:random_index).and_return(1) + expect(@random_action.action).to eq "paper" + end + + it "returns Scissors as an action" do + allow(@random_action).to receive(:random_index).and_return(2) + expect(@random_action.action).to eq "scissors" + end +end From 95c5ee0598d6a110c65ac676f11ab746dcf387ac Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 17:12:07 +0100 Subject: [PATCH 7/9] Add result logic --- config.ru | 1 + lib/app.rb | 2 ++ lib/game.rb | 11 ++++++++++ lib/views/result.erb | 14 ++++++++++++- spec/features/declare_result_spec.rb | 9 ++++++++ spec/units/game_spec.rb | 31 ++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 lib/game.rb create mode 100644 spec/features/declare_result_spec.rb create mode 100644 spec/units/game_spec.rb diff --git a/config.ru b/config.ru index ea1332a2..34addc69 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,5 @@ require_relative './lib/app' require_relative './lib/player' require_relative './lib/random_action' +require_relative './lib/game' run RockPaperScissors diff --git a/lib/app.rb b/lib/app.rb index 785be204..6b1468b5 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -27,8 +27,10 @@ class RockPaperScissors < Sinatra::Base end get '/result' do + @player_name = session[:player].name @player_action = session[:player].action @computer_action = RandomAction.new.action + @result = Game.new.result(@player_action, @computer_action) erb :result end diff --git a/lib/game.rb b/lib/game.rb new file mode 100644 index 00000000..79639aea --- /dev/null +++ b/lib/game.rb @@ -0,0 +1,11 @@ +class Game + def result(player, computer) + if player == computer + return "draw" + elsif (player == "rock" && computer == "scissors") || (player == "paper" && computer == "rock") || (player == "scissors" && computer == "paper") + return "win" + else + return "loss" + end + end +end diff --git a/lib/views/result.erb b/lib/views/result.erb index eb0b12dc..9860cbae 100644 --- a/lib/views/result.erb +++ b/lib/views/result.erb @@ -1 +1,13 @@ -
<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>
+
<%= @player_name %> chose <%= @player_action %>...

+ +
...the computer chose <%= @computer_action %>...

+ +
<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>

+ +<% if @result == "win" %> +
You won!
+<% elsif @result == "draw" %> +
It's a draw!
+<% elsif @result == "loss" %> +
You lost!
+<% end %> diff --git a/spec/features/declare_result_spec.rb b/spec/features/declare_result_spec.rb new file mode 100644 index 00000000..1b5fed75 --- /dev/null +++ b/spec/features/declare_result_spec.rb @@ -0,0 +1,9 @@ +feature 'declare result' do + context 'after player and computer actions have been selected' do + scenario 'diplay result' do + enter_name + click_button 'Rock' + expect(page).to have_content("You won!").or have_content("It's a draw").or have_content("You lost!") + end + end +end diff --git a/spec/units/game_spec.rb b/spec/units/game_spec.rb new file mode 100644 index 00000000..1d64a0e8 --- /dev/null +++ b/spec/units/game_spec.rb @@ -0,0 +1,31 @@ +require './lib/game' + +RSpec.describe Game do + before(:example) do + @game = Game.new + end + + context "when passed identical actions" do + it "declares a draw" do + expect(@game.result("rock", "rock")).to eq "draw" + expect(@game.result("paper", "paper")).to eq "draw" + expect(@game.result("scissors", "scissors")).to eq "draw" + end + end + + context "when player action beats computer action" do + it "declares a win" do + expect(@game.result("rock", "scissors")).to eq "win" + expect(@game.result("paper", "rock")).to eq "win" + expect(@game.result("scissors", "paper")).to eq "win" + end + end + + context "when computer action beats player action" do + it "declares a loss" do + expect(@game.result("rock", "paper")).to eq "loss" + expect(@game.result("paper", "scissors")).to eq "loss" + expect(@game.result("scissors", "rock")).to eq "loss" + end + end +end From 0eaf3a95dcb9123d5b66605ea0804a496b3479e4 Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 17:30:03 +0100 Subject: [PATCH 8/9] Add play again feature --- lib/game.rb | 22 +++++++++++++++------- lib/views/result.erb | 12 ++++++++---- spec/features/declare_result_spec.rb | 2 +- spec/features/play_again_spec.rb | 10 ++++++++++ spec/features/select_player_action_spec.rb | 2 +- 5 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 spec/features/play_again_spec.rb diff --git a/lib/game.rb b/lib/game.rb index 79639aea..06a7b8fe 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,11 +1,19 @@ class Game def result(player, computer) - if player == computer - return "draw" - elsif (player == "rock" && computer == "scissors") || (player == "paper" && computer == "rock") || (player == "scissors" && computer == "paper") - return "win" - else - return "loss" - end + return "draw" if draw?(player, computer) + return "win" if win?(player, computer) + return "loss" + end + + private + + def draw?(player, computer) + return player == computer + end + + def win?(player, computer) + return (player == "rock" && computer == "scissors") || + (player == "paper" && computer == "rock") || + (player == "scissors" && computer == "paper") end end diff --git a/lib/views/result.erb b/lib/views/result.erb index 9860cbae..62f6b82b 100644 --- a/lib/views/result.erb +++ b/lib/views/result.erb @@ -1,8 +1,8 @@ -
<%= @player_name %> chose <%= @player_action %>...

+
<%= @player_name %> chose <%= @player_action.capitalize %>...

-
...the computer chose <%= @computer_action %>...

+
...the Computer chose <%= @computer_action.capitalize %>

-
<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>

+

<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>


<% if @result == "win" %>
You won!
@@ -10,4 +10,8 @@
It's a draw!
<% elsif @result == "loss" %>
You lost!
-<% end %> +<% end %>
+ +
+ +
diff --git a/spec/features/declare_result_spec.rb b/spec/features/declare_result_spec.rb index 1b5fed75..20e54b8c 100644 --- a/spec/features/declare_result_spec.rb +++ b/spec/features/declare_result_spec.rb @@ -1,6 +1,6 @@ feature 'declare result' do context 'after player and computer actions have been selected' do - scenario 'diplay result' do + scenario 'display result' do enter_name click_button 'Rock' expect(page).to have_content("You won!").or have_content("It's a draw").or have_content("You lost!") diff --git a/spec/features/play_again_spec.rb b/spec/features/play_again_spec.rb new file mode 100644 index 00000000..4e58a663 --- /dev/null +++ b/spec/features/play_again_spec.rb @@ -0,0 +1,10 @@ +feature 'play again' do + context 'after result has been declared' do + scenario 'allow user to play again' do + enter_name + click_button 'Rock' + click_button 'Play again' + expect(page).to have_content("Choose Rock, Paper or Scissors") + end + end +end diff --git a/spec/features/select_player_action_spec.rb b/spec/features/select_player_action_spec.rb index dc4ded17..95c8d6e8 100644 --- a/spec/features/select_player_action_spec.rb +++ b/spec/features/select_player_action_spec.rb @@ -1,7 +1,7 @@ feature 'select player action' do scenario 'display available actions to user' do enter_name - expect(page).to have_content('Rock').and have_content('Paper').and have_content('Scissors') + expect(page).to have_content('Choose Rock, Paper or Scissors') end context 'when user selects Rock' do From 95a9f5afcdd039c49a06df2128ab1294d5eb3a8a Mon Sep 17 00:00:00 2001 From: olliefreeman94 Date: Fri, 8 Jul 2022 18:14:46 +0100 Subject: [PATCH 9/9] File tidyup --- lib/views/result.erb | 2 +- notes.txt | 11 +++++++++++ spec/features/register_player_name_spec.rb | 2 +- spec/features/select_player_action_spec.rb | 8 ++++---- spec/features/select_random_action_spec.rb | 2 +- spec/features/test_page_spec.rb | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 notes.txt diff --git a/lib/views/result.erb b/lib/views/result.erb index 62f6b82b..60976902 100644 --- a/lib/views/result.erb +++ b/lib/views/result.erb @@ -1,6 +1,6 @@
<%= @player_name %> chose <%= @player_action.capitalize %>...

-
...the Computer chose <%= @computer_action.capitalize %>

+
...the computer chose <%= @computer_action.capitalize %>

<%= @player_action.capitalize %> vs. <%= @computer_action.capitalize %>


diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000..54a1db03 --- /dev/null +++ b/notes.txt @@ -0,0 +1,11 @@ +To do: +- reformat HTML, and add styling +- add error handling + e.g. user leaves name field blank, + or navigates to /result directly + +Additional features: +- track results + e.g. "You've won 2 out of 5 games" +- track player action selection + e.g. "You choose Rock 40% of the time" diff --git a/spec/features/register_player_name_spec.rb b/spec/features/register_player_name_spec.rb index a2795894..86ef2cf7 100644 --- a/spec/features/register_player_name_spec.rb +++ b/spec/features/register_player_name_spec.rb @@ -1,6 +1,6 @@ feature 'register player name' do scenario 'display name entered by user' do enter_name - expect(page).to have_content('Ollie') + expect(page).to have_content("Ollie") end end diff --git a/spec/features/select_player_action_spec.rb b/spec/features/select_player_action_spec.rb index 95c8d6e8..db4d4b02 100644 --- a/spec/features/select_player_action_spec.rb +++ b/spec/features/select_player_action_spec.rb @@ -1,14 +1,14 @@ feature 'select player action' do scenario 'display available actions to user' do enter_name - expect(page).to have_content('Choose Rock, Paper or Scissors') + expect(page).to have_content("Choose Rock, Paper or Scissors") end context 'when user selects Rock' do scenario 'display Rock as selected action' do enter_name click_button 'Rock' - expect(page).to have_content('Rock') + expect(page).to have_content("Rock") end end @@ -16,7 +16,7 @@ scenario 'display Paper as selected action' do enter_name click_button 'Paper' - expect(page).to have_content('Paper') + expect(page).to have_content("Paper") end end @@ -24,7 +24,7 @@ scenario 'display Scissors as selected action' do enter_name click_button 'Scissors' - expect(page).to have_content('Scissors') + expect(page).to have_content("Scissors") end end end diff --git a/spec/features/select_random_action_spec.rb b/spec/features/select_random_action_spec.rb index ac45790d..398aa1f2 100644 --- a/spec/features/select_random_action_spec.rb +++ b/spec/features/select_random_action_spec.rb @@ -2,6 +2,6 @@ scenario 'display random action for computer' do enter_name click_button 'Rock' - expect(page).to have_content('Rock vs. Rock').or have_content('Rock vs. Scissors').or have_content('Rock vs. Paper') + expect(page).to have_content("Rock vs. Rock").or have_content("Rock vs. Scissors").or have_content("Rock vs. Paper") end end diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index 5d6e8919..0e72a5cc 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,6 +1,6 @@ feature 'test page' do scenario 'visit test page' do visit '/test' - expect(page).to have_content('test page') + expect(page).to have_content("test page") end end