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
|
--
-- tests/base/test_tree.lua
-- Automated test suite source code tree handling.
-- Copyright (c) 2009 Jason Perkins and the Premake project
--
T.tree = { }
local suite = T.tree
local tree = premake.tree
--
-- Setup/teardown
--
local tr, nodes
function suite.setup()
tr = tree.new()
nodes = { }
end
local function getresult()
tree.traverse(tr, {
onnode = function(node, depth)
table.insert(nodes, string.rep(">", depth) .. node.name)
end
})
return table.concat(nodes)
end
--
-- Tests for tree.new()
--
function suite.NewReturnsObject()
test.isnotnil(tr)
end
--
-- Tests for tree.add()
--
function suite.CanAddAtRoot()
tree.add(tr, "Root")
test.isequal("Root", getresult())
end
function suite.CanAddAtChild()
tree.add(tr, "Root/Child")
test.isequal("Root>Child", getresult())
end
function suite.CanAddAtGrandchild()
tree.add(tr, "Root/Child/Grandchild")
test.isequal("Root>Child>>Grandchild", getresult())
end
function suite.SkipsLeadingDotDots()
tree.add(tr, "../MyProject/hello")
test.isequal("MyProject>hello", getresult())
end
function suite.SkipsInlineDotDots()
tree.add(tr, "MyProject/../hello")
test.isequal("MyProject>hello", getresult())
end
function suite.AddsNodes_OnDifferentParentLevel()
tree.add(tr, "../Common")
tree.add(tr, "../../Common")
test.isequal(2, #tr.children)
test.isequal("Common", tr.children[1].name)
test.isequal("Common", tr.children[2].name)
test.isequal("../Common", tr.children[1].path)
test.isequal("../../Common", tr.children[2].path)
end
--
-- Tests for tree.getlocalpath()
--
function suite.GetLocalPath_ReturnsPath_OnNoParentPath()
local c = tree.add(tr, "Root/Child")
c.parent.path = nil
test.isequal("Root/Child", tree.getlocalpath(c))
end
function suite.GetLocalPath_ReturnsName_OnParentPathSet()
local c = tree.add(tr, "Root/Child")
test.isequal("Child", tree.getlocalpath(c))
end
--
-- Tests for tree.remove()
--
function suite.Remove_RemovesNodes()
local n1 = tree.add(tr, "1")
local n2 = tree.add(tr, "2")
local n3 = tree.add(tr, "3")
tree.remove(n2)
local r = ""
for _, n in ipairs(tr.children) do r = r .. n.name end
test.isequal("13", r)
end
function suite.Remove_WorksInTraversal()
tree.add(tr, "Root/1")
tree.add(tr, "Root/2")
tree.add(tr, "Root/3")
local r = ""
tree.traverse(tr, {
onleaf = function(node)
r = r .. node.name
tree.remove(node)
end
})
test.isequal("123", r)
test.isequal(0, #tr.children[1])
end
--
-- Tests for tree.sort()
--
function suite.Sort_SortsAllLevels()
tree.add(tr, "B/3")
tree.add(tr, "B/1")
tree.add(tr, "A/2")
tree.add(tr, "A/1")
tree.add(tr, "B/2")
tree.sort(tr)
test.isequal("A>1>2B>1>2>3", getresult(tr))
end
|