test_path.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. --
  2. -- tests/base/test_path.lua
  3. -- Automated test suite for the action list.
  4. -- Copyright (c) 2008-2010 Jason Perkins and the Premake project
  5. --
  6. T.path = { }
  7. local suite = T.path
  8. --
  9. -- path.getabsolute() tests
  10. --
  11. function suite.getabsolute_ReturnsCorrectPath_OnMissingSubdir()
  12. local expected = path.translate(os.getcwd(), "/") .. "/a/b/c"
  13. test.isequal(expected, path.getabsolute("a/b/c"))
  14. end
  15. function suite.getabsolute_RemovesDotDots_OnWindowsAbsolute()
  16. test.isequal("c:/ProjectB/bin", path.getabsolute("c:/ProjectA/../ProjectB/bin"))
  17. end
  18. function suite.getabsolute_RemovesDotDots_OnPosixAbsolute()
  19. test.isequal("/ProjectB/bin", path.getabsolute("/ProjectA/../ProjectB/bin"))
  20. end
  21. function suite.getabsolute_OnTrailingSlash()
  22. local expected = path.translate(os.getcwd(), "/") .. "/a/b/c"
  23. test.isequal(expected, path.getabsolute("a/b/c/"))
  24. end
  25. function suite.getabsolute_OnLeadingEnvVar()
  26. test.isequal("$(HOME)/user", path.getabsolute("$(HOME)/user"))
  27. end
  28. function suite.getabsolute_OnMultipleEnvVar()
  29. test.isequal("$(HOME)/$(USER)", path.getabsolute("$(HOME)/$(USER)"))
  30. end
  31. function suite.getabsolute_OnTrailingEnvVar()
  32. local expected = path.translate(os.getcwd(), "/") .. "/home/$(USER)"
  33. test.isequal(expected, path.getabsolute("home/$(USER)"))
  34. end
  35. --
  36. -- path.getbasename() tests
  37. --
  38. function suite.getbasename_ReturnsCorrectName_OnDirAndExtension()
  39. test.isequal("filename", path.getbasename("folder/filename.ext"))
  40. end
  41. --
  42. -- path.getdirectory() tests
  43. --
  44. function suite.getdirectory_ReturnsEmptyString_OnNoDirectory()
  45. test.isequal(".", path.getdirectory("filename.ext"))
  46. end
  47. function suite.getdirectory_ReturnsDirectory_OnSingleLevelPath()
  48. test.isequal("dir0", path.getdirectory("dir0/filename.ext"))
  49. end
  50. function suite.getdirectory_ReturnsDirectory_OnMultiLeveLPath()
  51. test.isequal("dir0/dir1/dir2", path.getdirectory("dir0/dir1/dir2/filename.ext"))
  52. end
  53. function suite.getdirectory_ReturnsRootPath_OnRootPathOnly()
  54. test.isequal("/", path.getdirectory("/filename.ext"))
  55. end
  56. --
  57. -- path.getdrive() tests
  58. --
  59. function suite.getdrive_ReturnsNil_OnNotWindows()
  60. test.isnil(path.getdrive("/hello"))
  61. end
  62. function suite.getdrive_ReturnsLetter_OnWindowsAbsolute()
  63. test.isequal("x", path.getdrive("x:/hello"))
  64. end
  65. --
  66. -- path.getextension() tests
  67. --
  68. function suite.getextension_ReturnsEmptyString_OnNoExtension()
  69. test.isequal("", path.getextension("filename"))
  70. end
  71. function suite.getextension_ReturnsExtension()
  72. test.isequal(".txt", path.getextension("filename.txt"))
  73. end
  74. function suite.getextension_OnMultipleDots()
  75. test.isequal(".txt", path.getextension("filename.mod.txt"))
  76. end
  77. function suite.getextension_OnLeadingNumeric()
  78. test.isequal(".7z", path.getextension("filename.7z"))
  79. end
  80. function suite.getextension_OnUnderscore()
  81. test.isequal(".a_c", path.getextension("filename.a_c"))
  82. end
  83. function suite.getextension_OnHyphen()
  84. test.isequal(".a-c", path.getextension("filename.a-c"))
  85. end
  86. --
  87. -- path.getrelative() tests
  88. --
  89. function suite.getrelative_ReturnsDot_OnMatchingPaths()
  90. test.isequal(".", path.getrelative("/a/b/c", "/a/b/c"))
  91. end
  92. function suite.getrelative_ReturnsDoubleDot_OnChildToParent()
  93. test.isequal("..", path.getrelative("/a/b/c", "/a/b"))
  94. end
  95. function suite.getrelative_ReturnsDoubleDot_OnSiblingToSibling()
  96. test.isequal("../d", path.getrelative("/a/b/c", "/a/b/d"))
  97. end
  98. function suite.getrelative_ReturnsChildPath_OnParentToChild()
  99. test.isequal("d", path.getrelative("/a/b/c", "/a/b/c/d"))
  100. end
  101. function suite.getrelative_ReturnsChildPath_OnWindowsAbsolute()
  102. test.isequal("obj/debug", path.getrelative("C:/Code/Premake4", "C:/Code/Premake4/obj/debug"))
  103. end
  104. function suite.getrelative_ReturnsAbsPath_OnDifferentDriveLetters()
  105. test.isequal("D:/Files", path.getrelative("C:/Code/Premake4", "D:/Files"))
  106. end
  107. function suite.getrelative_ReturnsAbsPath_OnDollarMacro()
  108. test.isequal("$(SDK_HOME)/include", path.getrelative("C:/Code/Premake4", "$(SDK_HOME)/include"))
  109. end
  110. function suite.getrelative_ReturnsAbsPath_OnRootedPath()
  111. test.isequal("/opt/include", path.getrelative("/home/me/src/project", "/opt/include"))
  112. end
  113. --
  114. -- path.isabsolute() tests
  115. --
  116. function suite.isabsolute_ReturnsTrue_OnAbsolutePosixPath()
  117. test.istrue(path.isabsolute("/a/b/c"))
  118. end
  119. function suite.isabsolute_ReturnsTrue_OnAbsoluteWindowsPathWithDrive()
  120. test.istrue(path.isabsolute("C:/a/b/c"))
  121. end
  122. function suite.isabsolute_ReturnsFalse_OnRelativePath()
  123. test.isfalse(path.isabsolute("a/b/c"))
  124. end
  125. function suite.isabsolute_ReturnsTrue_OnDollarSign()
  126. test.istrue(path.isabsolute("$(SDK_HOME)/include"))
  127. end
  128. --
  129. -- path.join() tests
  130. --
  131. function suite.join_OnValidParts()
  132. test.isequal("p1/p2", path.join("p1", "p2"))
  133. end
  134. function suite.join_OnAbsoluteUnixPath()
  135. test.isequal("/p2", path.join("p1", "/p2"))
  136. end
  137. function suite.join_OnAbsoluteWindowsPath()
  138. test.isequal("C:/p2", path.join("p1", "C:/p2"))
  139. end
  140. function suite.join_OnCurrentDirectory()
  141. test.isequal("p2", path.join(".", "p2"))
  142. end
  143. function suite.join_OnNilSecondPart()
  144. test.isequal("p1", path.join("p1", nil))
  145. end
  146. function suite.join_onMoreThanTwoParts()
  147. test.isequal("p1/p2/p3", path.join("p1", "p2", "p3"))
  148. end
  149. function suite.join_removesExtraInternalSlashes()
  150. test.isequal("p1/p2", path.join("p1/", "p2"))
  151. end
  152. function suite.join_removesTrailingSlash()
  153. test.isequal("p1/p2", path.join("p1", "p2/"))
  154. end
  155. function suite.join_ignoresNilParts()
  156. test.isequal("p2", path.join(nil, "p2", nil))
  157. end
  158. function suite.join_ignoresEmptyParts()
  159. test.isequal("p2", path.join("", "p2", ""))
  160. end
  161. --
  162. -- path.rebase() tests
  163. --
  164. function suite.rebase_WithEndingSlashOnPath()
  165. local cwd = os.getcwd()
  166. test.isequal("src", path.rebase("../src/", cwd, path.getdirectory(cwd)))
  167. end
  168. --
  169. -- path.translate() tests
  170. --
  171. function suite.translate_ReturnsTranslatedPath_OnValidPath()
  172. test.isequal("dir/dir/file", path.translate("dir\\dir\\file", "/"))
  173. end
  174. function suite.translate_ReturnsCorrectSeparator_OnMixedPath()
  175. local actual = path.translate("dir\\dir/file")
  176. if (os.is("windows")) then
  177. test.isequal("dir\\dir\\file", actual)
  178. else
  179. test.isequal("dir/dir/file", actual)
  180. end
  181. end
  182. --
  183. -- path.wildcards tests
  184. --
  185. function suite.wildcards_MatchesTrailingStar()
  186. local p = path.wildcards("**/xcode/*")
  187. test.isequal(".*/xcode/[^/]*", p)
  188. end
  189. function suite.wildcards_MatchPlusSign()
  190. local patt = path.wildcards("file+name.*")
  191. local name = "file+name.c"
  192. test.isequal(name, name:match(patt))
  193. end