aboutsummaryrefslogtreecommitdiff
path: root/test/paths.tm
blob: 84321babae3acc5042cec85f1dd242d35de3caa1 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Tests for file paths
func main():
    >> (/):exists()
    = yes
    >> (~/):exists()
    = yes

    >> tmpdir := (/tmp/tomo-test-path-XXXXXX):unique_directory()
    >> (/tmp):subdirectories():has(tmpdir)
    = yes

    >> tmpfile := (tmpdir++(./one.txt))
    >> tmpfile:write("Hello world")
    >> tmpfile:append("!")
    >> tmpfile:read()
    = "Hello world!"
    >> tmpdir:files():has(tmpfile)
    = yes

    >> tmpfile:remove()

    >> tmpdir:files():has(tmpfile)
    = no

    >> tmpdir:remove()

    >> p := (/foo/baz.x/qux.tar.gz)
    >> p:base_name()
    = "qux.tar.gz"
    >> p:parent()
    = (/foo/baz.x/)
    >> p:extension()
    = "tar.gz"
    >> p:extension(full=no)
    = "gz"
    >> (~/.foo):extension()
    = ""
    >> (~/foo):extension()
    = ""

    >> (~/.foo.baz.qux):extension()
    = "baz.qux"

    >> (/):parent()
    = (/)
    >> (~/x/.):parent()
    = (~/)
    >> (~/x):parent()
    = (~/)
    >> (./):parent()
    = (../)
    >> (../):parent()
    = (../../)
    >> (../foo):parent()
    = (../)


    # Concatenation tests:
    !! Basic relative path concatenation:
    >> (/foo) ++ (./baz)
    = (/foo/baz)

    !! Concatenation with a current directory (`.`):
    >> (/foo/bar) ++ (./.)
    = (/foo/bar)

    !! Trailing slash in the first path:
    >> (/foo/) ++ (./baz)
    = (/foo/baz)

    !! Trailing slash in the second path:
    >> (/foo/bar) ++ (./baz/)
    = (/foo/bar/baz/)

    !! Removing redundant current directory (`.`):
    >> (/foo/bar) ++ (./baz/./qux)
    = (/foo/bar/baz/qux)

    !! Removing redundant parent directory (`..`):
    >> (/foo/bar) ++ (./baz/qux/../quux)
    = (/foo/bar/baz/quux)

    !! Collapsing `..` to navigate up:
    >> (/foo/bar/baz) ++ (../qux)
    = (/foo/bar/qux)

    !! Current directory and parent directory mixed:
    >> (/foo/bar) ++ (././../baz)
    = (/foo/baz)

    !! Path begins with a `.`:
    >> (/foo) ++ (./baz/../qux)
    = (/foo/qux)

    !! Multiple slashes:
    >> (/foo) ++ (./baz//qux)
    = (/foo/baz/qux)

    !! Complex path with multiple `.` and `..`:
    >> (/foo/bar/baz) ++ (./.././qux/./../quux)
    = (/foo/bar/quux)