Created
August 13, 2018 11:15
-
-
Save craigjbass/cca4b1748c46eae076533a552c8d48d7 to your computer and use it in GitHub Desktop.
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
class Tennis | |
def initialize | |
@player1_score = 'Zero' | |
@player2_score = 'Zero' | |
end | |
def won_point(player) | |
if player == :player1 | |
@player1_score = next_score_for(@player1_score) | |
else | |
@player2_score = next_score_for(@player2_score) | |
end | |
end | |
def score | |
"#{@player1_score}-#{@player2_score}" | |
end | |
private | |
NEXT_SCORES = { | |
'Zero' => 'Fifteen', | |
'Fifteen' => 'Thirty', | |
'Thirty' => 'Forty' | |
}.freeze | |
def next_score_for(player_score) | |
NEXT_SCORES[player_score] | |
end | |
end | |
describe Tennis do | |
def expect_score_to_be(expected) | |
expect(tennis.score).to eq(expected) | |
end | |
let(:tennis) { Tennis.new } | |
it 'can score a game where no player has scored' do | |
expect_score_to_be('Zero-Zero') | |
end | |
it 'can score a game where player one has won a single point' do | |
tennis.won_point(:player1) | |
expect_score_to_be('Fifteen-Zero') | |
end | |
it 'can score a game where player one has won two points' do | |
2.times { tennis.won_point(:player1) } | |
expect_score_to_be('Thirty-Zero') | |
end | |
it 'can score a game where player one has won three points' do | |
3.times { tennis.won_point(:player1) } | |
expect_score_to_be('Forty-Zero') | |
end | |
it 'can score a game where player two has won one point' do | |
1.times { tennis.won_point(:player2) } | |
expect_score_to_be('Zero-Fifteen') | |
end | |
it 'can score a game where player two has won three points' do | |
3.times { tennis.won_point(:player2) } | |
expect_score_to_be('Zero-Forty') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment