2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
8 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
9 require 'rubygems/version'
11 class TestGemVersion < RubyGemTestCase
16 @v1_0 = Gem::Version.new '1.0'
17 @v1_2 = Gem::Version.new '1.2'
18 @v1_3 = Gem::Version.new '1.3'
22 assert_version Gem::Version.create('1.0')
23 assert_version Gem::Version.create("1.0 ")
24 assert_version Gem::Version.create(" 1.0 ")
25 assert_version Gem::Version.create("1.0\n")
26 assert_version Gem::Version.create("\n1.0\n")
28 assert_equal Gem::Version.create('1'), Gem::Version.create(1)
31 def test_class_create_malformed
32 e = assert_raise ArgumentError do Gem::Version.create("junk") end
33 assert_equal "Malformed version number string junk", e.message
35 e = assert_raise ArgumentError do Gem::Version.create("1.0\n2.0") end
36 assert_equal "Malformed version number string 1.0\n2.0", e.message
40 assert_inadequate( "", "> 0.1")
41 assert_inadequate( "1.2.3", "!= 1.2.3")
42 assert_inadequate( "1.2.003.0.0", "!= 1.02.3")
43 assert_inadequate( "4.5.6", "< 1.2.3")
44 assert_inadequate( "1.0", "> 1.1")
45 assert_inadequate( "0", ">")
46 assert_inadequate( "0", "<")
47 assert_inadequate( "", "= 0.1")
48 assert_inadequate( "1.1.1", "> 1.1.1")
49 assert_inadequate( "1.2", "= 1.1")
50 assert_inadequate( "1.40", "= 1.1")
51 assert_inadequate( "1.3", "= 1.40")
52 assert_inadequate( "9.3.3", "<= 9.3.2")
53 assert_inadequate( "9.3.1", ">= 9.3.2")
54 assert_inadequate( "9.3.03", "<= 9.3.2")
55 assert_inadequate( "1.0.0.1", "= 1.0")
58 def test_bump_trailing_zeros
59 v = Gem::Version.new("5.0.0")
60 assert_equal "5.1", v.bump.to_s
64 v = Gem::Version.new("5.2.4")
65 assert_equal "5.3", v.bump.to_s
68 def test_bump_one_level
69 v = Gem::Version.new("5")
70 assert_equal "6", v.bump.to_s
74 v = Gem::Version.new("1.2")
76 assert_equal true, v.eql?(@v1_2)
77 assert_equal true, @v1_2.eql?(v)
79 assert_equal false, @v1_2.eql?(@v1_3)
80 assert_equal false, @v1_3.eql?(@v1_2)
84 v = Gem::Version.new("1.2")
89 assert_not_equal @v1_2, @v1_3
90 assert_not_equal @v1_3, @v1_2
94 v = Gem::Version.new("1.2")
95 assert_equal v.hash, @v1_2.hash
96 assert_not_equal @v1_2.hash, @v1_3.hash
99 def test_illformed_requirements
100 [ ">>> 1.3.5", "> blah" ].each do |rq|
101 assert_raises(ArgumentError, "req [#{rq}] should fail") {
102 Gem::Version::Requirement.new(rq)
108 assert_equal [1], Gem::Version.new("1").to_ints
109 assert_equal [1], Gem::Version.new("1.0").to_ints
110 assert_equal [1, 1], Gem::Version.new("1.1").to_ints
114 assert_adequate( "0.2.33", "= 0.2.33")
115 assert_adequate( "0.2.34", "> 0.2.33")
116 assert_adequate( "1.0", "= 1.0")
117 assert_adequate( "1.0", "1.0")
118 assert_adequate( "1.8.2", "> 1.8.0")
119 assert_adequate( "1.112", "> 1.111")
120 assert_adequate( "0.2", "> 0.0.0")
121 assert_adequate( "0.0.0.0.0.2", "> 0.0.0")
122 assert_adequate( "0.0.1.0", "> 0.0.0.1")
123 assert_adequate( "10.3.2", "> 9.3.2")
124 assert_adequate( "1.0.0.0", "= 1.0")
125 assert_adequate( "10.3.2", "!= 9.3.4")
126 assert_adequate( "10.3.2", "> 9.3.2")
127 assert_adequate( "10.3.2", "> 9.3.2")
128 assert_adequate( " 9.3.2", ">= 9.3.2")
129 assert_adequate( "9.3.2 ", ">= 9.3.2")
130 assert_adequate( "", "= 0")
131 assert_adequate( "", "< 0.1")
132 assert_adequate( " ", "< 0.1 ")
133 assert_adequate( "", " < 0.1")
134 assert_adequate( "0", "=")
135 assert_adequate( "0", ">=")
136 assert_adequate( "0", "<=")
139 def test_satisfied_by_eh_boxed
140 assert_inadequate("1.3", "~> 1.4")
141 assert_adequate( "1.4", "~> 1.4")
142 assert_adequate( "1.5", "~> 1.4")
143 assert_inadequate("2.0", "~> 1.4")
145 assert_inadequate("1.3", "~> 1.4.4")
146 assert_inadequate("1.4", "~> 1.4.4")
147 assert_adequate( "1.4.4", "~> 1.4.4")
148 assert_adequate( "1.4.5", "~> 1.4.4")
149 assert_inadequate("1.5", "~> 1.4.4")
150 assert_inadequate("2.0", "~> 1.4.4")
153 def test_satisfied_by_eh_multiple
154 req = [">= 1.4", "<= 1.6", "!= 1.5"]
155 assert_inadequate("1.3", req)
156 assert_adequate( "1.4", req)
157 assert_inadequate("1.5", req)
158 assert_adequate( "1.6", req)
159 assert_inadequate("1.7", req)
160 assert_inadequate("2.0", req)
164 assert_equal 1, Gem::Version.new('1.8.2') <=> Gem::Version.new('0.0.0')
168 assert_inadequate("1.3", "~> 1.4")
169 assert_adequate( "1.4", "~> 1.4")
170 assert_adequate( "1.5", "~> 1.4")
171 assert_inadequate("2.0", "~> 1.4")
173 assert_inadequate("1.3", "~> 1.4.4")
174 assert_inadequate("1.4", "~> 1.4.4")
175 assert_adequate( "1.4.4", "~> 1.4.4")
176 assert_adequate( "1.4.5", "~> 1.4.4")
177 assert_inadequate("1.5", "~> 1.4.4")
178 assert_inadequate("2.0", "~> 1.4.4")
182 v = Gem::Version.new("5.2.4")
183 assert_equal "5.2.4", v.to_s
186 def assert_adequate(version, requirement)
187 ver = Gem::Version.new(version)
188 req = Gem::Requirement.new(requirement)
189 assert req.satisfied_by?(ver),
190 "Version #{version} should be adequate for Requirement #{requirement}"
193 def assert_inadequate(version, requirement)
194 ver = Gem::Version.new(version)
195 req = Gem::Version::Requirement.new(requirement)
196 assert ! req.satisfied_by?(ver),
197 "Version #{version} should not be adequate for Requirement #{requirement}"
200 def assert_version(actual)
201 assert_equal @v1_0, actual
202 assert_equal @v1_0.version, actual.version