-
Notifications
You must be signed in to change notification settings - Fork 140
/
test_path.lua.rename
193 lines (168 loc) · 6.08 KB
/
test_path.lua.rename
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
local zmod = require('z')
local windows = os.path.sep == '\\'
-----------------------------------------------------------------------
-- logo
-----------------------------------------------------------------------
function print_title(text)
print(string.rep('-', 72))
print('-- '.. text)
print(string.rep('-', 72))
end
-----------------------------------------------------------------------
-- os.path.normpath
-----------------------------------------------------------------------
print_title('os.path.normpath')
function assert_posix(path, result)
local x = os.path.normpath(path)
print('[test] normpath: ('..path..') -> (' .. result .. ')')
if x:gsub('\\', '/') ~= result then
print('failed: "' .. x .. '" != "'..result.. '"')
os.exit()
else
print('passed')
print()
end
end
function assert_windows(path, result)
local x = os.path.normpath(path)
print('[test] normpath: ('..path..') -> (' .. result .. ')')
if x ~= result then
print('failed: "' .. x .. '" != "'..result.. '"')
os.exit()
else
print('passed')
print()
end
end
assert_posix("", ".")
assert_posix("/", "/")
assert_posix("///", "/")
assert_posix("///foo/.//bar//", "/foo/bar")
assert_posix("///foo/.//bar//.//..//.//baz", "/foo/baz")
assert_posix("///..//./foo/.//bar", "/foo/bar")
if windows then
assert_windows('A//////././//.//B', 'A\\B')
assert_windows('A/./B', 'A\\B')
assert_windows('A/foo/../B', 'A\\B')
assert_windows('C:A//B', 'C:A\\B')
assert_windows('D:A/./B', 'D:A\\B')
assert_windows('e:A/foo/../B', 'e:A\\B')
assert_windows('C:///A//B', 'C:\\A\\B')
assert_windows('D:///A/./B', 'D:\\A\\B')
assert_windows('e:///A/foo/../B', 'e:\\A\\B')
assert_windows('..', '..')
assert_windows('.', '.')
assert_windows('', '.')
assert_windows('/', '\\')
assert_windows('c:/', 'c:\\')
assert_windows('/../.././..', '\\')
assert_windows('c:/../../..', 'c:\\')
assert_windows('../.././..', '..\\..\\..')
assert_windows('K:../.././..', 'K:..\\..\\..')
assert_windows('C:////a/b', 'C:\\a\\b')
end
print()
-----------------------------------------------------------------------
-- os.path.join
-----------------------------------------------------------------------
print_title('os.path.join')
function assert_join_posix(segments, result, isnt)
print('[test] join: '..zmod.dump(segments)..' -> (' .. result .. ')')
local path = ''
for _, item in ipairs(segments) do
path = os.path.join(path, item)
end
if windows and (not isnt) then
path = path:gsub('\\', '/')
end
if path ~= result then
print('failed: "' .. path .. '"')
os.exit()
else
print('passed')
end
end
function assert_join_windows(segments, result)
assert_join_posix(segments, result, 1)
end
assert_join_posix({"/foo", "bar", "/bar", "baz"}, "/bar/baz")
assert_join_posix({"/foo", "bar", "baz"}, "/foo/bar/baz")
assert_join_posix({"/foo/", "bar/", "baz/"}, "/foo/bar/baz/")
if windows then
assert_join_windows({""}, '')
assert_join_windows({"", "", ""}, '')
assert_join_windows({"a"}, 'a')
assert_join_windows({"/a"}, '/a')
assert_join_windows({"\\a"}, '\\a')
assert_join_windows({"a:"}, 'a:')
assert_join_windows({"a:", "\\b"}, 'a:\\b')
assert_join_windows({"a", "\\b"}, '\\b')
assert_join_windows({"a", "b", "c"}, 'a\\b\\c')
assert_join_windows({"a\\", "b", "c"}, 'a\\b\\c')
assert_join_windows({"a", "b\\", "c"}, 'a\\b\\c')
assert_join_windows({"a", "b", "\\c"}, '\\c')
assert_join_windows({"d:\\", "\\pleep"}, 'd:\\pleep')
assert_join_windows({"d:\\", "a", "b"}, 'd:\\a\\b')
assert_join_windows({'', 'a'}, 'a')
assert_join_windows({'', '', '', '', 'a'}, 'a')
assert_join_windows({'a', ''}, 'a\\')
assert_join_windows({'a', '', '', '', ''}, 'a\\')
assert_join_windows({'a\\', ''}, 'a\\')
assert_join_windows({'a\\', '', '', '', ''}, 'a\\')
assert_join_windows({'a/', ''}, 'a/')
assert_join_windows({'a/b', 'x/y'}, 'a/b\\x/y')
assert_join_windows({'/a/b', 'x/y'}, '/a/b\\x/y')
assert_join_windows({'/a/b/', 'x/y'}, '/a/b/x/y')
assert_join_windows({'c:', 'x/y'}, 'c:x/y')
assert_join_windows({'c:a/b', 'x/y'}, 'c:a/b\\x/y')
assert_join_windows({'c:a/b/', 'x/y'}, 'c:a/b/x/y')
assert_join_windows({'c:/', 'x/y'}, 'c:/x/y')
assert_join_windows({'c:/a/b', 'x/y'}, 'c:/a/b\\x/y')
assert_join_windows({'c:/a/b/', 'x/y'}, 'c:/a/b/x/y')
assert_join_windows({'a/b', '/x/y'}, '/x/y')
assert_join_windows({'/a/b', '/x/y'}, '/x/y')
assert_join_windows({'c:', '/x/y'}, 'c:/x/y')
assert_join_windows({'c:a/b', '/x/y'}, 'c:/x/y')
assert_join_windows({'c:/', '/x/y'}, 'c:/x/y')
assert_join_windows({'c:/a/b', '/x/y'}, 'c:/x/y')
assert_join_windows({'c:', 'C:x/y'}, 'C:x/y')
assert_join_windows({'c:a/b', 'C:x/y'}, 'C:a/b\\x/y')
assert_join_windows({'c:/', 'C:x/y'}, 'C:/x/y')
assert_join_windows({'c:/a/b', 'C:x/y'}, 'C:/a/b\\x/y')
for _, x in ipairs({'', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b'}) do
for _, y in ipairs({'d:', 'd:x/y', 'd:/', 'd:/x/y'}) do
assert_join_windows({x, y}, y)
end
end
end
print()
-----------------------------------------------------------------------
-- os.path.split
-----------------------------------------------------------------------
print_title('os.path.split')
function assert_split(path, sep1, sep2)
print('[test] split: "' .. path ..'" -> ("' .. sep1 .. '", "' .. sep2 .. '")')
local x, y = os.path.split(path)
if x ~= sep1 or y ~= sep2 then
print('failed: ("'..x..'", "'..y..'")')
os.exit()
else
print('passed')
end
end
assert_split("", "", "")
assert_split(".", "", ".")
assert_split("/foo/bar", "/foo", "bar")
assert_split("/", "/", "")
assert_split("foo", "", "foo")
assert_split("////foo", "////", "foo")
assert_split("//foo//bar", "//foo", "bar")
if windows then
assert_split("c:\\foo\\bar", 'c:\\foo', 'bar')
assert_split("\\\\conky\\mountpoint\\foo\\bar", '\\\\conky\\mountpoint\\foo', 'bar')
assert_split("c:\\", "c:\\", '')
assert_split("c:/", "c:/", '')
assert_split("c:test", "c:", 'test')
assert_split("c:", "c:", '')
-- assert_split("\\\\conky\\mountpoint\\", "\\\\conky\\mountpoint\\", '')
end