1require 'rubygems/test_case'
2require 'rubygems/platform'
3require 'rbconfig'
4
5class TestGemPlatform < Gem::TestCase
6
7  def test_self_local
8    util_set_arch 'i686-darwin8.10.1'
9
10    assert_equal Gem::Platform.new(%w[x86 darwin 8]), Gem::Platform.local
11  end
12
13  def test_self_match
14    assert Gem::Platform.match(nil), 'nil == ruby'
15    assert Gem::Platform.match(Gem::Platform.local), 'exact match'
16    assert Gem::Platform.match(Gem::Platform.local.to_s), '=~ match'
17    assert Gem::Platform.match(Gem::Platform::RUBY), 'ruby'
18  end
19
20  def test_self_new
21    assert_equal Gem::Platform.local, Gem::Platform.new(Gem::Platform::CURRENT)
22    assert_equal Gem::Platform::RUBY, Gem::Platform.new(Gem::Platform::RUBY)
23    assert_equal Gem::Platform::RUBY, Gem::Platform.new(nil)
24    assert_equal Gem::Platform::RUBY, Gem::Platform.new('')
25  end
26
27  def test_initialize
28    test_cases = {
29      'amd64-freebsd6'         => ['amd64',     'freebsd',   '6'],
30      'hppa2.0w-hpux11.31'     => ['hppa2.0w',  'hpux',      '11'],
31      'java'                   => [nil,         'java',      nil],
32      'jruby'                  => [nil,         'java',      nil],
33      'universal-dotnet'       => ['universal', 'dotnet',    nil],
34      'universal-dotnet2.0'    => ['universal', 'dotnet',  '2.0'],
35      'universal-dotnet4.0'    => ['universal', 'dotnet',  '4.0'],
36      'powerpc-aix5.3.0.0'     => ['powerpc',   'aix',       '5'],
37      'powerpc-darwin7'        => ['powerpc',   'darwin',    '7'],
38      'powerpc-darwin8'        => ['powerpc',   'darwin',    '8'],
39      'powerpc-linux'          => ['powerpc',   'linux',     nil],
40      'powerpc64-linux'        => ['powerpc64', 'linux',     nil],
41      'sparc-solaris2.10'      => ['sparc',     'solaris',   '2.10'],
42      'sparc-solaris2.8'       => ['sparc',     'solaris',   '2.8'],
43      'sparc-solaris2.9'       => ['sparc',     'solaris',   '2.9'],
44      'universal-darwin8'      => ['universal', 'darwin',    '8'],
45      'universal-darwin9'      => ['universal', 'darwin',    '9'],
46      'universal-macruby'      => ['universal', 'macruby',   nil],
47      'i386-cygwin'            => ['x86',       'cygwin',    nil],
48      'i686-darwin'            => ['x86',       'darwin',    nil],
49      'i686-darwin8.4.1'       => ['x86',       'darwin',    '8'],
50      'i386-freebsd4.11'       => ['x86',       'freebsd',   '4'],
51      'i386-freebsd5'          => ['x86',       'freebsd',   '5'],
52      'i386-freebsd6'          => ['x86',       'freebsd',   '6'],
53      'i386-freebsd7'          => ['x86',       'freebsd',   '7'],
54      'i386-freebsd'           => ['x86',       'freebsd',   nil],
55      'universal-freebsd'      => ['universal', 'freebsd',   nil],
56      'i386-java1.5'           => ['x86',       'java',      '1.5'],
57      'x86-java1.6'            => ['x86',       'java',      '1.6'],
58      'i386-java1.6'           => ['x86',       'java',      '1.6'],
59      'i686-linux'             => ['x86',       'linux',     nil],
60      'i586-linux'             => ['x86',       'linux',     nil],
61      'i486-linux'             => ['x86',       'linux',     nil],
62      'i386-linux'             => ['x86',       'linux',     nil],
63      'i586-linux-gnu'         => ['x86',       'linux',     nil],
64      'i386-linux-gnu'         => ['x86',       'linux',     nil],
65      'i386-mingw32'           => ['x86',       'mingw32',   nil],
66      'i386-mswin32'           => ['x86',       'mswin32',   nil],
67      'i386-mswin32_80'        => ['x86',       'mswin32',   '80'],
68      'i386-mswin32-80'        => ['x86',       'mswin32',   '80'],
69      'x86-mswin32'            => ['x86',       'mswin32',   nil],
70      'x86-mswin32_60'         => ['x86',       'mswin32',   '60'],
71      'x86-mswin32-60'         => ['x86',       'mswin32',   '60'],
72      'i386-netbsdelf'         => ['x86',       'netbsdelf', nil],
73      'i386-openbsd4.0'        => ['x86',       'openbsd',   '4.0'],
74      'i386-solaris2.10'       => ['x86',       'solaris',   '2.10'],
75      'i386-solaris2.8'        => ['x86',       'solaris',   '2.8'],
76      'mswin32'                => ['x86',       'mswin32',   nil],
77      'x86_64-linux'           => ['x86_64',    'linux',     nil],
78      'x86_64-openbsd3.9'      => ['x86_64',    'openbsd',   '3.9'],
79      'x86_64-openbsd4.0'      => ['x86_64',    'openbsd',   '4.0'],
80      'x86_64-openbsd'         => ['x86_64',    'openbsd',   nil],
81    }
82
83    test_cases.each do |arch, expected|
84      platform = Gem::Platform.new arch
85      assert_equal expected, platform.to_a, arch.inspect
86    end
87  end
88
89  def test_initialize_command_line
90    expected = ['x86', 'mswin32', nil]
91
92    platform = Gem::Platform.new 'i386-mswin32'
93
94    assert_equal expected, platform.to_a, 'i386-mswin32'
95
96    expected = ['x86', 'mswin32', '80']
97
98    platform = Gem::Platform.new 'i386-mswin32-80'
99
100    assert_equal expected, platform.to_a, 'i386-mswin32-80'
101
102    expected = ['x86', 'solaris', '2.10']
103
104    platform = Gem::Platform.new 'i386-solaris-2.10'
105
106    assert_equal expected, platform.to_a, 'i386-solaris-2.10'
107  end
108
109  def test_initialize_mswin32_vc6
110    orig_RUBY_SO_NAME = RbConfig::CONFIG['RUBY_SO_NAME']
111    RbConfig::CONFIG['RUBY_SO_NAME'] = 'msvcrt-ruby18'
112
113    expected = ['x86', 'mswin32', nil]
114
115    platform = Gem::Platform.new 'i386-mswin32'
116
117    assert_equal expected, platform.to_a, 'i386-mswin32 VC6'
118  ensure
119    RbConfig::CONFIG['RUBY_SO_NAME'] = orig_RUBY_SO_NAME
120  end
121
122  def test_initialize_platform
123    platform = Gem::Platform.new 'cpu-my_platform1'
124
125    assert_equal 'cpu', platform.cpu
126    assert_equal 'my_platform', platform.os
127    assert_equal '1', platform.version
128  end
129
130  def test_initialize_test
131    platform = Gem::Platform.new 'cpu-my_platform1'
132    assert_equal 'cpu', platform.cpu
133    assert_equal 'my_platform', platform.os
134    assert_equal '1', platform.version
135
136    platform = Gem::Platform.new 'cpu-other_platform1'
137    assert_equal 'cpu', platform.cpu
138    assert_equal 'other_platform', platform.os
139    assert_equal '1', platform.version
140  end
141
142  def test_to_s
143    if win_platform? then
144      assert_equal 'x86-mswin32-60', Gem::Platform.local.to_s
145    else
146      assert_equal 'x86-darwin-8', Gem::Platform.local.to_s
147    end
148  end
149
150  def test_equals2
151    my = Gem::Platform.new %w[cpu my_platform 1]
152    other = Gem::Platform.new %w[cpu other_platform 1]
153
154    assert_equal my, my
155    refute_equal my, other
156    refute_equal other, my
157  end
158
159  def test_equals3
160    my = Gem::Platform.new %w[cpu my_platform 1]
161    other = Gem::Platform.new %w[cpu other_platform 1]
162
163    assert(my === my)
164    refute(other === my)
165    refute(my === other)
166  end
167
168  def test_equals3_cpu
169    ppc_darwin8 = Gem::Platform.new 'powerpc-darwin8.0'
170    uni_darwin8 = Gem::Platform.new 'universal-darwin8.0'
171    x86_darwin8 = Gem::Platform.new 'i686-darwin8.0'
172
173    util_set_arch 'powerpc-darwin8'
174    assert((ppc_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
175    assert((uni_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
176    refute((x86_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
177
178    util_set_arch 'i686-darwin8'
179    refute((ppc_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
180    assert((uni_darwin8 === Gem::Platform.local), 'x86 =~ universal')
181    assert((x86_darwin8 === Gem::Platform.local), 'powerpc =~ universal')
182
183    util_set_arch 'universal-darwin8'
184    assert((ppc_darwin8 === Gem::Platform.local), 'universal =~ ppc')
185    assert((uni_darwin8 === Gem::Platform.local), 'universal =~ universal')
186    assert((x86_darwin8 === Gem::Platform.local), 'universal =~ x86')
187  end
188
189  def test_equals3_version
190    util_set_arch 'i686-darwin8'
191
192    x86_darwin = Gem::Platform.new ['x86', 'darwin', nil]
193    x86_darwin7 = Gem::Platform.new ['x86', 'darwin', '7']
194    x86_darwin8 = Gem::Platform.new ['x86', 'darwin', '8']
195    x86_darwin9 = Gem::Platform.new ['x86', 'darwin', '9']
196
197    assert((x86_darwin  === Gem::Platform.local), 'x86_darwin === x86_darwin8')
198    assert((x86_darwin8 === Gem::Platform.local), 'x86_darwin8 === x86_darwin8')
199
200    refute((x86_darwin7 === Gem::Platform.local), 'x86_darwin7 === x86_darwin8')
201    refute((x86_darwin9 === Gem::Platform.local), 'x86_darwin9 === x86_darwin8')
202  end
203
204  def test_equals_tilde
205    util_set_arch 'i386-mswin32'
206
207    assert_local_match 'mswin32'
208    assert_local_match 'i386-mswin32'
209
210    # oddballs
211    assert_local_match 'i386-mswin32-mq5.3'
212    assert_local_match 'i386-mswin32-mq6'
213    refute_local_match 'win32-1.8.2-VC7'
214    refute_local_match 'win32-1.8.4-VC6'
215    refute_local_match 'win32-source'
216    refute_local_match 'windows'
217
218    util_set_arch 'i686-linux'
219    assert_local_match 'i486-linux'
220    assert_local_match 'i586-linux'
221    assert_local_match 'i686-linux'
222
223    util_set_arch 'i686-darwin8'
224    assert_local_match 'i686-darwin8.4.1'
225    assert_local_match 'i686-darwin8.8.2'
226
227    util_set_arch 'java'
228    assert_local_match 'java'
229    assert_local_match 'jruby'
230
231    util_set_arch 'universal-dotnet2.0'
232    assert_local_match 'universal-dotnet'
233    assert_local_match 'universal-dotnet-2.0'
234    refute_local_match 'universal-dotnet-4.0'
235    assert_local_match 'dotnet'
236    assert_local_match 'dotnet-2.0'
237    refute_local_match 'dotnet-4.0'
238
239    util_set_arch 'universal-dotnet4.0'
240    assert_local_match 'universal-dotnet'
241    refute_local_match 'universal-dotnet-2.0'
242    assert_local_match 'universal-dotnet-4.0'
243    assert_local_match 'dotnet'
244    refute_local_match 'dotnet-2.0'
245    assert_local_match 'dotnet-4.0'
246
247    util_set_arch 'universal-macruby-1.0'
248    assert_local_match 'universal-macruby'
249    assert_local_match 'macruby'
250    refute_local_match 'universal-macruby-0.10'
251    assert_local_match 'universal-macruby-1.0'
252
253    util_set_arch 'powerpc-darwin'
254    assert_local_match 'powerpc-darwin'
255
256    util_set_arch 'powerpc-darwin7'
257    assert_local_match 'powerpc-darwin7.9.0'
258
259    util_set_arch 'powerpc-darwin8'
260    assert_local_match 'powerpc-darwin8.10.0'
261
262    util_set_arch 'sparc-solaris2.8'
263    assert_local_match 'sparc-solaris2.8-mq5.3'
264  end
265
266  def assert_local_match name
267    assert_match Gem::Platform.local, name
268  end
269
270  def refute_local_match name
271    refute_match Gem::Platform.local, name
272  end
273end
274
275