Skip to content

Instantly share code, notes, and snippets.

@nestorsalceda
Created March 19, 2015 17:23
Show Gist options
  • Save nestorsalceda/1b80a20ebd1d89ae33cb to your computer and use it in GitHub Desktop.
Save nestorsalceda/1b80a20ebd1d89ae33cb to your computer and use it in GitHub Desktop.
class Score
attr_reader :server_score, :receiver_score
def initialize(server_score, receiver_score)
@server_score = server_score
@receiver_score = receiver_score
end
def server_wins_ball
@server_score = increment_score(@server_score)
deuce
end
def receiver_wins_ball
@receiver_score = increment_score(@receiver_score)
deuce
end
private
def increment_score(score)
if score == 30
score += 10
elsif score == 40
score = "A"
else
score += 15
end
score
end
def deuce
if @server_score == 'A' and @receiver_score == 'A'
@server_score = 40
@receiver_score = 40
end
end
end
describe 'Tennis score' do
context "when the score is 0 - 0" do
context "and server wins a point" do
it "the score is 15 - 0" do
score = Score.new(0, 0)
score.server_wins_ball
expect(score.server_score).to eq(15)
expect(score.receiver_score).to eq(0)
end
end
end
context "when the score is 15 - 15" do
context "and receiver wins a point" do
it "the score is 15 - 30" do
score = Score.new(15, 15)
score.receiver_wins_ball
expect(score.server_score).to eq(15)
expect(score.receiver_score).to eq(30)
end
end
end
context "when the score is 30 - 30" do
context "and server wins a point" do
it "the score is 40 - 30" do
score = Score.new(30, 30)
score.server_wins_ball
expect(score.server_score).to eq(40)
expect(score.receiver_score).to eq(30)
end
end
context "and receiver wins a point" do
it "the score is 30 - 40" do
score = Score.new(30, 30)
score.receiver_wins_ball
expect(score.server_score).to eq(30)
expect(score.receiver_score).to eq(40)
end
end
end
#caso de 40 iguales
context "given the score to A:40 and receiver wins a point" do
it "Deuce 40 ==" do
score = Score.new('A',40)
score.receiver_wins_ball
expect(score.server_score).to eq(40)
expect(score.receiver_score).to eq(40)
end
end
context "when the score is 40 - 40" do
it "and the receiver wins a point" do
score = Score.new(40,40)
score.receiver_wins_ball
expect(score.server_score).to eq(40)
expect(score.receiver_score).to eq("A")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment