premake/tests/base/test_tree.lua

287 lines
4.5 KiB
Lua
Raw Normal View History

2009-08-18 19:09:17 +00:00
--
-- tests/base/test_tree.lua
-- Automated test suite source code tree handling.
-- Copyright (c) 2009-2012 Jason Perkins and the Premake project
2009-08-18 19:09:17 +00:00
--
T.tree = { }
2010-01-05 19:27:34 +00:00
local suite = T.tree
2009-08-18 19:09:17 +00:00
local tree = premake.tree
--
-- Setup/teardown
--
local tr
2010-01-05 19:27:34 +00:00
function suite.setup()
2009-08-18 19:09:17 +00:00
tr = tree.new()
end
local function prepare()
2009-08-18 19:09:17 +00:00
tree.traverse(tr, {
onnode = function(node, depth)
_p(depth + 2, node.name)
2009-08-18 19:09:17 +00:00
end
})
end
--
-- Tests for tree.new()
--
2010-01-05 19:27:34 +00:00
function suite.NewReturnsObject()
2009-08-18 19:09:17 +00:00
test.isnotnil(tr)
end
--
-- Tests for tree.add()
--
2010-01-05 19:27:34 +00:00
function suite.CanAddAtRoot()
2009-08-18 19:09:17 +00:00
tree.add(tr, "Root")
prepare()
test.capture [[
Root
]]
2009-08-18 19:09:17 +00:00
end
2010-01-05 19:27:34 +00:00
function suite.CanAddAtChild()
2009-08-18 19:09:17 +00:00
tree.add(tr, "Root/Child")
prepare()
test.capture [[
Root
Child
]]
2009-08-18 19:09:17 +00:00
end
2010-01-05 19:27:34 +00:00
function suite.CanAddAtGrandchild()
2009-08-18 19:09:17 +00:00
tree.add(tr, "Root/Child/Grandchild")
prepare()
test.capture [[
Root
Child
Grandchild
]]
end
2009-09-06 18:00:09 +00:00
2009-08-24 21:42:14 +00:00
--
-- Tests for tree.getlocalpath()
--
2010-01-05 19:27:34 +00:00
function suite.GetLocalPath_ReturnsPath_OnNoParentPath()
2009-08-24 21:42:14 +00:00
local c = tree.add(tr, "Root/Child")
c.parent.path = nil
test.isequal("Root/Child", tree.getlocalpath(c))
end
2010-01-05 19:27:34 +00:00
function suite.GetLocalPath_ReturnsName_OnParentPathSet()
2009-08-24 21:42:14 +00:00
local c = tree.add(tr, "Root/Child")
test.isequal("Child", tree.getlocalpath(c))
end
2009-09-02 22:18:49 +00:00
--
-- Tests for tree.remove()
--
2010-01-05 19:27:34 +00:00
function suite.Remove_RemovesNodes()
2009-09-02 22:18:49 +00:00
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
2010-01-05 19:27:34 +00:00
function suite.Remove_WorksInTraversal()
2009-09-02 22:18:49 +00:00
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
2010-01-05 19:27:34 +00:00
--
-- 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)
prepare()
test.capture [[
A
1
2
B
1
2
3
]]
end
--
-- If the root of the tree contains multiple items, it should not
-- be removed by trimroot()
--
function suite.trimroot_onItemsAtRoot()
tree.add(tr, "A/1")
tree.add(tr, "B/1")
tree.trimroot(tr)
prepare()
test.capture [[
A
1
B
1
]]
end
--
-- Should trim to first level with multiple items.
--
function suite.trimroot_onItemsInFirstNode()
tree.add(tr, "A/1")
tree.add(tr, "A/2")
tree.trimroot(tr)
prepare()
test.capture [[
1
2
]]
2010-01-05 19:27:34 +00:00
end
--
-- If the tree contains only a single node, don't trim it.
--
function suite.trimroot_onSingleNode()
tree.add(tr, "A")
tree.trimroot(tr)
prepare()
test.capture [[
A
]]
end
--
-- If the tree contains only a single node, don't trim it.
--
function suite.trimroot_onSingleLeafNode()
tree.add(tr, "A/1")
tree.trimroot(tr)
prepare()
test.capture [[
1
]]
end
--
-- A ".." folder containing a single subfolder should never appear
-- at the top of the source tree.
--
function suite.trimroot_removesDotDot_onTopLevelSiblings()
tree.add(tr, "../../tests/test_hello.c")
tree.add(tr, "../src/test.c")
tree.trimroot(tr)
prepare()
test.capture [[
tests
test_hello.c
src
test.c
]]
end
function suite.trimroot_removesDotDot_onTopLevel()
tree.add(tr, "../tests/test_hello.c")
tree.add(tr, "src/test.c")
tree.trimroot(tr)
prepare()
test.capture [[
tests
test_hello.c
src
test.c
]]
end
function suite.trimroot_removesDotDot_onMultipleNestings()
tree.add(tr, "../../../tests/test_hello.c")
tree.add(tr, "../src/test.c")
tree.trimroot(tr)
prepare()
test.capture [[
tests
test_hello.c
src
test.c
]]
end
--
-- When nodes are trimmed, the paths on the remaining nodes should
-- be updated to reflect the new hierarchy.
--
function suite.trimroot_updatesPaths_onNodesRemoved()
tree.add(tr, "A/1")
tree.add(tr, "A/2")
tree.trimroot(tr)
test.isequal("1", tr.children[1].path)
end
function suite.trimroot_updatesPaths_onDotDotRemoved()
tree.add(tr, "../../../tests/test_hello.c")
tree.add(tr, "../src/test.c")
tree.trimroot(tr)
test.isequal("tests", tr.children[1].path)
end
--
-- Nodes with the key "trim" set to false should be removed.
--
function suite.trimroot_respectsTrimFlag()
local n = tree.add(tr, "A")
tree.add(tr, "A/1")
n.trim = false
tree.trimroot(tr)
prepare()
test.capture [[
A
1
]]
end