pathname2 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +5 -0
  4. data/CHANGES +171 -0
  5. data/LICENSE +177 -0
  6. data/MANIFEST +40 -0
  7. data/README +97 -0
  8. data/Rakefile +222 -0
  9. data/benchmarks/bench_pathname.rb +127 -0
  10. data/benchmarks/bench_plus.rb +34 -0
  11. data/certs/djberg96_pub.pem +26 -0
  12. data/examples/example_pathname.rb +25 -0
  13. data/lib/pathname2.rb +1140 -0
  14. data/pathname2.gemspec +46 -0
  15. data/test/test_pathname.rb +486 -0
  16. data/test/test_version.rb +17 -0
  17. data/test/windows/test_append.rb +59 -0
  18. data/test/windows/test_aref.rb +37 -0
  19. data/test/windows/test_ascend.rb +51 -0
  20. data/test/windows/test_children.rb +42 -0
  21. data/test/windows/test_clean.rb +50 -0
  22. data/test/windows/test_clean_bang.rb +50 -0
  23. data/test/windows/test_constructor.rb +48 -0
  24. data/test/windows/test_descend.rb +51 -0
  25. data/test/windows/test_drive_number.rb +58 -0
  26. data/test/windows/test_each.rb +28 -0
  27. data/test/windows/test_facade.rb +64 -0
  28. data/test/windows/test_is_absolute.rb +47 -0
  29. data/test/windows/test_is_relative.rb +46 -0
  30. data/test/windows/test_is_root.rb +44 -0
  31. data/test/windows/test_is_unc.rb +51 -0
  32. data/test/windows/test_join.rb +52 -0
  33. data/test/windows/test_long_path.rb +41 -0
  34. data/test/windows/test_misc.rb +33 -0
  35. data/test/windows/test_parent.rb +37 -0
  36. data/test/windows/test_pstrip.rb +42 -0
  37. data/test/windows/test_pstrip_bang.rb +46 -0
  38. data/test/windows/test_realpath.rb +39 -0
  39. data/test/windows/test_relative_path_from.rb +60 -0
  40. data/test/windows/test_root.rb +59 -0
  41. data/test/windows/test_short_path.rb +41 -0
  42. data/test/windows/test_to_a.rb +46 -0
  43. data/test/windows/test_undecorate.rb +47 -0
  44. data/test/windows/test_undecorate_bang.rb +53 -0
  45. metadata +176 -0
  46. metadata.gz.sig +1 -0
@@ -0,0 +1,17 @@
1
+ ########################################################################
2
+ # test_version.rb
3
+ #
4
+ # Universal test file that tests for the proper version number.
5
+ ########################################################################
6
+ require 'pathname2'
7
+ require 'test-unit'
8
+
9
+ class TC_Pathname_Version < Test::Unit::TestCase
10
+ test "version is set to expected value" do
11
+ assert_equal('1.8.1', Pathname::VERSION)
12
+ end
13
+
14
+ test "version is frozen" do
15
+ assert_true(Pathname::VERSION.frozen?)
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ ########################################################################
2
+ # test_append.rb
3
+ #
4
+ # Test suite for the Pathname#append method.
5
+ ########################################################################
6
+ require 'test-unit'
7
+ require 'pathname2'
8
+
9
+ class TC_Pathname_Append < Test::Unit::TestCase
10
+ def setup
11
+ @abs_path = Pathname.new("C:\\foo\\bar")
12
+ @rel_path = Pathname.new("foo\\bar\\baz")
13
+ end
14
+
15
+ def assert_pathname_plus(a, b, c)
16
+ a = Pathname.new(a)
17
+ b = Pathname.new(b)
18
+ c = Pathname.new(c)
19
+ assert_equal(a, b + c)
20
+ end
21
+
22
+ test "appending a string to an absolute path works as expected" do
23
+ assert_pathname_plus("C:\\a\\b", "C:\\a", "b")
24
+ assert_pathname_plus("C:\\b", "a", "C:\\b")
25
+ assert_pathname_plus("a\\b", "a", "b")
26
+ assert_pathname_plus("C:\\b", "C:\\a", "..\\b")
27
+ assert_pathname_plus("C:\\a\\b", "C:\\a\\.", "\\b")
28
+ assert_pathname_plus("C:\\a\\b.txt", "C:\\a", "b.txt")
29
+ end
30
+
31
+ test "appending a string to a UNC path works as expected" do
32
+ assert_pathname_plus("\\\\foo\\bar", "\\\\foo", "bar")
33
+ assert_pathname_plus("\\\\foo", "\\\\", "foo")
34
+ assert_pathname_plus("\\\\", "\\\\", "")
35
+ assert_pathname_plus("\\\\foo\\baz", "\\\\foo\\bar", "\\..\\baz")
36
+ assert_pathname_plus("\\\\", "\\\\", "..\\..\\..\\..")
37
+ end
38
+
39
+ test "appending a plain string to a path works as expected" do
40
+ assert_nothing_raised{ @abs_path + "bar" }
41
+ assert_equal('C:\foo\bar\baz', @abs_path + 'baz')
42
+ assert_equal('C:\foo\bar', @abs_path)
43
+ end
44
+
45
+ test "appending an absolute path results in that absolute path" do
46
+ assert_pathname_plus('C:\foo\bar', @rel_path, @abs_path)
47
+ end
48
+
49
+ test "neither receiver nor argument are modified" do
50
+ assert_nothing_raised{ @abs_path + @rel_path }
51
+ assert_equal('C:\foo\bar\foo\bar\baz', @abs_path + @rel_path)
52
+ assert_equal('C:\foo\bar', @abs_path)
53
+ assert_equal('foo\bar\baz', @rel_path)
54
+ end
55
+
56
+ def teardown
57
+ @path = nil
58
+ end
59
+ end
@@ -0,0 +1,37 @@
1
+ ########################################################################
2
+ # test_aref.rb
3
+ #
4
+ # Test suite for the Pathname#[] method.
5
+ ########################################################################
6
+ require 'test-unit'
7
+ require 'pathname2'
8
+
9
+ class TC_Pathname_Aref < Test::Unit::TestCase
10
+ def setup
11
+ @path = Pathname.new("C:/Program Files/Windows NT/Accessories")
12
+ end
13
+
14
+ test "[] with index works as expected" do
15
+ assert_equal("C:", @path[0])
16
+ assert_equal("Program Files", @path[1])
17
+ assert_equal("Accessories", @path[-1])
18
+ assert_nil(@path[10])
19
+ end
20
+
21
+ test "[] with range argument works as expected" do
22
+ assert_equal("C:\\Program Files", @path[0..1])
23
+ assert_equal("C:\\Program Files\\Windows NT", @path[0..2])
24
+ assert_equal("Program Files\\Windows NT", @path[1..2])
25
+ #assert_equal(@path, @path[0..-1]) # TODO: Spews tons of warnings
26
+ end
27
+
28
+ test "[] with index and length works as expected" do
29
+ assert_equal("C:", @path[0,1])
30
+ assert_equal("C:\\Program Files", @path[0,2])
31
+ assert_equal("Program Files\\Windows NT", @path[1,2])
32
+ end
33
+
34
+ def teardown
35
+ @path = nil
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ ########################################################################
2
+ # test_ascend.rb
3
+ #
4
+ # Test suite for the Pathname#ascend method.
5
+ ########################################################################
6
+ require 'pathname2'
7
+ require 'test-unit'
8
+
9
+ class TC_Pathname_Ascend < Test::Unit::TestCase
10
+ def setup
11
+ @path = Pathname.new("C:\\foo\\bar\\baz")
12
+ end
13
+
14
+ test "ascend basic functionality" do
15
+ assert_respond_to(@path, :ascend)
16
+ assert_nothing_raised{ @path.ascend{} }
17
+ end
18
+
19
+ test "ascend works as expected on a standard absolute path" do
20
+ array = []
21
+ @path.ascend{ |e| array << e }
22
+ assert_equal('C:\foo\bar\baz', array[0])
23
+ assert_equal('C:\foo\bar', array[1])
24
+ assert_equal('C:\foo', array[2])
25
+ assert_equal('C:', array[3])
26
+ end
27
+
28
+ test "ascend works as expected on a UNC path" do
29
+ array = []
30
+ Pathname.new('//foo/bar/baz').ascend{ |e| array << e }
31
+ assert_equal("\\\\foo\\bar\\baz", array[0])
32
+ assert_equal("\\\\foo\\bar", array[1])
33
+ end
34
+
35
+ test "ascend works as expected on a relative path" do
36
+ array = []
37
+ Pathname.new('foo/bar/baz').ascend{ |e| array << e }
38
+ assert_equal('foo\bar\baz', array[0])
39
+ assert_equal('foo\bar', array[1])
40
+ assert_equal('foo', array[2])
41
+ end
42
+
43
+ test "ascend does not modify the receiver" do
44
+ @path.ascend{}
45
+ assert_equal('C:\foo\bar\baz', @path)
46
+ end
47
+
48
+ def teardown
49
+ @path = nil
50
+ end
51
+ end
@@ -0,0 +1,42 @@
1
+ ########################################################################
2
+ # test_children.rb
3
+ #
4
+ # Test suite for the Pathname#children method.
5
+ ########################################################################
6
+ require 'pathname2'
7
+ require 'test-unit'
8
+
9
+ class TC_Pathname_Children < Test::Unit::TestCase
10
+ def setup
11
+ @dir = 'foo'
12
+ @path = Pathname.new(File.dirname(File.dirname(__FILE__)))
13
+
14
+ Dir.mkdir(@dir)
15
+ Dir.chdir(@dir){
16
+ FileUtils.touch('alpha')
17
+ FileUtils.touch('beta')
18
+ FileUtils.touch('gamma')
19
+ }
20
+ end
21
+
22
+ test "children basic functionality" do
23
+ assert_respond_to(@path, :children)
24
+ assert_nothing_raised{ @path.children{} }
25
+ assert_kind_of(Array, @path.children)
26
+ end
27
+
28
+ test "children method returns expected results" do
29
+ path = Pathname.new(@dir)
30
+ assert_equal(%w[foo\alpha foo\beta foo\gamma], path.children)
31
+ end
32
+
33
+ test "each result of the children method is a Pathname object" do
34
+ path = Pathname.new(@dir)
35
+ assert_kind_of(Pathname, path.children.first)
36
+ end
37
+
38
+ def teardown
39
+ FileUtils.rm_rf(@dir) if File.exist?(@dir)
40
+ @path = nil
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ ########################################################################
2
+ # test_clean.rb
3
+ #
4
+ # Test suite for the Pathname#clean method.
5
+ ########################################################################
6
+ require 'test-unit'
7
+ require 'pathname2'
8
+
9
+ class TC_Pathname_Clean < Test::Unit::TestCase
10
+ def setup
11
+ @path = Pathname.new("C:\\foo\\..\\bar\\.\\baz")
12
+ end
13
+
14
+ test "clean basic functionality" do
15
+ assert_respond_to(@path, :clean)
16
+ assert_nothing_raised{ @path.clean }
17
+ assert_kind_of(Pathname, @path.clean)
18
+ end
19
+
20
+ test "clean returns expected results for unclean paths" do
21
+ assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean)
22
+ assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean)
23
+ assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean)
24
+ assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean)
25
+ assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean)
26
+ assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean)
27
+ end
28
+
29
+ test "clean returns already clean paths unmodified" do
30
+ assert_equal("C:\\", Pathname.new("C:\\").clean)
31
+ assert_equal("C:\\a", Pathname.new("C:\\a").clean)
32
+ assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean)
33
+ assert_equal("\\\\foo\\bar", Pathname.new("\\\\foo\\bar").clean)
34
+ assert_equal("a", Pathname.new("a").clean)
35
+ end
36
+
37
+ test "clean returns a slash for . and .." do
38
+ assert_equal("\\", Pathname.new(".").clean)
39
+ assert_equal("\\", Pathname.new("..").clean)
40
+ end
41
+
42
+ test "clean does not modify receiver" do
43
+ @path.clean
44
+ assert_equal("C:\\foo\\..\\bar\\.\\baz", @path)
45
+ end
46
+
47
+ def teardown
48
+ @path = nil
49
+ end
50
+ end
@@ -0,0 +1,50 @@
1
+ ########################################################################
2
+ # test_clean_bang.rb
3
+ #
4
+ # Test suite for the Pathname#clean! method.
5
+ ########################################################################
6
+ require 'test-unit'
7
+ require 'pathname2'
8
+
9
+ class TC_Pathname_CleanBang < Test::Unit::TestCase
10
+ def setup
11
+ @path = Pathname.new("C:\\foo\\..\\bar\\.\\baz")
12
+ end
13
+
14
+ test "clean basic functionality" do
15
+ assert_respond_to(@path, :clean!)
16
+ assert_nothing_raised{ @path.clean! }
17
+ assert_kind_of(Pathname, @path.clean!)
18
+ end
19
+
20
+ test "clean returns expected results for unclean paths" do
21
+ assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean!)
22
+ assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean!)
23
+ assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean!)
24
+ assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean!)
25
+ assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean!)
26
+ assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean!)
27
+ end
28
+
29
+ test "clean returns already clean paths unmodified" do
30
+ assert_equal("C:\\", Pathname.new("C:\\").clean!)
31
+ assert_equal("C:\\a", Pathname.new("C:\\a").clean!)
32
+ assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean!)
33
+ assert_equal("\\\\foo\\bar", Pathname.new("\\\\foo\\bar").clean!)
34
+ assert_equal("a", Pathname.new("a").clean!)
35
+ end
36
+
37
+ test "clean returns a slash for . and .." do
38
+ assert_equal("\\", Pathname.new(".").clean!)
39
+ assert_equal("\\", Pathname.new("..").clean!)
40
+ end
41
+
42
+ test "clean! modifies receiver" do
43
+ @path.clean!
44
+ assert_equal("C:\\bar\\baz", @path)
45
+ end
46
+
47
+ def teardown
48
+ @path = nil
49
+ end
50
+ end
@@ -0,0 +1,48 @@
1
+ ########################################################################
2
+ # test_constructor.rb
3
+ #
4
+ # Various tests for the Pathname.new method.
5
+ ########################################################################
6
+ require 'pathname2'
7
+ require 'test-unit'
8
+
9
+ class TC_Pathname_Constructor < Test::Unit::TestCase
10
+ def setup
11
+ @abs_path = "C:/Users"
12
+ @rel_path = "Users"
13
+ @url_path = "file:///C:/Documents%20and%20Settings"
14
+ end
15
+
16
+ test "constructor handles absolute paths properly" do
17
+ assert_nothing_raised{ Pathname.new(@abs_path) }
18
+ assert_equal("C:\\Users", Pathname.new(@abs_path).to_s)
19
+ end
20
+
21
+ test "constructor handles relative paths properly" do
22
+ assert_nothing_raised{ Pathname.new(@rel_path) }
23
+ assert_equal("Users", Pathname.new(@rel_path).to_s)
24
+ end
25
+
26
+ test "constructor handles file URL's properly" do
27
+ assert_nothing_raised{ Pathname.new(@url_path) }
28
+ assert_equal("C:\\Documents and Settings", Pathname.new(@url_path).to_s)
29
+ end
30
+
31
+ test "constructor returns a Pathname object" do
32
+ assert_kind_of(Pathname, Pathname.new(@abs_path))
33
+ end
34
+
35
+ test "constructor handles frozen arguments without issue" do
36
+ assert_nothing_raised{ Pathname.new(@abs_path.freeze) }
37
+ end
38
+
39
+ test "constructor raises an error if string argument is too long" do
40
+ assert_raise(ArgumentError){ Pathname.new("foo" * 1000) }
41
+ end
42
+
43
+ def teardown
44
+ @url_path = nil
45
+ @rel_path = nil
46
+ @abs_path = nil
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ ########################################################################
2
+ # test_descend.rb
3
+ #
4
+ # Test suite for the Pathname#descend method.
5
+ ########################################################################
6
+ require 'pathname2'
7
+ require 'test-unit'
8
+
9
+ class TC_Pathname_Descend < Test::Unit::TestCase
10
+ def setup
11
+ @path = Pathname.new("C:\\foo\\bar\\baz")
12
+ end
13
+
14
+ test "descend basic functionality" do
15
+ assert_respond_to(@path, :descend)
16
+ assert_nothing_raised{ @path.descend{} }
17
+ end
18
+
19
+ test "descend works as expected on a standard absolute path" do
20
+ array = []
21
+ @path.descend{ |e| array << e }
22
+ assert_equal('C:', array[0])
23
+ assert_equal('C:\foo', array[1])
24
+ assert_equal('C:\foo\bar', array[2])
25
+ assert_equal('C:\foo\bar\baz', array[3])
26
+ end
27
+
28
+ test "descend works as expected on a UNC path" do
29
+ array = []
30
+ Pathname.new('//foo/bar/baz').descend{ |e| array << e }
31
+ assert_equal("\\\\foo\\bar", array[0])
32
+ assert_equal("\\\\foo\\bar\\baz", array[1])
33
+ end
34
+
35
+ test "descend works as expected on a relative path" do
36
+ array = []
37
+ Pathname.new('foo/bar/baz').descend{ |e| array << e }
38
+ assert_equal('foo', array[0])
39
+ assert_equal('foo\bar', array[1])
40
+ assert_equal('foo\bar\baz', array[2])
41
+ end
42
+
43
+ test "descend does not modify the receiver" do
44
+ @path.descend{}
45
+ assert_equal('C:\foo\bar\baz', @path)
46
+ end
47
+
48
+ def teardown
49
+ @path = nil
50
+ end
51
+ end
@@ -0,0 +1,58 @@
1
+ ########################################################################
2
+ # test_drive_number.rb
3
+ #
4
+ # Test suite for the Pathname#drive_number method
5
+ ########################################################################
6
+ require 'test-unit'
7
+ require 'pathname2'
8
+
9
+ class TC_Pathname_DriveNumber < Test::Unit::TestCase
10
+ def setup
11
+ @abs_path = Pathname.new("C:\\Program Files")
12
+ @unc_path = Pathname.new("\\\\foo\\bar\\baz")
13
+ @rel_path = Pathname.new("foo\\bar\\baz")
14
+ end
15
+
16
+ test "drive_number method returns expected results for absolute paths" do
17
+ assert_equal(2, @abs_path.drive_number)
18
+ end
19
+
20
+ test "drive_number method returns expected results for paths with forward slashes" do
21
+ assert_equal(2, Pathname.new("C:/Program Files").drive_number)
22
+ end
23
+
24
+ test "drive_number method returns expected results for unc paths" do
25
+ assert_nil(@unc_path.drive_number)
26
+ assert_nil(Pathname.new("\\\\foo").drive_number)
27
+ assert_nil(Pathname.new("\\\\").drive_number)
28
+ end
29
+
30
+ test "drive_number method returns dot for relative paths" do
31
+ assert_nil(@rel_path.drive_number)
32
+ end
33
+
34
+ test "drive_number method returns expected result for root path" do
35
+ assert_equal(25, Pathname.new("Z:\\").drive_number)
36
+ end
37
+
38
+ test "drive_number method returns expected result for empty string" do
39
+ assert_nil(Pathname.new("").drive_number)
40
+ end
41
+
42
+ test "drive_number method returns expected result for dot and dotdot" do
43
+ assert_nil(Pathname.new(".").drive_number)
44
+ assert_nil(Pathname.new("..").drive_number)
45
+ end
46
+
47
+ test "drive_number method is not destructive" do
48
+ str = 'C:/Program Files'
49
+ assert_nothing_raised{ Pathname.new(str).drive_number }
50
+ assert_equal('C:/Program Files', str)
51
+ end
52
+
53
+ def teardown
54
+ @abs_path = nil
55
+ @unc_path = nil
56
+ @rel_path = nil
57
+ end
58
+ end