Add Version::NULL singleton

This commit is contained in:
Misty De Meo 2016-11-03 16:28:16 -07:00
parent c2815fbb9a
commit 9bac107b31
3 changed files with 67 additions and 0 deletions

View File

@ -30,6 +30,29 @@ class VersionTokenTests < Homebrew::TestCase
end end
end end
class NullVersionTests < Homebrew::TestCase
def test_null_version_is_always_smaller
assert_operator Version::NULL, :<, version("1")
end
def test_null_version_is_never_greater
refute_operator Version::NULL, :>, version("0")
end
def test_null_version_is_not_equal_to_itself
refute_eql Version::NULL, Version::NULL
end
def test_null_version_creates_an_empty_string
assert_eql "", Version::NULL.to_s
end
def test_null_version_produces_nan_as_a_float
# Float::NAN is not equal to itself so compare object IDs
assert_eql Float::NAN.object_id, Version::NULL.to_f.object_id
end
end
class VersionNullTokenTests < Homebrew::TestCase class VersionNullTokenTests < Homebrew::TestCase
def test_inspect def test_inspect
assert_equal "#<Version::NullToken>", Version::NullToken.new.inspect assert_equal "#<Version::NullToken>", Version::NullToken.new.inspect

View File

@ -1,3 +1,5 @@
require "version/null"
class Version class Version
include Comparable include Comparable
@ -206,6 +208,10 @@ class Version
false false
end end
def null?
false
end
def <=>(other) def <=>(other)
return unless other.is_a?(Version) return unless other.is_a?(Version)
return 0 if version == other.version return 0 if version == other.version

View File

@ -0,0 +1,38 @@
class Version
NULL = Class.new do
include Comparable
def <=>(_other)
-1
end
def eql?(_other)
# Makes sure that the same instance of Version::NULL
# will never equal itself; normally Comparable#==
# will return true for this regardless of the return
# value of #<=>
false
end
def detected_from_url?
false
end
def head?
false
end
def null?
true
end
def to_f
Float::NAN
end
def to_s
""
end
alias_method :to_str, :to_s
end.new
end