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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
--
-- Tests for the system module
--
local path = require 'path'
local tasty = require 'tasty'
local group = tasty.test_group
local test = tasty.test_case
local assert = tasty.assert
-- Check existence static fields
return {
group 'static fields' {
test('separator', function ()
assert.are_equal(type(path.separator), 'string')
end),
test('search_path_separator', function ()
assert.are_equal(type(path.search_path_separator), 'string')
end),
},
group 'directory' {
test('directory from file path', function ()
print(path.join{'/', '2020', 'img', 'mask.jpg'})
assert.are_equal(
path.directory(path.join{'/', '2020', 'img', 'mask.jpg'}),
path.join{'/', '2020', 'img'}
)
end),
test('drops trailing path sep', function ()
assert.are_equal(
path.directory(path.join{'this', 'that'} .. path.separator),
path.join{'this', 'that'}
)
end),
test('returns dot if given just a filename', function ()
assert.are_equal(
path.directory('index.html'),
'.'
)
end)
},
group 'filename' {
test('removes directories', function ()
assert.are_equal(
path.filename(path.join{'paper', 'refs.bib'}),
'refs.bib'
)
assert.are_equal(
path.filename(path.join{'www', '.htaccess'}),
'.htaccess'
)
end),
test('empty if no filename present', function ()
assert.are_equal(
path.filename(path.join{'usr', 'bin'} .. path.separator),
''
)
end)
},
group 'is_absolute' {
test('network path is absolute', function ()
local paths = {'c:', 'Applications'}
assert.is_truthy(path.is_absolute '//foo/bar')
end),
test('pure filename is not absolute', function ()
assert.is_falsy(path.is_absolute '1337.txt')
end),
},
group 'is_relative' {
test('joined paths are relative', function ()
local paths = {'one', 'two', 'test'}
assert.is_truthy(path.is_relative(path.join(paths)))
end),
test('pure filename is relative', function ()
assert.is_truthy(path.is_relative '1337.txt')
end),
test('network path is not relative', function ()
assert.is_falsy(path.is_relative '//foo/bar')
end),
},
group 'splitting/joining' {
test('split_path is inverse of join', function ()
local paths = {'one', 'two', 'test'}
assert.are_same(path.split(path.join(paths)), paths)
end),
},
group 'split_extensions' {
test('filename', function ()
assert.are_equal(path.split_extension 'image.jpg', 'image')
end),
test('extension', function ()
assert.is_truthy(select(2, path.split_extension 'thesis.tex'), '.tex')
assert.are_equal(select(2, path.split_extension 'fstab'), '')
end),
test('concat gives inverts split', function ()
local filenames = {'/etc/passwd', '34a90-1.bat', 'backup.tar.gz'}
for _, filename in ipairs(filenames) do
local base, ext = path.split_extension(filename)
assert.are_equal(base .. ext, filename)
end
end),
},
group 'normalize' {
test('removes leading `./`', function ()
assert.are_equal(path.normalize('./a.md'), 'a.md')
end),
test('dedupe path separators', function ()
assert.are_equal(path.normalize('a//b'), path.join{'a', 'b'})
end)
},
group 'relative or absolute' {
test('xor', function ()
local test_paths = {
path.join{ 'hello', 'rudi'},
path.join{ '.', 'autoexec.bat'},
path.join{ 'C:', 'config.sys'},
path.join{ '/', 'etc', 'passwd'}
}
for _, fp in ipairs(test_paths) do
assert.is_truthy(path.is_relative(fp) == not path.is_absolute(fp))
end
end)
},
group 'strings as path objects' {
test('setup', path.treat_strings_as_paths),
test('split extension', function ()
local img = 'mandrill.jpg'
assert.are_equal(img:split_extension(), 'mandrill')
end),
test('conbine paths with `/`', function ()
assert.are_equal('a' / 'b', path.join{'a', 'b'})
end),
test('add extension with `+`', function ()
assert.are_equal('a' + 'b', path.join{'a.b'})
end),
},
group 'make_relative' {
group 'safe' {
test('just the filename if file is within path', function()
assert.are_equal(
path.make_relative('/foo/bar/file.txt', '/foo/bar'),
'file.txt'
)
end),
test('no change if name outside of reference dir', function()
assert.are_equal(
path.make_relative('/foo/baz/file.txt', '/foo/bar'),
'/foo/baz/file.txt'
)
end),
test('return dot if both paths are the same', function()
assert.are_equal(
path.make_relative('/one/two/three', '/one/two/three/'),
'.'
)
end),
},
group 'unsafe' {
test('just the filename if file is within path', function()
assert.are_equal(
path.make_relative('/foo/bar/file.txt', '/foo/bar', true),
'file.txt'
)
end),
test('use `..` to reach parent directory', function()
assert.are_equal(
path.make_relative('/foo/baz/file.txt', '/foo/bar', true),
path.join{'..', 'baz', 'file.txt'}
)
end),
test('no change if base differs', function()
if path.separator == '\\' then
-- we're on windows
assert.are_equal(
path.make_relative('c:/foo/baz/file.txt', 'd:/foo/bar', true),
'c:/foo/baz/file.txt'
)
else
assert.are_equal(
path.make_relative('foo/baz/file.txt', '/foo/bar', true),
'foo/baz/file.txt'
)
end
end),
test('long base path ', function()
assert.are_equal(
path.make_relative('a/d.png', 'a/b/c', true),
path.join{'..', '..', 'd.png'}
)
end),
test('return dot if both paths are the same', function()
assert.are_equal(
path.make_relative('/one/two/three', '/one/two/three/', true),
'.'
)
end)
}
},
}
|