aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew
diff options
context:
space:
mode:
authorMisty De Meo2016-11-03 16:28:16 -0700
committerMisty De Meo2016-11-10 15:08:36 -0800
commit9bac107b31f05cca65d8aab23be05e1b867bd289 (patch)
treef6e7684d7fe576b742a3d322d7426613c8aebb9b /Library/Homebrew
parentc2815fbb9af4fe4518246cba7df418935fd3b711 (diff)
downloadbrew-9bac107b31f05cca65d8aab23be05e1b867bd289.tar.bz2
Add Version::NULL singleton
Diffstat (limited to 'Library/Homebrew')
-rw-r--r--Library/Homebrew/test/test_versions.rb23
-rw-r--r--Library/Homebrew/version.rb6
-rw-r--r--Library/Homebrew/version/null.rb38
3 files changed, 67 insertions, 0 deletions
diff --git a/Library/Homebrew/test/test_versions.rb b/Library/Homebrew/test/test_versions.rb
index 21bf324a3..6f85fe7a0 100644
--- a/Library/Homebrew/test/test_versions.rb
+++ b/Library/Homebrew/test/test_versions.rb
@@ -30,6 +30,29 @@ class VersionTokenTests < Homebrew::TestCase
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
diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb
index 60a833609..433964dc7 100644
--- a/Library/Homebrew/version.rb
+++ b/Library/Homebrew/version.rb
@@ -1,3 +1,5 @@
+require "version/null"
+
class Version
include Comparable
@@ -206,6 +208,10 @@ class Version
false
end
+ def null?
+ false
+ end
+
def <=>(other)
return unless other.is_a?(Version)
return 0 if version == other.version
diff --git a/Library/Homebrew/version/null.rb b/Library/Homebrew/version/null.rb
new file mode 100644
index 000000000..77106bcce
--- /dev/null
+++ b/Library/Homebrew/version/null.rb
@@ -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