Fix BundleVersion comparison.
This commit is contained in:
parent
53de9f38a2
commit
a7cc9a8b41
@ -62,8 +62,24 @@ module Homebrew
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
[version, short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL } <=>
|
||||
[other.version, other.short_version].map { |v| v&.yield_self(&Version.public_method(:new)) || Version::NULL }
|
||||
return super unless instance_of?(other.class)
|
||||
|
||||
make_version = ->(v) { v ? Version.new(v) : Version::NULL }
|
||||
|
||||
version = self.version.then(&make_version)
|
||||
other_version = other.version.then(&make_version)
|
||||
|
||||
difference = version <=> other_version
|
||||
|
||||
# If `version` is equal or cannot be compared, compare `short_version` instead.
|
||||
if difference.nil? || difference.zero?
|
||||
short_version = self.short_version.then(&make_version)
|
||||
other_short_version = other.short_version.then(&make_version)
|
||||
|
||||
return short_version <=> other_short_version
|
||||
end
|
||||
|
||||
difference
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
|
||||
@ -3,6 +3,28 @@
|
||||
require "bundle_version"
|
||||
|
||||
describe Homebrew::BundleVersion do
|
||||
describe "#<=>" do
|
||||
it "compares both the `short_version` and `version`" do
|
||||
expect(described_class.new("1.2.3", "3000")).to be < described_class.new("1.2.3", "4000")
|
||||
expect(described_class.new("1.2.3", "4000")).to be <= described_class.new("1.2.3", "4000")
|
||||
expect(described_class.new("1.2.3", "4000")).to be >= described_class.new("1.2.3", "4000")
|
||||
expect(described_class.new("1.2.4", "4000")).to be > described_class.new("1.2.3", "4000")
|
||||
end
|
||||
|
||||
it "compares `version` first" do
|
||||
expect(described_class.new("1.2.4", "3000")).to be < described_class.new("1.2.3", "4000")
|
||||
end
|
||||
|
||||
it "does not fail when `short_version` or `version` is missing" do
|
||||
expect(described_class.new("1.06", nil)).to be < described_class.new("1.12", "1.12")
|
||||
expect(described_class.new("1.06", "471")).to be > described_class.new(nil, "311")
|
||||
expect(described_class.new("1.2.3", nil)).to be < described_class.new("1.2.4", nil)
|
||||
expect(described_class.new(nil, "1.2.3")).to be < described_class.new(nil, "1.2.4")
|
||||
expect(described_class.new("1.2.3", nil)).to be < described_class.new(nil, "1.2.3")
|
||||
expect(described_class.new(nil, "1.2.3")).to be > described_class.new("1.2.3", nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#nice_version" do
|
||||
expected_mappings = {
|
||||
["1.2", nil] => "1.2",
|
||||
@ -25,10 +47,4 @@ describe Homebrew::BundleVersion do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#<=>" do
|
||||
it "does not fail when a `version` is nil" do
|
||||
expect(described_class.new("1.06", nil)).to be < described_class.new("1.12", "1.12")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user