Skip to content

Commit

Permalink
Add Version::NULL singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Nov 10, 2016
1 parent c2815fb commit 9bac107
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Library/Homebrew/test/test_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def test_to_s
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
def test_inspect
assert_equal "#<Version::NullToken>", Version::NullToken.new.inspect
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "version/null"

class Version
include Comparable

Expand Down Expand Up @@ -206,6 +208,10 @@ def head?
false
end

def null?
false
end

def <=>(other)
return unless other.is_a?(Version)
return 0 if version == other.version
Expand Down
38 changes: 38 additions & 0 deletions Library/Homebrew/version/null.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9bac107

Please sign in to comment.