2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2011-01-28 18:46:47 -05:00
|
|
|
require 'rubygems/test_case'
|
2011-01-18 19:08:49 -05:00
|
|
|
require "rubygems/version"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
require "minitest/benchmark"
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemVersion < Gem::TestCase
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2014-02-05 17:18:25 -05:00
|
|
|
class V < ::Gem::Version
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_bump
|
|
|
|
assert_bumped_version_equal "5.3", "5.2.4"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_bump_alpha
|
|
|
|
assert_bumped_version_equal "5.3", "5.2.4.a"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def test_bump_alphanumeric
|
|
|
|
assert_bumped_version_equal "5.3", "5.2.4.a10"
|
|
|
|
end
|
|
|
|
|
2007-12-20 03:39:12 -05:00
|
|
|
def test_bump_trailing_zeros
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_bumped_version_equal "5.1", "5.0.0"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_bump_one_level
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_bumped_version_equal "6", "5"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
# A Gem::Version is already a Gem::Version and therefore not transformed by
|
|
|
|
# Gem::Version.create
|
2007-12-20 03:39:12 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_class_create
|
2013-10-15 20:14:16 -04:00
|
|
|
real = Gem::Version.new(1.0)
|
2007-12-20 03:39:12 -05:00
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
assert_same real, Gem::Version.create(real)
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_nil Gem::Version.create(nil)
|
|
|
|
assert_equal v("5.1"), Gem::Version.create("5.1")
|
2012-11-29 03:10:23 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
ver = '1.1'.freeze
|
|
|
|
assert_equal v('1.1'), Gem::Version.create(ver)
|
2007-12-20 03:39:12 -05:00
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def test_class_correct
|
|
|
|
assert_equal true, Gem::Version.correct?("5.1")
|
|
|
|
assert_equal false, Gem::Version.correct?("an incorrect version")
|
2018-08-27 06:05:04 -04:00
|
|
|
|
|
|
|
expected = "nil versions are discouraged and will be deprecated in Rubygems 4\n"
|
|
|
|
assert_output nil, expected do
|
|
|
|
Gem::Version.correct?(nil)
|
|
|
|
end
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
|
2014-02-05 17:18:25 -05:00
|
|
|
def test_class_new_subclass
|
|
|
|
v1 = Gem::Version.new '1'
|
|
|
|
v2 = V.new '1'
|
|
|
|
|
|
|
|
refute_same v1, v2
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_eql_eh
|
2011-01-18 19:08:49 -05:00
|
|
|
assert_version_eql "1.2", "1.2"
|
|
|
|
refute_version_eql "1.2", "1.2.0"
|
|
|
|
refute_version_eql "1.2", "1.3"
|
|
|
|
refute_version_eql "1.2.b1", "1.2.b.1"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def test_equals2
|
|
|
|
assert_version_equal "1.2", "1.2"
|
|
|
|
refute_version_equal "1.2", "1.3"
|
|
|
|
assert_version_equal "1.2.b1", "1.2.b.1"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# REVISIT: consider removing as too impl-bound
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_hash
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_equal v("1.2").hash, v("1.2").hash
|
|
|
|
refute_equal v("1.2").hash, v("1.3").hash
|
2017-10-07 21:32:18 -04:00
|
|
|
assert_equal v("1.2").hash, v("1.2.0").hash
|
|
|
|
assert_equal v("1.2.pre.1").hash, v("1.2.0.pre.1.0").hash
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_initialize
|
2012-11-29 01:52:18 -05:00
|
|
|
["1.0", "1.0 ", " 1.0 ", "1.0\n", "\n1.0\n", "1.0".freeze].each do |good|
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_version_equal "1.0", good
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_version_equal "1", 1
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def test_initialize_invalid
|
|
|
|
invalid_versions = %W[
|
2013-09-24 20:53:19 -04:00
|
|
|
junk
|
|
|
|
1.0\n2.0
|
|
|
|
1..2
|
|
|
|
1.2\ 3.4
|
2017-10-07 21:32:18 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
# DON'T TOUCH THIS WITHOUT CHECKING CVE-2013-4287
|
|
|
|
invalid_versions << "2.3422222.222.222222222.22222.ads0as.dasd0.ddd2222.2.qd3e."
|
|
|
|
|
|
|
|
invalid_versions.each do |invalid|
|
|
|
|
e = assert_raises ArgumentError, invalid do
|
|
|
|
Gem::Version.new invalid
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert_equal "Malformed version number string #{invalid}", e.message, invalid
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
def bench_anchored_version_pattern
|
|
|
|
assert_performance_linear 0.5 do |count|
|
|
|
|
version_string = count.times.map {|i| "0" * i.succ }.join(".") << "."
|
|
|
|
version_string =~ Gem::Version::ANCHORED_VERSION_PATTERN
|
|
|
|
end
|
|
|
|
rescue RegexpError
|
|
|
|
skip "It fails to allocate the memory for regex pattern of Gem::Version::ANCHORED_VERSION_PATTERN"
|
|
|
|
end
|
|
|
|
|
2017-01-23 21:38:57 -05:00
|
|
|
def test_empty_version
|
|
|
|
["", " ", " "].each do |empty|
|
|
|
|
assert_equal "0", Gem::Version.new(empty).version
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def test_prerelease
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_prerelease "1.2.0.a"
|
|
|
|
assert_prerelease "2.9.b"
|
|
|
|
assert_prerelease "22.1.50.0.d"
|
|
|
|
assert_prerelease "1.2.d.42"
|
|
|
|
|
|
|
|
assert_prerelease '1.A'
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
assert_prerelease '1-1'
|
|
|
|
assert_prerelease '1-a'
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
refute_prerelease "1.2.0"
|
|
|
|
refute_prerelease "2.9"
|
|
|
|
refute_prerelease "22.1.50.0"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2009-12-08 02:19:09 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def test_release
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_release_equal "1.2.0", "1.2.0.a"
|
|
|
|
assert_release_equal "1.1", "1.1.rc10"
|
|
|
|
assert_release_equal "1.9.3", "1.9.3.alpha.5"
|
|
|
|
assert_release_equal "1.9.3", "1.9.3"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_spaceship
|
2019-02-14 07:59:03 -05:00
|
|
|
assert_equal(0, v("1.0") <=> v("1.0.0"))
|
|
|
|
assert_equal(1, v("1.0") <=> v("1.0.a"))
|
|
|
|
assert_equal(1, v("1.8.2") <=> v("0.0.0"))
|
|
|
|
assert_equal(1, v("1.8.2") <=> v("1.8.2.a"))
|
|
|
|
assert_equal(1, v("1.8.2.b") <=> v("1.8.2.a"))
|
|
|
|
assert_equal(-1, v("1.8.2.a") <=> v("1.8.2"))
|
|
|
|
assert_equal(1, v("1.8.2.a10") <=> v("1.8.2.a9"))
|
|
|
|
assert_equal(0, v("") <=> v("0"))
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2019-04-26 07:26:21 -04:00
|
|
|
assert_equal(0, v("0.beta.1") <=> v("0.0.beta.1"))
|
|
|
|
assert_equal(-1, v("0.0.beta") <=> v("0.0.beta.1"))
|
|
|
|
assert_equal(-1, v("0.0.beta") <=> v("0.beta.1"))
|
|
|
|
|
|
|
|
assert_equal(-1, v("5.a") <=> v("5.0.0.rc2"))
|
|
|
|
assert_equal(1, v("5.x") <=> v("5.0.0.rc2"))
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
assert_nil v("1.0") <=> "whatever"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_approximate_recommendation
|
|
|
|
assert_approximate_equal "~> 1.0", "1"
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_approximate_satisfies_itself "1"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_approximate_equal "~> 1.0", "1.0"
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_approximate_satisfies_itself "1.0"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_approximate_equal "~> 1.2", "1.2"
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_approximate_satisfies_itself "1.2"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_approximate_equal "~> 1.2", "1.2.0"
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_approximate_satisfies_itself "1.2.0"
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_approximate_equal "~> 1.2", "1.2.3"
|
2018-08-27 06:05:04 -04:00
|
|
|
assert_approximate_satisfies_itself "1.2.3"
|
|
|
|
|
|
|
|
assert_approximate_equal "~> 1.2.a", "1.2.3.a.4"
|
|
|
|
assert_approximate_satisfies_itself "1.2.3.a.4"
|
|
|
|
|
|
|
|
assert_approximate_equal "~> 1.9.a", "1.9.0.dev"
|
|
|
|
assert_approximate_satisfies_itself "1.9.0.dev"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_to_s
|
|
|
|
assert_equal "5.2.4", v("5.2.4").to_s
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_semver
|
|
|
|
assert_less_than "1.0.0-alpha", "1.0.0-alpha.1"
|
|
|
|
assert_less_than "1.0.0-alpha.1", "1.0.0-beta.2"
|
|
|
|
assert_less_than "1.0.0-beta.2", "1.0.0-beta.11"
|
|
|
|
assert_less_than "1.0.0-beta.11", "1.0.0-rc.1"
|
|
|
|
assert_less_than "1.0.0-rc1", "1.0.0"
|
|
|
|
assert_less_than "1.0.0-1", "1"
|
|
|
|
end
|
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
# modifying the segments of a version should not affect the segments of the cached version object
|
|
|
|
def test_segments
|
|
|
|
v('9.8.7').segments[2] += 1
|
|
|
|
|
|
|
|
refute_version_equal "9.8.8", "9.8.7"
|
|
|
|
assert_equal [9,8,7], v("9.8.7").segments
|
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def test_canonical_segments
|
|
|
|
assert_equal [1], v("1.0.0").canonical_segments
|
|
|
|
assert_equal [1, "a", 1], v("1.0.0.a.1.0").canonical_segments
|
|
|
|
assert_equal [1, 2, 3, "pre", 1], v("1.2.3-1").canonical_segments
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Asserts that +version+ is a prerelease.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_prerelease(version)
|
2010-02-21 21:52:35 -05:00
|
|
|
assert v(version).prerelease?, "#{version} is a prerelease"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
# Assert that +expected+ is the "approximate" recommendation for +version+.
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_approximate_equal(expected, version)
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal expected, v(version).approximate_recommendation
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
# Assert that the "approximate" recommendation for +version+ satifies +version+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_approximate_satisfies_itself(version)
|
2018-08-27 06:05:04 -04:00
|
|
|
gem_version = v(version)
|
|
|
|
|
|
|
|
assert Gem::Requirement.new(gem_version.approximate_recommendation).satisfied_by?(gem_version)
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Assert that bumping the +unbumped+ version yields the +expected+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_bumped_version_equal(expected, unbumped)
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_version_equal expected, v(unbumped).bump
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Assert that +release+ is the correct non-prerelease +version+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_release_equal(release, version)
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_version_equal release, v(version).release
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Assert that two versions are equal. Handles strings or
|
|
|
|
# Gem::Version instances.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_version_equal(expected, actual)
|
2010-02-21 21:52:35 -05:00
|
|
|
assert_equal v(expected), v(actual)
|
2017-10-07 21:32:18 -04:00
|
|
|
assert_equal v(expected).hash, v(actual).hash, "since #{actual} == #{expected}, they must have the same hash"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Assert that two versions are eql?. Checks both directions.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_version_eql(first, second)
|
2010-02-21 21:52:35 -05:00
|
|
|
first, second = v(first), v(second)
|
|
|
|
assert first.eql?(second), "#{first} is eql? #{second}"
|
|
|
|
assert second.eql?(first), "#{second} is eql? #{first}"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def assert_less_than(left, right)
|
2013-09-14 04:59:02 -04:00
|
|
|
l = v(left)
|
|
|
|
r = v(right)
|
|
|
|
assert l < r, "#{left} not less than #{right}"
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Refute the assumption that +version+ is a prerelease.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def refute_prerelease(version)
|
2010-02-21 21:52:35 -05:00
|
|
|
refute v(version).prerelease?, "#{version} is NOT a prerelease"
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Refute the assumption that two versions are eql?. Checks both
|
|
|
|
# directions.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def refute_version_eql(first, second)
|
2010-02-21 21:52:35 -05:00
|
|
|
first, second = v(first), v(second)
|
|
|
|
refute first.eql?(second), "#{first} is NOT eql? #{second}"
|
|
|
|
refute second.eql?(first), "#{second} is NOT eql? #{first}"
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
# Refute the assumption that the two versions are equal?.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def refute_version_equal(unexpected, actual)
|
2010-02-21 21:52:35 -05:00
|
|
|
refute_equal v(unexpected), v(actual)
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|