test_configset.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. --
  2. -- tests/base/test_configset.lua
  3. -- Test suite for the configset API.
  4. -- Copyright (c) 2012-2014 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("configset")
  8. local configset = p.configset
  9. local field = p.field
  10. --
  11. -- Setup and teardown
  12. --
  13. local cset, parentset
  14. function suite.setup()
  15. local wks = test.createWorkspace()
  16. parentset = configset.new()
  17. cset = configset.new(parentset)
  18. end
  19. --
  20. -- Make sure that new() returns a valid object.
  21. --
  22. function suite.new_returnsValidObject()
  23. test.isequal("table", type(cset))
  24. end
  25. --
  26. -- Check the default values for different field types.
  27. --
  28. function suite.defaultValue_onString()
  29. test.isnil(configset.fetch(cset, field.get("targetextension")))
  30. end
  31. function suite.defaultValue_onList()
  32. test.isequal({}, configset.fetch(cset, field.get("defines")))
  33. end
  34. --
  35. -- Make sure that I can roundtrip a value stored into the
  36. -- initial, default configuration.
  37. --
  38. function suite.canRoundtrip_onDefaultBlock()
  39. local f = field.get("targetextension")
  40. configset.store(cset, f, ".so")
  41. test.isequal(".so", configset.fetch(cset, f, {}))
  42. end
  43. --
  44. -- Make sure that I can roundtrip a value stored into a block
  45. -- with a simple matching term.
  46. --
  47. function suite.canRoundtrip_onSimpleTermMatch()
  48. local f = field.get("targetextension")
  49. configset.addblock(cset, { "Windows" })
  50. configset.store(cset, f, ".dll")
  51. test.isequal(".dll", configset.fetch(cset, f, { "windows" }))
  52. end
  53. --
  54. -- Make sure that blocks that do not match the context terms
  55. -- do not contribute to the result.
  56. --
  57. function suite.skipsBlock_onTermMismatch()
  58. local f = field.get("targetextension")
  59. configset.store(cset, f, ".so")
  60. configset.addblock(cset, { "Windows" })
  61. configset.store(cset, f, ".dll")
  62. test.isequal(".so", configset.fetch(cset, f, { "linux" }))
  63. end
  64. --
  65. -- Values stored in a parent configuration set should propagate into child.
  66. --
  67. function suite.canRoundtrip_fromParentToChild()
  68. local f = field.get("targetextension")
  69. configset.store(parentset, f, ".so")
  70. test.isequal(".so", configset.fetch(cset, f, {}))
  71. end
  72. --
  73. -- Child should be able to override parent values.
  74. --
  75. function suite.child_canOverrideStringValueFromParent()
  76. local f = field.get("targetextension")
  77. configset.store(parentset, f, ".so")
  78. configset.store(cset, f, ".dll")
  79. test.isequal(".dll", configset.fetch(cset, f, {}))
  80. end
  81. --
  82. -- If a base directory is set, filename tests should be performed
  83. -- relative to this path.
  84. --
  85. function suite.filenameMadeRelative_onBaseDirSet()
  86. local f = field.get("buildaction")
  87. configset.addblock(cset, { "hello.c" }, os.getcwd())
  88. configset.store(cset, f, "Copy")
  89. test.isequal("Copy", configset.fetch(cset, f, { files=path.join(os.getcwd(), "hello.c"):lower() }))
  90. end
  91. --
  92. -- List fields should return an empty list of not set.
  93. --
  94. function suite.lists_returnsEmptyTable_onNotSet()
  95. test.isequal({}, configset.fetch(cset, field.get("buildoptions"), {}))
  96. end
  97. --
  98. -- List fields should merge values fetched from different blocks.
  99. --
  100. function suite.lists_mergeValues_onFetch()
  101. local f = field.get("buildoptions")
  102. configset.store(cset, f, "v1")
  103. configset.addblock(cset, { "windows" })
  104. configset.store(cset, f, "v2")
  105. test.isequal({"v1", "v2"}, configset.fetch(cset, f, {"windows"}))
  106. end
  107. --
  108. -- Multiple adds to a list field in the same block should be merged together.
  109. --
  110. function suite.lists_mergeValues_onAdd()
  111. local f = field.get("buildoptions")
  112. configset.store(cset, f, "v1")
  113. configset.store(cset, f, "v2")
  114. test.isequal({"v1", "v2"}, configset.fetch(cset, f, {"windows"}))
  115. end
  116. --
  117. -- Fetched lists should be both keyed and indexed.
  118. --
  119. function suite.lists_includeValueKeys()
  120. local f = field.get("buildoptions")
  121. configset.store(cset, f, { "v1", "v2" })
  122. local x = configset.fetch(cset, f, {})
  123. test.isequal("v2", x.v2)
  124. end
  125. --
  126. -- Check removing a value with an exact match.
  127. --
  128. function suite.remove_onExactValueMatch()
  129. local f = field.get("flags")
  130. local r, err = configset.store(cset, f, { "Symbols", "WinMain", "MFC" })
  131. test.isnil(err)
  132. configset.remove(cset, f, { "WinMain" })
  133. local result = configset.fetch(cset, f)
  134. test.isequal({ "Symbols", "MFC" }, result)
  135. end
  136. function suite.remove_onMultipleValues()
  137. local f = field.get("flags")
  138. local r, err = configset.store(cset, f, { "Symbols", "Maps", "WinMain", "MFC" })
  139. test.isnil(err)
  140. configset.remove(cset, f, { "Maps", "MFC" })
  141. local result = configset.fetch(cset, f)
  142. test.isequal({ "Symbols", "WinMain" }, result)
  143. end
  144. --
  145. -- Remove should also accept wildcards.
  146. --
  147. function suite.remove_onWildcard()
  148. local f = field.get("defines")
  149. configset.store(cset, f, { "WIN32", "WIN64", "LINUX", "MACOSX" })
  150. configset.remove(cset, f, { "WIN*" })
  151. test.isequal({ "LINUX", "MACOSX" }, configset.fetch(cset, f, {}))
  152. end
  153. --
  154. -- Keyed values should merge keys fetched from different blocks.
  155. --
  156. function suite.keyed_mergesKeys_onFetch()
  157. local f = field.get("configmap")
  158. configset.store(cset, f, { Debug="Debug", Release="Release" })
  159. configset.addblock(cset, { "windows" })
  160. configset.store(cset, f, { Profile="Profile" })
  161. local x = configset.fetch(cset, f, {"windows"})
  162. test.istrue(x[1].Debug and x[1].Release and x[2].Profile)
  163. end
  164. --
  165. -- Multiple adds to a keyed value field in the same block should be merged.
  166. --
  167. function suite.keyed_mergesKeys_onAdd()
  168. local f = field.get("configmap")
  169. configset.store(cset, f, { Debug="Debug", Release="Release" })
  170. configset.store(cset, f, { Profile="Profile" })
  171. local x = configset.fetch(cset, f, {"windows"})
  172. test.istrue(x[1].Debug and x[1].Release and x[2].Profile)
  173. end
  174. --
  175. -- Keyed values should overwrite when non-merged fields are fetched.
  176. --
  177. function suite.keyed_overwritesValues_onNonMergeFetch()
  178. local f = field.get("configmap")
  179. configset.store(cset, f, { Debug="Debug" })
  180. configset.addblock(cset, { "windows" })
  181. configset.store(cset, f, { Debug="Development" })
  182. local x = configset.fetch(cset, f, {"windows"})
  183. test.isequal({"Development"}, x[2].Debug)
  184. end
  185. function suite.keyed_overwritesValues_onNonMergeAdd()
  186. local f = field.get("configmap")
  187. configset.store(cset, f, { Debug="Debug" })
  188. configset.store(cset, f, { Debug="Development" })
  189. local x = configset.fetch(cset, f, {"windows"})
  190. test.isequal({"Development"}, x[2].Debug)
  191. end