tomo/test/paths.tm

131 lines
2.8 KiB
Plaintext
Raw Normal View History

2024-09-08 23:43:15 -07:00
# Tests for file paths
func main():
>> (/):exists()
= yes
>> (~/):exists()
= yes
2024-09-09 12:03:51 -07:00
>> (~/Downloads/file(1).txt)
= (~/Downloads/file(1).txt)
2024-09-09 12:03:51 -07:00
>> (/half\)paren)
= (/half\)paren)
2024-09-09 12:03:51 -07:00
2024-09-09 11:37:13 -07:00
>> filename := "example.txt"
>> (~):child(filename)
= (~/example.txt)
2024-09-09 11:37:13 -07:00
2024-09-08 23:43:15 -07:00
>> 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!"?
2025-04-04 22:08:12 -07:00
>> tmpfile:read_bytes()!
= [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21]
2024-09-08 23:43:15 -07:00
>> tmpdir:files():has(tmpfile)
= yes
2024-09-15 12:53:38 -07:00
if tmp_lines := tmpfile:by_line():
>> [line for line in tmp_lines]
= ["Hello world!"]
else:
fail("Couldn't read lines in $tmpfile")
>> (./does-not-exist.xxx):read()
2025-04-04 22:07:09 -07:00
= none
2024-09-15 12:53:38 -07:00
>> (./does-not-exist.xxx):read_bytes()
2025-04-04 22:07:09 -07:00
= none
2024-09-15 12:53:38 -07:00
if lines := (./does-not-exist.xxx):by_line():
fail("I could read lines in a nonexistent file")
else:
pass
2024-09-08 23:43:15 -07:00
>> tmpfile:remove()
>> tmpdir:files():has(tmpfile)
= no
>> tmpdir:remove()
2024-09-09 00:52:56 -07:00
>> p := (/foo/baz.x/qux.tar.gz)
>> p:base_name()
= "qux.tar.gz"
>> p:parent()
= (/foo/baz.x)
2024-09-09 00:52:56 -07:00
>> p:extension()
= "tar.gz"
>> p:extension(full=no)
= "gz"
>> (~/.foo):extension()
= ""
>> (~/foo):extension()
= ""
2024-09-09 01:49:02 -07:00
>> (~/.foo.baz.qux):extension()
= "baz.qux"
>> (/):parent()
= (/)
2024-09-09 01:49:02 -07:00
>> (~/x/.):parent()
= (~)
2024-09-09 01:49:02 -07:00
>> (~/x):parent()
= (~)
>> (.):parent()
= (..)
>> (..):parent()
= (../..)
2024-09-09 01:49:02 -07:00
>> (../foo):parent()
= (..)
# Concatenation tests:
2025-04-06 10:40:17 -07:00
say("Basic relative path concatenation:")
>> (/foo) ++ (./baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Concatenation with a current directory (`.`):")
>> (/foo/bar) ++ (./.)
= (/foo/bar)
2025-04-06 10:40:17 -07:00
say("Trailing slash in the first path:")
>> (/foo/) ++ (./baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Trailing slash in the second path:")
>> (/foo/bar) ++ (./baz/)
= (/foo/bar/baz)
2025-04-06 10:40:17 -07:00
say("Removing redundant current directory (`.`):")
>> (/foo/bar) ++ (./baz/./qux)
= (/foo/bar/baz/qux)
2025-04-06 10:40:17 -07:00
say("Removing redundant parent directory (`..`):")
>> (/foo/bar) ++ (./baz/qux/../quux)
= (/foo/bar/baz/quux)
2025-04-06 10:40:17 -07:00
say("Collapsing `..` to navigate up:")
>> (/foo/bar/baz) ++ (../qux)
= (/foo/bar/qux)
2025-04-06 10:40:17 -07:00
say("Current directory and parent directory mixed:")
>> (/foo/bar) ++ (././../baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Path begins with a `.`:")
>> (/foo) ++ (./baz/../qux)
= (/foo/qux)
2025-04-06 10:40:17 -07:00
say("Multiple slashes:")
>> (/foo) ++ (./baz//qux)
= (/foo/baz/qux)
2025-04-06 10:40:17 -07:00
say("Complex path with multiple `.` and `..`:")
>> (/foo/bar/baz) ++ (./.././qux/./../quux)
= (/foo/bar/quux)
2025-04-06 10:40:17 -07:00
say("Globbing:")
2024-10-29 11:36:49 -07:00
>> (./*.tm):glob()