test_tree.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. --
  2. -- tests/base/test_tree.lua
  3. -- Automated test suite source code tree handling.
  4. -- Copyright (c) 2009 Jason Perkins and the Premake project
  5. --
  6. T.tree = { }
  7. local suite = T.tree
  8. local tree = premake.tree
  9. --
  10. -- Setup/teardown
  11. --
  12. local tr, nodes
  13. function suite.setup()
  14. tr = tree.new()
  15. nodes = { }
  16. end
  17. local function getresult()
  18. tree.traverse(tr, {
  19. onnode = function(node, depth)
  20. table.insert(nodes, string.rep(">", depth) .. node.name)
  21. end
  22. })
  23. return table.concat(nodes)
  24. end
  25. --
  26. -- Tests for tree.new()
  27. --
  28. function suite.NewReturnsObject()
  29. test.isnotnil(tr)
  30. end
  31. --
  32. -- Tests for tree.add()
  33. --
  34. function suite.CanAddAtRoot()
  35. tree.add(tr, "Root")
  36. test.isequal("Root", getresult())
  37. end
  38. function suite.CanAddAtChild()
  39. tree.add(tr, "Root/Child")
  40. test.isequal("Root>Child", getresult())
  41. end
  42. function suite.CanAddAtGrandchild()
  43. tree.add(tr, "Root/Child/Grandchild")
  44. test.isequal("Root>Child>>Grandchild", getresult())
  45. end
  46. function suite.SkipsLeadingDotDots()
  47. tree.add(tr, "../MyProject/hello")
  48. test.isequal("MyProject>hello", getresult())
  49. end
  50. function suite.SkipsInlineDotDots()
  51. tree.add(tr, "MyProject/../hello")
  52. test.isequal("MyProject>hello", getresult())
  53. end
  54. function suite.AddsNodes_OnDifferentParentLevel()
  55. tree.add(tr, "../Common")
  56. tree.add(tr, "../../Common")
  57. test.isequal(2, #tr.children)
  58. test.isequal("Common", tr.children[1].name)
  59. test.isequal("Common", tr.children[2].name)
  60. test.isequal("../Common", tr.children[1].path)
  61. test.isequal("../../Common", tr.children[2].path)
  62. end
  63. --
  64. -- Tests for tree.getlocalpath()
  65. --
  66. function suite.GetLocalPath_ReturnsPath_OnNoParentPath()
  67. local c = tree.add(tr, "Root/Child")
  68. c.parent.path = nil
  69. test.isequal("Root/Child", tree.getlocalpath(c))
  70. end
  71. function suite.GetLocalPath_ReturnsName_OnParentPathSet()
  72. local c = tree.add(tr, "Root/Child")
  73. test.isequal("Child", tree.getlocalpath(c))
  74. end
  75. --
  76. -- Tests for tree.remove()
  77. --
  78. function suite.Remove_RemovesNodes()
  79. local n1 = tree.add(tr, "1")
  80. local n2 = tree.add(tr, "2")
  81. local n3 = tree.add(tr, "3")
  82. tree.remove(n2)
  83. local r = ""
  84. for _, n in ipairs(tr.children) do r = r .. n.name end
  85. test.isequal("13", r)
  86. end
  87. function suite.Remove_WorksInTraversal()
  88. tree.add(tr, "Root/1")
  89. tree.add(tr, "Root/2")
  90. tree.add(tr, "Root/3")
  91. local r = ""
  92. tree.traverse(tr, {
  93. onleaf = function(node)
  94. r = r .. node.name
  95. tree.remove(node)
  96. end
  97. })
  98. test.isequal("123", r)
  99. test.isequal(0, #tr.children[1])
  100. end
  101. --
  102. -- Tests for tree.sort()
  103. --
  104. function suite.Sort_SortsAllLevels()
  105. tree.add(tr, "B/3")
  106. tree.add(tr, "B/1")
  107. tree.add(tr, "A/2")
  108. tree.add(tr, "A/1")
  109. tree.add(tr, "B/2")
  110. tree.sort(tr)
  111. test.isequal("A>1>2B>1>2>3", getresult(tr))
  112. end