PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/WebKit/Tools/Scripts/webkitpy/style/filter_unittest.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 256 lines | 191 code | 17 blank | 48 comment | 2 complexity | 4f2eb592703fe09289af8f4ff6a1095b MD5 | raw file
  1. # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  13. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  16. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. """Unit tests for filter.py."""
  23. import unittest
  24. from filter import _CategoryFilter as CategoryFilter
  25. from filter import validate_filter_rules
  26. from filter import FilterConfiguration
  27. # On Testing __eq__() and __ne__():
  28. #
  29. # In the tests below, we deliberately do not use assertEqual() or
  30. # assertNotEquals() to test __eq__() or __ne__(). We do this to be
  31. # very explicit about what we are testing, especially in the case
  32. # of assertNotEquals().
  33. #
  34. # Part of the reason is that it is not immediately clear what
  35. # expression the unittest module uses to assert "not equals" -- the
  36. # negation of __eq__() or __ne__(), which are not necessarily
  37. # equivalent expresions in Python. For example, from Python's "Data
  38. # Model" documentation--
  39. #
  40. # "There are no implied relationships among the comparison
  41. # operators. The truth of x==y does not imply that x!=y is
  42. # false. Accordingly, when defining __eq__(), one should
  43. # also define __ne__() so that the operators will behave as
  44. # expected."
  45. #
  46. # (from http://docs.python.org/reference/datamodel.html#object.__ne__ )
  47. class ValidateFilterRulesTest(unittest.TestCase):
  48. """Tests validate_filter_rules() function."""
  49. def test_validate_filter_rules(self):
  50. all_categories = ["tabs", "whitespace", "build/include"]
  51. bad_rules = [
  52. "tabs",
  53. "*tabs",
  54. " tabs",
  55. " +tabs",
  56. "+whitespace/newline",
  57. "+xxx",
  58. ]
  59. good_rules = [
  60. "+tabs",
  61. "-tabs",
  62. "+build"
  63. ]
  64. for rule in bad_rules:
  65. self.assertRaises(ValueError, validate_filter_rules,
  66. [rule], all_categories)
  67. for rule in good_rules:
  68. # This works: no error.
  69. validate_filter_rules([rule], all_categories)
  70. class CategoryFilterTest(unittest.TestCase):
  71. """Tests CategoryFilter class."""
  72. def test_init(self):
  73. """Test __init__ method."""
  74. # Test that the attributes are getting set correctly.
  75. filter = CategoryFilter(["+"])
  76. self.assertEqual(["+"], filter._filter_rules)
  77. def test_init_default_arguments(self):
  78. """Test __init__ method default arguments."""
  79. filter = CategoryFilter()
  80. self.assertEqual([], filter._filter_rules)
  81. def test_str(self):
  82. """Test __str__ "to string" operator."""
  83. filter = CategoryFilter(["+a", "-b"])
  84. self.assertEqual(str(filter), "+a,-b")
  85. def test_eq(self):
  86. """Test __eq__ equality function."""
  87. filter1 = CategoryFilter(["+a", "+b"])
  88. filter2 = CategoryFilter(["+a", "+b"])
  89. filter3 = CategoryFilter(["+b", "+a"])
  90. # See the notes at the top of this module about testing
  91. # __eq__() and __ne__().
  92. self.assertTrue(filter1.__eq__(filter2))
  93. self.assertFalse(filter1.__eq__(filter3))
  94. def test_ne(self):
  95. """Test __ne__ inequality function."""
  96. # By default, __ne__ always returns true on different objects.
  97. # Thus, just check the distinguishing case to verify that the
  98. # code defines __ne__.
  99. #
  100. # Also, see the notes at the top of this module about testing
  101. # __eq__() and __ne__().
  102. self.assertFalse(CategoryFilter().__ne__(CategoryFilter()))
  103. def test_should_check(self):
  104. """Test should_check() method."""
  105. filter = CategoryFilter()
  106. self.assertTrue(filter.should_check("everything"))
  107. # Check a second time to exercise cache.
  108. self.assertTrue(filter.should_check("everything"))
  109. filter = CategoryFilter(["-"])
  110. self.assertFalse(filter.should_check("anything"))
  111. # Check a second time to exercise cache.
  112. self.assertFalse(filter.should_check("anything"))
  113. filter = CategoryFilter(["-", "+ab"])
  114. self.assertTrue(filter.should_check("abc"))
  115. self.assertFalse(filter.should_check("a"))
  116. filter = CategoryFilter(["+", "-ab"])
  117. self.assertFalse(filter.should_check("abc"))
  118. self.assertTrue(filter.should_check("a"))
  119. class FilterConfigurationTest(unittest.TestCase):
  120. """Tests FilterConfiguration class."""
  121. def _config(self, base_rules, path_specific, user_rules):
  122. """Return a FilterConfiguration instance."""
  123. return FilterConfiguration(base_rules=base_rules,
  124. path_specific=path_specific,
  125. user_rules=user_rules)
  126. def test_init(self):
  127. """Test __init__ method."""
  128. # Test that the attributes are getting set correctly.
  129. # We use parameter values that are different from the defaults.
  130. base_rules = ["-"]
  131. path_specific = [(["path"], ["+a"])]
  132. user_rules = ["+"]
  133. config = self._config(base_rules, path_specific, user_rules)
  134. self.assertEqual(base_rules, config._base_rules)
  135. self.assertEqual(path_specific, config._path_specific)
  136. self.assertEqual(user_rules, config._user_rules)
  137. def test_default_arguments(self):
  138. # Test that the attributes are getting set correctly to the defaults.
  139. config = FilterConfiguration()
  140. self.assertEqual([], config._base_rules)
  141. self.assertEqual([], config._path_specific)
  142. self.assertEqual([], config._user_rules)
  143. def test_eq(self):
  144. """Test __eq__ method."""
  145. # See the notes at the top of this module about testing
  146. # __eq__() and __ne__().
  147. self.assertTrue(FilterConfiguration().__eq__(FilterConfiguration()))
  148. # Verify that a difference in any argument causes equality to fail.
  149. config = FilterConfiguration()
  150. # These parameter values are different from the defaults.
  151. base_rules = ["-"]
  152. path_specific = [(["path"], ["+a"])]
  153. user_rules = ["+"]
  154. self.assertFalse(config.__eq__(FilterConfiguration(
  155. base_rules=base_rules)))
  156. self.assertFalse(config.__eq__(FilterConfiguration(
  157. path_specific=path_specific)))
  158. self.assertFalse(config.__eq__(FilterConfiguration(
  159. user_rules=user_rules)))
  160. def test_ne(self):
  161. """Test __ne__ method."""
  162. # By default, __ne__ always returns true on different objects.
  163. # Thus, just check the distinguishing case to verify that the
  164. # code defines __ne__.
  165. #
  166. # Also, see the notes at the top of this module about testing
  167. # __eq__() and __ne__().
  168. self.assertFalse(FilterConfiguration().__ne__(FilterConfiguration()))
  169. def test_base_rules(self):
  170. """Test effect of base_rules on should_check()."""
  171. base_rules = ["-b"]
  172. path_specific = []
  173. user_rules = []
  174. config = self._config(base_rules, path_specific, user_rules)
  175. self.assertTrue(config.should_check("a", "path"))
  176. self.assertFalse(config.should_check("b", "path"))
  177. def test_path_specific(self):
  178. """Test effect of path_rules_specifier on should_check()."""
  179. base_rules = ["-"]
  180. path_specific = [(["path1"], ["+b"]),
  181. (["path2"], ["+c"])]
  182. user_rules = []
  183. config = self._config(base_rules, path_specific, user_rules)
  184. self.assertFalse(config.should_check("c", "path1"))
  185. self.assertTrue(config.should_check("c", "path2"))
  186. # Test that first match takes precedence.
  187. self.assertFalse(config.should_check("c", "path2/path1"))
  188. def test_path_with_different_case(self):
  189. """Test a path that differs only in case."""
  190. base_rules = ["-"]
  191. path_specific = [(["Foo/"], ["+whitespace"])]
  192. user_rules = []
  193. config = self._config(base_rules, path_specific, user_rules)
  194. self.assertFalse(config.should_check("whitespace", "Fooo/bar.txt"))
  195. self.assertTrue(config.should_check("whitespace", "Foo/bar.txt"))
  196. # Test different case.
  197. self.assertTrue(config.should_check("whitespace", "FOO/bar.txt"))
  198. def test_user_rules(self):
  199. """Test effect of user_rules on should_check()."""
  200. base_rules = ["-"]
  201. path_specific = []
  202. user_rules = ["+b"]
  203. config = self._config(base_rules, path_specific, user_rules)
  204. self.assertFalse(config.should_check("a", "path"))
  205. self.assertTrue(config.should_check("b", "path"))