aboutsummaryrefslogtreecommitdiff
path: root/test/paths.tm
blob: d7630fc39a8dabb3034522c011b0fe9be14413ae (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
103
104
105
106
107
108
109
110
# Tests for file paths
func main()
    assert (/).exists()
    assert (~/).exists()

    assert (~/Downloads/file(1).txt) == (~/Downloads/file(1).txt)

    assert (/half\)paren) == (/half\)paren)

    >> filename := "example.txt"
    assert (~).child(filename) == (~/example.txt)

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

    >> optional_path : Path? = (./foo)
    assert optional_path == (./foo)

    >> tmpfile := (tmpdir++(./one.txt))
    >> tmpfile.write("Hello world")
    >> tmpfile.append("!")
    assert tmpfile.read() == "Hello world!"
    assert tmpfile.read_bytes()! == [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21]
    assert tmpdir.files().has(tmpfile)

    if tmp_lines := tmpfile.by_line() then
        assert [line for line in tmp_lines] == ["Hello world!"]
    else
        fail("Couldn't read lines in $tmpfile")

    assert (./does-not-exist.xxx).read() == none
    assert (./does-not-exist.xxx).read_bytes() == none
    if lines := (./does-not-exist.xxx).by_line() then
        fail("I could read lines in a nonexistent file")
    else
        pass

    >> tmpfile.remove()

    assert tmpdir.files().has(tmpfile) == no

    >> tmpdir.remove()

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

    assert (~/.foo.baz.qux).extension() == "baz.qux"

    assert (/).parent() == (/)
    assert (~/x/.).parent() == (~)
    assert (~/x).parent() == (~)
    assert (.).parent() == (..)
    assert (..).parent() == (../..)
    assert (../foo).parent() == (..)

    # Concatenation tests:
    say("Basic relative path concatenation:")
    assert (/foo) ++ (./baz) == (/foo/baz)

    say("Concatenation with a current directory (`.`):")
    assert (/foo/bar) ++ (./.) == (/foo/bar)

    say("Trailing slash in the first path:")
    assert (/foo/) ++ (./baz) == (/foo/baz)

    say("Trailing slash in the second path:")
    assert (/foo/bar) ++ (./baz/) == (/foo/bar/baz)

    say("Removing redundant current directory (`.`):")
    assert (/foo/bar) ++ (./baz/./qux) == (/foo/bar/baz/qux)

    say("Removing redundant parent directory (`..`):")
    assert (/foo/bar) ++ (./baz/qux/../quux) == (/foo/bar/baz/quux)

    say("Collapsing `..` to navigate up:")
    assert (/foo/bar/baz) ++ (../qux) == (/foo/bar/qux)

    say("Current directory and parent directory mixed:")
    assert (/foo/bar) ++ (././../baz) == (/foo/baz)

    say("Path begins with a `.`:")
    assert (/foo) ++ (./baz/../qux) == (/foo/qux)

    say("Multiple slashes:")
    assert (/foo) ++ (./baz//qux) == (/foo/baz/qux)

    say("Complex path with multiple `.` and `..`:")
    assert (/foo/bar/baz) ++ (./.././qux/./../quux) == (/foo/bar/quux)

    say("Globbing:")
    >> (./*.tm).glob()

    assert (./foo).RelativePath
    assert (/foo).AbsolutePath
    assert (~/foo).HomePath
    assert (/foo/baz).components() == ["foo", "baz"]
    assert Path.RelativePath(["foo", "baz"]) == (./foo/baz)