Skip to content

Instantly share code, notes, and snippets.

@davelyon
Created January 15, 2010 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davelyon/278254 to your computer and use it in GitHub Desktop.
Save davelyon/278254 to your computer and use it in GitHub Desktop.
require 'test/unit'
class Piece
def self.beats(type)
self.instance_eval do
define_method "beats_#{type}?".intern do
true
end
end
end
def method_missing(type)
false
end
end
class Rock < Piece
beats :scissors
end
class Paper < Piece
beats :rock
end
class Scissors < Piece
beats :paper
end
# TEST METHODS
class RPS_Test < Test::Unit::TestCase
def setup
@rock = Rock.new()
@scissors = Scissors.new()
@paper = Paper.new()
end
def test_rock_should_beat_scissors
assert(@rock.beats_scissors?, "Rock should beat scissors.")
end
def test_scissors_should_beat_paper
assert(@scissors.beats_paper?, "Scissors should beat paper.")
end
def test_paper_should_beat_rock
assert(@paper.beats_rock?, "Paper should beat rock.")
end
def test_scissors_should_lose_to_rock
assert(!@scissors.beats_rock?, "Scissors cannot beat rock.")
end
def test_rock_should_lose_to_paper
assert(!@rock.beats_paper?, "Rock cannot beat paper.")
end
def test_paper_should_lose_to_scissors
assert(!@paper.beats_scissors?, "Paper cannot beat scissors.")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment