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
|
Path = {}
function Path:new(p)
local path = { segments = p:split('/') }
setmetatable(path, self)
self.__index = self
return path
end
function Path:name()
return self.segments[#self.segments]
end
function Path:stem()
return self.segments[#self.segments]:match('[^.]+')
end
function Path:parent()
local path = { segments = { table.unpack(self.segments, 1, #self.segments - 1) } }
setmetatable(path, self)
self.__index = self
return path
end
function Path:__tostring()
return table.concat(self.segments, "/")
end
function Path:__div(next)
return Path:new(self:__tostring() .. "/" .. next)
end
return {
tests = {
function()
local path = Path:new("some/path/here")
assert.equals("here", path:name())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("here", path:stem())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("some/path", path:parent():__tostring())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("some/path/here.txt", path:__tostring())
end,
function()
local path = Path:new("some/path/here")
path = path / 'next.txt'
assert.equals("some/path/here/next.txt", path:__tostring())
end
}
}
|