Created
May 4, 2009 11:12
-
-
Save zmalltalker/106423 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'test/unit' | |
class Size | |
include Comparable | |
attr_reader :size | |
SIZE_LIST = %w(xxs xs s m l xl xxl) | |
def self.method_missing(m,*args,&blk) | |
if SIZE_LIST.include?(m.to_s) | |
return new(m.to_s) | |
else | |
super | |
end | |
end | |
def initialize(s) | |
@size = s.downcase | |
end | |
def <=>(another) | |
return SIZE_LIST.index(size) <=> SIZE_LIST.index(another.size) | |
end | |
end | |
class SizeTest < Test::Unit::TestCase | |
def test_comparable | |
xl = Size.new('XL') | |
l = Size.new('L') | |
m = Size.new('M') | |
assert xl > l | |
assert l > m | |
assert xl > m | |
m2 = Size.new('m') | |
assert m2 == m | |
end | |
def test_magic_methods | |
xl = Size.xxl | |
l = Size.xl | |
assert xl > l | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment