Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test to Home page #251

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
ruby '3.0.2'

gem 'sinatra'
gem 'webrick'

group :test do
gem 'capybara'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ GEM
unicode-display_width (>= 1.1.1, < 3)
tilt (2.0.10)
unicode-display_width (1.6.1)
webrick (1.7.0)
xpath (3.2.0)
nokogiri (~> 1.8)

Expand All @@ -96,6 +97,7 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
31 changes: 29 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
require 'sinatra/base'


class RockPaperScissors < Sinatra::Base
get '/test' do
'test page'

enable :sessions

# get '/test' do
# 'home page'
# end

get '/' do
erb :index
end

post '/names' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation isn't in line with rest of code

session[:player_name] = params[:player_name]
redirect '/play'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be done without /names? Maybe in /play instead to save having to redirect the user?

end

get '/play' do
@player_name = session[:player_name]
erb :play
end

get '/result' do
@player_name = session[:player_name]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation needs fixing

erb :result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this, you should access the button/input value from the user (rock, paper or scissors).

You could then sample the three choices for the computer, and work out a result before returning to the Result page.

end




run! if app_file == $0
end
38 changes: 34 additions & 4 deletions spec/features/test_page_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
feature 'test page' do
scenario 'visit test page' do
visit '/test'
expect(page).to have_content('test page')
# feature 'test page' do
# scenario 'visit test page' do
# visit '/test'
# expect(page).to have_content('home page')
# end
# end


feature 'Enter name' do
scenario 'submitting name' do
sign_in_and_play
# save_and_open_page # will save the web page and open the browser to display it
expect(page).to have_content 'Hannah vs. Computer'
end
end

feature 'Display reasults' do
scenario 'choose rock' do
sign_in_and_play
click_button 'Rock'
expect(page).to have_content 'Hannah, the results are in!'
end

scenario 'choose paper' do
sign_in_and_play
click_button 'Paper'
expect(page).to have_content 'Hannah, the results are in!'
end

scenario 'choose scissors' do
sign_in_and_play
click_button 'Scissors'
expect(page).to have_content 'Hannah, the results are in!'
end
end

5 changes: 5 additions & 0 deletions spec/features/web_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def sign_in_and_play
visit('/')
fill_in :player_name, with: 'Hannah'
click_button 'Submit'
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'capybara/rspec'
require 'simplecov'
require 'simplecov-console'
require 'features/web_helpers'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
Expand Down
16 changes: 16 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML structure isn't required for ERB files to work

<html>
<head>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<h2>Sign in to play</h2>
<form action="/names" method="post">
<label for="player_name">
Enter name
<input type="text" name="player_name">
</label>
<input type="submit" value="Submit">
</form>
</body>
</html>
18 changes: 18 additions & 0 deletions views/play.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%= @player_name %> vs. Computer

<p>Make your selection: Rock, Paper or Scissors</p>

<form action="/result" method="get" class="rps">
<input type="submit" value="Rock">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a button might be more helpful - "Input" does take a value but it might be more useful. Button requires a name, which can then be accessed by app.rb

<input type="submit" value="Paper">
<input type="submit" value="Scissors">
</form>


</body>
</html>
9 changes: 9 additions & 0 deletions views/result.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%= @player_name %>, the results are in!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs results

</body>
</html>