test_tree.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. --
  2. -- tests/base/test_tree.lua
  3. -- Automated test suite source code tree handling.
  4. -- Copyright (c) 2009-2012 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("base_tree")
  8. local tree = p.tree
  9. --
  10. -- Setup/teardown
  11. --
  12. local tr
  13. function suite.setup()
  14. tr = tree.new()
  15. end
  16. local function prepare()
  17. tree.traverse(tr, {
  18. onnode = function(node, depth)
  19. _p(depth + 2, node.name)
  20. end
  21. })
  22. end
  23. --
  24. -- Tests for tree.new()
  25. --
  26. function suite.NewReturnsObject()
  27. test.isnotnil(tr)
  28. end
  29. --
  30. -- Tests for tree.add()
  31. --
  32. function suite.CanAddAtRoot()
  33. tree.add(tr, "Root")
  34. prepare()
  35. test.capture [[
  36. Root
  37. ]]
  38. end
  39. function suite.CanAddAtChild()
  40. tree.add(tr, "Root/Child")
  41. prepare()
  42. test.capture [[
  43. Root
  44. Child
  45. ]]
  46. end
  47. function suite.CanAddAtGrandchild()
  48. tree.add(tr, "Root/Child/Grandchild")
  49. prepare()
  50. test.capture [[
  51. Root
  52. Child
  53. Grandchild
  54. ]]
  55. end
  56. --
  57. -- Tests for tree.getlocalpath()
  58. --
  59. function suite.GetLocalPath_ReturnsPath_OnNoParentPath()
  60. local c = tree.add(tr, "Root/Child")
  61. c.parent.path = nil
  62. test.isequal("Root/Child", tree.getlocalpath(c))
  63. end
  64. function suite.GetLocalPath_ReturnsName_OnParentPathSet()
  65. local c = tree.add(tr, "Root/Child")
  66. test.isequal("Child", tree.getlocalpath(c))
  67. end
  68. --
  69. -- Tests for tree.remove()
  70. --
  71. function suite.Remove_RemovesNodes()
  72. local n1 = tree.add(tr, "1")
  73. local n2 = tree.add(tr, "2")
  74. local n3 = tree.add(tr, "3")
  75. tree.remove(n2)
  76. local r = ""
  77. for _, n in ipairs(tr.children) do r = r .. n.name end
  78. test.isequal("13", r)
  79. end
  80. function suite.Remove_WorksInTraversal()
  81. tree.add(tr, "Root/1")
  82. tree.add(tr, "Root/2")
  83. tree.add(tr, "Root/3")
  84. local r = ""
  85. tree.traverse(tr, {
  86. onleaf = function(node)
  87. r = r .. node.name
  88. tree.remove(node)
  89. end
  90. })
  91. test.isequal("123", r)
  92. test.isequal(0, #tr.children[1])
  93. end
  94. --
  95. -- Tests for tree.sort()
  96. --
  97. function suite.Sort_SortsAllLevels()
  98. tree.add(tr, "B/3")
  99. tree.add(tr, "B/1")
  100. tree.add(tr, "A/2")
  101. tree.add(tr, "A/1")
  102. tree.add(tr, "B/2")
  103. tree.sort(tr)
  104. prepare()
  105. test.capture [[
  106. A
  107. 1
  108. 2
  109. B
  110. 1
  111. 2
  112. 3
  113. ]]
  114. end
  115. --
  116. -- If the root of the tree contains multiple items, it should not
  117. -- be removed by trimroot()
  118. --
  119. function suite.trimroot_onItemsAtRoot()
  120. tree.add(tr, "A/1")
  121. tree.add(tr, "B/1")
  122. tree.trimroot(tr)
  123. prepare()
  124. test.capture [[
  125. A
  126. 1
  127. B
  128. 1
  129. ]]
  130. end
  131. --
  132. -- Should trim to first level with multiple items.
  133. --
  134. function suite.trimroot_onItemsInFirstNode()
  135. tree.add(tr, "A/1")
  136. tree.add(tr, "A/2")
  137. tree.trimroot(tr)
  138. prepare()
  139. test.capture [[
  140. 1
  141. 2
  142. ]]
  143. end
  144. --
  145. -- If the tree contains only a single node, don't trim it.
  146. --
  147. function suite.trimroot_onSingleNode()
  148. tree.add(tr, "A")
  149. tree.trimroot(tr)
  150. prepare()
  151. test.capture [[
  152. A
  153. ]]
  154. end
  155. --
  156. -- If the tree contains only a single node, don't trim it.
  157. --
  158. function suite.trimroot_onSingleLeafNode()
  159. tree.add(tr, "A/1")
  160. tree.trimroot(tr)
  161. prepare()
  162. test.capture [[
  163. 1
  164. ]]
  165. end
  166. --
  167. -- A ".." folder containing a single subfolder should never appear
  168. -- at the top of the source tree.
  169. --
  170. function suite.trimroot_removesDotDot_onTopLevelSiblings()
  171. tree.add(tr, "../../tests/test_hello.c")
  172. tree.add(tr, "../src/test.c")
  173. tree.trimroot(tr)
  174. prepare()
  175. test.capture [[
  176. tests
  177. test_hello.c
  178. src
  179. test.c
  180. ]]
  181. end
  182. function suite.trimroot_removesDotDot_onTopLevel()
  183. tree.add(tr, "../tests/test_hello.c")
  184. tree.add(tr, "src/test.c")
  185. tree.trimroot(tr)
  186. prepare()
  187. test.capture [[
  188. tests
  189. test_hello.c
  190. src
  191. test.c
  192. ]]
  193. end
  194. function suite.trimroot_removesDotDot_onMultipleNestings()
  195. tree.add(tr, "../../../tests/test_hello.c")
  196. tree.add(tr, "../src/test.c")
  197. tree.trimroot(tr)
  198. prepare()
  199. test.capture [[
  200. tests
  201. test_hello.c
  202. src
  203. test.c
  204. ]]
  205. end
  206. --
  207. -- When nodes are trimmed, the paths on the remaining nodes should
  208. -- be updated to reflect the new hierarchy.
  209. --
  210. function suite.trimroot_updatesPaths_onNodesRemoved()
  211. tree.add(tr, "A/1")
  212. tree.add(tr, "A/2")
  213. tree.trimroot(tr)
  214. test.isequal("1", tr.children[1].path)
  215. end
  216. function suite.trimroot_updatesPaths_onDotDotRemoved()
  217. tree.add(tr, "../../../tests/test_hello.c")
  218. tree.add(tr, "../src/test.c")
  219. tree.trimroot(tr)
  220. test.isequal("tests", tr.children[1].path)
  221. end
  222. --
  223. -- Nodes with the key "trim" set to false should be removed.
  224. --
  225. function suite.trimroot_respectsTrimFlag()
  226. local n = tree.add(tr, "A")
  227. tree.add(tr, "A/1")
  228. n.trim = false
  229. tree.trimroot(tr)
  230. prepare()
  231. test.capture [[
  232. A
  233. 1
  234. ]]
  235. end