test_criteria.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. --
  2. -- tests/base/test_criteria.lua
  3. -- Test suite for the criteria matching API.
  4. -- Copyright (c) 2012-2015 Jason Perkins and the Premake project
  5. --
  6. local p = premake
  7. local suite = test.declare("criteria")
  8. local criteria = p.criteria
  9. --
  10. -- Setup and teardown
  11. --
  12. local crit
  13. --
  14. -- A criteria with no terms should satisfy any context.
  15. --
  16. function suite.matches_alwaysTrue_onNoFilterTerms()
  17. crit = criteria.new {}
  18. test.istrue(criteria.matches(crit, { configurations="Debug", system="Windows" }))
  19. end
  20. --
  21. -- Should not match if any term is missing in the context.
  22. --
  23. function suite.matches_fails_onMissingContext()
  24. crit = criteria.new { "system:Windows", "architecture:x86" }
  25. test.isfalse(criteria.matches(crit, { configurations="Debug", system="Windows" }))
  26. end
  27. --
  28. -- Context terms must match the entire criteria term.
  29. --
  30. function suite.matches_fails_onIncompleteTermMatch()
  31. crit = criteria.new { "platforms:win64" }
  32. test.isfalse(criteria.matches(crit, { platforms="win64 dll dcrt" }))
  33. end
  34. --
  35. -- Wildcard matches should work.
  36. --
  37. function suite.matches_passes_onPatternMatch()
  38. crit = criteria.new { "action:vs*" }
  39. test.istrue(criteria.matches(crit, { action="vs2005" }))
  40. end
  41. --
  42. -- The "not" modifier should fail the test if the term is matched.
  43. --
  44. function suite.matches_fails_onMatchWithNotModifier_afterPrefix()
  45. crit = criteria.new { "system:not windows" }
  46. test.isfalse(criteria.matches(crit, { system="windows" }))
  47. end
  48. function suite.matches_fails_onMatchWithNotModifier_beforePrefix()
  49. crit = criteria.new { "not system:windows" }
  50. test.isfalse(criteria.matches(crit, { system="windows" }))
  51. end
  52. function suite.matches_passes_onMissWithNotModifier_afterPrefix()
  53. crit = criteria.new { "system:not windows" }
  54. test.istrue(criteria.matches(crit, { system="linux" }))
  55. end
  56. function suite.matches_passes_onMissWithNotModifier_beforePrefix()
  57. crit = criteria.new { "not system:windows" }
  58. test.istrue(criteria.matches(crit, { system="linux" }))
  59. end
  60. function suite.matches_passes_onMissWithNotModifier_noPrefix()
  61. crit = criteria.new { "not debug" }
  62. test.istrue(criteria.matches(crit, { configurations="release" }))
  63. end
  64. --
  65. -- The "or" modifier should pass if either term is present.
  66. --
  67. function suite.matches_passes_onFirstOrTermMatched()
  68. crit = criteria.new { "system:windows or linux" }
  69. test.istrue(criteria.matches(crit, { system="windows" }))
  70. end
  71. function suite.matches_passes_onSecondOrTermMatched()
  72. crit = criteria.new { "system:windows or linux" }
  73. test.istrue(criteria.matches(crit, { system="linux" }))
  74. end
  75. function suite.matches_passes_onThirdOrTermMatched()
  76. crit = criteria.new { "system:windows or linux or vs2005" }
  77. test.istrue(criteria.matches(crit, { system="vs2005" }))
  78. end
  79. function suite.matches_fails_onNoOrTermMatched()
  80. crit = criteria.new { "system:windows or linux" }
  81. test.isfalse(criteria.matches(crit, { system="vs2005" }))
  82. end
  83. function suite.matches_passes_onMixedPrefixes_firstTermMatched_projectContext()
  84. crit = criteria.new { "system:windows or files:core*" }
  85. test.istrue(criteria.matches(crit, { system="windows" }))
  86. end
  87. function suite.matches_fails_onMixedPrefixes_firstTermMatched_fileContext()
  88. crit = criteria.new { "system:windows or files:core*" }
  89. test.isfalse(criteria.matches(crit, { system="windows", files="hello.cpp" }))
  90. end
  91. function suite.matches_passes_onMixedPrefixes_secondTermMatched()
  92. crit = criteria.new { "system:windows or files:core*" }
  93. test.istrue(criteria.matches(crit, { system="linux", files="coregraphics.cpp" }))
  94. end
  95. function suite.matches_fails_onMixedPrefixes_noTermMatched()
  96. crit = criteria.new { "system:windows or files:core*" }
  97. test.isfalse(criteria.matches(crit, { system="linux", files="hello.cpp" }))
  98. end
  99. --
  100. -- The "not" modifier should fail on any match with an "or" modifier.
  101. --
  102. function suite.matches_passes_onNotOrMatchesFirst()
  103. crit = criteria.new { "system:not windows or linux" }
  104. test.isfalse(criteria.matches(crit, { system="windows" }))
  105. end
  106. function suite.matches_passes_onNotOrMatchesSecond()
  107. crit = criteria.new { "system:windows or not linux" }
  108. test.isfalse(criteria.matches(crit, { system="linux" }))
  109. end
  110. --
  111. -- The "not" modifier should succeed with "or" if there are no matches.
  112. --
  113. function suite.matches_passes_onNoNotMatch()
  114. crit = criteria.new { "system:not windows or linux" }
  115. test.istrue(criteria.matches(crit, { system="macosx" }))
  116. end
  117. --
  118. -- If the context specifies a filename, the filter must match it explicitly.
  119. --
  120. function suite.matches_passes_onFilenameAndMatchingPattern()
  121. crit = criteria.new { "files:**.c", "system:windows" }
  122. test.istrue(criteria.matches(crit, { system="windows", files="hello.c" }))
  123. end
  124. function suite.matches_fails_onFilenameAndNoMatchingPattern()
  125. crit = criteria.new { "system:windows" }
  126. test.isfalse(criteria.matches(crit, { system="windows", files="hello.c" }))
  127. end
  128. --
  129. -- Test criteria creation through a table.
  130. --
  131. function suite.createCriteriaWithTable()
  132. crit = criteria.new {
  133. files = { "**.c" },
  134. system = "windows"
  135. }
  136. test.istrue(criteria.matches(crit, { system="windows", files="hello.c" }))
  137. end
  138. function suite.createCriteriaWithTable2()
  139. crit = criteria.new {
  140. system = "not windows"
  141. }
  142. test.isfalse(criteria.matches(crit, { system="windows" }))
  143. end
  144. function suite.createCriteriaWithTable3()
  145. crit = criteria.new {
  146. system = "not windows or linux"
  147. }
  148. test.istrue(criteria.matches(crit, { system="macosx" }))
  149. end
  150. function suite.createCriteriaWithTable4()
  151. crit = criteria.new {
  152. system = "windows or linux"
  153. }
  154. test.istrue(criteria.matches(crit, { system="windows" }))
  155. end
  156. --
  157. -- "Not" modifiers can also be used on filenames.
  158. --
  159. function suite.matches_passes_onFilenameMissAndNotModifier()
  160. crit = criteria.new { "files:not **.c", "system:windows" }
  161. test.istrue(criteria.matches(crit, { system="windows", files="hello.h" }))
  162. end
  163. function suite.matches_fails_onFilenameHitAndNotModifier()
  164. crit = criteria.new { "files:not **.c", "system:windows" }
  165. test.isfalse(criteria.matches(crit, { system="windows", files="hello.c" }))
  166. end
  167. --
  168. -- If context provides a list of values, match against them.
  169. --
  170. function suite.matches_passes_termMatchesList()
  171. crit = criteria.new { "options:debug" }
  172. test.istrue(criteria.matches(crit, { options={ "debug", "logging" }}))
  173. end
  174. --
  175. -- If no prefix is specified, default to "configurations".
  176. --
  177. function suite.matches_usesDefaultPrefix_onSingleTerm()
  178. crit = criteria.new { "debug" }
  179. test.istrue(criteria.matches(crit, { configurations="debug" }))
  180. end
  181. --
  182. -- These tests use the older, unprefixed style of filter terms. This
  183. -- approach will get phased out eventually, but are still included here
  184. -- for backward compatibility testing.
  185. --
  186. function suite.matches_onEmptyCriteria_Unprefixed()
  187. crit = criteria.new({}, true)
  188. test.istrue(criteria.matches(crit, { "apple", "orange" }))
  189. end
  190. function suite.fails_onMissingContext_Unprefixed()
  191. crit = criteria.new({ "orange", "pear" }, true)
  192. test.isfalse(criteria.matches(crit, { "apple", "orange" }))
  193. end
  194. function suite.fails_onIncompleteMatch_Unprefixed()
  195. crit = criteria.new({ "win64" }, true)
  196. test.isfalse(criteria.matches(crit, { "win64 dll dcrt" }))
  197. end
  198. function suite.passes_onPatternMatch_Unprefixed()
  199. crit = criteria.new({ "vs*" }, true)
  200. test.istrue(criteria.matches(crit, { "vs2005" }))
  201. end
  202. function suite.fails_onNotMatch_Unprefixed()
  203. crit = criteria.new({ "not windows" }, true)
  204. test.isfalse(criteria.matches(crit, { "windows" }))
  205. end
  206. function suite.passes_onNotUnmatched_Unprefixed()
  207. crit = criteria.new({ "not windows" }, true)
  208. test.istrue(criteria.matches(crit, { "linux" }))
  209. end
  210. function suite.passes_onFirstOrTermMatched_Unprefixed()
  211. crit = criteria.new({ "windows or linux" }, true)
  212. test.istrue(criteria.matches(crit, { "windows" }))
  213. end
  214. function suite.passes_onSecondOrTermMatched_Unprefixed()
  215. crit = criteria.new({ "windows or linux" }, true)
  216. test.istrue(criteria.matches(crit, { "linux" }))
  217. end
  218. function suite.passes_onThirdOrTermMatched_Unprefixed()
  219. crit = criteria.new({ "windows or linux or vs2005" }, true)
  220. test.istrue(criteria.matches(crit, { "vs2005" }))
  221. end
  222. function suite.fails_onNoOrTermMatched_Unprefixed()
  223. crit = criteria.new({ "windows or linux" }, true)
  224. test.isfalse(criteria.matches(crit, { "vs2005" }))
  225. end
  226. function suite.passes_onNotOrMatchesFirst_Unprefixed()
  227. crit = criteria.new({ "not windows or linux" }, true)
  228. test.isfalse(criteria.matches(crit, { "windows" }))
  229. end
  230. function suite.passes_onNotOrMatchesSecond_Unprefixed()
  231. crit = criteria.new({ "windows or not linux" }, true)
  232. test.isfalse(criteria.matches(crit, { "linux" }))
  233. end
  234. function suite.passes_onNoNotMatch_Unprefixed()
  235. crit = criteria.new({ "not windows or linux" }, true)
  236. test.istrue(criteria.matches(crit, { "macosx" }))
  237. end
  238. function suite.passes_onFilenameAndMatchingPattern_Unprefixed()
  239. crit = criteria.new({ "**.c", "windows" }, true)
  240. test.istrue(criteria.matches(crit, { system="windows", files="hello.c" }))
  241. end
  242. function suite.fails_onFilenameAndNoMatchingPattern_Unprefixed()
  243. crit = criteria.new({ "windows" }, true)
  244. test.isfalse(criteria.matches(crit, { system="windows", files="hello.c" }))
  245. end
  246. function suite.fails_onFilenameAndNotModifier_Unprefixed()
  247. crit = criteria.new({ "not linux" }, true)
  248. test.isfalse(criteria.matches(crit, { system="windows", files="hello.c" }))
  249. end
  250. function suite.matches_passes_termMatchesList_Unprefixed()
  251. crit = criteria.new({ "debug" }, true)
  252. test.istrue(criteria.matches(crit, { options={ "debug", "logging" }}))
  253. end
  254. --
  255. -- Should return nil and an error message on an invalid prefix.
  256. --
  257. function suite.returnsNilAndError_onInvalidPrefix()
  258. crit, err = criteria.new { "gibble:Debug" }
  259. test.isnil(crit)
  260. test.isnotnil(err)
  261. end
  262. --
  263. -- Should respect field value aliases, if present.
  264. --
  265. function suite.passes_onAliasedValue()
  266. p.api.addAliases("system", { ["gnu-linux"] = "linux" })
  267. crit = criteria.new { "system:gnu-linux" }
  268. test.istrue(criteria.matches(crit, { system="linux" }))
  269. end
  270. function suite.passes_onAliasedValue_withMixedCase()
  271. p.api.addAliases("system", { ["gnu-linux"] = "linux" })
  272. crit = criteria.new { "System:GNU-Linux" }
  273. test.istrue(criteria.matches(crit, { system="linux" }))
  274. end