code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(103 lines)
1 # Tests for file paths
2 func main()
3 assert (/).exists()
4 assert (~/).exists()
6 assert (~/Downloads/file(1).txt) == ~/Downloads/file(1).txt
7 assert (/half\)paren) == /half\)paren
9 >> filename := "example.txt"
10 assert (~).child(filename) == ~/example.txt
12 >> tmpdir := (/tmp/tomo-test-path-XXXXXX).unique_directory()
13 assert (/tmp).subdirectories().has(tmpdir)
15 >> optional_path : Path? = ./foo
16 assert optional_path == ./foo
18 >> tmpfile := tmpdir ++ ./one.txt
19 >> tmpfile.write("Hello world")!
20 >> tmpfile.append("!")!
21 assert tmpfile.read() == "Hello world!"
22 assert tmpfile.read_bytes()! == [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21]
23 assert tmpdir.files().has(tmpfile)
25 if tmp_lines := tmpfile.by_line() then
26 assert [line for line in tmp_lines] == ["Hello world!"]
27 else
28 fail("Couldn't read lines in $tmpfile")
30 assert (./does-not-exist.xxx).read() == none
31 assert (./does-not-exist.xxx).read_bytes() == none
32 if (./does-not-exist.xxx).by_line()
33 fail("I could read lines in a nonexistent file")
34 else
35 pass
37 >> tmpfile.remove()!
39 assert tmpdir.files().has(tmpfile) == no
41 >> tmpdir.remove()!
43 >> p := /foo/baz.x/qux.tar.gz
44 assert p.base_name() == "qux.tar.gz"
45 assert p.parent() == /foo/baz.x
46 assert p.extension() == "tar.gz"
47 assert p.extension(full=no) == "gz"
48 assert p.has_extension("gz") == yes
49 assert p.has_extension(".gz") == yes
50 assert p.has_extension("tar.gz") == yes
51 assert p.has_extension("txt") == no
52 assert p.has_extension("") == no
53 assert (./foo).has_extension("") == yes
54 assert (..).has_extension("") == yes
55 assert (~/.foo).has_extension("foo") == no
56 assert (~/.foo).extension() == ""
57 assert (~/foo).extension() == ""
59 assert (~/.foo.baz.qux).extension() == "baz.qux"
61 assert (~/x/.).parent() == (~)
62 assert (~/x).parent() == (~)
63 assert (.).parent() == (..)
64 assert (..).parent() == (../..)
65 assert (../foo).parent() == (..)
66 assert (/).parent() == none
68 # Concatenation tests:
69 say("Basic relative path concatenation:")
70 assert /foo ++ ./baz == /foo/baz
72 say("Concatenation with a current directory (`.`):")
73 assert /foo/bar ++ . == /foo/bar
75 say("Trailing slash in the first path:")
76 assert /foo/ ++ ./baz == /foo/baz
78 say("Trailing slash in the second path:")
79 assert /foo/bar ++ ./baz/ == /foo/bar/baz
81 say("Removing redundant current directory (`.`):")
82 assert /foo/bar ++ ./baz/./qux == /foo/bar/baz/qux
84 say("Removing redundant parent directory (`..`):")
85 assert /foo/bar ++ ./baz/qux/../quux == /foo/bar/baz/quux
87 say("Collapsing `..` to navigate up:")
88 assert /foo/bar/baz ++ ../qux == /foo/bar/qux
90 say("Current directory and parent directory mixed:")
91 assert /foo/bar ++ ././../baz == /foo/baz
93 say("Path begins with a `.`:")
94 assert /foo ++ ./baz/../qux == /foo/qux
96 say("Multiple slashes:")
97 assert /foo ++ ./baz//qux == /foo/baz/qux
99 say("Complex path with multiple `.` and `..`:")
100 assert /foo/bar/baz ++ ./.././qux/./../quux == /foo/bar/quux
102 say("Globbing:")
103 >> (./*.tm).glob()