test_api.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. --
  2. -- tests/base/test_api.lua
  3. -- Automated test suite for the project API support functions.
  4. -- Copyright (c) 2008-2011 Jason Perkins and the Premake project
  5. --
  6. T.api = { }
  7. local suite = T.api
  8. local sln
  9. function suite.setup()
  10. sln = solution "MySolution"
  11. end
  12. --
  13. -- premake.getobject() tests
  14. --
  15. function suite.getobject_RaisesError_OnNoContainer()
  16. premake.CurrentContainer = nil
  17. c, err = premake.getobject("container")
  18. test.istrue(c == nil)
  19. test.isequal("no active solution or project", err)
  20. end
  21. function suite.getobject_RaisesError_OnNoActiveSolution()
  22. premake.CurrentContainer = { }
  23. c, err = premake.getobject("solution")
  24. test.istrue(c == nil)
  25. test.isequal("no active solution", err)
  26. end
  27. function suite.getobject_RaisesError_OnNoActiveConfig()
  28. premake.CurrentConfiguration = nil
  29. c, err = premake.getobject("config")
  30. test.istrue(c == nil)
  31. test.isequal("no active solution, project, or configuration", err)
  32. end
  33. --
  34. -- premake.setarray() tests
  35. --
  36. function suite.setarray_Inserts_OnStringValue()
  37. premake.CurrentConfiguration = { }
  38. premake.CurrentConfiguration.myfield = { }
  39. premake.setarray("config", "myfield", "hello")
  40. test.isequal("hello", premake.CurrentConfiguration.myfield[1])
  41. end
  42. function suite.setarray_Inserts_OnTableValue()
  43. premake.CurrentConfiguration = { }
  44. premake.CurrentConfiguration.myfield = { }
  45. premake.setarray("config", "myfield", { "hello", "goodbye" })
  46. test.isequal("hello", premake.CurrentConfiguration.myfield[1])
  47. test.isequal("goodbye", premake.CurrentConfiguration.myfield[2])
  48. end
  49. function suite.setarray_Appends_OnNewValues()
  50. premake.CurrentConfiguration = { }
  51. premake.CurrentConfiguration.myfield = { "hello" }
  52. premake.setarray("config", "myfield", "goodbye")
  53. test.isequal("hello", premake.CurrentConfiguration.myfield[1])
  54. test.isequal("goodbye", premake.CurrentConfiguration.myfield[2])
  55. end
  56. function suite.setarray_FlattensTables()
  57. premake.CurrentConfiguration = { }
  58. premake.CurrentConfiguration.myfield = { }
  59. premake.setarray("config", "myfield", { {"hello"}, {"goodbye"} })
  60. test.isequal("hello", premake.CurrentConfiguration.myfield[1])
  61. test.isequal("goodbye", premake.CurrentConfiguration.myfield[2])
  62. end
  63. function suite.setarray_RaisesError_OnInvalidValue()
  64. premake.CurrentConfiguration = { }
  65. premake.CurrentConfiguration.myfield = { }
  66. ok, err = pcall(function () premake.setarray("config", "myfield", "bad", { "Good", "Better", "Best" }) end)
  67. test.isfalse(ok)
  68. end
  69. function suite.setarray_CorrectsCase_OnConstrainedValue()
  70. premake.CurrentConfiguration = { }
  71. premake.CurrentConfiguration.myfield = { }
  72. premake.setarray("config", "myfield", "better", { "Good", "Better", "Best" })
  73. test.isequal("Better", premake.CurrentConfiguration.myfield[1])
  74. end
  75. --
  76. -- premake.setstring() tests
  77. --
  78. function suite.setstring_Sets_OnNewProperty()
  79. premake.CurrentConfiguration = { }
  80. premake.setstring("config", "myfield", "hello")
  81. test.isequal("hello", premake.CurrentConfiguration.myfield)
  82. end
  83. function suite.setstring_Overwrites_OnExistingProperty()
  84. premake.CurrentConfiguration = { }
  85. premake.CurrentConfiguration.myfield = "hello"
  86. premake.setstring("config", "myfield", "goodbye")
  87. test.isequal("goodbye", premake.CurrentConfiguration.myfield)
  88. end
  89. function suite.setstring_RaisesError_OnInvalidValue()
  90. premake.CurrentConfiguration = { }
  91. ok, err = pcall(function () premake.setstring("config", "myfield", "bad", { "Good", "Better", "Best" }) end)
  92. test.isfalse(ok)
  93. end
  94. function suite.setstring_CorrectsCase_OnConstrainedValue()
  95. premake.CurrentConfiguration = { }
  96. premake.setstring("config", "myfield", "better", { "Good", "Better", "Best" })
  97. test.isequal("Better", premake.CurrentConfiguration.myfield)
  98. end
  99. --
  100. -- premake.setkeyvalue() tests
  101. --
  102. function suite.setkeyvalue_Inserts_OnStringValue()
  103. premake.CurrentConfiguration = { }
  104. premake.setkeyvalue("config", "vpaths", { ["Headers"] = "*.h" })
  105. test.isequal({"*.h"}, premake.CurrentConfiguration.vpaths["Headers"])
  106. end
  107. function suite.setkeyvalue_Inserts_OnTableValue()
  108. premake.CurrentConfiguration = { }
  109. premake.setkeyvalue("config", "vpaths", { ["Headers"] = {"*.h","*.hpp"} })
  110. test.isequal({"*.h","*.hpp"}, premake.CurrentConfiguration.vpaths["Headers"])
  111. end
  112. function suite.setkeyvalue_Inserts_OnEmptyStringKey()
  113. premake.CurrentConfiguration = { }
  114. premake.setkeyvalue("config", "vpaths", { [""] = "src" })
  115. test.isequal({"src"}, premake.CurrentConfiguration.vpaths[""])
  116. end
  117. function suite.setkeyvalue_RaisesError_OnString()
  118. premake.CurrentConfiguration = { }
  119. ok, err = pcall(function () premake.setkeyvalue("config", "vpaths", "Headers") end)
  120. test.isfalse(ok)
  121. end
  122. function suite.setkeyvalue_InsertsString_IntoExistingKey()
  123. premake.CurrentConfiguration = { }
  124. premake.setkeyvalue("config", "vpaths", { ["Headers"] = "*.h" })
  125. premake.setkeyvalue("config", "vpaths", { ["Headers"] = "*.hpp" })
  126. test.isequal({"*.h","*.hpp"}, premake.CurrentConfiguration.vpaths["Headers"])
  127. end
  128. function suite.setkeyvalue_InsertsTable_IntoExistingKey()
  129. premake.CurrentConfiguration = { }
  130. premake.setkeyvalue("config", "vpaths", { ["Headers"] = {"*.h"} })
  131. premake.setkeyvalue("config", "vpaths", { ["Headers"] = {"*.hpp"} })
  132. test.isequal({"*.h","*.hpp"}, premake.CurrentConfiguration.vpaths["Headers"])
  133. end
  134. --
  135. -- accessor tests
  136. --
  137. function suite.accessor_CanRetrieveString()
  138. sln.blocks[1].kind = "ConsoleApp"
  139. test.isequal("ConsoleApp", kind())
  140. end
  141. --
  142. -- solution() tests
  143. --
  144. function suite.solution_SetsCurrentContainer_OnName()
  145. test.istrue(sln == premake.CurrentContainer)
  146. end
  147. function suite.solution_CreatesNewObject_OnNewName()
  148. solution "MySolution2"
  149. test.isfalse(sln == premake.CurrentContainer)
  150. end
  151. function suite.solution_ReturnsPrevious_OnExistingName()
  152. solution "MySolution2"
  153. local sln2 = solution "MySolution"
  154. test.istrue(sln == sln2)
  155. end
  156. function suite.solution_SetsCurrentContainer_OnExistingName()
  157. solution "MySolution2"
  158. solution "MySolution"
  159. test.istrue(sln == premake.CurrentContainer)
  160. end
  161. function suite.solution_ReturnsNil_OnNoActiveSolutionAndNoName()
  162. premake.CurrentContainer = nil
  163. test.isnil(solution())
  164. end
  165. function suite.solution_ReturnsCurrentSolution_OnActiveSolutionAndNoName()
  166. test.istrue(sln == solution())
  167. end
  168. function suite.solution_ReturnsCurrentSolution_OnActiveProjectAndNoName()
  169. project "MyProject"
  170. test.istrue(sln == solution())
  171. end
  172. function suite.solution_LeavesProjectActive_OnActiveProjectAndNoName()
  173. local prj = project "MyProject"
  174. solution()
  175. test.istrue(prj == premake.CurrentContainer)
  176. end
  177. function suite.solution_LeavesConfigActive_OnActiveSolutionAndNoName()
  178. local cfg = configuration "windows"
  179. solution()
  180. test.istrue(cfg == premake.CurrentConfiguration)
  181. end
  182. function suite.solution_LeavesConfigActive_OnActiveProjectAndNoName()
  183. project "MyProject"
  184. local cfg = configuration "windows"
  185. solution()
  186. test.istrue(cfg == premake.CurrentConfiguration)
  187. end
  188. function suite.solution_SetsName_OnNewName()
  189. test.isequal("MySolution", sln.name)
  190. end
  191. function suite.solution_AddsNewConfig_OnNewName()
  192. test.istrue(#sln.blocks == 1)
  193. end
  194. function suite.solution_AddsNewConfig_OnName()
  195. local num = #sln.blocks
  196. solution "MySolution"
  197. test.istrue(#sln.blocks == num + 1)
  198. end
  199. --
  200. -- configuration() tests
  201. --
  202. function suite.configuration_RaisesError_OnNoContainer()
  203. premake.CurrentContainer = nil
  204. local fn = function() configuration{"Debug"} end
  205. ok, err = pcall(fn)
  206. test.isfalse(ok)
  207. end
  208. function suite.configuration_SetsCurrentConfiguration_OnKeywords()
  209. local cfg = configuration {"Debug"}
  210. test.istrue(premake.CurrentConfiguration == cfg)
  211. end
  212. function suite.configuration_AddsToContainer_OnKeywords()
  213. local cfg = configuration {"Debug"}
  214. test.istrue(cfg == sln.blocks[#sln.blocks])
  215. end
  216. function suite.configuration_ReturnsCurrent_OnNoKeywords()
  217. local cfg = configuration()
  218. test.istrue(cfg == sln.blocks[1])
  219. end
  220. function suite.configuration_SetsTerms()
  221. local cfg = configuration {"aa", "bb"}
  222. test.isequal({"aa", "bb"}, cfg.terms)
  223. end
  224. function suite.configuration_SetsTermsWithNestedTables()
  225. local cfg = configuration { {"aa", "bb"}, "cc" }
  226. test.isequal({"aa", "bb", "cc"}, cfg.terms)
  227. end
  228. function suite.configuration_CanReuseTerms()
  229. local cfg = configuration { "aa", "bb" }
  230. local cfg2 = configuration { cfg.terms, "cc" }
  231. test.isequal({"aa", "bb", "cc"}, cfg2.terms)
  232. end
  233. --
  234. -- project() tests
  235. --
  236. function suite.project_RaisesError_OnNoSolution()
  237. premake.CurrentContainer = nil
  238. local fn = function() project("MyProject") end
  239. ok, err = pcall(fn)
  240. test.isfalse(ok)
  241. end
  242. function suite.project_SetsCurrentContainer_OnName()
  243. local prj = project "MyProject"
  244. test.istrue(prj == premake.CurrentContainer)
  245. end
  246. function suite.project_CreatesNewObject_OnNewName()
  247. local prj = project "MyProject"
  248. local pr2 = project "MyProject2"
  249. test.isfalse(prj == premake.CurrentContainer)
  250. end
  251. function suite.project_AddsToSolution_OnNewName()
  252. local prj = project "MyProject"
  253. test.istrue(prj == sln.projects[1])
  254. end
  255. function suite.project_ReturnsPrevious_OnExistingName()
  256. local prj = project "MyProject"
  257. local pr2 = project "MyProject2"
  258. local pr3 = project "MyProject"
  259. test.istrue(prj == pr3)
  260. end
  261. function suite.project_SetsCurrentContainer_OnExistingName()
  262. local prj = project "MyProject"
  263. local pr2 = project "MyProject2"
  264. local pr3 = project "MyProject"
  265. test.istrue(prj == premake.CurrentContainer)
  266. end
  267. function suite.project_ReturnsNil_OnNoActiveProjectAndNoName()
  268. test.isnil(project())
  269. end
  270. function suite.project_ReturnsCurrentProject_OnActiveProjectAndNoName()
  271. local prj = project "MyProject"
  272. test.istrue(prj == project())
  273. end
  274. function suite.project_LeavesProjectActive_OnActiveProjectAndNoName()
  275. local prj = project "MyProject"
  276. project()
  277. test.istrue(prj == premake.CurrentContainer)
  278. end
  279. function suite.project_LeavesConfigActive_OnActiveProjectAndNoName()
  280. local prj = project "MyProject"
  281. local cfg = configuration "Windows"
  282. project()
  283. test.istrue(cfg == premake.CurrentConfiguration)
  284. end
  285. function suite.project_SetsName_OnNewName()
  286. prj = project("MyProject")
  287. test.isequal("MyProject", prj.name)
  288. end
  289. function suite.project_SetsSolution_OnNewName()
  290. prj = project("MyProject")
  291. test.istrue(sln == prj.solution)
  292. end
  293. function suite.project_SetsConfiguration()
  294. prj = project("MyProject")
  295. test.istrue(premake.CurrentConfiguration == prj.blocks[1])
  296. end
  297. function suite.project_SetsUUID()
  298. local prj = project "MyProject"
  299. test.istrue(prj.uuid)
  300. end
  301. --
  302. -- uuid() tests
  303. --
  304. function suite.uuid_makes_uppercase()
  305. premake.CurrentContainer = {}
  306. uuid "7CBB5FC2-7449-497f-947F-129C5129B1FB"
  307. test.isequal(premake.CurrentContainer.uuid, "7CBB5FC2-7449-497F-947F-129C5129B1FB")
  308. end
  309. --
  310. -- Fields with allowed value lists should be case-insensitive.
  311. --
  312. function suite.flags_onCaseMismatch()
  313. premake.CurrentConfiguration = {}
  314. flags "symbols"
  315. test.isequal(premake.CurrentConfiguration.flags[1], "Symbols")
  316. end
  317. function suite.flags_onCaseMismatchAndAlias()
  318. premake.CurrentConfiguration = {}
  319. flags "optimisespeed"
  320. test.isequal(premake.CurrentConfiguration.flags[1], "OptimizeSpeed")
  321. end