Skip to content

Instantly share code, notes, and snippets.

@louis-wu
Created January 25, 2010 18:53
Show Gist options
  • Save louis-wu/286118 to your computer and use it in GitHub Desktop.
Save louis-wu/286118 to your computer and use it in GitHub Desktop.
Ruby rspec测试:tennis scorer
#---
# Excerpted from "Programming Ruby", P210-214
# 运行测试:$ spec rspec_tennis_scorer.rb
#---
require "tennis_scorer"
describe TennisScorer, "basic scoring" do
before(:each) do
@ts = TennisScorer.new
end
it "should start with a score of 0-0" do
@ts.score.should == "0-0"
end
it "should be 15-0 if the server wins a point" do
@ts.give_point_to(:server)
@ts.score.should == "15-0"
end
it "should be 0-15 if the receiver wins a point" do
@ts.give_point_to(:receiver)
@ts.score.should == "0-15"
end
it "should be 15-15 after they both win a point" do
@ts.give_point_to(:receiver)
@ts.give_point_to(:server)
@ts.score.should == "15-15"
end
end
#---
# Excerpted from "Programming Ruby", P210-214
#---
class TennisScorer
OPPOSITE_SIDE_OF_NET = {
:server => :receiver,
:receiver => :server
}
def initialize
@score = { :server => 0, :receiver => 0 }
end
def score
"#{@score[:server]*15}-#{@score[:receiver]*15}"
end
def give_point_to(player)
other = OPPOSITE_SIDE_OF_NET[player]
fail "Unknown player #{player}" unless other
@score[player] += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment