test_configuration_unittest.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the Google name nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import unittest2 as unittest
  29. from webkitpy.layout_tests.models.test_configuration import *
  30. def make_mock_all_test_configurations_set():
  31. all_test_configurations = set()
  32. for version, architecture in (('snowleopard', 'x86'), ('xp', 'x86'), ('win7', 'x86'), ('vista', 'x86'), ('lucid', 'x86'), ('lucid', 'x86_64')):
  33. for build_type in ('debug', 'release'):
  34. all_test_configurations.add(TestConfiguration(version, architecture, build_type))
  35. return all_test_configurations
  36. MOCK_MACROS = {
  37. 'mac': ['snowleopard'],
  38. 'win': ['xp', 'vista', 'win7'],
  39. 'linux': ['lucid'],
  40. }
  41. class TestConfigurationTest(unittest.TestCase):
  42. def test_items(self):
  43. config = TestConfiguration('xp', 'x86', 'release')
  44. result_config_dict = {}
  45. for category, specifier in config.items():
  46. result_config_dict[category] = specifier
  47. self.assertEqual({'version': 'xp', 'architecture': 'x86', 'build_type': 'release'}, result_config_dict)
  48. def test_keys(self):
  49. config = TestConfiguration('xp', 'x86', 'release')
  50. result_config_keys = []
  51. for category in config.keys():
  52. result_config_keys.append(category)
  53. self.assertEqual(set(['version', 'architecture', 'build_type']), set(result_config_keys))
  54. def test_str(self):
  55. config = TestConfiguration('xp', 'x86', 'release')
  56. self.assertEqual('<xp, x86, release>', str(config))
  57. def test_repr(self):
  58. config = TestConfiguration('xp', 'x86', 'release')
  59. self.assertEqual("TestConfig(version='xp', architecture='x86', build_type='release')", repr(config))
  60. def test_hash(self):
  61. config_dict = {}
  62. config_dict[TestConfiguration('xp', 'x86', 'release')] = True
  63. self.assertIn(TestConfiguration('xp', 'x86', 'release'), config_dict)
  64. self.assertTrue(config_dict[TestConfiguration('xp', 'x86', 'release')])
  65. def query_unknown_key():
  66. return config_dict[TestConfiguration('xp', 'x86', 'debug')]
  67. self.assertRaises(KeyError, query_unknown_key)
  68. self.assertIn(TestConfiguration('xp', 'x86', 'release'), config_dict)
  69. self.assertNotIn(TestConfiguration('xp', 'x86', 'debug'), config_dict)
  70. configs_list = [TestConfiguration('xp', 'x86', 'release'), TestConfiguration('xp', 'x86', 'debug'), TestConfiguration('xp', 'x86', 'debug')]
  71. self.assertEqual(len(configs_list), 3)
  72. self.assertEqual(len(set(configs_list)), 2)
  73. def test_eq(self):
  74. self.assertEqual(TestConfiguration('xp', 'x86', 'release'), TestConfiguration('xp', 'x86', 'release'))
  75. self.assertNotEquals(TestConfiguration('xp', 'x86', 'release'), TestConfiguration('xp', 'x86', 'debug'))
  76. def test_values(self):
  77. config = TestConfiguration('xp', 'x86', 'release')
  78. result_config_values = []
  79. for value in config.values():
  80. result_config_values.append(value)
  81. self.assertEqual(set(['xp', 'x86', 'release']), set(result_config_values))
  82. class SpecifierSorterTest(unittest.TestCase):
  83. def __init__(self, testFunc):
  84. self._all_test_configurations = make_mock_all_test_configurations_set()
  85. unittest.TestCase.__init__(self, testFunc)
  86. def test_init(self):
  87. sorter = SpecifierSorter()
  88. self.assertIsNone(sorter.category_for_specifier('control'))
  89. sorter = SpecifierSorter(self._all_test_configurations)
  90. self.assertEqual(sorter.category_for_specifier('xp'), 'version')
  91. sorter = SpecifierSorter(self._all_test_configurations, MOCK_MACROS)
  92. self.assertEqual(sorter.category_for_specifier('mac'), 'version')
  93. def test_add_specifier(self):
  94. sorter = SpecifierSorter()
  95. self.assertIsNone(sorter.category_for_specifier('control'))
  96. sorter.add_specifier('version', 'control')
  97. self.assertEqual(sorter.category_for_specifier('control'), 'version')
  98. sorter.add_specifier('version', 'one')
  99. self.assertEqual(sorter.category_for_specifier('one'), 'version')
  100. sorter.add_specifier('architecture', 'renaissance')
  101. self.assertEqual(sorter.category_for_specifier('one'), 'version')
  102. self.assertEqual(sorter.category_for_specifier('renaissance'), 'architecture')
  103. def test_add_macros(self):
  104. sorter = SpecifierSorter(self._all_test_configurations)
  105. sorter.add_macros(MOCK_MACROS)
  106. self.assertEqual(sorter.category_for_specifier('mac'), 'version')
  107. self.assertEqual(sorter.category_for_specifier('win'), 'version')
  108. self.assertEqual(sorter.category_for_specifier('x86'), 'architecture')
  109. def test_category_priority(self):
  110. sorter = SpecifierSorter(self._all_test_configurations)
  111. self.assertEqual(sorter.category_priority('version'), 0)
  112. self.assertEqual(sorter.category_priority('build_type'), 2)
  113. def test_specifier_priority(self):
  114. sorter = SpecifierSorter(self._all_test_configurations)
  115. self.assertEqual(sorter.specifier_priority('x86'), 1)
  116. self.assertEqual(sorter.specifier_priority('snowleopard'), 0)
  117. def test_sort_specifiers(self):
  118. sorter = SpecifierSorter(self._all_test_configurations, MOCK_MACROS)
  119. self.assertEqual(sorter.sort_specifiers(set()), [])
  120. self.assertEqual(sorter.sort_specifiers(set(['x86'])), ['x86'])
  121. self.assertEqual(sorter.sort_specifiers(set(['x86', 'win7'])), ['win7', 'x86'])
  122. self.assertEqual(sorter.sort_specifiers(set(['x86', 'debug', 'win7'])), ['win7', 'x86', 'debug'])
  123. self.assertEqual(sorter.sort_specifiers(set(['snowleopard', 'x86', 'debug', 'win7'])), ['snowleopard', 'win7', 'x86', 'debug'])
  124. self.assertEqual(sorter.sort_specifiers(set(['x86', 'mac', 'debug', 'win7'])), ['mac', 'win7', 'x86', 'debug'])
  125. class TestConfigurationConverterTest(unittest.TestCase):
  126. def __init__(self, testFunc):
  127. self._all_test_configurations = make_mock_all_test_configurations_set()
  128. unittest.TestCase.__init__(self, testFunc)
  129. def test_symmetric_difference(self):
  130. self.assertEqual(TestConfigurationConverter.symmetric_difference([set(['a', 'b']), set(['b', 'c'])]), set(['a', 'c']))
  131. self.assertEqual(TestConfigurationConverter.symmetric_difference([set(['a', 'b']), set(['b', 'c']), set(['b', 'd'])]), set(['a', 'c', 'd']))
  132. def test_to_config_set(self):
  133. converter = TestConfigurationConverter(self._all_test_configurations)
  134. self.assertEqual(converter.to_config_set(set()), self._all_test_configurations)
  135. self.assertEqual(converter.to_config_set(set(['foo'])), set())
  136. self.assertEqual(converter.to_config_set(set(['xp', 'foo'])), set())
  137. errors = []
  138. self.assertEqual(converter.to_config_set(set(['xp', 'foo']), errors), set())
  139. self.assertEqual(errors, ["Unrecognized modifier 'foo'"])
  140. self.assertEqual(converter.to_config_set(set(['xp', 'x86_64'])), set())
  141. configs_to_match = set([
  142. TestConfiguration('xp', 'x86', 'release'),
  143. ])
  144. self.assertEqual(converter.to_config_set(set(['xp', 'release'])), configs_to_match)
  145. configs_to_match = set([
  146. TestConfiguration('snowleopard', 'x86', 'release'),
  147. TestConfiguration('vista', 'x86', 'release'),
  148. TestConfiguration('win7', 'x86', 'release'),
  149. TestConfiguration('xp', 'x86', 'release'),
  150. TestConfiguration('lucid', 'x86', 'release'),
  151. TestConfiguration('lucid', 'x86_64', 'release'),
  152. ])
  153. self.assertEqual(converter.to_config_set(set(['release'])), configs_to_match)
  154. configs_to_match = set([
  155. TestConfiguration('lucid', 'x86_64', 'release'),
  156. TestConfiguration('lucid', 'x86_64', 'debug'),
  157. ])
  158. self.assertEqual(converter.to_config_set(set(['x86_64'])), configs_to_match)
  159. configs_to_match = set([
  160. TestConfiguration('lucid', 'x86_64', 'release'),
  161. TestConfiguration('lucid', 'x86_64', 'debug'),
  162. TestConfiguration('lucid', 'x86', 'release'),
  163. TestConfiguration('lucid', 'x86', 'debug'),
  164. TestConfiguration('snowleopard', 'x86', 'release'),
  165. TestConfiguration('snowleopard', 'x86', 'debug'),
  166. ])
  167. self.assertEqual(converter.to_config_set(set(['lucid', 'snowleopard'])), configs_to_match)
  168. configs_to_match = set([
  169. TestConfiguration('lucid', 'x86', 'release'),
  170. TestConfiguration('lucid', 'x86', 'debug'),
  171. TestConfiguration('snowleopard', 'x86', 'release'),
  172. TestConfiguration('snowleopard', 'x86', 'debug'),
  173. ])
  174. self.assertEqual(converter.to_config_set(set(['lucid', 'snowleopard', 'x86'])), configs_to_match)
  175. configs_to_match = set([
  176. TestConfiguration('lucid', 'x86_64', 'release'),
  177. TestConfiguration('lucid', 'x86', 'release'),
  178. TestConfiguration('snowleopard', 'x86', 'release'),
  179. ])
  180. self.assertEqual(converter.to_config_set(set(['lucid', 'snowleopard', 'release'])), configs_to_match)
  181. def test_macro_expansion(self):
  182. converter = TestConfigurationConverter(self._all_test_configurations, MOCK_MACROS)
  183. configs_to_match = set([
  184. TestConfiguration('xp', 'x86', 'release'),
  185. TestConfiguration('vista', 'x86', 'release'),
  186. TestConfiguration('win7', 'x86', 'release'),
  187. ])
  188. self.assertEqual(converter.to_config_set(set(['win', 'release'])), configs_to_match)
  189. configs_to_match = set([
  190. TestConfiguration('xp', 'x86', 'release'),
  191. TestConfiguration('vista', 'x86', 'release'),
  192. TestConfiguration('win7', 'x86', 'release'),
  193. TestConfiguration('lucid', 'x86', 'release'),
  194. TestConfiguration('lucid', 'x86_64', 'release'),
  195. ])
  196. self.assertEqual(converter.to_config_set(set(['win', 'lucid', 'release'])), configs_to_match)
  197. configs_to_match = set([
  198. TestConfiguration('xp', 'x86', 'release'),
  199. TestConfiguration('vista', 'x86', 'release'),
  200. TestConfiguration('win7', 'x86', 'release'),
  201. TestConfiguration('snowleopard', 'x86', 'release'),
  202. ])
  203. self.assertEqual(converter.to_config_set(set(['win', 'mac', 'release'])), configs_to_match)
  204. def test_to_specifier_lists(self):
  205. converter = TestConfigurationConverter(self._all_test_configurations, MOCK_MACROS)
  206. self.assertEqual(converter.to_specifiers_list(set(self._all_test_configurations)), [[]])
  207. self.assertEqual(converter.to_specifiers_list(set()), [])
  208. configs_to_match = set([
  209. TestConfiguration('xp', 'x86', 'release'),
  210. ])
  211. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['release', 'xp'])])
  212. configs_to_match = set([
  213. TestConfiguration('xp', 'x86', 'release'),
  214. TestConfiguration('xp', 'x86', 'debug'),
  215. ])
  216. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['xp'])])
  217. configs_to_match = set([
  218. TestConfiguration('lucid', 'x86_64', 'debug'),
  219. TestConfiguration('xp', 'x86', 'release'),
  220. ])
  221. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['release', 'xp']), set(['debug', 'x86_64', 'linux'])])
  222. configs_to_match = set([
  223. TestConfiguration('xp', 'x86', 'release'),
  224. TestConfiguration('xp', 'x86', 'release'),
  225. TestConfiguration('lucid', 'x86_64', 'debug'),
  226. TestConfiguration('lucid', 'x86', 'debug'),
  227. TestConfiguration('lucid', 'x86_64', 'debug'),
  228. TestConfiguration('lucid', 'x86', 'debug'),
  229. ])
  230. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['release', 'xp']), set(['debug', 'linux'])])
  231. configs_to_match = set([
  232. TestConfiguration('xp', 'x86', 'release'),
  233. TestConfiguration('snowleopard', 'x86', 'release'),
  234. TestConfiguration('vista', 'x86', 'release'),
  235. TestConfiguration('win7', 'x86', 'release'),
  236. TestConfiguration('lucid', 'x86', 'release'),
  237. TestConfiguration('lucid', 'x86_64', 'release'),
  238. ])
  239. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['release'])])
  240. configs_to_match = set([
  241. TestConfiguration('xp', 'x86', 'release'),
  242. TestConfiguration('snowleopard', 'x86', 'release'),
  243. ])
  244. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['xp', 'mac', 'release'])])
  245. configs_to_match = set([
  246. TestConfiguration('xp', 'x86', 'release'),
  247. TestConfiguration('snowleopard', 'x86', 'release'),
  248. TestConfiguration('win7', 'x86', 'release'),
  249. TestConfiguration('win7', 'x86', 'debug'),
  250. TestConfiguration('lucid', 'x86', 'release'),
  251. ])
  252. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win7']), set(['release', 'linux', 'x86']), set(['release', 'xp', 'mac'])])
  253. def test_macro_collapsing(self):
  254. macros = {'foo': ['bar', 'baz'], 'people': ['bob', 'alice', 'john']}
  255. specifiers_list = [set(['john', 'godzilla', 'bob', 'alice'])]
  256. TestConfigurationConverter.collapse_macros(macros, specifiers_list)
  257. self.assertEqual(specifiers_list, [set(['people', 'godzilla'])])
  258. specifiers_list = [set(['john', 'godzilla', 'alice'])]
  259. TestConfigurationConverter.collapse_macros(macros, specifiers_list)
  260. self.assertEqual(specifiers_list, [set(['john', 'godzilla', 'alice', 'godzilla'])])
  261. specifiers_list = [set(['bar', 'godzilla', 'baz', 'bob', 'alice', 'john'])]
  262. TestConfigurationConverter.collapse_macros(macros, specifiers_list)
  263. self.assertEqual(specifiers_list, [set(['foo', 'godzilla', 'people'])])
  264. specifiers_list = [set(['bar', 'godzilla', 'baz', 'bob']), set(['bar', 'baz']), set(['people', 'alice', 'bob', 'john'])]
  265. TestConfigurationConverter.collapse_macros(macros, specifiers_list)
  266. self.assertEqual(specifiers_list, [set(['bob', 'foo', 'godzilla']), set(['foo']), set(['people'])])
  267. def test_converter_macro_collapsing(self):
  268. converter = TestConfigurationConverter(self._all_test_configurations, MOCK_MACROS)
  269. configs_to_match = set([
  270. TestConfiguration('xp', 'x86', 'release'),
  271. TestConfiguration('vista', 'x86', 'release'),
  272. TestConfiguration('win7', 'x86', 'release'),
  273. ])
  274. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win', 'release'])])
  275. configs_to_match = set([
  276. TestConfiguration('xp', 'x86', 'release'),
  277. TestConfiguration('vista', 'x86', 'release'),
  278. TestConfiguration('win7', 'x86', 'release'),
  279. TestConfiguration('lucid', 'x86', 'release'),
  280. TestConfiguration('lucid', 'x86_64', 'release'),
  281. ])
  282. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win', 'linux', 'release'])])
  283. configs_to_match = set([
  284. TestConfiguration('xp', 'x86', 'release'),
  285. TestConfiguration('vista', 'x86', 'release'),
  286. TestConfiguration('win7', 'x86', 'release'),
  287. TestConfiguration('snowleopard', 'x86', 'release'),
  288. ])
  289. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win', 'mac', 'release'])])
  290. configs_to_match = set([
  291. TestConfiguration('xp', 'x86', 'release'),
  292. TestConfiguration('vista', 'x86', 'release'),
  293. TestConfiguration('win7', 'x86', 'release'),
  294. TestConfiguration('snowleopard', 'x86', 'release'),
  295. ])
  296. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win', 'mac', 'release'])])
  297. configs_to_match = set([
  298. TestConfiguration('xp', 'x86', 'release'),
  299. TestConfiguration('vista', 'x86', 'release'),
  300. TestConfiguration('win7', 'x86', 'release'),
  301. ])
  302. self.assertEqual(converter.to_specifiers_list(configs_to_match), [set(['win', 'release'])])
  303. def test_specifier_converter_access(self):
  304. specifier_sorter = TestConfigurationConverter(self._all_test_configurations, MOCK_MACROS).specifier_sorter()
  305. self.assertEqual(specifier_sorter.category_for_specifier('snowleopard'), 'version')
  306. self.assertEqual(specifier_sorter.category_for_specifier('mac'), 'version')