-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Juan Pinzon
committed
Jul 12, 2015
1 parent
c2ddf8f
commit dce846f
Showing
34 changed files
with
1,600 additions
and
0 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,186 @@ | ||
require 'mechanize' | ||
require 'json' | ||
|
||
# A module that mechs the following spec tests look much cleaner | ||
module MechanizeHelpers | ||
|
||
@@BASE_URI = ENV['HEROKU_URI'] !~ /^http:\/\// ? "http://" + ENV['HEROKU_URI'] : ENV['HEROKU_URI'] | ||
|
||
def guess(letter) | ||
page = @mech.current_page | ||
page.form_with(:action => '/guess') do |f| | ||
f.field_with(:name => 'guess').value = letter | ||
end.click_button | ||
end | ||
|
||
def guesses(letters) | ||
letters.each_char do |letter| | ||
guess letter | ||
end | ||
end | ||
|
||
def new_game(word) | ||
@mech.post(@@BASE_URI + "/create", {:word => word}) | ||
end | ||
|
||
def click_new_game | ||
page = @mech.get(@@BASE_URI + "/new") | ||
form = page.form_with(:action => '/create') | ||
form.click_button | ||
end | ||
|
||
def word_board | ||
page = @mech.current_page | ||
page.uri.request_uri.should eq("/show") | ||
page.at("span.word").content | ||
end | ||
|
||
def wrong_guesses | ||
page = @mech.current_page | ||
page.uri.request_uri.should eq "/show" | ||
page.at("span.guesses").content | ||
end | ||
|
||
def flash_message | ||
page = @mech.current_page | ||
msg = page.at("span.error") | ||
if msg.nil? | ||
"" | ||
else | ||
msg.content | ||
end | ||
end | ||
|
||
def goto(route) | ||
@mech.get(@@BASE_URI + route) | ||
end | ||
|
||
def current_route | ||
@mech.current_page.uri.request_uri | ||
end | ||
end | ||
|
||
# BEGIN SPEC TESTS | ||
# (cucumber tests in disguise) | ||
describe "Hangperson" do | ||
|
||
include MechanizeHelpers | ||
before :each do | ||
@mech = Mechanize.new | ||
end | ||
|
||
describe "game over" do | ||
it "because the word is guessed [5 points]" do | ||
new_game "foobar" | ||
guesses "fobar" | ||
expect(current_route).to eq("/win") | ||
end | ||
it "because the guess limit is reached [5 points]" do | ||
new_game "zebra" | ||
guesses "isuckhar" | ||
expect(current_route).to eq("/show") | ||
guess "d" | ||
expect(current_route).to eq("/lose") | ||
end | ||
end | ||
|
||
describe "guessing" do | ||
it "should reveal the letter if it is correct [5 points]" do | ||
new_game "garply" | ||
guess "g" | ||
expect(word_board).to eq("g-----") | ||
end | ||
it "should reveal the letter if it appears many times [5 points]" do | ||
new_game "animal" | ||
guess "a" | ||
expect(word_board).to eq("a---a-") | ||
end | ||
it "should add to wrong guesses when incorrect [5 points]" do | ||
new_game "xylophone" | ||
guess "a" | ||
expect(wrong_guesses).to eq("a") | ||
end | ||
it "should continuously track right and wrong guesses [5 points]" do | ||
new_game "foobar" | ||
guesses "azxo" | ||
expect(word_board).to eq("-oo-a-") | ||
expect(wrong_guesses).to eq("zx") | ||
end | ||
it "should treat guesses as case insensitive [5 points]" do | ||
new_game "fiddlesticks" | ||
guess "t" | ||
word_board1 = word_board | ||
guess "T" | ||
word_board2 = word_board | ||
guess "q" | ||
wrong_guesses1 = wrong_guesses | ||
guess "Q" | ||
wrong_guesses2 = wrong_guesses | ||
expect(word_board1).to eq(word_board2) | ||
expect(wrong_guesses1).to eq(wrong_guesses2) | ||
end | ||
end | ||
|
||
describe "guessing repeats" do | ||
it "should not count as wrong when correct [5 points]" do | ||
new_game "bumblebee" | ||
guess "b" | ||
guess "b" | ||
guess "b" | ||
expect(word_board).to eq("b--b--b--") | ||
end | ||
it "should not double count a repeated wrong guess [5 points]" do | ||
new_game "giraffe" | ||
guess "z" | ||
guess "z" | ||
expect(wrong_guesses).to eq("z") | ||
end | ||
it "should not count a repeated wrong guess towards losing [5 points]" do | ||
new_game "snake" | ||
30.times do | ||
guess "z" | ||
end | ||
expect(current_route).to eq("/show") | ||
end | ||
it "should display an error message [5 points]" do | ||
new_game "figs" | ||
guess "i" | ||
guess "i" | ||
expect(flash_message).to eq("You have already used that letter.") | ||
end | ||
end | ||
|
||
describe "invalid guess" do | ||
it "should label a non-letter as invalid [5 points]" do | ||
new_game "helterskelter" | ||
guess "^" | ||
expect(flash_message).to eq("Invalid guess.") | ||
end | ||
it "should label a blank form as invalid [5 points]" do | ||
new_game "pumpkin" | ||
guess "" | ||
expect(flash_message).to eq("Invalid guess.") | ||
end | ||
end | ||
|
||
describe "starting a new game" do | ||
it "should take me to the show page [5 points]" do | ||
click_new_game | ||
expect(current_route).to eq("/show") | ||
end | ||
end | ||
|
||
describe "cheating" do | ||
it "should not be able to fake a win [5 points]" do | ||
new_game "math" | ||
goto "/win" | ||
expect(current_route).to eq("/show") | ||
end | ||
it "should not be able to fake a loss [5 points]" do | ||
new_game "howdy" | ||
goto "/lose" | ||
expect(current_route).to eq("/show") | ||
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'spec_helper' | ||
require 'json' | ||
|
||
describe "Hangperson" do | ||
describe "redirects -" do | ||
it "GET '/' should redirect to '/new'" do | ||
get '/' | ||
last_response.should be_redirect | ||
follow_redirect! | ||
last_request.url.should include('/new') | ||
end | ||
|
||
it "GET '/create' should redirect to '/show'" do | ||
post '/create' | ||
last_response.should be_redirect | ||
follow_redirect! | ||
last_request.url.should include('/show') | ||
end | ||
end | ||
|
||
describe "initialization" do | ||
it "should initialize session values" do | ||
stub_random_word('garply') | ||
post '/create' | ||
session[:word].should == 'garply' | ||
session[:guesses].should == "" | ||
session[:wrong_guesses].should == "" | ||
end | ||
end | ||
|
||
describe "word_with_guesses " do | ||
it "should be blank with no guesses" do | ||
@app = app | ||
@app.word_with_guesses('foobar', '').should == "------" | ||
end | ||
it "should fill in single correct guesses" do | ||
app.word_with_guesses('garply', 'g').should == "g-----" | ||
app.word_with_guesses('foobar', 'o').should == "-oo---" | ||
app.word_with_guesses('zebra', 'b').should == "--b--" | ||
end | ||
it "should completely fill in >1 guesses" do | ||
app.word_with_guesses('razmataz', 'zm').should == '--zm---z' | ||
app.word_with_guesses('hooplah', 'ohpal').should == 'hooplah' | ||
end | ||
end | ||
|
||
describe "guessing" do | ||
it "should increase wrong guesses when incorrect" do | ||
post '/guess', {:guess => 'a'}, "rack.session" => {:word => "xylophone", :guesses => "xy", :wrong_guesses => "t"} | ||
session[:wrong_guesses].should == 'ta' | ||
end | ||
it "should increase right guesses when correct" do | ||
#set_session("animal", "ai", "z") | ||
post '/guess', {:guess => 'n'}, "rack.session" => {:word => "animal", :guesses => "ai", :wrong_guesses => "z" } | ||
session[:guesses].should == "ain" | ||
end | ||
it "should not log the same correct guess twice" do | ||
post '/guess', {:guess => 'o'}, "rack.session" => {:word => 'foobar', :guesses => 'of', :wrong_guesses => 'xyz'} | ||
session[:guesses].should == 'of' | ||
session[:wrong_guesses].should == 'xyz' | ||
end | ||
it "should not log the same incorrect guess twice" do | ||
post '/guess', {:guess => 'y'}, "rack.session" => {:word => 'foobar', :guesses => 'of', :wrong_guesses => 'xyz'} | ||
session[:guesses].should == 'of' | ||
session[:wrong_guesses].should == 'xyz' | ||
end | ||
end | ||
|
||
describe "after game ends" do | ||
it "redirects to '/loss' when 7 wrong guesses are reached" do | ||
session = { "rack.session" => {:word => "tribulation", :guesses => "aibtorul", :wrong_guesses => "xyzwqpj"} } | ||
get '/show', {}, session | ||
last_response.should be_redirect | ||
follow_redirect! | ||
last_request.url.should include('/lose') | ||
end | ||
it "redirects to '/win' when the full word is guessed" do | ||
session = { "rack.session" => {:word => "tribulation", :guesses => "aibtnorul", :wrong_guesses => "xyzwqp"} } | ||
get '/show', {}, session | ||
last_response.should be_redirect | ||
follow_redirect! | ||
last_request.url.should include('/win') | ||
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Feature: prevent cheating | ||
|
||
As a game designer | ||
To prevent cheating | ||
I want to make sure winning and losing cannot be faked | ||
|
||
Background: I am in the middle of a game | ||
|
||
Given I start a new game with word "cheat" | ||
|
||
Scenario: cannot fake winning | ||
|
||
When I try to go to the URL "/win" | ||
Then I should be on the show page | ||
|
||
Scenario: cannot fake losing | ||
|
||
When I try to go to the URL "/lose" | ||
Then I should be on the show page | ||
|
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,8 @@ | ||
Still TBD for final version: | ||
|
||
- explain views/layout.erb | ||
- explain Procfile | ||
|
||
- remove this file | ||
- merge everything to master | ||
|
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,3 @@ | ||
coverage | ||
.ruby-version | ||
*.swp |
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 @@ | ||
--format nested --colour |
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,3 @@ | ||
SimpleCov.start do | ||
add_filter '/features/' | ||
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,20 @@ | ||
source 'https://rubygems.org' | ||
ruby '1.9.3' | ||
|
||
gem 'sinatra', '>= 1.4' | ||
gem 'sinatra-flash' | ||
|
||
group :development, :test do | ||
gem 'capybara' | ||
gem 'cucumber' | ||
gem 'cucumber-sinatra' | ||
gem 'debugger' | ||
gem 'launchy' | ||
gem 'rspec' | ||
gem 'rack-test' | ||
gem 'rack_session_access' | ||
gem 'rerun' | ||
gem 'simplecov' | ||
gem 'webmock' | ||
gem 'ZenTest' | ||
end |
Oops, something went wrong.