Changeset 54234 in webkit


Ignore:
Timestamp:
Feb 2, 2010 7:28:11 AM (14 years ago)
Author:
Chris Jerdonek
Message:

2010-02-02 Chris Jerdonek <Chris Jerdonek>

Reviewed by Shinichiro Hamaji.

Moved filter-related check-webkit-style code into a separate
filter module.

https://bugs.webkit.org/show_bug.cgi?id=34408

This is preparatory refactoring for Bug 33684, which will allow
file and folder-specific filter rules.

  • Scripts/webkitpy/style/checker.py:
    • Removed CategoryFilter class (moved to filter.py).
  • Scripts/webkitpy/style/checker_unittest.py:
    • Removed CategoryFilter unit tests (moved to filter_unittest.py).
  • Scripts/webkitpy/style/filter.py: Added.
    • Added CategoryFilter class (moved from checker.py).
  • Scripts/webkitpy/style/filter_unittest.py: Added.
    • Added CategoryFilter unit tests (moved from checker_unittest.py).
  • Scripts/webkitpy/style/unittests.py:
    • Added reference to filter_unittest.py.
Location:
trunk/WebKitTools
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r54206 r54234  
     12010-02-02  Chris Jerdonek  <cjerdonek@webkit.org>
     2
     3        Reviewed by Shinichiro Hamaji.
     4
     5        Moved filter-related check-webkit-style code into a separate
     6        filter module.
     7
     8        https://bugs.webkit.org/show_bug.cgi?id=34408
     9
     10        This is preparatory refactoring for Bug 33684, which will allow
     11        file and folder-specific filter rules.
     12
     13        * Scripts/webkitpy/style/checker.py:
     14          - Removed CategoryFilter class (moved to filter.py).
     15
     16        * Scripts/webkitpy/style/checker_unittest.py:
     17          - Removed CategoryFilter unit tests (moved to filter_unittest.py).
     18
     19        * Scripts/webkitpy/style/filter.py: Added.
     20          - Added CategoryFilter class (moved from checker.py).
     21
     22        * Scripts/webkitpy/style/filter_unittest.py: Added.
     23          - Added CategoryFilter unit tests (moved from checker_unittest.py).
     24
     25        * Scripts/webkitpy/style/unittests.py:
     26          - Added reference to filter_unittest.py.
     27
    1282010-02-01  Chris Jerdonek  <cjerdonek@webkit.org>
    229
  • trunk/WebKitTools/Scripts/webkitpy/style/checker.py

    r54206 r54234  
    3838from error_handlers import DefaultStyleErrorHandler
    3939from error_handlers import PatchStyleErrorHandler
     40from filter import CategoryFilter
    4041from processors.common import check_no_carriage_return
    4142from processors.common import categories as CommonCategories
     
    109110
    110111
    111 # FIXME: Check that the keys are in _style_categories().
    112 #
    113112# The maximum number of errors to report per file, per category.
    114113# If a category is not a key, then it has no maximum.
     
    201200
    202201    return usage
    203 
    204 
    205 class CategoryFilter(object):
    206 
    207     """Filters whether to check style categories."""
    208 
    209     def __init__(self, filter_rules=None):
    210         """Create a category filter.
    211 
    212         This method performs argument validation but does not strip
    213         leading or trailing white space.
    214 
    215         Args:
    216           filter_rules: A list of strings that are filter rules, which
    217                         are strings beginning with the plus or minus
    218                         symbol (+/-). The list should include any
    219                         default filter rules at the beginning.
    220                         Defaults to the empty list.
    221 
    222         Raises:
    223           ValueError: Invalid filter rule if a rule does not start with
    224                       plus ("+") or minus ("-").
    225 
    226         """
    227         if filter_rules is None:
    228             filter_rules = []
    229 
    230         for rule in filter_rules:
    231             if not (rule.startswith('+') or rule.startswith('-')):
    232                 raise ValueError('Invalid filter rule "%s": every rule '
    233                                  'rule in the --filter flag must start '
    234                                  'with + or -.' % rule)
    235 
    236         self._filter_rules = filter_rules
    237         self._should_check_category = {} # Cached dictionary of category to True/False
    238 
    239     def __str__(self):
    240         return ",".join(self._filter_rules)
    241 
    242     # Useful for unit testing.
    243     def __eq__(self, other):
    244         """Return whether this CategoryFilter instance is equal to another."""
    245         return self._filter_rules == other._filter_rules
    246 
    247     # Useful for unit testing.
    248     def __ne__(self, other):
    249         # Python does not automatically deduce from __eq__().
    250         return not (self == other)
    251 
    252     def should_check(self, category):
    253         """Return whether the category should be checked.
    254 
    255         The rules for determining whether a category should be checked
    256         are as follows. By default all categories should be checked.
    257         Then apply the filter rules in order from first to last, with
    258         later flags taking precedence.
    259 
    260         A filter rule applies to a category if the string after the
    261         leading plus/minus (+/-) matches the beginning of the category
    262         name. A plus (+) means the category should be checked, while a
    263         minus (-) means the category should not be checked.
    264 
    265         """
    266         if category in self._should_check_category:
    267             return self._should_check_category[category]
    268 
    269         should_check = True # All categories checked by default.
    270         for rule in self._filter_rules:
    271             if not category.startswith(rule[1:]):
    272                 continue
    273             should_check = rule.startswith('+')
    274         self._should_check_category[category] = should_check # Update cache.
    275         return should_check
    276202
    277203
  • trunk/WebKitTools/Scripts/webkitpy/style/checker_unittest.py

    r54206 r54234  
    3838
    3939import checker as style
    40 from checker import CategoryFilter
    4140from checker import ProcessorDispatcher
    4241from checker import ProcessorOptions
    4342from checker import StyleChecker
     43from filter import CategoryFilter
    4444from processors.cpp import CppProcessor
    4545from processors.text import TextProcessor
    46 
    47 class CategoryFilterTest(unittest.TestCase):
    48 
    49     """Tests CategoryFilter class."""
    50 
    51     def test_init(self):
    52         """Test __init__ constructor."""
    53         self.assertRaises(ValueError, CategoryFilter, ["no_prefix"])
    54         CategoryFilter() # No ValueError: works
    55         CategoryFilter(["+"]) # No ValueError: works
    56         CategoryFilter(["-"]) # No ValueError: works
    57 
    58     def test_str(self):
    59         """Test __str__ "to string" operator."""
    60         filter = CategoryFilter(["+a", "-b"])
    61         self.assertEquals(str(filter), "+a,-b")
    62 
    63     def test_eq(self):
    64         """Test __eq__ equality function."""
    65         filter1 = CategoryFilter(["+a", "+b"])
    66         filter2 = CategoryFilter(["+a", "+b"])
    67         filter3 = CategoryFilter(["+b", "+a"])
    68 
    69         # == calls __eq__.
    70         self.assertTrue(filter1 == filter2)
    71         self.assertFalse(filter1 == filter3) # Cannot test with assertNotEqual.
    72 
    73     def test_ne(self):
    74         """Test __ne__ inequality function."""
    75         # != calls __ne__.
    76         # By default, __ne__ always returns true on different objects.
    77         # Thus, just check the distinguishing case to verify that the
    78         # code defines __ne__.
    79         self.assertFalse(CategoryFilter() != CategoryFilter())
    80 
    81     def test_should_check(self):
    82         """Test should_check() method."""
    83         filter = CategoryFilter()
    84         self.assertTrue(filter.should_check("everything"))
    85         # Check a second time to exercise cache.
    86         self.assertTrue(filter.should_check("everything"))
    87 
    88         filter = CategoryFilter(["-"])
    89         self.assertFalse(filter.should_check("anything"))
    90         # Check a second time to exercise cache.
    91         self.assertFalse(filter.should_check("anything"))
    92 
    93         filter = CategoryFilter(["-", "+ab"])
    94         self.assertTrue(filter.should_check("abc"))
    95         self.assertFalse(filter.should_check("a"))
    96 
    97         filter = CategoryFilter(["+", "-ab"])
    98         self.assertFalse(filter.should_check("abc"))
    99         self.assertTrue(filter.should_check("a"))
    10046
    10147
  • trunk/WebKitTools/Scripts/webkitpy/style/unittests.py

    r54206 r54234  
    3838from checker_unittest import *
    3939from error_handlers_unittest import *
     40from filter_unittest import *
    4041from processors.common_unittest import *
    4142from processors.cpp_unittest import *
Note: See TracChangeset for help on using the changeset viewer.