File: test_relative_path_from.rb

package info (click to toggle)
ruby-pathname2 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 340 kB
  • sloc: ruby: 2,033; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 2,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
########################################################################
# test_relative_path_from.rb
#
# Test suite for the Pathname#relative_path_from method.
########################################################################
require 'test-unit'
require 'pathname2'

class TC_Pathname_RelativePathFrom < Test::Unit::TestCase
  def assert_relpath(result, dest, base)
    assert_equal(result, Pathname.new(dest).relative_path_from(base))
  end

  def assert_relative_path_error(to, from)
    assert_raise(ArgumentError){ Pathname.new(to).relative_path_from(from) }
  end

  test "relative_path_from works as expected between two relative paths" do
    assert_relpath("..\\a", 'a', 'b')
    assert_relpath("..\\a", 'a', 'b/')
    assert_relpath("..\\a", 'a/', 'b')
    assert_relpath("..\\a", 'a/', 'b/')
    assert_relpath("..\\b", "a\\b", "a\\c")
    assert_relpath("..\\a", "..\\a", "..\\b")
    assert_relpath("..\\b\\c", "a\\b\\c", "a\\d")
    assert_relpath("..", "a\\..", "a")
    assert_relpath(".", "a\\..\\b", "b")
    assert_relpath("a", "a", "b\\..")
    assert_relpath("b\\c", "b\\c", "b\\..")
  end

  test "relative_path_from works as expected between two absolute paths" do
    assert_relpath("..\\a", "c:\\a", "c:\\b")
    assert_relpath("..\\a", "c:\\a", "c:\\b\\")
    assert_relpath("..\\a", "c:\\a\\", "c:\\b")
    assert_relpath("..\\a", "c:\\a\\", "c:\\b\\")
    assert_relpath("c\\d", "c:\\a\\b\\c\\d", "c:\\a\\b")
    assert_relpath("..\\..", "c:\\a\\b", "c:\\a\\b\\c\\d")
    assert_relpath("..\\..\\..\\..\\e", "c:\\e", "c:\\a\\b\\c\\d")
    assert_relpath("..\\a", "c:\\..\\a", "c:\\b")
    assert_relpath(".", "c:\\a\\..\\..\\b", "c:\\b")
  end

  test "relative_path_from works as expected between for . and .." do
    assert_relpath("a", "a", ".")
    assert_relpath("..", ".", "a")
    assert_relpath(".", ".", ".")
    assert_relpath(".", "..", "..")
    assert_relpath("..", "..", ".")
  end

  test "relative_path_from is not allowed between relative and absolute paths" do
    assert_relative_path_error("c:\\", ".")
    assert_relative_path_error(".", "c:\\")
    assert_relative_path_error("a", "..")
    assert_relative_path_error(".", "..")
    assert_relative_path_error("C:\\Temp", "D:\\Temp")
    assert_relative_path_error("\\\\Server\\Temp", "D:\\Temp")
  end
end