summaryrefslogtreecommitdiffstats
path: root/WebKitTools/Scripts/webkitpy/style/filter_unittest.py
blob: 7b8a5402a8b23df79b211aaf9853261f7c818110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Unit tests for filter.py."""

import unittest

from filter import _CategoryFilter as CategoryFilter
from filter import validate_filter_rules
from filter import FilterConfiguration

# On Testing __eq__() and __ne__():
#
# In the tests below, we deliberately do not use assertEquals() or
# assertNotEquals() to test __eq__() or __ne__().  We do this to be
# very explicit about what we are testing, especially in the case
# of assertNotEquals().
#
# Part of the reason is that it is not immediately clear what
# expression the unittest module uses to assert "not equals" -- the
# negation of __eq__() or __ne__(), which are not necessarily
# equivalent expresions in Python.  For example, from Python's "Data
# Model" documentation--
#
#   "There are no implied relationships among the comparison
#    operators. The truth of x==y does not imply that x!=y is
#    false.  Accordingly, when defining __eq__(), one should
#    also define __ne__() so that the operators will behave as
#    expected."
#
#   (from http://docs.python.org/reference/datamodel.html#object.__ne__ )

class ValidateFilterRulesTest(unittest.TestCase):

    """Tests validate_filter_rules() function."""

    def test_validate_filter_rules(self):
        all_categories = ["tabs", "whitespace", "build/include"]

        bad_rules = [
            "tabs",
            "*tabs",
            " tabs",
            " +tabs",
            "+whitespace/newline",
            "+xxx",
            ]

        good_rules = [
            "+tabs",
            "-tabs",
            "+build"
            ]

        for rule in bad_rules:
            self.assertRaises(ValueError, validate_filter_rules,
                              [rule], all_categories)

        for rule in good_rules:
            # This works: no error.
            validate_filter_rules([rule], all_categories)


class CategoryFilterTest(unittest.TestCase):

    """Tests CategoryFilter class."""

    def test_init(self):
        """Test __init__ method."""
        # Test that the attributes are getting set correctly.
        filter = CategoryFilter(["+"])
        self.assertEquals(["+"], filter._filter_rules)

    def test_init_default_arguments(self):
        """Test __init__ method default arguments."""
        filter = CategoryFilter()
        self.assertEquals([], filter._filter_rules)

    def test_str(self):
        """Test __str__ "to string" operator."""
        filter = CategoryFilter(["+a", "-b"])
        self.assertEquals(str(filter), "+a,-b")

    def test_eq(self):
        """Test __eq__ equality function."""
        filter1 = CategoryFilter(["+a", "+b"])
        filter2 = CategoryFilter(["+a", "+b"])
        filter3 = CategoryFilter(["+b", "+a"])

        # See the notes at the top of this module about testing
        # __eq__() and __ne__().
        self.assertTrue(filter1.__eq__(filter2))
        self.assertFalse(filter1.__eq__(filter3))

    def test_ne(self):
        """Test __ne__ inequality function."""
        # By default, __ne__ always returns true on different objects.
        # Thus, just check the distinguishing case to verify that the
        # code defines __ne__.
        #
        # Also, see the notes at the top of this module about testing
        # __eq__() and __ne__().
        self.assertFalse(CategoryFilter().__ne__(CategoryFilter()))

    def test_should_check(self):
        """Test should_check() method."""
        filter = CategoryFilter()
        self.assertTrue(filter.should_check("everything"))
        # Check a second time to exercise cache.
        self.assertTrue(filter.should_check("everything"))

        filter = CategoryFilter(["-"])
        self.assertFalse(filter.should_check("anything"))
        # Check a second time to exercise cache.
        self.assertFalse(filter.should_check("anything"))

        filter = CategoryFilter(["-", "+ab"])
        self.assertTrue(filter.should_check("abc"))
        self.assertFalse(filter.should_check("a"))

        filter = CategoryFilter(["+", "-ab"])
        self.assertFalse(filter.should_check("abc"))
        self.assertTrue(filter.should_check("a"))


class FilterConfigurationTest(unittest.TestCase):

    """Tests FilterConfiguration class."""

    def _config(self, base_rules, path_specific, user_rules):
        """Return a FilterConfiguration instance."""
        return FilterConfiguration(base_rules=base_rules,
                                   path_specific=path_specific,
                                   user_rules=user_rules)

    def test_init(self):
        """Test __init__ method."""
        # Test that the attributes are getting set correctly.
        # We use parameter values that are different from the defaults.
        base_rules = ["-"]
        path_specific = [(["path"], ["+a"])]
        user_rules = ["+"]

        config = self._config(base_rules, path_specific, user_rules)

        self.assertEquals(base_rules, config._base_rules)
        self.assertEquals(path_specific, config._path_specific)
        self.assertEquals(user_rules, config._user_rules)

    def test_default_arguments(self):
        # Test that the attributes are getting set correctly to the defaults.
        config = FilterConfiguration()

        self.assertEquals([], config._base_rules)
        self.assertEquals([], config._path_specific)
        self.assertEquals([], config._user_rules)

    def test_eq(self):
        """Test __eq__ method."""
        # See the notes at the top of this module about testing
        # __eq__() and __ne__().
        self.assertTrue(FilterConfiguration().__eq__(FilterConfiguration()))

        # Verify that a difference in any argument causes equality to fail.
        config = FilterConfiguration()

        # These parameter values are different from the defaults.
        base_rules = ["-"]
        path_specific = [(["path"], ["+a"])]
        user_rules = ["+"]

        self.assertFalse(config.__eq__(FilterConfiguration(
                                           base_rules=base_rules)))
        self.assertFalse(config.__eq__(FilterConfiguration(
                                           path_specific=path_specific)))
        self.assertFalse(config.__eq__(FilterConfiguration(
                                           user_rules=user_rules)))

    def test_ne(self):
        """Test __ne__ method."""
        # By default, __ne__ always returns true on different objects.
        # Thus, just check the distinguishing case to verify that the
        # code defines __ne__.
        #
        # Also, see the notes at the top of this module about testing
        # __eq__() and __ne__().
        self.assertFalse(FilterConfiguration().__ne__(FilterConfiguration()))

    def test_base_rules(self):
        """Test effect of base_rules on should_check()."""
        base_rules = ["-b"]
        path_specific = []
        user_rules = []

        config = self._config(base_rules, path_specific, user_rules)

        self.assertTrue(config.should_check("a", "path"))
        self.assertFalse(config.should_check("b", "path"))

    def test_path_specific(self):
        """Test effect of path_rules_specifier on should_check()."""
        base_rules = ["-"]
        path_specific = [(["path1"], ["+b"]),
                         (["path2"], ["+c"])]
        user_rules = []

        config = self._config(base_rules, path_specific, user_rules)

        self.assertFalse(config.should_check("c", "path1"))
        self.assertTrue(config.should_check("c", "path2"))
        # Test that first match takes precedence.
        self.assertFalse(config.should_check("c", "path2/path1"))

    def test_path_with_different_case(self):
        """Test a path that differs only in case."""
        base_rules = ["-"]
        path_specific = [(["Foo/"], ["+whitespace"])]
        user_rules = []

        config = self._config(base_rules, path_specific, user_rules)

        self.assertFalse(config.should_check("whitespace", "Fooo/bar.txt"))
        self.assertTrue(config.should_check("whitespace", "Foo/bar.txt"))
        # Test different case.
        self.assertTrue(config.should_check("whitespace", "FOO/bar.txt"))

    def test_user_rules(self):
        """Test effect of user_rules on should_check()."""
        base_rules = ["-"]
        path_specific = []
        user_rules = ["+b"]

        config = self._config(base_rules, path_specific, user_rules)

        self.assertFalse(config.should_check("a", "path"))
        self.assertTrue(config.should_check("b", "path"))