From 9bac107b31f05cca65d8aab23be05e1b867bd289 Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Thu, 3 Nov 2016 16:28:16 -0700 Subject: [PATCH] Add Version::NULL singleton --- Library/Homebrew/test/test_versions.rb | 23 ++++++++++++++++ Library/Homebrew/version.rb | 6 ++++ Library/Homebrew/version/null.rb | 38 ++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 Library/Homebrew/version/null.rb diff --git a/Library/Homebrew/test/test_versions.rb b/Library/Homebrew/test/test_versions.rb index 21bf324a38..6f85fe7a06 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.new.inspect diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb index 60a8336095..433964dc70 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 0000000000..77106bcce2 --- /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