pathname2 1.6.3 → 1.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,662 +5,674 @@
5
5
  # against the C extension pass the letter 'c' as an argument. You should
6
6
  # use the 'rake test' task to run this test suite.
7
7
  ###########################################################################
8
+ require 'rubygems'
9
+ gem 'test-unit'
10
+
8
11
  require 'facade'
9
12
  require 'pathname2'
10
13
  require 'test/unit'
14
+ require 'windows/system_info'
11
15
 
12
16
  class MyPathname < Pathname; end
13
17
 
14
18
  class TC_Pathname_MSWin < Test::Unit::TestCase
15
-
16
- def setup
17
- @fpath = Pathname.new("C:/Program Files/Windows NT/Accessories")
18
- @bpath = Pathname.new("C:\\Program Files\\Windows NT\\Accessories")
19
- @dpath = Pathname.new("C:\\Program Files\\File[5].txt")
20
- @spath = Pathname.new("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1")
21
- @upath = Pathname.new("\\\\foo\\bar\\baz")
22
- @npath = Pathname.new("foo\\bar\\baz")
23
- @rpath = Pathname.new("Z:\\")
24
- @xpath = Pathname.new("\\\\foo\\bar")
25
- @ypath = Pathname.new("\\\\foo")
26
- @zpath = Pathname.new("\\\\")
27
- @epath = Pathname.new("")
28
- @ppath = Pathname.new("C:\\foo\\bar\\")
29
- @cpath = Pathname.new("C:\\foo\\..\\bar\\.\\baz")
30
- @tpath = Pathname.new("C:\\foo\\bar")
31
-
32
- @url_path = Pathname.new("file:///C:/Documents%20and%20Settings")
33
- @cur_path = Pathname.new(Dir.pwd)
34
-
35
- @mypath = MyPathname.new("C:\\Program Files")
36
-
37
- @abs_array = []
38
- @rel_array = []
39
- @unc_array = []
40
- end
41
-
42
- def test_aref_with_range
43
- assert_equal("C:\\Program Files", @fpath[0..1])
44
- assert_equal("C:\\Program Files\\Windows NT", @fpath[0..2])
45
- assert_equal("Program Files\\Windows NT", @fpath[1..2])
46
- assert_equal(@fpath, @fpath[0..-1])
47
- end
48
-
49
- def test_aref_with_index_and_length
50
- assert_equal("C:", @fpath[0,1])
51
- assert_equal("C:\\Program Files", @fpath[0,2])
52
- assert_equal("Program Files\\Windows NT", @fpath[1,2])
53
- end
54
-
55
- def test_aref_with_index
56
- assert_equal("C:", @fpath[0])
57
- assert_equal("Program Files", @fpath[1])
58
- assert_equal("Accessories", @fpath[-1])
59
- assert_equal(nil, @fpath[10])
60
- end
61
-
62
- def test_version
63
- assert_equal('1.6.3', Pathname::VERSION)
64
- end
65
-
66
- # Convenience method for test_plus
67
- def assert_pathname_plus(a, b, c)
68
- a = Pathname.new(a)
69
- b = Pathname.new(b)
70
- c = Pathname.new(c)
71
- assert_equal(a, b + c)
72
- end
73
-
74
- # Convenience method for test_spaceship operator
75
- def assert_pathname_cmp(int, s1, s2)
76
- p1 = Pathname.new(s1)
77
- p2 = Pathname.new(s2)
78
- result = p1 <=> p2
79
- assert_equal(int, result)
80
- end
81
-
82
- # Convenience method for test_relative_path_from
83
- def assert_relpath(result, dest, base)
84
- assert_equal(result, Pathname.new(dest).relative_path_from(base))
85
- end
86
-
87
- # Convenience method for test_relative_path_from_expected_errors
88
- def assert_relative_path_error(to, from)
89
- assert_raise(ArgumentError) {
90
- Pathname.new(to).relative_path_from(from)
91
- }
92
- end
93
-
94
- def test_file_urls
95
- assert_equal("C:\\Documents and Settings", @url_path)
96
- assert_raises(Pathname::Error){ Pathname.new('http://rubyforge.org') }
97
- end
98
-
99
- def test_realpath
100
- assert_respond_to(@fpath, :realpath)
101
- assert_equal(@cur_path, Pathname.new('.').realpath)
102
- assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
103
- end
104
-
105
- def test_relative_path_from
106
- assert_relpath("..\\a", "a", "b")
107
- assert_relpath("..\\a", "a", "b\\")
108
- assert_relpath("..\\a", "a\\", "b")
109
- assert_relpath("..\\a", "a\\", "b\\")
110
- assert_relpath("..\\a", "c:\\a", "c:\\b")
111
- assert_relpath("..\\a", "c:\\a", "c:\\b\\")
112
- assert_relpath("..\\a", "c:\\a\\", "c:\\b")
113
- assert_relpath("..\\a", "c:\\a\\", "c:\\b\\")
114
-
115
- assert_relpath("..\\b", "a\\b", "a\\c")
116
- assert_relpath("..\\a", "..\\a", "..\\b")
117
-
118
- assert_relpath("a", "a", ".")
119
- assert_relpath("..", ".", "a")
120
-
121
- assert_relpath(".", ".", ".")
122
- assert_relpath(".", "..", "..")
123
- assert_relpath("..", "..", ".")
124
-
125
- assert_relpath("c\\d", "c:\\a\\b\\c\\d", "c:\\a\\b")
126
- assert_relpath("..\\..", "c:\\a\\b", "c:\\a\\b\\c\\d")
127
- assert_relpath("..\\..\\..\\..\\e", "c:\\e", "c:\\a\\b\\c\\d")
128
- assert_relpath("..\\b\\c", "a\\b\\c", "a\\d")
129
-
130
- assert_relpath("..\\a", "c:\\..\\a", "c:\\b")
131
- #assert_relpath("..\\..\\a", "..\\a", "b") # fails
132
- assert_relpath(".", "c:\\a\\..\\..\\b", "c:\\b")
133
- assert_relpath("..", "a\\..", "a")
134
- assert_relpath(".", "a\\..\\b", "b")
135
-
136
- assert_relpath("a", "a", "b\\..")
137
- assert_relpath("b\\c", "b\\c", "b\\..")
138
- end
139
-
140
- def test_relative_path_from_expected_errors
141
- assert_relative_path_error("c:\\", ".")
142
- assert_relative_path_error(".", "c:\\")
143
- assert_relative_path_error("a", "..")
144
- assert_relative_path_error(".", "..")
145
- assert_relative_path_error("C:\\Temp", "D:\\Temp")
146
- assert_relative_path_error("\\\\Server\\Temp", "D:\\Temp")
147
- end
148
-
149
- # Convenience method to verify that the receiver was not modified
150
- # except perhaps slashes
151
- def assert_non_destructive
152
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath)
153
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
154
- assert_equal("C:\\Program Files\\File[5].txt", @dpath)
155
- assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @spath)
156
- assert_equal("\\\\foo\\bar\\baz", @upath)
157
- assert_equal("foo\\bar\\baz", @npath)
158
- assert_equal("Z:\\", @rpath)
159
- assert_equal("\\\\foo\\bar", @xpath)
160
- assert_equal("\\\\foo", @ypath)
161
- assert_equal("\\\\", @zpath)
162
- assert_equal("", @epath)
163
- assert_equal("C:\\foo\\bar\\", @ppath)
164
- assert_equal("C:\\foo\\..\\bar\\.\\baz", @cpath)
165
- end
166
-
167
- def test_parent
168
- assert_respond_to(@bpath, :parent)
169
- assert_equal("C:\\Program Files\\Windows NT", @bpath.parent)
170
- assert_equal("foo\\bar", @npath.parent)
171
- assert_equal("Z:\\", @rpath.parent)
172
- end
173
-
174
- def test_short_path
175
- assert_respond_to(@bpath, :short_path)
176
- assert_nothing_raised{ @bpath.short_path }
177
- assert_kind_of(Pathname, @bpath.short_path)
178
- assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @bpath.short_path)
179
-
180
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
181
- end
182
-
183
- def test_long_path
184
- assert_respond_to(@spath, :long_path)
185
- assert_nothing_raised{ @spath.long_path }
186
- assert_kind_of(Pathname, @spath.long_path)
187
-
188
- assert_equal(
189
- "C:\\Program Files\\Windows NT\\Accessories",
190
- @spath.long_path
191
- )
192
-
193
- assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @spath)
194
- end
195
-
196
- def test_undecorate
197
- assert_respond_to(@dpath, :undecorate)
198
- assert_nothing_raised{ @dpath.undecorate }
199
- assert_kind_of(Pathname, @dpath.undecorate)
200
-
201
- assert_equal('C:\Program Files\File.txt', @dpath.undecorate)
202
- assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate)
203
- assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate)
204
- assert_equal('C:\Path\[3].txt',
205
- Pathname.new('C:\Path\[3].txt').undecorate
206
- )
207
- assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate)
208
- assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate)
209
- assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate)
210
-
211
- assert_equal("C:\\Program Files\\File[5].txt", @dpath)
212
- end
213
-
214
- def test_undecorate_bang
215
- assert_respond_to(@dpath, :undecorate!)
216
- assert_nothing_raised{ @dpath.undecorate! }
217
- assert_kind_of(Pathname, @dpath.undecorate!)
218
-
219
- assert_equal('C:\Program Files\File.txt', @dpath.undecorate!)
220
- assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate!)
221
- assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate!)
222
- assert_equal('C:\Path\[3].txt',
223
- Pathname.new('C:\Path\[3].txt').undecorate!
224
- )
225
- assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate!)
226
- assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate!)
227
- assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate!)
228
-
229
- assert_equal('C:\Program Files\File.txt', @dpath)
230
- end
231
-
232
- def test_unc
233
- assert_respond_to(@upath, :unc?)
234
- assert_nothing_raised{ @upath.unc? }
235
-
236
- assert_equal(true, @upath.unc?)
237
- assert_equal(true, @xpath.unc?)
238
- assert_equal(true, @ypath.unc?)
239
- assert_equal(true, @zpath.unc?)
240
- assert_equal(false, @fpath.unc?)
241
- assert_equal(false, @bpath.unc?)
242
- assert_equal(false, @dpath.unc?)
243
- assert_equal(false, @spath.unc?)
244
- assert_equal(false, @epath.unc?)
245
-
246
- # Arguably a bug in the PathIsUNC() function since drive letters
247
- # are, in fact, a legal part of a UNC path (for historical reasons).
248
- assert_equal(false, Pathname.new("C:\\\\foo\\bar\\baz").unc?)
249
-
250
- assert_non_destructive
251
- end
252
-
253
- def test_pstrip
254
- assert_respond_to(@ppath, :pstrip)
255
- assert_nothing_raised{ @ppath.pstrip }
256
- assert_nothing_raised{ @fpath.pstrip }
257
- assert_kind_of(Pathname, @ppath.pstrip)
258
-
259
- assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip)
260
- assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip)
261
- assert_equal("", Pathname.new("").pstrip)
262
-
263
- assert_equal("C:\\foo\\bar\\", @ppath)
264
- end
265
-
266
- def test_pstrip_bang
267
- assert_respond_to(@ppath, :pstrip!)
268
- assert_nothing_raised{ @ppath.pstrip! }
269
- assert_nothing_raised{ @fpath.pstrip! }
270
- assert_kind_of(Pathname, @ppath.pstrip!)
271
-
272
- assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip!)
273
- assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip!)
274
- assert_equal("", Pathname.new("").pstrip!)
275
-
276
- assert_equal("C:\\foo\\bar", @ppath)
277
- end
278
-
279
- def test_exists
280
- assert_respond_to(@fpath, :exists?)
281
- assert_nothing_raised{ @fpath.exists? }
282
- assert_equal(true, Pathname.new("C:\\").exists?)
283
- assert_equal(false, Pathname.new("X:\\foo\\bar\\baz").exists?)
284
- end
285
-
286
- def test_each
287
- array = []
288
-
289
- assert_respond_to(@fpath, :each)
290
- assert_nothing_raised{ @fpath.each{ |e| array.push(e) } }
291
- assert_equal(["C:", "Program Files", "Windows NT", "Accessories"], array)
292
- end
293
-
294
- def test_descend
295
- assert_respond_to(@bpath, :descend)
296
- assert_nothing_raised{ @bpath.descend{} }
297
-
298
- @bpath.descend{ |path| @abs_array.push(path) }
299
- @npath.descend{ |path| @rel_array.push(path) }
300
- @upath.descend{ |path| @unc_array.push(path) }
301
-
302
- assert_equal("C:", @abs_array[0])
303
- assert_equal("C:\\Program Files", @abs_array[1])
304
- assert_equal("C:\\Program Files\\Windows NT", @abs_array[2])
305
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[3])
306
-
307
- assert_equal("foo", @rel_array[0])
308
- assert_equal("foo\\bar", @rel_array[1])
309
- assert_equal("foo\\bar\\baz", @rel_array[2])
310
-
311
- assert_equal("\\\\foo\\bar", @unc_array[0])
312
- assert_equal("\\\\foo\\bar\\baz", @unc_array[1])
313
-
314
- assert_non_destructive
315
- end
316
-
317
- def test_ascend
318
- assert_respond_to(@bpath, :ascend)
319
- assert_nothing_raised{ @bpath.ascend{} }
320
-
321
- @bpath.ascend{ |path| @abs_array.push(path) }
322
- @npath.ascend{ |path| @rel_array.push(path) }
323
- @upath.ascend{ |path| @unc_array.push(path) }
324
-
325
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[0])
326
- assert_equal("C:\\Program Files\\Windows NT", @abs_array[1])
327
- assert_equal("C:\\Program Files", @abs_array[2])
328
- assert_equal("C:", @abs_array[3])
329
- assert_equal(4, @abs_array.length)
330
-
331
- assert_equal("foo\\bar\\baz", @rel_array[0])
332
- assert_equal("foo\\bar", @rel_array[1])
333
- assert_equal("foo", @rel_array[2])
334
- assert_equal(3, @rel_array.length)
335
-
336
- assert_equal("\\\\foo\\bar\\baz", @unc_array[0])
337
- assert_equal("\\\\foo\\bar", @unc_array[1])
338
- assert_equal(2, @unc_array.length)
339
-
340
- assert_non_destructive
341
- end
342
-
343
- def test_immutability
344
- path = "C:\\Program Files\\foo\\bar".freeze
345
- assert_equal(true, path.frozen?)
346
- assert_nothing_raised{ Pathname.new(path) }
347
- assert_nothing_raised{ Pathname.new(path).root }
348
- end
349
-
350
- def test_plus_operator
351
- # Standard stuff
352
- assert_pathname_plus("C:\\a\\b", "C:\\a", "b")
353
- assert_pathname_plus("C:\\b", "a", "C:\\b")
354
- assert_pathname_plus("a\\b", "a", "b")
355
- assert_pathname_plus("C:\\b", "C:\\a", "..\\b")
356
- assert_pathname_plus("C:\\a\\b", "C:\\a\\.", "\\b")
357
- assert_pathname_plus("C:\\a\\b.txt", "C:\\a", "b.txt")
358
-
359
- # UNC paths
360
- assert_pathname_plus("\\\\foo\\bar", "\\\\foo", "bar")
361
- assert_pathname_plus("\\\\foo", "\\\\", "foo")
362
- assert_pathname_plus("\\\\", "\\\\", "")
363
- assert_pathname_plus("\\\\foo\\baz", "\\\\foo\\bar", "\\..\\baz")
364
- assert_pathname_plus("\\\\", "\\\\", "..\\..\\..\\..")
365
-
366
- # Pathname + String
367
- assert_nothing_raised{ @tpath + "bar" }
368
- assert_equal('C:\foo\bar\baz', @tpath + 'baz')
369
- assert_equal('C:\foo\bar', @tpath)
370
-
371
- # Ensure neither left nor right operand are modified
372
- assert_nothing_raised{ @tpath + @npath }
373
- assert_equal('C:\foo\bar\foo\bar\baz', @tpath + @npath)
374
- assert_equal('C:\foo\bar', @tpath)
375
- assert_equal('foo\bar\baz', @npath)
376
- end
377
-
378
- def test_clean
379
- assert_respond_to(@cpath, :clean)
380
- assert_nothing_raised{ @cpath.clean }
381
- assert_kind_of(Pathname, @cpath.clean)
382
-
383
- # Our preset stuff
384
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean)
385
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean)
386
- assert_equal("\\\\foo\\bar\\baz", @upath.clean)
387
- assert_equal("foo\\bar\\baz", @npath.clean)
388
- assert_equal("Z:\\", @rpath.clean)
389
- assert_equal("\\\\foo\\bar", @xpath.clean)
390
- assert_equal("\\\\foo", @ypath.clean)
391
- assert_equal("\\\\", @zpath.clean)
392
- assert_equal("", @epath.clean)
393
- assert_equal("C:\\bar\\baz", @cpath.clean)
394
-
395
- # Standard stuff
396
- assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean)
397
- assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean)
398
- assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean)
399
- assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean)
400
- assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean)
401
- assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean)
402
- assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean)
403
-
404
- # Edge cases
405
- assert_equal("\\", Pathname.new(".").clean)
406
- assert_equal("\\", Pathname.new("..").clean)
407
-
408
- assert_non_destructive
409
- end
410
-
411
- def test_clean_bang
412
- assert_respond_to(@cpath, :clean!)
413
- assert_nothing_raised{ @cpath.clean! }
414
- assert_kind_of(Pathname, @cpath.clean!)
415
-
416
- # Our preset stuff
417
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean!)
418
- assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean!)
419
- assert_equal("\\\\foo\\bar\\baz", @upath.clean!)
420
- assert_equal("foo\\bar\\baz", @npath.clean!)
421
- assert_equal("Z:\\", @rpath.clean!)
422
- assert_equal("\\\\foo\\bar", @xpath.clean!)
423
- assert_equal("\\\\foo", @ypath.clean!)
424
- assert_equal("\\\\", @zpath.clean!)
425
- assert_equal("", @epath.clean!)
426
- assert_equal("C:\\bar\\baz", @cpath.clean!)
427
-
428
- # Standard stuff
429
- assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean!)
430
- assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean!)
431
- assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean!)
432
- assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean!)
433
- assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean!)
434
- assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean!)
435
- assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean!)
436
-
437
- # Edge cases
438
- assert_equal("\\", Pathname.new(".").clean!)
439
- assert_equal("\\", Pathname.new("..").clean!)
440
-
441
- assert_equal("C:\\bar\\baz", @cpath)
442
- end
443
-
444
- def test_absolute
445
- assert_equal(true, @fpath.absolute?)
446
- assert_equal(true, @bpath.absolute?)
447
- assert_equal(true, @upath.absolute?)
448
- assert_equal(false, @npath.absolute?)
449
- assert_equal(true, @rpath.absolute?)
450
- assert_equal(true, @xpath.absolute?)
451
- assert_equal(true, @ypath.absolute?)
452
- assert_equal(true, @zpath.absolute?)
453
- assert_equal(false, @epath.absolute?)
454
-
455
- assert_non_destructive
456
- end
457
-
458
- def test_relative
459
- assert_equal(false, @fpath.relative?)
460
- assert_equal(false, @bpath.relative?)
461
- assert_equal(false, @upath.relative?)
462
- assert_equal(true, @npath.relative?)
463
- assert_equal(false, @rpath.relative?)
464
- assert_equal(false, @xpath.relative?)
465
- assert_equal(false, @ypath.relative?)
466
- assert_equal(false, @zpath.relative?)
467
- assert_equal(true, @epath.relative?)
468
-
469
- assert_non_destructive
470
- end
471
-
472
- def test_root
473
- assert_equal("C:\\", @fpath.root)
474
- assert_equal("C:\\", @bpath.root)
475
- assert_equal("\\\\foo\\bar", @upath.root)
476
- assert_equal(".", @npath.root)
477
- assert_equal("Z:\\", @rpath.root)
478
- assert_equal("\\\\foo\\bar", @xpath.root)
479
- assert_equal("\\\\foo", @ypath.root)
480
- assert_equal("\\\\", @zpath.root)
481
- assert_equal(".", @epath.root)
482
-
483
- # Edge cases
484
- assert_equal(".", Pathname.new("..").root)
485
- assert_equal(".", Pathname.new(".").root)
486
-
487
- assert_non_destructive
488
- end
489
-
490
- def test_drive_number
491
- assert_equal(2, @fpath.drive_number)
492
- assert_equal(2, @bpath.drive_number)
493
- assert_equal(nil, @upath.drive_number)
494
- assert_equal(nil, @npath.drive_number)
495
- assert_equal(25, @rpath.drive_number)
496
- assert_equal(nil, @xpath.drive_number)
497
- assert_equal(nil, @ypath.drive_number)
498
- assert_equal(nil, @zpath.drive_number)
499
- assert_equal(nil, @epath.drive_number)
500
-
501
- # Edge cases
502
- assert_equal(nil, Pathname.new("..").drive_number)
503
- assert_equal(nil, Pathname.new(".").drive_number)
504
-
505
- assert_non_destructive
506
- end
507
-
508
- def test_to_a
509
- expected = ["C:", "Program Files", "Windows NT", "Accessories"]
510
- assert_equal(expected, @fpath.to_a)
511
- assert_equal(expected, @bpath.to_a)
512
- assert_equal(["foo","bar","baz"], @upath.to_a)
513
- assert_equal(["foo","bar","baz"], @npath.to_a)
514
- assert_equal(["Z:"], @rpath.to_a)
515
- assert_equal(["foo","bar"], @xpath.to_a)
516
- assert_equal(["foo"], @ypath.to_a)
517
- assert_equal([], @zpath.to_a)
518
- assert_equal([], @epath.to_a)
519
- assert_equal(["C:", "foo", "bar"], @ppath.to_a)
520
-
521
- assert_non_destructive
522
- end
523
-
524
- def test_is_root
525
- assert_equal(false, @fpath.root?)
526
- assert_equal(false, @bpath.root?)
527
- assert_equal(false, @upath.root?)
528
- assert_equal(false, @npath.root?)
529
- assert_equal(true, @rpath.root?)
530
- assert_equal(true, @xpath.root?)
531
- assert_equal(true, @ypath.root?)
532
- assert_equal(true, @zpath.root?)
533
- assert_equal(false, @epath.root?)
534
- assert_equal(false, @ppath.root?)
535
-
536
- assert_non_destructive
537
- end
538
-
539
- # These are the methods from IO we have to explicitly define since
540
- # they aren't handled by Facade.
541
- def test_facade_io
542
- assert_respond_to(@fpath, :foreach)
543
- assert_respond_to(@fpath, :read)
544
- assert_respond_to(@fpath, :readlines)
545
- assert_respond_to(@fpath, :sysopen)
546
- end
547
-
548
- def test_facade_file
549
- File.methods(false).each{ |method|
550
- assert_respond_to(@fpath, method.to_sym)
551
- }
552
- end
553
-
554
- def test_facade_dir
555
- Dir.methods(false).each{ |method|
556
- assert_respond_to(@fpath, method.to_sym)
557
- }
558
- end
559
-
560
- def test_facade_fileutils
561
- methods = FileUtils.public_instance_methods
562
- methods -= File.methods(false)
563
- methods -= Dir.methods(false)
564
- methods.delete_if{ |m| m =~ /stream/ }
565
- methods.delete_if{ |m| m =~ /^ln/ }
566
- methods.delete("identical?")
567
-
568
- methods.each{ |method|
569
- assert_respond_to(@fpath, method.to_sym)
570
- }
571
- end
572
-
573
- def test_facade_find
574
- assert_respond_to(@fpath, :find)
575
- assert_nothing_raised{ @fpath.find{} }
576
-
577
- Pathname.new(Dir.pwd).find{ |f|
578
- Find.prune if f.match("CVS")
579
- assert_kind_of(Pathname, f)
580
- }
581
- end
582
-
583
- def test_children
584
- assert_respond_to(@cur_path, :children)
585
- assert_nothing_raised{ @cur_path.children }
586
- assert_kind_of(Array, @cur_path.children)
587
-
588
- # Delete Eclipse related files
589
- children = @cur_path.children
590
- children.delete_if{ |e| File.basename(e) == "CVS" }
591
- children.delete_if{ |e| File.basename(e) == ".cvsignore" }
592
- children.delete_if{ |e| File.basename(e) == ".project" }
593
- children.delete_if{ |e| File.basename(e) == ".loadpath" }
594
-
595
- assert_equal(
596
- [
597
- Dir.pwd + "/benchmarks",
598
- Dir.pwd + "/CHANGES",
599
- Dir.pwd + "/examples",
600
- Dir.pwd + "/lib",
601
- Dir.pwd + "/MANIFEST",
602
- Dir.pwd + "/pathname2.gemspec",
603
- Dir.pwd + "/Rakefile",
604
- Dir.pwd + "/README",
605
- Dir.pwd + "/test"
606
- ].map{ |e| e.tr("/", "\\") },
607
- children
608
- )
609
-
610
- # Delete Eclipse related files
611
- children = @cur_path.children(false)
612
- children.delete("CVS")
613
- children.delete(".cvsignore")
614
- children.delete(".project")
615
- children.delete(".loadpath")
616
-
617
- assert_equal(
618
- [
619
- "benchmarks", "CHANGES", "examples", "lib", "MANIFEST",
620
- "pathname2.gemspec", "Rakefile", "README", "test"
621
- ],
622
- children
623
- )
624
- end
625
-
626
- # Ensures that subclasses return the subclass as the class, not a hard
627
- # coded Pathname.
628
- def test_subclasses
629
- assert_kind_of(MyPathname, @mypath)
630
- assert_kind_of(MyPathname, @mypath + MyPathname.new('foo'))
631
- assert_kind_of(MyPathname, @mypath.realpath)
632
- assert_kind_of(MyPathname, @mypath.children.first)
633
- end
634
-
635
- # Test to ensure that the pn{ } shortcut works
636
- #
637
- def test_kernel_method
638
- assert_respond_to(Kernel, :pn)
639
- assert_nothing_raised{ pn{'c:\foo'} }
640
- assert_kind_of(Pathname, pn{'c:\foo'})
641
- assert_equal('c:\foo', pn{'c:\foo'})
642
- end
643
-
644
- def teardown
645
- @fpath = nil
646
- @bpath = nil
647
- @dpath = nil
648
- @spath = nil
649
- @upath = nil
650
- @npath = nil
651
- @rpath = nil
652
- @xpath = nil
653
- @ypath = nil
654
- @zpath = nil
655
- @epath = nil
656
- @ppath = nil
657
- @cpath = nil
658
- @tpath = nil
659
-
660
- @cur_path = nil
661
-
662
- @abs_array.clear
663
- @rel_array.clear
664
- @unc_array.clear
665
- end
19
+ include Windows::SystemInfo
20
+
21
+ def setup
22
+ @fpath = Pathname.new("C:/Program Files/Windows NT/Accessories")
23
+ @bpath = Pathname.new("C:\\Program Files\\Windows NT\\Accessories")
24
+ @dpath = Pathname.new("C:\\Program Files\\File[5].txt")
25
+ @spath = Pathname.new("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1")
26
+ @upath = Pathname.new("\\\\foo\\bar\\baz")
27
+ @npath = Pathname.new("foo\\bar\\baz")
28
+ @rpath = Pathname.new("Z:\\")
29
+ @xpath = Pathname.new("\\\\foo\\bar")
30
+ @ypath = Pathname.new("\\\\foo")
31
+ @zpath = Pathname.new("\\\\")
32
+ @epath = Pathname.new("")
33
+ @ppath = Pathname.new("C:\\foo\\bar\\")
34
+ @cpath = Pathname.new("C:\\foo\\..\\bar\\.\\baz")
35
+ @tpath = Pathname.new("C:\\foo\\bar")
36
+
37
+ @url_path = Pathname.new("file:///C:/Documents%20and%20Settings")
38
+ @cur_path = Pathname.new(Dir.pwd)
39
+
40
+ @mypath = MyPathname.new("C:\\Program Files")
41
+
42
+ @abs_array = []
43
+ @rel_array = []
44
+ @unc_array = []
45
+ end
46
+
47
+ def test_aref_with_range
48
+ assert_equal("C:\\Program Files", @fpath[0..1])
49
+ assert_equal("C:\\Program Files\\Windows NT", @fpath[0..2])
50
+ assert_equal("Program Files\\Windows NT", @fpath[1..2])
51
+ assert_equal(@fpath, @fpath[0..-1])
52
+ end
53
+
54
+ def test_aref_with_index_and_length
55
+ assert_equal("C:", @fpath[0,1])
56
+ assert_equal("C:\\Program Files", @fpath[0,2])
57
+ assert_equal("Program Files\\Windows NT", @fpath[1,2])
58
+ end
59
+
60
+ def test_aref_with_index
61
+ assert_equal("C:", @fpath[0])
62
+ assert_equal("Program Files", @fpath[1])
63
+ assert_equal("Accessories", @fpath[-1])
64
+ assert_equal(nil, @fpath[10])
65
+ end
66
+
67
+ def test_version
68
+ assert_equal('1.6.4', Pathname::VERSION)
69
+ end
70
+
71
+ # Convenience method for test_plus
72
+ def assert_pathname_plus(a, b, c)
73
+ a = Pathname.new(a)
74
+ b = Pathname.new(b)
75
+ c = Pathname.new(c)
76
+ assert_equal(a, b + c)
77
+ end
78
+
79
+ # Convenience method for test_spaceship operator
80
+ def assert_pathname_cmp(int, s1, s2)
81
+ p1 = Pathname.new(s1)
82
+ p2 = Pathname.new(s2)
83
+ result = p1 <=> p2
84
+ assert_equal(int, result)
85
+ end
86
+
87
+ # Convenience method for test_relative_path_from
88
+ def assert_relpath(result, dest, base)
89
+ assert_equal(result, Pathname.new(dest).relative_path_from(base))
90
+ end
91
+
92
+ # Convenience method for test_relative_path_from_expected_errors
93
+ def assert_relative_path_error(to, from)
94
+ assert_raise(ArgumentError){
95
+ Pathname.new(to).relative_path_from(from)
96
+ }
97
+ end
98
+
99
+ def test_file_urls
100
+ assert_equal("C:\\Documents and Settings", @url_path)
101
+ assert_raises(Pathname::Error){ Pathname.new('http://rubyforge.org') }
102
+ end
103
+
104
+ def test_realpath
105
+ assert_respond_to(@fpath, :realpath)
106
+ assert_equal(@cur_path, Pathname.new('.').realpath)
107
+ assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
108
+ end
109
+
110
+ def test_relative_path_from
111
+ assert_relpath("..\\a", "a", "b")
112
+ assert_relpath("..\\a", "a", "b\\")
113
+ assert_relpath("..\\a", "a\\", "b")
114
+ assert_relpath("..\\a", "a\\", "b\\")
115
+ assert_relpath("..\\a", "c:\\a", "c:\\b")
116
+ assert_relpath("..\\a", "c:\\a", "c:\\b\\")
117
+ assert_relpath("..\\a", "c:\\a\\", "c:\\b")
118
+ assert_relpath("..\\a", "c:\\a\\", "c:\\b\\")
119
+
120
+ assert_relpath("..\\b", "a\\b", "a\\c")
121
+ assert_relpath("..\\a", "..\\a", "..\\b")
122
+
123
+ assert_relpath("a", "a", ".")
124
+ assert_relpath("..", ".", "a")
125
+
126
+ assert_relpath(".", ".", ".")
127
+ assert_relpath(".", "..", "..")
128
+ assert_relpath("..", "..", ".")
129
+
130
+ assert_relpath("c\\d", "c:\\a\\b\\c\\d", "c:\\a\\b")
131
+ assert_relpath("..\\..", "c:\\a\\b", "c:\\a\\b\\c\\d")
132
+ assert_relpath("..\\..\\..\\..\\e", "c:\\e", "c:\\a\\b\\c\\d")
133
+ assert_relpath("..\\b\\c", "a\\b\\c", "a\\d")
134
+
135
+ assert_relpath("..\\a", "c:\\..\\a", "c:\\b")
136
+ #assert_relpath("..\\..\\a", "..\\a", "b") # fails
137
+ assert_relpath(".", "c:\\a\\..\\..\\b", "c:\\b")
138
+ assert_relpath("..", "a\\..", "a")
139
+ assert_relpath(".", "a\\..\\b", "b")
140
+
141
+ assert_relpath("a", "a", "b\\..")
142
+ assert_relpath("b\\c", "b\\c", "b\\..")
143
+ end
144
+
145
+ def test_relative_path_from_expected_errors
146
+ assert_relative_path_error("c:\\", ".")
147
+ assert_relative_path_error(".", "c:\\")
148
+ assert_relative_path_error("a", "..")
149
+ assert_relative_path_error(".", "..")
150
+ assert_relative_path_error("C:\\Temp", "D:\\Temp")
151
+ assert_relative_path_error("\\\\Server\\Temp", "D:\\Temp")
152
+ end
153
+
154
+ # Convenience method to verify that the receiver was not modified
155
+ # except perhaps slashes
156
+ def assert_non_destructive
157
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath)
158
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
159
+ assert_equal("C:\\Program Files\\File[5].txt", @dpath)
160
+ assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @spath)
161
+ assert_equal("\\\\foo\\bar\\baz", @upath)
162
+ assert_equal("foo\\bar\\baz", @npath)
163
+ assert_equal("Z:\\", @rpath)
164
+ assert_equal("\\\\foo\\bar", @xpath)
165
+ assert_equal("\\\\foo", @ypath)
166
+ assert_equal("\\\\", @zpath)
167
+ assert_equal("", @epath)
168
+ assert_equal("C:\\foo\\bar\\", @ppath)
169
+ assert_equal("C:\\foo\\..\\bar\\.\\baz", @cpath)
170
+ end
171
+
172
+ def test_parent
173
+ assert_respond_to(@bpath, :parent)
174
+ assert_equal("C:\\Program Files\\Windows NT", @bpath.parent)
175
+ assert_equal("foo\\bar", @npath.parent)
176
+ assert_equal("Z:\\", @rpath.parent)
177
+ end
178
+
179
+ def test_short_path
180
+ assert_respond_to(@bpath, :short_path)
181
+ assert_nothing_raised{ @bpath.short_path }
182
+ assert_kind_of(Pathname, @bpath.short_path)
183
+ assert_match(/C:\\PROGRA~1\\WINDOW~\d\\ACCESS~\d/, @bpath.short_path)
184
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
185
+ end
186
+
187
+ test "long_path basic functionality" do
188
+ assert_respond_to(@spath, :long_path)
189
+ assert_nothing_raised{ @spath.long_path }
190
+ assert_kind_of(Pathname, @spath.long_path)
191
+ end
192
+
193
+ test "long_path returns expected result" do
194
+ if windows_7?
195
+ spath = Pathname.new("C:\\PROGRA~1\\Window~2\\ACCESS~1")
196
+ lpath = "C:\\Program Files\\Windows NT\\Accessories"
197
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", spath.long_path)
198
+ assert_match(/C:\\PROGRA~1\\WINDOW~\d\\ACCESS~\d/i, spath)
199
+ else
200
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @spath.long_path)
201
+ assert_match(/C:\\PROGRA~1\\WINDOW~\d\\ACCESS~\d/i, @spath)
202
+ end
203
+ end
204
+
205
+ test "undecorate basic functionality" do
206
+ assert_respond_to(@dpath, :undecorate)
207
+ assert_nothing_raised{ @dpath.undecorate }
208
+ assert_kind_of(Pathname, @dpath.undecorate)
209
+ end
210
+
211
+ test "undecorate returns expected results" do
212
+ assert_equal('C:\Program Files\File.txt', @dpath.undecorate)
213
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate)
214
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate)
215
+ assert_equal('C:\Path\[3].txt', Pathname.new('C:\Path\[3].txt').undecorate)
216
+ assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate)
217
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate)
218
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate)
219
+ assert_equal("C:\\Program Files\\File[5].txt", @dpath)
220
+ end
221
+
222
+ test "undecorate_bang basic functionality" do
223
+ assert_respond_to(@dpath, :undecorate!)
224
+ assert_nothing_raised{ @dpath.undecorate! }
225
+ assert_kind_of(Pathname, @dpath.undecorate!)
226
+ end
227
+
228
+ test "undecorate_bang returns expected results" do
229
+ assert_equal('C:\Program Files\File.txt', @dpath.undecorate!)
230
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate!)
231
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate!)
232
+ assert_equal('C:\Path\[3].txt', Pathname.new('C:\Path\[3].txt').undecorate!)
233
+ assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate!)
234
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate!)
235
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate!)
236
+ assert_equal('C:\Program Files\File.txt', @dpath)
237
+ end
238
+
239
+ test "unc basic functionality" do
240
+ assert_respond_to(@upath, :unc?)
241
+ assert_nothing_raised{ @upath.unc? }
242
+ end
243
+
244
+ test "unc returns expected results" do
245
+ assert_true(@upath.unc?)
246
+ assert_true(@xpath.unc?)
247
+ assert_true(@ypath.unc?)
248
+ assert_true(@zpath.unc?)
249
+ assert_false(@fpath.unc?)
250
+ assert_false(@bpath.unc?)
251
+ assert_false(@dpath.unc?)
252
+ assert_false(@spath.unc?)
253
+ assert_false(@epath.unc?)
254
+
255
+ # Arguably a bug in the PathIsUNC() function since drive letters
256
+ # are, in fact, a legal part of a UNC path (for historical reasons).
257
+ assert_false(Pathname.new("C:\\\\foo\\bar\\baz").unc?)
258
+
259
+ assert_non_destructive
260
+ end
261
+
262
+ def test_pstrip
263
+ assert_respond_to(@ppath, :pstrip)
264
+ assert_nothing_raised{ @ppath.pstrip }
265
+ assert_nothing_raised{ @fpath.pstrip }
266
+ assert_kind_of(Pathname, @ppath.pstrip)
267
+
268
+ assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip)
269
+ assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip)
270
+ assert_equal("", Pathname.new("").pstrip)
271
+
272
+ assert_equal("C:\\foo\\bar\\", @ppath)
273
+ end
274
+
275
+ def test_pstrip_bang
276
+ assert_respond_to(@ppath, :pstrip!)
277
+ assert_nothing_raised{ @ppath.pstrip! }
278
+ assert_nothing_raised{ @fpath.pstrip! }
279
+ assert_kind_of(Pathname, @ppath.pstrip!)
280
+
281
+ assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip!)
282
+ assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip!)
283
+ assert_equal("", Pathname.new("").pstrip!)
284
+
285
+ assert_equal("C:\\foo\\bar", @ppath)
286
+ end
287
+
288
+ def test_exists
289
+ assert_respond_to(@fpath, :exists?)
290
+ assert_nothing_raised{ @fpath.exists? }
291
+ assert_true(Pathname.new("C:\\").exists?)
292
+ assert_false(Pathname.new("X:\\foo\\bar\\baz").exists?)
293
+ end
294
+
295
+ def test_each
296
+ array = []
297
+
298
+ assert_respond_to(@fpath, :each)
299
+ assert_nothing_raised{ @fpath.each{ |e| array.push(e) } }
300
+ assert_equal(["C:", "Program Files", "Windows NT", "Accessories"], array)
301
+ end
302
+
303
+ def test_descend
304
+ assert_respond_to(@bpath, :descend)
305
+ assert_nothing_raised{ @bpath.descend{} }
306
+
307
+ @bpath.descend{ |path| @abs_array.push(path) }
308
+ @npath.descend{ |path| @rel_array.push(path) }
309
+ @upath.descend{ |path| @unc_array.push(path) }
310
+
311
+ assert_equal("C:", @abs_array[0])
312
+ assert_equal("C:\\Program Files", @abs_array[1])
313
+ assert_equal("C:\\Program Files\\Windows NT", @abs_array[2])
314
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[3])
315
+
316
+ assert_equal("foo", @rel_array[0])
317
+ assert_equal("foo\\bar", @rel_array[1])
318
+ assert_equal("foo\\bar\\baz", @rel_array[2])
319
+
320
+ assert_equal("\\\\foo\\bar", @unc_array[0])
321
+ assert_equal("\\\\foo\\bar\\baz", @unc_array[1])
322
+
323
+ assert_non_destructive
324
+ end
325
+
326
+ def test_ascend
327
+ assert_respond_to(@bpath, :ascend)
328
+ assert_nothing_raised{ @bpath.ascend{} }
329
+
330
+ @bpath.ascend{ |path| @abs_array.push(path) }
331
+ @npath.ascend{ |path| @rel_array.push(path) }
332
+ @upath.ascend{ |path| @unc_array.push(path) }
333
+
334
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[0])
335
+ assert_equal("C:\\Program Files\\Windows NT", @abs_array[1])
336
+ assert_equal("C:\\Program Files", @abs_array[2])
337
+ assert_equal("C:", @abs_array[3])
338
+ assert_equal(4, @abs_array.length)
339
+
340
+ assert_equal("foo\\bar\\baz", @rel_array[0])
341
+ assert_equal("foo\\bar", @rel_array[1])
342
+ assert_equal("foo", @rel_array[2])
343
+ assert_equal(3, @rel_array.length)
344
+
345
+ assert_equal("\\\\foo\\bar\\baz", @unc_array[0])
346
+ assert_equal("\\\\foo\\bar", @unc_array[1])
347
+ assert_equal(2, @unc_array.length)
348
+
349
+ assert_non_destructive
350
+ end
351
+
352
+ def test_immutability
353
+ path = "C:\\Program Files\\foo\\bar".freeze
354
+ assert_true(path.frozen?)
355
+ assert_nothing_raised{ Pathname.new(path) }
356
+ assert_nothing_raised{ Pathname.new(path).root }
357
+ end
358
+
359
+ def test_plus_operator
360
+ # Standard stuff
361
+ assert_pathname_plus("C:\\a\\b", "C:\\a", "b")
362
+ assert_pathname_plus("C:\\b", "a", "C:\\b")
363
+ assert_pathname_plus("a\\b", "a", "b")
364
+ assert_pathname_plus("C:\\b", "C:\\a", "..\\b")
365
+ assert_pathname_plus("C:\\a\\b", "C:\\a\\.", "\\b")
366
+ assert_pathname_plus("C:\\a\\b.txt", "C:\\a", "b.txt")
367
+
368
+ # UNC paths
369
+ assert_pathname_plus("\\\\foo\\bar", "\\\\foo", "bar")
370
+ assert_pathname_plus("\\\\foo", "\\\\", "foo")
371
+ assert_pathname_plus("\\\\", "\\\\", "")
372
+ assert_pathname_plus("\\\\foo\\baz", "\\\\foo\\bar", "\\..\\baz")
373
+ assert_pathname_plus("\\\\", "\\\\", "..\\..\\..\\..")
374
+
375
+ # Pathname + String
376
+ assert_nothing_raised{ @tpath + "bar" }
377
+ assert_equal('C:\foo\bar\baz', @tpath + 'baz')
378
+ assert_equal('C:\foo\bar', @tpath)
379
+
380
+ # Ensure neither left nor right operand are modified
381
+ assert_nothing_raised{ @tpath + @npath }
382
+ assert_equal('C:\foo\bar\foo\bar\baz', @tpath + @npath)
383
+ assert_equal('C:\foo\bar', @tpath)
384
+ assert_equal('foo\bar\baz', @npath)
385
+ end
386
+
387
+ def test_clean
388
+ assert_respond_to(@cpath, :clean)
389
+ assert_nothing_raised{ @cpath.clean }
390
+ assert_kind_of(Pathname, @cpath.clean)
391
+
392
+ # Our preset stuff
393
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean)
394
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean)
395
+ assert_equal("\\\\foo\\bar\\baz", @upath.clean)
396
+ assert_equal("foo\\bar\\baz", @npath.clean)
397
+ assert_equal("Z:\\", @rpath.clean)
398
+ assert_equal("\\\\foo\\bar", @xpath.clean)
399
+ assert_equal("\\\\foo", @ypath.clean)
400
+ assert_equal("\\\\", @zpath.clean)
401
+ assert_equal("", @epath.clean)
402
+ assert_equal("C:\\bar\\baz", @cpath.clean)
403
+
404
+ # Standard stuff
405
+ assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean)
406
+ assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean)
407
+ assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean)
408
+ assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean)
409
+ assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean)
410
+ assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean)
411
+ assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean)
412
+
413
+ # Edge cases
414
+ assert_equal("\\", Pathname.new(".").clean)
415
+ assert_equal("\\", Pathname.new("..").clean)
416
+
417
+ assert_non_destructive
418
+ end
419
+
420
+ def test_clean_bang
421
+ assert_respond_to(@cpath, :clean!)
422
+ assert_nothing_raised{ @cpath.clean! }
423
+ assert_kind_of(Pathname, @cpath.clean!)
424
+
425
+ # Our preset stuff
426
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean!)
427
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean!)
428
+ assert_equal("\\\\foo\\bar\\baz", @upath.clean!)
429
+ assert_equal("foo\\bar\\baz", @npath.clean!)
430
+ assert_equal("Z:\\", @rpath.clean!)
431
+ assert_equal("\\\\foo\\bar", @xpath.clean!)
432
+ assert_equal("\\\\foo", @ypath.clean!)
433
+ assert_equal("\\\\", @zpath.clean!)
434
+ assert_equal("", @epath.clean!)
435
+ assert_equal("C:\\bar\\baz", @cpath.clean!)
436
+
437
+ # Standard stuff
438
+ assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean!)
439
+ assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean!)
440
+ assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean!)
441
+ assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean!)
442
+ assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean!)
443
+ assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean!)
444
+ assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean!)
445
+
446
+ # Edge cases
447
+ assert_equal("\\", Pathname.new(".").clean!)
448
+ assert_equal("\\", Pathname.new("..").clean!)
449
+
450
+ assert_equal("C:\\bar\\baz", @cpath)
451
+ end
452
+
453
+ def test_absolute
454
+ assert_true(@fpath.absolute?)
455
+ assert_true(@bpath.absolute?)
456
+ assert_true(@upath.absolute?)
457
+ assert_false(@npath.absolute?)
458
+ assert_true(@rpath.absolute?)
459
+ assert_true(@xpath.absolute?)
460
+ assert_true(@ypath.absolute?)
461
+ assert_true(@zpath.absolute?)
462
+ assert_false(@epath.absolute?)
463
+
464
+ assert_non_destructive
465
+ end
466
+
467
+ def test_relative
468
+ assert_false(@fpath.relative?)
469
+ assert_false(@bpath.relative?)
470
+ assert_false(@upath.relative?)
471
+ assert_true(@npath.relative?)
472
+ assert_false(@rpath.relative?)
473
+ assert_false(@xpath.relative?)
474
+ assert_false(@ypath.relative?)
475
+ assert_false(@zpath.relative?)
476
+ assert_true(@epath.relative?)
477
+ assert_non_destructive
478
+ end
479
+
480
+ def test_root
481
+ assert_equal("C:\\", @fpath.root)
482
+ assert_equal("C:\\", @bpath.root)
483
+ assert_equal("\\\\foo\\bar", @upath.root)
484
+ assert_equal(".", @npath.root)
485
+ assert_equal("Z:\\", @rpath.root)
486
+ assert_equal("\\\\foo\\bar", @xpath.root)
487
+ assert_equal("\\\\foo", @ypath.root)
488
+ assert_equal("\\\\", @zpath.root)
489
+ assert_equal(".", @epath.root)
490
+
491
+ # Edge cases
492
+ assert_equal(".", Pathname.new("..").root)
493
+ assert_equal(".", Pathname.new(".").root)
494
+
495
+ assert_non_destructive
496
+ end
497
+
498
+ def test_drive_number
499
+ assert_equal(2, @fpath.drive_number)
500
+ assert_equal(2, @bpath.drive_number)
501
+ assert_equal(nil, @upath.drive_number)
502
+ assert_equal(nil, @npath.drive_number)
503
+ assert_equal(25, @rpath.drive_number)
504
+ assert_equal(nil, @xpath.drive_number)
505
+ assert_equal(nil, @ypath.drive_number)
506
+ assert_equal(nil, @zpath.drive_number)
507
+ assert_equal(nil, @epath.drive_number)
508
+
509
+ # Edge cases
510
+ assert_equal(nil, Pathname.new("..").drive_number)
511
+ assert_equal(nil, Pathname.new(".").drive_number)
512
+
513
+ assert_non_destructive
514
+ end
515
+
516
+ def test_to_a
517
+ expected = ["C:", "Program Files", "Windows NT", "Accessories"]
518
+ assert_equal(expected, @fpath.to_a)
519
+ assert_equal(expected, @bpath.to_a)
520
+ assert_equal(["foo","bar","baz"], @upath.to_a)
521
+ assert_equal(["foo","bar","baz"], @npath.to_a)
522
+ assert_equal(["Z:"], @rpath.to_a)
523
+ assert_equal(["foo","bar"], @xpath.to_a)
524
+ assert_equal(["foo"], @ypath.to_a)
525
+ assert_equal([], @zpath.to_a)
526
+ assert_equal([], @epath.to_a)
527
+ assert_equal(["C:", "foo", "bar"], @ppath.to_a)
528
+
529
+ assert_non_destructive
530
+ end
531
+
532
+ def test_is_root
533
+ assert_false(@fpath.root?)
534
+ assert_false(@bpath.root?)
535
+ assert_false(@upath.root?)
536
+ assert_false(@npath.root?)
537
+ assert_true(@rpath.root?)
538
+ assert_true(@xpath.root?)
539
+ assert_true(@ypath.root?)
540
+ assert_true(@zpath.root?)
541
+ assert_false(@epath.root?)
542
+ assert_false(@ppath.root?)
543
+
544
+ assert_non_destructive
545
+ end
546
+
547
+ # These are the methods from IO we have to explicitly define since
548
+ # they aren't handled by Facade.
549
+ def test_facade_io
550
+ assert_respond_to(@fpath, :foreach)
551
+ assert_respond_to(@fpath, :read)
552
+ assert_respond_to(@fpath, :readlines)
553
+ assert_respond_to(@fpath, :sysopen)
554
+ end
555
+
556
+ def test_facade_file
557
+ File.methods(false).each{ |method|
558
+ assert_respond_to(@fpath, method.to_sym)
559
+ }
560
+ end
561
+
562
+ def test_facade_dir
563
+ Dir.methods(false).each{ |method|
564
+ assert_respond_to(@fpath, method.to_sym)
565
+ }
566
+ end
567
+
568
+ def test_facade_fileutils
569
+ methods = FileUtils.public_instance_methods
570
+ methods -= File.methods(false)
571
+ methods -= Dir.methods(false)
572
+ methods.delete_if{ |m| m =~ /stream/ }
573
+ methods.delete_if{ |m| m =~ /^ln/ }
574
+ methods.delete("identical?")
575
+
576
+ methods.each{ |method|
577
+ assert_respond_to(@fpath, method.to_sym)
578
+ }
579
+ end
580
+
581
+ def test_facade_find
582
+ assert_respond_to(@fpath, :find)
583
+ assert_nothing_raised{ @fpath.find{} }
584
+
585
+ Pathname.new(Dir.pwd).find{ |f|
586
+ Find.prune if f.match("CVS")
587
+ assert_kind_of(Pathname, f)
588
+ }
589
+ end
590
+
591
+ def test_children
592
+ assert_respond_to(@cur_path, :children)
593
+ assert_nothing_raised{ @cur_path.children }
594
+ assert_kind_of(Array, @cur_path.children)
595
+
596
+ # Delete Eclipse related files
597
+ children = @cur_path.children
598
+ children.delete_if{ |e| File.basename(e) == "CVS" }
599
+ children.delete_if{ |e| File.basename(e) == ".git" }
600
+ children.delete_if{ |e| File.basename(e) == ".gitignore" }
601
+ children.delete_if{ |e| File.basename(e) == ".cvsignore" }
602
+ children.delete_if{ |e| File.basename(e) == ".project" }
603
+ children.delete_if{ |e| File.basename(e) == ".loadpath" }
604
+
605
+ assert_equal(
606
+ [
607
+ Dir.pwd + "/benchmarks",
608
+ Dir.pwd + "/CHANGES",
609
+ Dir.pwd + "/examples",
610
+ Dir.pwd + "/lib",
611
+ Dir.pwd + "/MANIFEST",
612
+ Dir.pwd + "/pathname2.gemspec",
613
+ Dir.pwd + "/Rakefile",
614
+ Dir.pwd + "/README",
615
+ Dir.pwd + "/test"
616
+ ].map{ |e| e.tr("/", "\\") },
617
+ children
618
+ )
619
+
620
+ # Delete Eclipse related files
621
+ children = @cur_path.children(false)
622
+ children.delete("CVS")
623
+ children.delete(".git")
624
+ children.delete(".gitignore")
625
+ children.delete(".cvsignore")
626
+ children.delete(".project")
627
+ children.delete(".loadpath")
628
+
629
+ assert_equal(
630
+ [
631
+ "benchmarks", "CHANGES", "examples", "lib", "MANIFEST",
632
+ "pathname2.gemspec", "Rakefile", "README", "test"
633
+ ],
634
+ children
635
+ )
636
+ end
637
+
638
+ # Ensures that subclasses return the subclass as the class, not a hard
639
+ # coded Pathname.
640
+ def test_subclasses
641
+ assert_kind_of(MyPathname, @mypath)
642
+ assert_kind_of(MyPathname, @mypath + MyPathname.new('foo'))
643
+ assert_kind_of(MyPathname, @mypath.realpath)
644
+ assert_kind_of(MyPathname, @mypath.children.first)
645
+ end
646
+
647
+ # Test to ensure that the pn{ } shortcut works
648
+ #
649
+ def test_kernel_method
650
+ assert_respond_to(Kernel, :pn)
651
+ assert_nothing_raised{ pn{'c:\foo'} }
652
+ assert_kind_of(Pathname, pn{'c:\foo'})
653
+ assert_equal('c:\foo', pn{'c:\foo'})
654
+ end
655
+
656
+ def teardown
657
+ @fpath = nil
658
+ @bpath = nil
659
+ @dpath = nil
660
+ @spath = nil
661
+ @upath = nil
662
+ @npath = nil
663
+ @rpath = nil
664
+ @xpath = nil
665
+ @ypath = nil
666
+ @zpath = nil
667
+ @epath = nil
668
+ @ppath = nil
669
+ @cpath = nil
670
+ @tpath = nil
671
+
672
+ @cur_path = nil
673
+
674
+ @abs_array.clear
675
+ @rel_array.clear
676
+ @unc_array.clear
677
+ end
666
678
  end