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 v1_2 = Gem::Version.new '1.2'
75 v1_2_0 = Gem::Version.new '1.2.0'
77 assert_equal true, v1_2.eql?(@v1_2)
78 assert_equal true, @v1_2.eql?(v1_2)
80 assert_equal false, v1_2_0.eql?(@v1_2)
81 assert_equal false, @v1_2.eql?(v1_2_0)
83 assert_equal false, @v1_2.eql?(@v1_3)
84 assert_equal false, @v1_3.eql?(@v1_2)
88 v = Gem::Version.new("1.2")
93 assert_not_equal @v1_2, @v1_3
94 assert_not_equal @v1_3, @v1_2
98 v1_2 = Gem::Version.new "1.2"
99 v1_2_0 = Gem::Version.new "1.2.0"
101 assert_equal v1_2.hash, @v1_2.hash
103 assert_not_equal v1_2_0.hash, @v1_2.hash
105 assert_not_equal @v1_2.hash, @v1_3.hash
108 def test_illformed_requirements
109 [ ">>> 1.3.5", "> blah" ].each do |rq|
110 assert_raises(ArgumentError, "req [#{rq}] should fail") {
111 Gem::Version::Requirement.new(rq)
117 assert_equal [1], Gem::Version.new("1").to_ints
118 assert_equal [1], Gem::Version.new("1.0").to_ints
119 assert_equal [1, 1], Gem::Version.new("1.1").to_ints
123 assert_adequate( "0.2.33", "= 0.2.33")
124 assert_adequate( "0.2.34", "> 0.2.33")
125 assert_adequate( "1.0", "= 1.0")
126 assert_adequate( "1.0", "1.0")
127 assert_adequate( "1.8.2", "> 1.8.0")
128 assert_adequate( "1.112", "> 1.111")
129 assert_adequate( "0.2", "> 0.0.0")
130 assert_adequate( "0.0.0.0.0.2", "> 0.0.0")
131 assert_adequate( "0.0.1.0", "> 0.0.0.1")
132 assert_adequate( "10.3.2", "> 9.3.2")
133 assert_adequate( "1.0.0.0", "= 1.0")
134 assert_adequate( "10.3.2", "!= 9.3.4")
135 assert_adequate( "10.3.2", "> 9.3.2")
136 assert_adequate( "10.3.2", "> 9.3.2")
137 assert_adequate( " 9.3.2", ">= 9.3.2")
138 assert_adequate( "9.3.2 ", ">= 9.3.2")
139 assert_adequate( "", "= 0")
140 assert_adequate( "", "< 0.1")
141 assert_adequate( " ", "< 0.1 ")
142 assert_adequate( "", " < 0.1")
143 assert_adequate( "0", "=")
144 assert_adequate( "0", ">=")
145 assert_adequate( "0", "<=")
148 def test_satisfied_by_eh_boxed
149 assert_inadequate("1.3", "~> 1.4")
150 assert_adequate( "1.4", "~> 1.4")
151 assert_adequate( "1.5", "~> 1.4")
152 assert_inadequate("2.0", "~> 1.4")
154 assert_inadequate("1.3", "~> 1.4.4")
155 assert_inadequate("1.4", "~> 1.4.4")
156 assert_adequate( "1.4.4", "~> 1.4.4")
157 assert_adequate( "1.4.5", "~> 1.4.4")
158 assert_inadequate("1.5", "~> 1.4.4")
159 assert_inadequate("2.0", "~> 1.4.4")
162 def test_satisfied_by_eh_multiple
163 req = [">= 1.4", "<= 1.6", "!= 1.5"]
164 assert_inadequate("1.3", req)
165 assert_adequate( "1.4", req)
166 assert_inadequate("1.5", req)
167 assert_adequate( "1.6", req)
168 assert_inadequate("1.7", req)
169 assert_inadequate("2.0", req)
173 assert_equal 1, Gem::Version.new('1.8.2') <=> Gem::Version.new('0.0.0')
177 assert_inadequate("1.3", "~> 1.4")
178 assert_adequate( "1.4", "~> 1.4")
179 assert_adequate( "1.5", "~> 1.4")
180 assert_inadequate("2.0", "~> 1.4")
182 assert_inadequate("1.3", "~> 1.4.4")
183 assert_inadequate("1.4", "~> 1.4.4")
184 assert_adequate( "1.4.4", "~> 1.4.4")
185 assert_adequate( "1.4.5", "~> 1.4.4")
186 assert_inadequate("1.5", "~> 1.4.4")
187 assert_inadequate("2.0", "~> 1.4.4")
191 v = Gem::Version.new("5.2.4")
192 assert_equal "5.2.4", v.to_s
195 def assert_adequate(version, requirement)
196 ver = Gem::Version.new(version)
197 req = Gem::Requirement.new(requirement)
198 assert req.satisfied_by?(ver),
199 "Version #{version} should be adequate for Requirement #{requirement}"
202 def assert_inadequate(version, requirement)
203 ver = Gem::Version.new(version)
204 req = Gem::Version::Requirement.new(requirement)
205 assert ! req.satisfied_by?(ver),
206 "Version #{version} should not be adequate for Requirement #{requirement}"
209 def assert_version(actual)
210 assert_equal @v1_0, actual
211 assert_equal @v1_0.version, actual.version