test_detoken.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. --
  2. -- tests/base/test_detoken.lua
  3. -- Test suite for the token expansion API.
  4. -- Copyright (c) 2011-2014 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("detoken")
  8. local detoken = p.detoken
  9. --
  10. -- Setup
  11. --
  12. local x, action
  13. local environ = {}
  14. function suite.setup()
  15. action = p.action.get("test")
  16. end
  17. function suite.teardown()
  18. action.pathVars = nil
  19. end
  20. --
  21. -- The contents of the token should be executed and the results returned.
  22. --
  23. function suite.executesTokenContents()
  24. x = detoken.expand("MyProject%{1+1}", environ)
  25. test.isequal("MyProject2", x)
  26. end
  27. --
  28. -- If the value contains more than one token, then should all be expanded.
  29. --
  30. function suite.expandsMultipleTokens()
  31. x = detoken.expand("MyProject%{'X'}and%{'Y'}and%{'Z'}", environ)
  32. test.isequal("MyProjectXandYandZ", x)
  33. end
  34. --
  35. -- If the token replacement values contain tokens themselves, those
  36. -- should also get expanded.
  37. --
  38. function suite.expandsNestedTokens()
  39. environ.wks = { name="MyWorkspace%{'X'}" }
  40. x = detoken.expand("%{wks.name}", environ)
  41. test.isequal("MyWorkspaceX", x)
  42. end
  43. --
  44. -- Verify that the global namespace is still accessible.
  45. --
  46. function suite.canUseGlobalFunctions()
  47. x = detoken.expand("%{iif(true, 'a', 'b')}", environ)
  48. test.isequal("a", x)
  49. end
  50. --
  51. -- If a path field contains a token, and if that token expands to an
  52. -- absolute path itself, that should be returned as the new value.
  53. --
  54. function suite.canExpandToAbsPath()
  55. environ.cfg = { basedir = os.getcwd() }
  56. x = detoken.expand("bin/debug/%{cfg.basedir}", environ, {paths=true})
  57. test.isequal(os.getcwd(), x)
  58. end
  59. --
  60. -- If a non-path field contains a token that expands to a path, that
  61. -- path should be converted to a relative value.
  62. --
  63. function suite.canExpandToRelPath()
  64. local cwd = os.getcwd()
  65. environ.cfg = { basedir = path.getdirectory(cwd) }
  66. x = detoken.expand("cd %{cfg.basedir}", environ, {}, cwd)
  67. test.isequal("cd ..", x)
  68. end
  69. --
  70. -- but not if it is prefixed with a !
  71. --
  72. function suite.canExpandWithExclamationMark()
  73. local cwd = os.getcwd()
  74. environ.cfg = { basedir = path.getdirectory(cwd) }
  75. x = detoken.expand("%{!cfg.basedir}", environ, {}, cwd)
  76. test.isequal(path.getdirectory(os.getcwd()), x)
  77. end
  78. --
  79. -- If a path field contains a token that expands to a deferred join,
  80. -- it should be resolved before performing detoken.
  81. --
  82. function suite.canExpandWithDeferredJoin()
  83. local cwd = os.getcwd()
  84. x = detoken.expand(path.deferredjoin(os.getcwd(), "%{_ACTION}"), environ, {}, cwd)
  85. test.isequal(os.getcwd() .. "/test", x)
  86. end
  87. --
  88. -- If the value being expanded is a table, iterate over all of its values.
  89. --
  90. function suite.expandsAllItemsInList()
  91. x = detoken.expand({ "A%{1}", "B%{2}", "C%{3}" }, environ)
  92. test.isequal({ "A1", "B2", "C3" }, x)
  93. end
  94. --
  95. -- If the field being expanded supports path variable mapping, and the
  96. -- action provides a map, replace tokens with the mapped values.
  97. --
  98. function suite.replacesToken_onSupportedAndMapped()
  99. action.pathVars = { ["cfg.objdir"] = { absolute = true, token = "$(IntDir)" }, }
  100. x = detoken.expand("cmd %{cfg.objdir}/file", environ, {pathVars=true})
  101. test.isequal("cmd $(IntDir)/file", x)
  102. end
  103. function suite.replacesToken_onSupportedAndMapped_inAbsPath()
  104. action.pathVars = { ["cfg.objdir"] = { absolute = true, token = "$(IntDir)" }, }
  105. x = detoken.expand(os.getcwd() .. "/%{cfg.objdir}/file", environ, {paths=true,pathVars=true})
  106. test.isequal("$(IntDir)/file", x)
  107. end
  108. function suite.replacesToken_onSupportedAndMapped_inRelPath()
  109. action.pathVars = { ["cfg.objdir"] = { absolute = false, token = "$(IntDir)" }, }
  110. x = detoken.expand(os.getcwd() .. "/%{cfg.objdir}/file", environ, {paths=true,pathVars=true})
  111. test.isequal(os.getcwd() .. "/$(IntDir)/file", x)
  112. end
  113. --
  114. -- Escapes backslashes correctly.
  115. --
  116. function suite.escapesBackslashes()
  117. environ.foo = "some/path"
  118. x = detoken.expand("%{foo:gsub('/', '\\')}", environ)
  119. test.isequal("some\\path", x)
  120. end
  121. --
  122. -- Escapes backslashes correctly, but not outside tokens.
  123. --
  124. function suite.escapesBackslashes2()
  125. environ.foo = "some/path"
  126. x = detoken.expand("%{foo:gsub('/', '\\')}\\already\\escaped", environ)
  127. test.isequal("some\\path\\already\\escaped", x)
  128. end