2009-12-18 Fumitoshi Ukai <ukai@chromium.org>
[WebKit-https.git] / WebKitTools / Scripts / modules / cpp_style_unittest.py
1 #!/usr/bin/python
2 # -*- coding: utf-8; -*-
3 #
4 # Copyright (C) 2009 Google Inc. All rights reserved.
5 # Copyright (C) 2009 Torch Mobile Inc.
6 # Copyright (C) 2009 Apple Inc. All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
12 #    * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 #    * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following disclaimer
16 # in the documentation and/or other materials provided with the
17 # distribution.
18 #    * Neither the name of Google Inc. nor the names of its
19 # contributors may be used to endorse or promote products derived from
20 # this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 """Unit test for cpp_style.py."""
35
36 # FIXME: Add a good test that tests UpdateIncludeState.
37
38 import codecs
39 import os
40 import random
41 import re
42 import unittest
43 import cpp_style
44
45
46 # This class works as an error collector and replaces cpp_style.Error
47 # function for the unit tests.  We also verify each category we see
48 # is in cpp_style._STYLE_CATEGORIES, to help keep that list up to date.
49 class ErrorCollector:
50     _all_style_categories = cpp_style._STYLE_CATEGORIES
51     # This a list including all categories seen in any unit test.
52     _seen_style_categories = {}
53
54     def __init__(self, assert_fn):
55         """assert_fn: a function to call when we notice a problem."""
56         self._assert_fn = assert_fn
57         self._errors = []
58
59     def __call__(self, unused_filename, unused_linenum,
60                  category, confidence, message):
61         self._assert_fn(category in self._all_style_categories,
62                         'Message "%s" has category "%s",'
63                         ' which is not in _STYLE_CATEGORIES' % (message, category))
64         self._seen_style_categories[category] = 1
65         if cpp_style._should_print_error(category, confidence):
66             self._errors.append('%s  [%s] [%d]' % (message, category, confidence))
67
68     def results(self):
69         if len(self._errors) < 2:
70             return ''.join(self._errors)  # Most tests expect to have a string.
71         else:
72             return self._errors  # Let's give a list if there is more than one.
73
74     def result_list(self):
75         return self._errors
76
77     def verify_all_categories_are_seen(self):
78         """Fails if there's a category in _all_style_categories - _seen_style_categories.
79
80         This should only be called after all tests are run, so
81         _seen_style_categories has had a chance to fully populate.  Since
82         this isn't called from within the normal unittest framework, we
83         can't use the normal unittest assert macros.  Instead we just exit
84         when we see an error.  Good thing this test is always run last!
85         """
86         for category in self._all_style_categories:
87             if category not in self._seen_style_categories:
88                 import sys
89                 sys.exit('FATAL ERROR: There are no tests for category "%s"' % category)
90
91     def remove_if_present(self, substr):
92         for (index, error) in enumerate(self._errors):
93             if error.find(substr) != -1:
94                 self._errors = self._errors[0:index] + self._errors[(index + 1):]
95                 break
96
97
98 # This class is a lame mock of codecs. We do not verify filename, mode, or
99 # encoding, but for the current use case it is not needed.
100 class MockIo:
101     def __init__(self, mock_file):
102         self.mock_file = mock_file
103
104     def open(self, unused_filename, unused_mode, unused_encoding, _):  # NOLINT
105         # (lint doesn't like open as a method name)
106         return self.mock_file
107
108
109 class CppStyleTestBase(unittest.TestCase):
110     """Provides some useful helper functions for cpp_style tests."""
111
112     # Perform lint on single line of input and return the error message.
113     def perform_single_line_lint(self, code, file_name):
114         error_collector = ErrorCollector(self.assert_)
115         lines = code.split('\n')
116         cpp_style.remove_multi_line_comments(file_name, lines, error_collector)
117         clean_lines = cpp_style.CleansedLines(lines)
118         include_state = cpp_style._IncludeState()
119         function_state = cpp_style._FunctionState()
120         ext = file_name[file_name.rfind('.') + 1:]
121         class_state = cpp_style._ClassState()
122         file_state = cpp_style._FileState()
123         cpp_style.process_line(file_name, ext, clean_lines, 0,
124                                include_state, function_state,
125                                class_state, file_state, error_collector)
126         # Single-line lint tests are allowed to fail the 'unlintable function'
127         # check.
128         error_collector.remove_if_present(
129             'Lint failed to find start of function body.')
130         return error_collector.results()
131
132     # Perform lint over multiple lines and return the error message.
133     def perform_multi_line_lint(self, code, file_name):
134         error_collector = ErrorCollector(self.assert_)
135         lines = code.split('\n')
136         cpp_style.remove_multi_line_comments(file_name, lines, error_collector)
137         lines = cpp_style.CleansedLines(lines)
138         ext = file_name[file_name.rfind('.') + 1:]
139         class_state = cpp_style._ClassState()
140         file_state = cpp_style._FileState()
141         for i in xrange(lines.num_lines()):
142             cpp_style.check_style(file_name, lines, i, ext, file_state, error_collector)
143             cpp_style.check_for_non_standard_constructs(file_name, lines, i, class_state,
144                                                         error_collector)
145         class_state.check_finished(file_name, error_collector)
146         return error_collector.results()
147
148     # Similar to perform_multi_line_lint, but calls check_language instead of
149     # check_for_non_standard_constructs
150     def perform_language_rules_check(self, file_name, code):
151         error_collector = ErrorCollector(self.assert_)
152         include_state = cpp_style._IncludeState()
153         lines = code.split('\n')
154         cpp_style.remove_multi_line_comments(file_name, lines, error_collector)
155         lines = cpp_style.CleansedLines(lines)
156         ext = file_name[file_name.rfind('.') + 1:]
157         for i in xrange(lines.num_lines()):
158             cpp_style.check_language(file_name, lines, i, ext, include_state,
159                                      error_collector)
160         return error_collector.results()
161
162     def perform_function_lengths_check(self, code):
163         """Perform Lint function length check on block of code and return warnings.
164
165         Builds up an array of lines corresponding to the code and strips comments
166         using cpp_style functions.
167
168         Establishes an error collector and invokes the function length checking
169         function following cpp_style's pattern.
170
171         Args:
172           code: C++ source code expected to generate a warning message.
173
174         Returns:
175           The accumulated errors.
176         """
177         file_name = 'foo.cpp'
178         error_collector = ErrorCollector(self.assert_)
179         function_state = cpp_style._FunctionState()
180         lines = code.split('\n')
181         cpp_style.remove_multi_line_comments(file_name, lines, error_collector)
182         lines = cpp_style.CleansedLines(lines)
183         for i in xrange(lines.num_lines()):
184             cpp_style.check_for_function_lengths(file_name, lines, i,
185                                                  function_state, error_collector)
186         return error_collector.results()
187
188     def perform_include_what_you_use(self, code, filename='foo.h', io=codecs):
189         # First, build up the include state.
190         error_collector = ErrorCollector(self.assert_)
191         include_state = cpp_style._IncludeState()
192         lines = code.split('\n')
193         cpp_style.remove_multi_line_comments(filename, lines, error_collector)
194         lines = cpp_style.CleansedLines(lines)
195         for i in xrange(lines.num_lines()):
196             cpp_style.check_language(filename, lines, i, '.h', include_state,
197                                      error_collector)
198         # We could clear the error_collector here, but this should
199         # also be fine, since our IncludeWhatYouUse unittests do not
200         # have language problems.
201
202         # Second, look for missing includes.
203         cpp_style.check_for_include_what_you_use(filename, lines, include_state,
204                                                  error_collector, io)
205         return error_collector.results()
206
207     # Perform lint and compare the error message with "expected_message".
208     def assert_lint(self, code, expected_message, file_name='foo.cpp'):
209         self.assertEquals(expected_message, self.perform_single_line_lint(code, file_name))
210
211     def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_name='foo.cpp'):
212         messages = self.perform_single_line_lint(code, file_name)
213         for message in messages:
214             if re.search(expected_message_re, message):
215                 return
216
217         self.assertEquals(expected_message, messages)
218
219     def assert_multi_line_lint(self, code, expected_message, file_name='foo.h'):
220         self.assertEquals(expected_message, self.perform_multi_line_lint(code, file_name))
221
222     def assert_multi_line_lint_re(self, code, expected_message_re, file_name='foo.h'):
223         message = self.perform_multi_line_lint(code, file_name)
224         if not re.search(expected_message_re, message):
225             self.fail('Message was:\n' + message + 'Expected match to "' + expected_message_re + '"')
226
227     def assert_language_rules_check(self, file_name, code, expected_message):
228         self.assertEquals(expected_message,
229                           self.perform_language_rules_check(file_name, code))
230
231     def assert_include_what_you_use(self, code, expected_message):
232         self.assertEquals(expected_message,
233                           self.perform_include_what_you_use(code))
234
235     def assert_blank_lines_check(self, lines, start_errors, end_errors):
236         error_collector = ErrorCollector(self.assert_)
237         cpp_style.process_file_data('foo.cpp', 'cpp', lines, error_collector)
238         self.assertEquals(
239             start_errors,
240             error_collector.results().count(
241                 'Blank line at the start of a code block.  Is this needed?'
242                 '  [whitespace/blank_line] [2]'))
243         self.assertEquals(
244             end_errors,
245             error_collector.results().count(
246                 'Blank line at the end of a code block.  Is this needed?'
247                 '  [whitespace/blank_line] [3]'))
248
249
250 class CppStyleTest(CppStyleTestBase):
251
252     # Test get line width.
253     def test_get_line_width(self):
254         self.assertEquals(0, cpp_style.get_line_width(''))
255         self.assertEquals(10, cpp_style.get_line_width(u'x' * 10))
256         self.assertEquals(16, cpp_style.get_line_width(u'都|道|府|県|支庁'))
257
258     def test_find_next_multi_line_comment_start(self):
259         self.assertEquals(1, cpp_style.find_next_multi_line_comment_start([''], 0))
260
261         lines = ['a', 'b', '/* c']
262         self.assertEquals(2, cpp_style.find_next_multi_line_comment_start(lines, 0))
263
264         lines = ['char a[] = "/*";']  # not recognized as comment.
265         self.assertEquals(1, cpp_style.find_next_multi_line_comment_start(lines, 0))
266
267     def test_find_next_multi_line_comment_end(self):
268         self.assertEquals(1, cpp_style.find_next_multi_line_comment_end([''], 0))
269         lines = ['a', 'b', ' c */']
270         self.assertEquals(2, cpp_style.find_next_multi_line_comment_end(lines, 0))
271
272     def test_remove_multi_line_comments_from_range(self):
273         lines = ['a', '  /* comment ', ' * still comment', ' comment */   ', 'b']
274         cpp_style.remove_multi_line_comments_from_range(lines, 1, 4)
275         self.assertEquals(['a', '// dummy', '// dummy', '// dummy', 'b'], lines)
276
277     def test_spaces_at_end_of_line(self):
278         self.assert_lint(
279             '// Hello there ',
280             'Line ends in whitespace.  Consider deleting these extra spaces.'
281             '  [whitespace/end_of_line] [4]')
282
283     # Test C-style cast cases.
284     def test_cstyle_cast(self):
285         self.assert_lint(
286             'int a = (int)1.0;',
287             'Using C-style cast.  Use static_cast<int>(...) instead'
288             '  [readability/casting] [4]')
289         self.assert_lint(
290             'int *a = (int *)DEFINED_VALUE;',
291             'Using C-style cast.  Use reinterpret_cast<int *>(...) instead'
292             '  [readability/casting] [4]', 'foo.c')
293         self.assert_lint(
294             'uint16 a = (uint16)1.0;',
295             'Using C-style cast.  Use static_cast<uint16>(...) instead'
296             '  [readability/casting] [4]')
297         self.assert_lint(
298             'int32 a = (int32)1.0;',
299             'Using C-style cast.  Use static_cast<int32>(...) instead'
300             '  [readability/casting] [4]')
301         self.assert_lint(
302             'uint64 a = (uint64)1.0;',
303             'Using C-style cast.  Use static_cast<uint64>(...) instead'
304             '  [readability/casting] [4]')
305
306     # Test taking address of casts (runtime/casting)
307     def test_runtime_casting(self):
308         self.assert_lint(
309             'int* x = &static_cast<int*>(foo);',
310             'Are you taking an address of a cast?  '
311             'This is dangerous: could be a temp var.  '
312             'Take the address before doing the cast, rather than after'
313             '  [runtime/casting] [4]')
314
315         self.assert_lint(
316             'int* x = &dynamic_cast<int *>(foo);',
317             ['Are you taking an address of a cast?  '
318              'This is dangerous: could be a temp var.  '
319              'Take the address before doing the cast, rather than after'
320              '  [runtime/casting] [4]',
321              'Do not use dynamic_cast<>.  If you need to cast within a class '
322              'hierarchy, use static_cast<> to upcast.  Google doesn\'t support '
323              'RTTI.  [runtime/rtti] [5]'])
324
325         self.assert_lint(
326             'int* x = &reinterpret_cast<int *>(foo);',
327             'Are you taking an address of a cast?  '
328             'This is dangerous: could be a temp var.  '
329             'Take the address before doing the cast, rather than after'
330             '  [runtime/casting] [4]')
331
332         # It's OK to cast an address.
333         self.assert_lint(
334             'int* x = reinterpret_cast<int *>(&foo);',
335             '')
336
337     def test_runtime_selfinit(self):
338         self.assert_lint(
339             'Foo::Foo(Bar r, Bel l) : r_(r_), l_(l_) { }',
340             'You seem to be initializing a member variable with itself.'
341             '  [runtime/init] [4]')
342         self.assert_lint(
343             'Foo::Foo(Bar r, Bel l) : r_(r), l_(l) { }',
344             '')
345         self.assert_lint(
346             'Foo::Foo(Bar r) : r_(r), l_(r_), ll_(l_) { }',
347             '')
348
349     def test_runtime_rtti(self):
350         statement = 'int* x = dynamic_cast<int*>(&foo);'
351         error_message = (
352             'Do not use dynamic_cast<>.  If you need to cast within a class '
353             'hierarchy, use static_cast<> to upcast.  Google doesn\'t support '
354             'RTTI.  [runtime/rtti] [5]')
355         # dynamic_cast is disallowed in most files.
356         self.assert_language_rules_check('foo.cpp', statement, error_message)
357         self.assert_language_rules_check('foo.h', statement, error_message)
358         # It is explicitly allowed in tests, however.
359         self.assert_language_rules_check('foo_test.cpp', statement, '')
360         self.assert_language_rules_check('foo_unittest.cpp', statement, '')
361         self.assert_language_rules_check('foo_regtest.cpp', statement, '')
362
363     # We cannot test this functionality because of difference of
364     # function definitions.  Anyway, we may never enable this.
365     #
366     # # Test for unnamed arguments in a method.
367     # def test_check_for_unnamed_params(self):
368     #   message = ('All parameters should be named in a function'
369     #              '  [readability/function] [3]')
370     #   self.assert_lint('virtual void A(int*) const;', message)
371     #   self.assert_lint('virtual void B(void (*fn)(int*));', message)
372     #   self.assert_lint('virtual void C(int*);', message)
373     #   self.assert_lint('void *(*f)(void *) = x;', message)
374     #   self.assert_lint('void Method(char*) {', message)
375     #   self.assert_lint('void Method(char*);', message)
376     #   self.assert_lint('void Method(char* /*x*/);', message)
377     #   self.assert_lint('typedef void (*Method)(int32);', message)
378     #   self.assert_lint('static void operator delete[](void*) throw();', message)
379     # 
380     #   self.assert_lint('virtual void D(int* p);', '')
381     #   self.assert_lint('void operator delete(void* x) throw();', '')
382     #   self.assert_lint('void Method(char* x)\n{', '')
383     #   self.assert_lint('void Method(char* /*x*/)\n{', '')
384     #   self.assert_lint('void Method(char* x);', '')
385     #   self.assert_lint('typedef void (*Method)(int32 x);', '')
386     #   self.assert_lint('static void operator delete[](void* x) throw();', '')
387     #   self.assert_lint('static void operator delete[](void* /*x*/) throw();', '')
388     # 
389     #   # This one should technically warn, but doesn't because the function
390     #   # pointer is confusing.
391     #   self.assert_lint('virtual void E(void (*fn)(int* p));', '')
392
393     # Test deprecated casts such as int(d)
394     def test_deprecated_cast(self):
395         self.assert_lint(
396             'int a = int(2.2);',
397             'Using deprecated casting style.  '
398             'Use static_cast<int>(...) instead'
399             '  [readability/casting] [4]')
400         # Checks for false positives...
401         self.assert_lint(
402             'int a = int(); // Constructor, o.k.',
403             '')
404         self.assert_lint(
405             'X::X() : a(int()) {} // default Constructor, o.k.',
406             '')
407         self.assert_lint(
408             'operator bool(); // Conversion operator, o.k.',
409             '')
410
411     # The second parameter to a gMock method definition is a function signature
412     # that often looks like a bad cast but should not picked up by lint.
413     def test_mock_method(self):
414         self.assert_lint(
415             'MOCK_METHOD0(method, int());',
416             '')
417         self.assert_lint(
418             'MOCK_CONST_METHOD1(method, float(string));',
419             '')
420         self.assert_lint(
421             'MOCK_CONST_METHOD2_T(method, double(float, float));',
422             '')
423
424     # Test sizeof(type) cases.
425     def test_sizeof_type(self):
426         self.assert_lint(
427             'sizeof(int);',
428             'Using sizeof(type).  Use sizeof(varname) instead if possible'
429             '  [runtime/sizeof] [1]')
430         self.assert_lint(
431             'sizeof(int *);',
432             'Using sizeof(type).  Use sizeof(varname) instead if possible'
433             '  [runtime/sizeof] [1]')
434
435     # Test typedef cases.  There was a bug that cpp_style misidentified
436     # typedef for pointer to function as C-style cast and produced
437     # false-positive error messages.
438     def test_typedef_for_pointer_to_function(self):
439         self.assert_lint(
440             'typedef void (*Func)(int x);',
441             '')
442         self.assert_lint(
443             'typedef void (*Func)(int *x);',
444             '')
445         self.assert_lint(
446             'typedef void Func(int x);',
447             '')
448         self.assert_lint(
449             'typedef void Func(int *x);',
450             '')
451
452     def test_include_what_you_use_no_implementation_files(self):
453         code = 'std::vector<int> foo;'
454         self.assertEquals('Add #include <vector> for vector<>'
455                           '  [build/include_what_you_use] [4]',
456                           self.perform_include_what_you_use(code, 'foo.h'))
457         self.assertEquals('',
458                           self.perform_include_what_you_use(code, 'foo.cpp'))
459
460     def test_include_what_you_use(self):
461         self.assert_include_what_you_use(
462             '''#include <vector>
463                std::vector<int> foo;
464             ''',
465             '')
466         self.assert_include_what_you_use(
467             '''#include <map>
468                std::pair<int,int> foo;
469             ''',
470             '')
471         self.assert_include_what_you_use(
472             '''#include <multimap>
473                std::pair<int,int> foo;
474             ''',
475             '')
476         self.assert_include_what_you_use(
477             '''#include <hash_map>
478                std::pair<int,int> foo;
479             ''',
480             '')
481         self.assert_include_what_you_use(
482             '''#include <utility>
483                std::pair<int,int> foo;
484             ''',
485             '')
486         self.assert_include_what_you_use(
487             '''#include <vector>
488                DECLARE_string(foobar);
489             ''',
490             '')
491         self.assert_include_what_you_use(
492             '''#include <vector>
493                DEFINE_string(foobar, "", "");
494             ''',
495             '')
496         self.assert_include_what_you_use(
497             '''#include <vector>
498                std::pair<int,int> foo;
499             ''',
500             'Add #include <utility> for pair<>'
501             '  [build/include_what_you_use] [4]')
502         self.assert_include_what_you_use(
503             '''#include "base/foobar.h"
504                std::vector<int> foo;
505             ''',
506             'Add #include <vector> for vector<>'
507             '  [build/include_what_you_use] [4]')
508         self.assert_include_what_you_use(
509             '''#include <vector>
510                std::set<int> foo;
511             ''',
512             'Add #include <set> for set<>'
513             '  [build/include_what_you_use] [4]')
514         self.assert_include_what_you_use(
515             '''#include "base/foobar.h"
516               hash_map<int, int> foobar;
517             ''',
518             'Add #include <hash_map> for hash_map<>'
519             '  [build/include_what_you_use] [4]')
520         self.assert_include_what_you_use(
521             '''#include "base/foobar.h"
522                bool foobar = std::less<int>(0,1);
523             ''',
524             'Add #include <functional> for less<>'
525             '  [build/include_what_you_use] [4]')
526         self.assert_include_what_you_use(
527             '''#include "base/foobar.h"
528                bool foobar = min<int>(0,1);
529             ''',
530             'Add #include <algorithm> for min  [build/include_what_you_use] [4]')
531         self.assert_include_what_you_use(
532             'void a(const string &foobar);',
533             'Add #include <string> for string  [build/include_what_you_use] [4]')
534         self.assert_include_what_you_use(
535             '''#include "base/foobar.h"
536                bool foobar = swap(0,1);
537             ''',
538             'Add #include <algorithm> for swap  [build/include_what_you_use] [4]')
539         self.assert_include_what_you_use(
540             '''#include "base/foobar.h"
541                bool foobar = transform(a.begin(), a.end(), b.start(), Foo);
542             ''',
543             'Add #include <algorithm> for transform  '
544             '[build/include_what_you_use] [4]')
545         self.assert_include_what_you_use(
546             '''#include "base/foobar.h"
547                bool foobar = min_element(a.begin(), a.end());
548             ''',
549             'Add #include <algorithm> for min_element  '
550             '[build/include_what_you_use] [4]')
551         self.assert_include_what_you_use(
552             '''foo->swap(0,1);
553                foo.swap(0,1);
554             ''',
555             '')
556         self.assert_include_what_you_use(
557             '''#include <string>
558                void a(const std::multimap<int,string> &foobar);
559             ''',
560             'Add #include <map> for multimap<>'
561             '  [build/include_what_you_use] [4]')
562         self.assert_include_what_you_use(
563             '''#include <queue>
564                void a(const std::priority_queue<int> &foobar);
565             ''',
566             '')
567         self.assert_include_what_you_use(
568              '''#include "base/basictypes.h"
569                 #include "base/port.h"
570                 #include <assert.h>
571                 #include <string>
572                 #include <vector>
573                 vector<string> hajoa;''', '')
574         self.assert_include_what_you_use(
575             '''#include <string>
576                int i = numeric_limits<int>::max()
577             ''',
578             'Add #include <limits> for numeric_limits<>'
579             '  [build/include_what_you_use] [4]')
580         self.assert_include_what_you_use(
581             '''#include <limits>
582                int i = numeric_limits<int>::max()
583             ''',
584             '')
585
586         # Test the UpdateIncludeState code path.
587         mock_header_contents = ['#include "blah/foo.h"', '#include "blah/bar.h"']
588         message = self.perform_include_what_you_use(
589             '#include "config.h"\n'
590             '#include "blah/a.h"\n',
591             filename='blah/a.cpp',
592             io=MockIo(mock_header_contents))
593         self.assertEquals(message, '')
594
595         mock_header_contents = ['#include <set>']
596         message = self.perform_include_what_you_use(
597             '''#include "config.h"
598                #include "blah/a.h"
599
600                std::set<int> foo;''',
601             filename='blah/a.cpp',
602             io=MockIo(mock_header_contents))
603         self.assertEquals(message, '')
604
605         # If there's just a .cpp and the header can't be found then it's ok.
606         message = self.perform_include_what_you_use(
607             '''#include "config.h"
608                #include "blah/a.h"
609
610                std::set<int> foo;''',
611             filename='blah/a.cpp')
612         self.assertEquals(message, '')
613
614         # Make sure we find the headers with relative paths.
615         mock_header_contents = ['']
616         message = self.perform_include_what_you_use(
617             '''#include "config.h"
618                #include "%s/a.h"
619
620                std::set<int> foo;''' % os.path.basename(os.getcwd()),
621             filename='a.cpp',
622             io=MockIo(mock_header_contents))
623         self.assertEquals(message, 'Add #include <set> for set<>  '
624                                    '[build/include_what_you_use] [4]')
625
626     def test_files_belong_to_same_module(self):
627         f = cpp_style.files_belong_to_same_module
628         self.assertEquals((True, ''), f('a.cpp', 'a.h'))
629         self.assertEquals((True, ''), f('base/google.cpp', 'base/google.h'))
630         self.assertEquals((True, ''), f('base/google_test.cpp', 'base/google.h'))
631         self.assertEquals((True, ''),
632                           f('base/google_unittest.cpp', 'base/google.h'))
633         self.assertEquals((True, ''),
634                           f('base/internal/google_unittest.cpp',
635                             'base/public/google.h'))
636         self.assertEquals((True, 'xxx/yyy/'),
637                           f('xxx/yyy/base/internal/google_unittest.cpp',
638                             'base/public/google.h'))
639         self.assertEquals((True, 'xxx/yyy/'),
640                           f('xxx/yyy/base/google_unittest.cpp',
641                             'base/public/google.h'))
642         self.assertEquals((True, ''),
643                           f('base/google_unittest.cpp', 'base/google-inl.h'))
644         self.assertEquals((True, '/home/build/google3/'),
645                           f('/home/build/google3/base/google.cpp', 'base/google.h'))
646
647         self.assertEquals((False, ''),
648                           f('/home/build/google3/base/google.cpp', 'basu/google.h'))
649         self.assertEquals((False, ''), f('a.cpp', 'b.h'))
650
651     def test_cleanse_line(self):
652         self.assertEquals('int foo = 0;  ',
653                           cpp_style.cleanse_comments('int foo = 0;  // danger!'))
654         self.assertEquals('int o = 0;',
655                           cpp_style.cleanse_comments('int /* foo */ o = 0;'))
656         self.assertEquals('foo(int a, int b);',
657                           cpp_style.cleanse_comments('foo(int a /* abc */, int b);'))
658         self.assertEqual('f(a, b);',
659                          cpp_style.cleanse_comments('f(a, /* name */ b);'))
660         self.assertEqual('f(a, b);',
661                          cpp_style.cleanse_comments('f(a /* name */, b);'))
662         self.assertEqual('f(a, b);',
663                          cpp_style.cleanse_comments('f(a, /* name */b);'))
664
665     def test_multi_line_comments(self):
666         # missing explicit is bad
667         self.assert_multi_line_lint(
668             r'''int a = 0;
669                 /* multi-liner
670                 class Foo {
671                 Foo(int f);  // should cause a lint warning in code
672                 }
673             */ ''',
674         '')
675         self.assert_multi_line_lint(
676             r'''/* int a = 0; multi-liner
677             static const int b = 0;''',
678       'Could not find end of multi-line comment'
679       '  [readability/multiline_comment] [5]')
680         self.assert_multi_line_lint(r'''    /* multi-line comment''',
681                                     'Could not find end of multi-line comment'
682                                     '  [readability/multiline_comment] [5]')
683         self.assert_multi_line_lint(r'''    // /* comment, but not multi-line''', '')
684
685     def test_multiline_strings(self):
686         multiline_string_error_message = (
687             'Multi-line string ("...") found.  This lint script doesn\'t '
688             'do well with such strings, and may give bogus warnings.  They\'re '
689             'ugly and unnecessary, and you should use concatenation instead".'
690             '  [readability/multiline_string] [5]')
691
692         file_path = 'mydir/foo.cpp'
693
694         error_collector = ErrorCollector(self.assert_)
695         cpp_style.process_file_data(file_path, 'cpp',
696                                     ['const char* str = "This is a\\',
697                                      ' multiline string.";'],
698                                     error_collector)
699         self.assertEquals(
700             2,  # One per line.
701             error_collector.result_list().count(multiline_string_error_message))
702
703     # Test non-explicit single-argument constructors
704     def test_explicit_single_argument_constructors(self):
705         # missing explicit is bad
706         self.assert_multi_line_lint(
707             '''class Foo {
708                  Foo(int f);
709                };''',
710             'Single-argument constructors should be marked explicit.'
711             '  [runtime/explicit] [5]')
712         # missing explicit is bad, even with whitespace
713         self.assert_multi_line_lint(
714             '''class Foo {
715                  Foo (int f);
716                };''',
717             ['Extra space before ( in function call  [whitespace/parens] [4]',
718              'Single-argument constructors should be marked explicit.'
719              '  [runtime/explicit] [5]'])
720         # missing explicit, with distracting comment, is still bad
721         self.assert_multi_line_lint(
722             '''class Foo {
723                  Foo(int f); // simpler than Foo(blargh, blarg)
724                };''',
725             'Single-argument constructors should be marked explicit.'
726             '  [runtime/explicit] [5]')
727         # missing explicit, with qualified classname
728         self.assert_multi_line_lint(
729             '''class Qualifier::AnotherOne::Foo {
730                  Foo(int f);
731                };''',
732             'Single-argument constructors should be marked explicit.'
733             '  [runtime/explicit] [5]')
734         # structs are caught as well.
735         self.assert_multi_line_lint(
736             '''struct Foo {
737                  Foo(int f);
738                };''',
739             'Single-argument constructors should be marked explicit.'
740             '  [runtime/explicit] [5]')
741         # Templatized classes are caught as well.
742         self.assert_multi_line_lint(
743             '''template<typename T> class Foo {
744                  Foo(int f);
745                };''',
746             'Single-argument constructors should be marked explicit.'
747             '  [runtime/explicit] [5]')
748         # proper style is okay
749         self.assert_multi_line_lint(
750             '''class Foo {
751                  explicit Foo(int f);
752                };''',
753             '')
754         # two argument constructor is okay
755         self.assert_multi_line_lint(
756             '''class Foo {
757                  Foo(int f, int b);
758                };''',
759             '')
760         # two argument constructor, across two lines, is okay
761         self.assert_multi_line_lint(
762             '''class Foo {
763                  Foo(int f,
764                      int b);
765                };''',
766             '')
767         # non-constructor (but similar name), is okay
768         self.assert_multi_line_lint(
769             '''class Foo {
770                  aFoo(int f);
771                };''',
772             '')
773         # constructor with void argument is okay
774         self.assert_multi_line_lint(
775             '''class Foo {
776                  Foo(void);
777                };''',
778             '')
779         # single argument method is okay
780         self.assert_multi_line_lint(
781             '''class Foo {
782                  Bar(int b);
783                };''',
784             '')
785         # comments should be ignored
786         self.assert_multi_line_lint(
787             '''class Foo {
788                // Foo(int f);
789                };''',
790             '')
791         # single argument function following class definition is okay
792         # (okay, it's not actually valid, but we don't want a false positive)
793         self.assert_multi_line_lint(
794             '''class Foo {
795                  Foo(int f, int b);
796                };
797                Foo(int f);''',
798             '')
799         # single argument function is okay
800         self.assert_multi_line_lint(
801             '''static Foo(int f);''',
802             '')
803         # single argument copy constructor is okay.
804         self.assert_multi_line_lint(
805             '''class Foo {
806                  Foo(const Foo&);
807                };''',
808             '')
809         self.assert_multi_line_lint(
810             '''class Foo {
811                  Foo(Foo&);
812                };''',
813             '')
814
815     def test_slash_star_comment_on_single_line(self):
816         self.assert_multi_line_lint(
817             '''/* static */ Foo(int f);''',
818             '')
819         self.assert_multi_line_lint(
820             '''/*/ static */  Foo(int f);''',
821             '')
822         self.assert_multi_line_lint(
823             '''/*/ static Foo(int f);''',
824             'Could not find end of multi-line comment'
825             '  [readability/multiline_comment] [5]')
826         self.assert_multi_line_lint(
827             '''    /*/ static Foo(int f);''',
828             'Could not find end of multi-line comment'
829             '  [readability/multiline_comment] [5]')
830         self.assert_multi_line_lint(
831             '''    /**/ static Foo(int f);''',
832             '')
833
834     # Test suspicious usage of "if" like this:
835     # if (a == b) {
836     #   DoSomething();
837     # } if (a == c) {   // Should be "else if".
838     #   DoSomething();  // This gets called twice if a == b && a == c.
839     # }
840     def test_suspicious_usage_of_if(self):
841         self.assert_lint(
842             '    if (a == b) {',
843             '')
844         self.assert_lint(
845             '    } if (a == b) {',
846             'Did you mean "else if"? If not, start a new line for "if".'
847             '  [readability/braces] [4]')
848
849     # Test suspicious usage of memset. Specifically, a 0
850     # as the final argument is almost certainly an error.
851     def test_suspicious_usage_of_memset(self):
852         # Normal use is okay.
853         self.assert_lint(
854             '    memset(buf, 0, sizeof(buf))',
855             '')
856
857         # A 0 as the final argument is almost certainly an error.
858         self.assert_lint(
859             '    memset(buf, sizeof(buf), 0)',
860             'Did you mean "memset(buf, 0, sizeof(buf))"?'
861             '  [runtime/memset] [4]')
862         self.assert_lint(
863             '    memset(buf, xsize * ysize, 0)',
864             'Did you mean "memset(buf, 0, xsize * ysize)"?'
865             '  [runtime/memset] [4]')
866
867         # There is legitimate test code that uses this form.
868         # This is okay since the second argument is a literal.
869         self.assert_lint(
870             "    memset(buf, 'y', 0)",
871             '')
872         self.assert_lint(
873             '    memset(buf, 4, 0)',
874             '')
875         self.assert_lint(
876             '    memset(buf, -1, 0)',
877             '')
878         self.assert_lint(
879             '    memset(buf, 0xF1, 0)',
880             '')
881         self.assert_lint(
882             '    memset(buf, 0xcd, 0)',
883             '')
884
885     def test_check_posix_threading(self):
886         self.assert_lint('sctime_r()', '')
887         self.assert_lint('strtok_r()', '')
888         self.assert_lint('    strtok_r(foo, ba, r)', '')
889         self.assert_lint('brand()', '')
890         self.assert_lint('_rand()', '')
891         self.assert_lint('.rand()', '')
892         self.assert_lint('>rand()', '')
893         self.assert_lint('rand()',
894                          'Consider using rand_r(...) instead of rand(...)'
895                          ' for improved thread safety.'
896                          '  [runtime/threadsafe_fn] [2]')
897         self.assert_lint('strtok()',
898                          'Consider using strtok_r(...) '
899                          'instead of strtok(...)'
900                          ' for improved thread safety.'
901                          '  [runtime/threadsafe_fn] [2]')
902
903     # Test potential format string bugs like printf(foo).
904     def test_format_strings(self):
905         self.assert_lint('printf("foo")', '')
906         self.assert_lint('printf("foo: %s", foo)', '')
907         self.assert_lint('DocidForPrintf(docid)', '')  # Should not trigger.
908         self.assert_lint(
909             'printf(foo)',
910             'Potential format string bug. Do printf("%s", foo) instead.'
911             '  [runtime/printf] [4]')
912         self.assert_lint(
913             'printf(foo.c_str())',
914             'Potential format string bug. '
915             'Do printf("%s", foo.c_str()) instead.'
916             '  [runtime/printf] [4]')
917         self.assert_lint(
918             'printf(foo->c_str())',
919             'Potential format string bug. '
920             'Do printf("%s", foo->c_str()) instead.'
921             '  [runtime/printf] [4]')
922         self.assert_lint(
923             'StringPrintf(foo)',
924             'Potential format string bug. Do StringPrintf("%s", foo) instead.'
925             ''
926             '  [runtime/printf] [4]')
927
928     # Variable-length arrays are not permitted.
929     def test_variable_length_array_detection(self):
930         errmsg = ('Do not use variable-length arrays.  Use an appropriately named '
931                   "('k' followed by CamelCase) compile-time constant for the size."
932                   '  [runtime/arrays] [1]')
933
934         self.assert_lint('int a[any_old_variable];', errmsg)
935         self.assert_lint('int doublesize[some_var * 2];', errmsg)
936         self.assert_lint('int a[afunction()];', errmsg)
937         self.assert_lint('int a[function(kMaxFooBars)];', errmsg)
938         self.assert_lint('bool aList[items_->size()];', errmsg)
939         self.assert_lint('namespace::Type buffer[len+1];', errmsg)
940
941         self.assert_lint('int a[64];', '')
942         self.assert_lint('int a[0xFF];', '')
943         self.assert_lint('int first[256], second[256];', '')
944         self.assert_lint('int arrayName[kCompileTimeConstant];', '')
945         self.assert_lint('char buf[somenamespace::kBufSize];', '')
946         self.assert_lint('int arrayName[ALL_CAPS];', '')
947         self.assert_lint('AClass array1[foo::bar::ALL_CAPS];', '')
948         self.assert_lint('int a[kMaxStrLen + 1];', '')
949         self.assert_lint('int a[sizeof(foo)];', '')
950         self.assert_lint('int a[sizeof(*foo)];', '')
951         self.assert_lint('int a[sizeof foo];', '')
952         self.assert_lint('int a[sizeof(struct Foo)];', '')
953         self.assert_lint('int a[128 - sizeof(const bar)];', '')
954         self.assert_lint('int a[(sizeof(foo) * 4)];', '')
955         self.assert_lint('int a[(arraysize(fixed_size_array)/2) << 1];', 'Missing spaces around /  [whitespace/operators] [3]')
956         self.assert_lint('delete a[some_var];', '')
957         self.assert_lint('return a[some_var];', '')
958
959     # Brace usage
960     def test_braces(self):
961         # Braces shouldn't be followed by a ; unless they're defining a struct
962         # or initializing an array
963         self.assert_lint('int a[3] = { 1, 2, 3 };', '')
964         self.assert_lint(
965             '''const int foo[] =
966                    {1, 2, 3 };''',
967             '')
968         # For single line, unmatched '}' with a ';' is ignored (not enough context)
969         self.assert_multi_line_lint(
970             '''int a[3] = { 1,
971                             2,
972                             3 };''',
973             '')
974         self.assert_multi_line_lint(
975             '''int a[2][3] = { { 1, 2 },
976                              { 3, 4 } };''',
977             '')
978         self.assert_multi_line_lint(
979             '''int a[2][3] =
980                    { { 1, 2 },
981                      { 3, 4 } };''',
982             '')
983
984     # CHECK/EXPECT_TRUE/EXPECT_FALSE replacements
985     def test_check_check(self):
986         self.assert_lint('CHECK(x == 42)',
987                          'Consider using CHECK_EQ instead of CHECK(a == b)'
988                          '  [readability/check] [2]')
989         self.assert_lint('CHECK(x != 42)',
990                          'Consider using CHECK_NE instead of CHECK(a != b)'
991                          '  [readability/check] [2]')
992         self.assert_lint('CHECK(x >= 42)',
993                          'Consider using CHECK_GE instead of CHECK(a >= b)'
994                          '  [readability/check] [2]')
995         self.assert_lint('CHECK(x > 42)',
996                          'Consider using CHECK_GT instead of CHECK(a > b)'
997                          '  [readability/check] [2]')
998         self.assert_lint('CHECK(x <= 42)',
999                          'Consider using CHECK_LE instead of CHECK(a <= b)'
1000                          '  [readability/check] [2]')
1001         self.assert_lint('CHECK(x < 42)',
1002                          'Consider using CHECK_LT instead of CHECK(a < b)'
1003                          '  [readability/check] [2]')
1004
1005         self.assert_lint('DCHECK(x == 42)',
1006                          'Consider using DCHECK_EQ instead of DCHECK(a == b)'
1007                          '  [readability/check] [2]')
1008         self.assert_lint('DCHECK(x != 42)',
1009                          'Consider using DCHECK_NE instead of DCHECK(a != b)'
1010                          '  [readability/check] [2]')
1011         self.assert_lint('DCHECK(x >= 42)',
1012                          'Consider using DCHECK_GE instead of DCHECK(a >= b)'
1013                          '  [readability/check] [2]')
1014         self.assert_lint('DCHECK(x > 42)',
1015                          'Consider using DCHECK_GT instead of DCHECK(a > b)'
1016                          '  [readability/check] [2]')
1017         self.assert_lint('DCHECK(x <= 42)',
1018                          'Consider using DCHECK_LE instead of DCHECK(a <= b)'
1019                          '  [readability/check] [2]')
1020         self.assert_lint('DCHECK(x < 42)',
1021                          'Consider using DCHECK_LT instead of DCHECK(a < b)'
1022                          '  [readability/check] [2]')
1023
1024         self.assert_lint(
1025             'EXPECT_TRUE("42" == x)',
1026             'Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)'
1027             '  [readability/check] [2]')
1028         self.assert_lint(
1029             'EXPECT_TRUE("42" != x)',
1030             'Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)'
1031             '  [readability/check] [2]')
1032         self.assert_lint(
1033             'EXPECT_TRUE(+42 >= x)',
1034             'Consider using EXPECT_GE instead of EXPECT_TRUE(a >= b)'
1035             '  [readability/check] [2]')
1036         self.assert_lint(
1037             'EXPECT_TRUE_M(-42 > x)',
1038             'Consider using EXPECT_GT_M instead of EXPECT_TRUE_M(a > b)'
1039             '  [readability/check] [2]')
1040         self.assert_lint(
1041             'EXPECT_TRUE_M(42U <= x)',
1042             'Consider using EXPECT_LE_M instead of EXPECT_TRUE_M(a <= b)'
1043             '  [readability/check] [2]')
1044         self.assert_lint(
1045             'EXPECT_TRUE_M(42L < x)',
1046             'Consider using EXPECT_LT_M instead of EXPECT_TRUE_M(a < b)'
1047             '  [readability/check] [2]')
1048
1049         self.assert_lint(
1050             'EXPECT_FALSE(x == 42)',
1051             'Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)'
1052             '  [readability/check] [2]')
1053         self.assert_lint(
1054             'EXPECT_FALSE(x != 42)',
1055             'Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)'
1056             '  [readability/check] [2]')
1057         self.assert_lint(
1058             'EXPECT_FALSE(x >= 42)',
1059             'Consider using EXPECT_LT instead of EXPECT_FALSE(a >= b)'
1060             '  [readability/check] [2]')
1061         self.assert_lint(
1062             'ASSERT_FALSE(x > 42)',
1063             'Consider using ASSERT_LE instead of ASSERT_FALSE(a > b)'
1064             '  [readability/check] [2]')
1065         self.assert_lint(
1066             'ASSERT_FALSE(x <= 42)',
1067             'Consider using ASSERT_GT instead of ASSERT_FALSE(a <= b)'
1068             '  [readability/check] [2]')
1069         self.assert_lint(
1070             'ASSERT_FALSE_M(x < 42)',
1071             'Consider using ASSERT_GE_M instead of ASSERT_FALSE_M(a < b)'
1072             '  [readability/check] [2]')
1073
1074         self.assert_lint('CHECK(some_iterator == obj.end())', '')
1075         self.assert_lint('EXPECT_TRUE(some_iterator == obj.end())', '')
1076         self.assert_lint('EXPECT_FALSE(some_iterator == obj.end())', '')
1077
1078         self.assert_lint('CHECK(CreateTestFile(dir, (1 << 20)));', '')
1079         self.assert_lint('CHECK(CreateTestFile(dir, (1 >> 20)));', '')
1080
1081         self.assert_lint('CHECK(x<42)',
1082                          ['Missing spaces around <'
1083                           '  [whitespace/operators] [3]',
1084                           'Consider using CHECK_LT instead of CHECK(a < b)'
1085                           '  [readability/check] [2]'])
1086         self.assert_lint('CHECK(x>42)',
1087                          'Consider using CHECK_GT instead of CHECK(a > b)'
1088                          '  [readability/check] [2]')
1089
1090         self.assert_lint(
1091             '    EXPECT_TRUE(42 < x) // Random comment.',
1092             'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
1093             '  [readability/check] [2]')
1094         self.assert_lint(
1095             'EXPECT_TRUE( 42 < x )',
1096             ['Extra space after ( in function call'
1097              '  [whitespace/parens] [4]',
1098              'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
1099              '  [readability/check] [2]'])
1100         self.assert_lint(
1101             'CHECK("foo" == "foo")',
1102             'Consider using CHECK_EQ instead of CHECK(a == b)'
1103             '  [readability/check] [2]')
1104
1105         self.assert_lint('CHECK_EQ("foo", "foo")', '')
1106
1107     def test_brace_at_begin_of_line(self):
1108         self.assert_lint('{',
1109                          'This { should be at the end of the previous line'
1110                          '  [whitespace/braces] [4]')
1111         self.assert_multi_line_lint(
1112             '#endif\n'
1113             '{\n'
1114             '}\n',
1115             '')
1116         self.assert_multi_line_lint(
1117             'if (condition) {',
1118             '')
1119         self.assert_multi_line_lint(
1120             '    MACRO1(macroArg) {',
1121             '')
1122         self.assert_multi_line_lint(
1123             'ACCESSOR_GETTER(MessageEventPorts) {',
1124             'Place brace on its own line for function definitions.  [whitespace/braces] [4]')
1125         self.assert_multi_line_lint(
1126             'int foo() {',
1127             'Place brace on its own line for function definitions.  [whitespace/braces] [4]')
1128         self.assert_multi_line_lint(
1129             'int foo() const {',
1130             'Place brace on its own line for function definitions.  [whitespace/braces] [4]')
1131         self.assert_multi_line_lint(
1132             'int foo() const\n'
1133             '{\n'
1134             '}\n',
1135             '')
1136         self.assert_multi_line_lint(
1137             'if (condition\n'
1138             '    && condition2\n'
1139             '    && condition3) {\n'
1140             '}\n',
1141             '')
1142
1143     def test_mismatching_spaces_in_parens(self):
1144         self.assert_lint('if (foo ) {', 'Mismatching spaces inside () in if'
1145                          '  [whitespace/parens] [5]')
1146         self.assert_lint('switch ( foo) {', 'Mismatching spaces inside () in switch'
1147                          '  [whitespace/parens] [5]')
1148         self.assert_lint('for (foo; ba; bar ) {', 'Mismatching spaces inside () in for'
1149                          '  [whitespace/parens] [5]')
1150         self.assert_lint('for (; foo; bar) {', '')
1151         self.assert_lint('for ( ; foo; bar) {', '')
1152         self.assert_lint('for ( ; foo; bar ) {', '')
1153         self.assert_lint('for (foo; bar; ) {', '')
1154         self.assert_lint('foreach (foo, foos ) {', 'Mismatching spaces inside () in foreach'
1155                          '  [whitespace/parens] [5]')
1156         self.assert_lint('foreach ( foo, foos) {', 'Mismatching spaces inside () in foreach'
1157                          '  [whitespace/parens] [5]')
1158         self.assert_lint('while (  foo  ) {', 'Should have zero or one spaces inside'
1159                          ' ( and ) in while  [whitespace/parens] [5]')
1160
1161     def test_spacing_for_fncall(self):
1162         self.assert_lint('if (foo) {', '')
1163         self.assert_lint('for (foo;bar;baz) {', '')
1164         self.assert_lint('foreach (foo, foos) {', '')
1165         self.assert_lint('while (foo) {', '')
1166         self.assert_lint('switch (foo) {', '')
1167         self.assert_lint('new (RenderArena()) RenderInline(document())', '')
1168         self.assert_lint('foo( bar)', 'Extra space after ( in function call'
1169                          '  [whitespace/parens] [4]')
1170         self.assert_lint('foobar( \\', '')
1171         self.assert_lint('foobar(     \\', '')
1172         self.assert_lint('( a + b)', 'Extra space after ('
1173                          '  [whitespace/parens] [2]')
1174         self.assert_lint('((a+b))', '')
1175         self.assert_lint('foo (foo)', 'Extra space before ( in function call'
1176                          '  [whitespace/parens] [4]')
1177         self.assert_lint('typedef foo (*foo)(foo)', '')
1178         self.assert_lint('typedef foo (*foo12bar_)(foo)', '')
1179         self.assert_lint('typedef foo (Foo::*bar)(foo)', '')
1180         self.assert_lint('foo (Foo::*bar)(',
1181                          'Extra space before ( in function call'
1182                          '  [whitespace/parens] [4]')
1183         self.assert_lint('typedef foo (Foo::*bar)(', '')
1184         self.assert_lint('(foo)(bar)', '')
1185         self.assert_lint('Foo (*foo)(bar)', '')
1186         self.assert_lint('Foo (*foo)(Bar bar,', '')
1187         self.assert_lint('char (*p)[sizeof(foo)] = &foo', '')
1188         self.assert_lint('char (&ref)[sizeof(foo)] = &foo', '')
1189         self.assert_lint('const char32 (*table[])[6];', '')
1190
1191     def test_spacing_before_braces(self):
1192         self.assert_lint('if (foo){', 'Missing space before {'
1193                          '  [whitespace/braces] [5]')
1194         self.assert_lint('for{', 'Missing space before {'
1195                          '  [whitespace/braces] [5]')
1196         self.assert_lint('for {', '')
1197         self.assert_lint('EXPECT_DEBUG_DEATH({', '')
1198
1199     def test_spacing_around_else(self):
1200         self.assert_lint('}else {', 'Missing space before else'
1201                          '  [whitespace/braces] [5]')
1202         self.assert_lint('} else{', 'Missing space before {'
1203                          '  [whitespace/braces] [5]')
1204         self.assert_lint('} else {', '')
1205         self.assert_lint('} else if', '')
1206
1207     def test_spacing_for_binary_ops(self):
1208         self.assert_lint('if (foo<=bar) {', 'Missing spaces around <='
1209                          '  [whitespace/operators] [3]')
1210         self.assert_lint('if (foo<bar) {', 'Missing spaces around <'
1211                          '  [whitespace/operators] [3]')
1212         self.assert_lint('if (foo<bar->baz) {', 'Missing spaces around <'
1213                          '  [whitespace/operators] [3]')
1214         self.assert_lint('if (foo<bar->bar) {', 'Missing spaces around <'
1215                          '  [whitespace/operators] [3]')
1216         self.assert_lint('typedef hash_map<Foo, Bar', 'Missing spaces around <'
1217                          '  [whitespace/operators] [3]')
1218         self.assert_lint('typedef hash_map<FoooooType, BaaaaarType,', '')
1219         self.assert_lint('a<Foo> t+=b;', 'Missing spaces around +='
1220                          '  [whitespace/operators] [3]')
1221         self.assert_lint('a<Foo> t-=b;', 'Missing spaces around -='
1222                          '  [whitespace/operators] [3]')
1223         self.assert_lint('a<Foo*> t*=b;', 'Missing spaces around *='
1224                          '  [whitespace/operators] [3]')
1225         self.assert_lint('a<Foo*> t/=b;', 'Missing spaces around /='
1226                          '  [whitespace/operators] [3]')
1227         self.assert_lint('a<Foo*> t|=b;', 'Missing spaces around |='
1228                          '  [whitespace/operators] [3]')
1229         self.assert_lint('a<Foo*> t&=b;', 'Missing spaces around &='
1230                          '  [whitespace/operators] [3]')
1231         self.assert_lint('a<Foo*> t<<=b;', 'Missing spaces around <<='
1232                          '  [whitespace/operators] [3]')
1233         self.assert_lint('a<Foo*> t>>=b;', 'Missing spaces around >>='
1234                          '  [whitespace/operators] [3]')
1235         self.assert_lint('a<Foo*> t>>=&b|c;', 'Missing spaces around >>='
1236                          '  [whitespace/operators] [3]')
1237         self.assert_lint('a<Foo*> t<<=*b/c;', 'Missing spaces around <<='
1238                          '  [whitespace/operators] [3]')
1239         self.assert_lint('a<Foo> t -= b;', '')
1240         self.assert_lint('a<Foo> t += b;', '')
1241         self.assert_lint('a<Foo*> t *= b;', '')
1242         self.assert_lint('a<Foo*> t /= b;', '')
1243         self.assert_lint('a<Foo*> t |= b;', '')
1244         self.assert_lint('a<Foo*> t &= b;', '')
1245         self.assert_lint('a<Foo*> t <<= b;', '')
1246         self.assert_lint('a<Foo*> t >>= b;', '')
1247         self.assert_lint('a<Foo*> t >>= &b|c;', 'Missing spaces around |'
1248                          '  [whitespace/operators] [3]')
1249         self.assert_lint('a<Foo*> t <<= *b/c;', 'Missing spaces around /'
1250                          '  [whitespace/operators] [3]')
1251         self.assert_lint('a<Foo*> t <<= b/c; //Test', [
1252                          'Should have a space between // and comment  '
1253                          '[whitespace/comments] [4]', 'Missing'
1254                          ' spaces around /  [whitespace/operators] [3]'])
1255         self.assert_lint('a<Foo*> t <<= b||c;  //Test', ['One space before end'
1256                          ' of line comments  [whitespace/comments] [5]',
1257                          'Should have a space between // and comment  '
1258                          '[whitespace/comments] [4]',
1259                          'Missing spaces around ||  [whitespace/operators] [3]'])
1260         self.assert_lint('a<Foo*> t <<= b&&c; // Test', 'Missing spaces around'
1261                          ' &&  [whitespace/operators] [3]')
1262         self.assert_lint('a<Foo*> t <<= b&&&c; // Test', 'Missing spaces around'
1263                          ' &&  [whitespace/operators] [3]')
1264         self.assert_lint('a<Foo*> t <<= b&&*c; // Test', 'Missing spaces around'
1265                          ' &&  [whitespace/operators] [3]')
1266         self.assert_lint('a<Foo*> t <<= b && *c; // Test', '')
1267         self.assert_lint('a<Foo*> t <<= b && &c; // Test', '')
1268         self.assert_lint('a<Foo*> t <<= b || &c;  /*Test', 'Complex multi-line '
1269                          '/*...*/-style comment found. Lint may give bogus '
1270                          'warnings.  Consider replacing these with //-style'
1271                          ' comments, with #if 0...#endif, or with more clearly'
1272                          ' structured multi-line comments.  [readability/multiline_comment] [5]')
1273         self.assert_lint('a<Foo&> t <<= &b | &c;', '')
1274         self.assert_lint('a<Foo*> t <<= &b & &c; // Test', '')
1275         self.assert_lint('a<Foo*> t <<= *b / &c; // Test', '')
1276         self.assert_lint('if (a=b == 1)', 'Missing spaces around =  [whitespace/operators] [4]')
1277         self.assert_lint('a = 1<<20', 'Missing spaces around <<  [whitespace/operators] [3]')
1278         self.assert_lint('if (a = b == 1)', '')
1279         self.assert_lint('a = 1 << 20', '')
1280         self.assert_multi_line_lint('#include "config.h"\n#include <sys/io.h>\n',
1281                                     '')
1282         self.assert_multi_line_lint('#include "config.h"\n#import <foo/bar.h>\n',
1283                                     '')
1284
1285     def test_spacing_before_last_semicolon(self):
1286         self.assert_lint('call_function() ;',
1287                          'Extra space before last semicolon. If this should be an '
1288                          'empty statement, use { } instead.'
1289                          '  [whitespace/semicolon] [5]')
1290         self.assert_lint('while (true) ;',
1291                          'Extra space before last semicolon. If this should be an '
1292                          'empty statement, use { } instead.'
1293                          '  [whitespace/semicolon] [5]')
1294         self.assert_lint('default:;',
1295                          'Semicolon defining empty statement. Use { } instead.'
1296                          '  [whitespace/semicolon] [5]')
1297         self.assert_lint('      ;',
1298                          'Line contains only semicolon. If this should be an empty '
1299                          'statement, use { } instead.'
1300                          '  [whitespace/semicolon] [5]')
1301         self.assert_lint('for (int i = 0; ;', '')
1302
1303     # Static or global STL strings.
1304     def test_static_or_global_stlstrings(self):
1305         self.assert_lint('string foo;',
1306                          'For a static/global string constant, use a C style '
1307                          'string instead: "char foo[]".'
1308                          '  [runtime/string] [4]')
1309         self.assert_lint('string kFoo = "hello"; // English',
1310                          'For a static/global string constant, use a C style '
1311                          'string instead: "char kFoo[]".'
1312                          '  [runtime/string] [4]')
1313         self.assert_lint('static string foo;',
1314                          'For a static/global string constant, use a C style '
1315                          'string instead: "static char foo[]".'
1316                          '  [runtime/string] [4]')
1317         self.assert_lint('static const string foo;',
1318                          'For a static/global string constant, use a C style '
1319                          'string instead: "static const char foo[]".'
1320                          '  [runtime/string] [4]')
1321         self.assert_lint('string Foo::bar;',
1322                          'For a static/global string constant, use a C style '
1323                          'string instead: "char Foo::bar[]".'
1324                          '  [runtime/string] [4]')
1325         # Rare case.
1326         self.assert_lint('string foo("foobar");',
1327                          'For a static/global string constant, use a C style '
1328                          'string instead: "char foo[]".'
1329                          '  [runtime/string] [4]')
1330         # Should not catch local or member variables.
1331         self.assert_lint('    string foo', '')
1332         # Should not catch functions.
1333         self.assert_lint('string EmptyString() { return ""; }', '')
1334         self.assert_lint('string EmptyString () { return ""; }', '')
1335         self.assert_lint('string VeryLongNameFunctionSometimesEndsWith(\n'
1336                          '    VeryLongNameType very_long_name_variable) {}', '')
1337         self.assert_lint('template<>\n'
1338                          'string FunctionTemplateSpecialization<SomeType>(\n'
1339                          '      int x) { return ""; }', '')
1340         self.assert_lint('template<>\n'
1341                          'string FunctionTemplateSpecialization<vector<A::B>* >(\n'
1342                          '      int x) { return ""; }', '')
1343
1344         # should not catch methods of template classes.
1345         self.assert_lint('string Class<Type>::Method() const\n'
1346                          '{\n'
1347                          '  return "";\n'
1348                          '}\n', '')
1349         self.assert_lint('string Class<Type>::Method(\n'
1350                          '   int arg) const\n'
1351                          '{\n'
1352                          '  return "";\n'
1353                          '}\n', '')
1354
1355     def test_no_spaces_in_function_calls(self):
1356         self.assert_lint('TellStory(1, 3);',
1357                          '')
1358         self.assert_lint('TellStory(1, 3 );',
1359                          'Extra space before )'
1360                          '  [whitespace/parens] [2]')
1361         self.assert_lint('TellStory(1 /* wolf */, 3 /* pigs */);',
1362                          '')
1363         self.assert_multi_line_lint('#endif\n    );',
1364                                     '')
1365
1366     def test_two_spaces_between_code_and_comments(self):
1367         self.assert_lint('} // namespace foo',
1368                          '')
1369         self.assert_lint('}// namespace foo',
1370                          'One space before end of line comments'
1371                          '  [whitespace/comments] [5]')
1372         self.assert_lint('printf("foo"); // Outside quotes.',
1373                          '')
1374         self.assert_lint('int i = 0; // Having one space is fine.','')
1375         self.assert_lint('int i = 0;  // Having two spaces is bad.',
1376                          'One space before end of line comments'
1377                          '  [whitespace/comments] [5]')
1378         self.assert_lint('int i = 0;   // Having three spaces is bad.',
1379                          'One space before end of line comments'
1380                          '  [whitespace/comments] [5]')
1381         self.assert_lint('// Top level comment', '')
1382         self.assert_lint('    // Line starts with four spaces.', '')
1383         self.assert_lint('foo();\n'
1384                          '{ // A scope is opening.', '')
1385         self.assert_lint('    foo();\n'
1386                          '    { // An indented scope is opening.', '')
1387         self.assert_lint('if (foo) { // not a pure scope',
1388                          '')
1389         self.assert_lint('printf("// In quotes.")', '')
1390         self.assert_lint('printf("\\"%s // In quotes.")', '')
1391         self.assert_lint('printf("%s", "// In quotes.")', '')
1392
1393     def test_space_after_comment_marker(self):
1394         self.assert_lint('//', '')
1395         self.assert_lint('//x', 'Should have a space between // and comment'
1396                          '  [whitespace/comments] [4]')
1397         self.assert_lint('// x', '')
1398         self.assert_lint('//----', '')
1399         self.assert_lint('//====', '')
1400         self.assert_lint('//////', '')
1401         self.assert_lint('////// x', '')
1402         self.assert_lint('/// x', '')
1403         self.assert_lint('////x', 'Should have a space between // and comment'
1404                          '  [whitespace/comments] [4]')
1405
1406     def test_newline_at_eof(self):
1407         def do_test(self, data, is_missing_eof):
1408             error_collector = ErrorCollector(self.assert_)
1409             cpp_style.process_file_data('foo.cpp', 'cpp', data.split('\n'),
1410                                         error_collector)
1411             # The warning appears only once.
1412             self.assertEquals(
1413                 int(is_missing_eof),
1414                 error_collector.results().count(
1415                     'Could not find a newline character at the end of the file.'
1416                     '  [whitespace/ending_newline] [5]'))
1417
1418         do_test(self, '// Newline\n// at EOF\n', False)
1419         do_test(self, '// No newline\n// at EOF', True)
1420
1421     def test_invalid_utf8(self):
1422         def do_test(self, raw_bytes, has_invalid_utf8):
1423             error_collector = ErrorCollector(self.assert_)
1424             cpp_style.process_file_data(
1425                 'foo.cpp', 'cpp',
1426                 unicode(raw_bytes, 'utf8', 'replace').split('\n'),
1427                 error_collector)
1428             # The warning appears only once.
1429             self.assertEquals(
1430                 int(has_invalid_utf8),
1431                 error_collector.results().count(
1432                     'Line contains invalid UTF-8'
1433                     ' (or Unicode replacement character).'
1434                     '  [readability/utf8] [5]'))
1435
1436         do_test(self, 'Hello world\n', False)
1437         do_test(self, '\xe9\x8e\xbd\n', False)
1438         do_test(self, '\xe9x\x8e\xbd\n', True)
1439         # This is the encoding of the replacement character itself (which
1440         # you can see by evaluating codecs.getencoder('utf8')(u'\ufffd')).
1441         do_test(self, '\xef\xbf\xbd\n', True)
1442
1443     def test_is_blank_line(self):
1444         self.assert_(cpp_style.is_blank_line(''))
1445         self.assert_(cpp_style.is_blank_line(' '))
1446         self.assert_(cpp_style.is_blank_line(' \t\r\n'))
1447         self.assert_(not cpp_style.is_blank_line('int a;'))
1448         self.assert_(not cpp_style.is_blank_line('{'))
1449
1450     def test_blank_lines_check(self):
1451         self.assert_blank_lines_check(['{\n', '\n', '\n', '}\n'], 1, 1)
1452         self.assert_blank_lines_check(['  if (foo) {\n', '\n', '  }\n'], 1, 1)
1453         self.assert_blank_lines_check(
1454             ['\n', '// {\n', '\n', '\n', '// Comment\n', '{\n', '}\n'], 0, 0)
1455         self.assert_blank_lines_check(['\n', 'run("{");\n', '\n'], 0, 0)
1456         self.assert_blank_lines_check(['\n', '  if (foo) { return 0; }\n', '\n'], 0, 0)
1457
1458     def test_allow_blank_line_before_closing_namespace(self):
1459         error_collector = ErrorCollector(self.assert_)
1460         cpp_style.process_file_data('foo.cpp', 'cpp',
1461                                     ['namespace {', '', '}  // namespace'],
1462                                     error_collector)
1463         self.assertEquals(0, error_collector.results().count(
1464             'Blank line at the end of a code block.  Is this needed?'
1465             '  [whitespace/blank_line] [3]'))
1466
1467     def test_allow_blank_line_before_if_else_chain(self):
1468         error_collector = ErrorCollector(self.assert_)
1469         cpp_style.process_file_data('foo.cpp', 'cpp',
1470                                     ['if (hoge) {',
1471                                      '',  # No warning
1472                                      '} else if (piyo) {',
1473                                      '',  # No warning
1474                                      '} else if (piyopiyo) {',
1475                                      '  hoge = true;',  # No warning
1476                                      '} else {',
1477                                      '',  # Warning on this line
1478                                      '}'],
1479                                     error_collector)
1480         self.assertEquals(1, error_collector.results().count(
1481             'Blank line at the end of a code block.  Is this needed?'
1482             '  [whitespace/blank_line] [3]'))
1483
1484     def test_else_on_same_line_as_closing_braces(self):
1485         error_collector = ErrorCollector(self.assert_)
1486         cpp_style.process_file_data('foo.cpp', 'cpp',
1487                                     ['if (hoge) {',
1488                                      '',
1489                                      '}',
1490                                      ' else {'  # Warning on this line
1491                                      '',
1492                                      '}'],
1493                                     error_collector)
1494         self.assertEquals(1, error_collector.results().count(
1495             'An else should appear on the same line as the preceding }'
1496             '  [whitespace/newline] [4]'))
1497
1498     def test_else_clause_not_on_same_line_as_else(self):
1499         self.assert_lint('    else DoSomethingElse();',
1500                          'Else clause should never be on same line as else '
1501                          '(use 2 lines)  [whitespace/newline] [4]')
1502         self.assert_lint('    else ifDoSomethingElse();',
1503                          'Else clause should never be on same line as else '
1504                          '(use 2 lines)  [whitespace/newline] [4]')
1505         self.assert_lint('    else if (blah) {', '')
1506         self.assert_lint('    variable_ends_in_else = true;', '')
1507
1508     def test_comma(self):
1509         self.assert_lint('a = f(1,2);',
1510                          'Missing space after ,  [whitespace/comma] [3]')
1511         self.assert_lint('int tmp=a,a=b,b=tmp;',
1512                          ['Missing spaces around =  [whitespace/operators] [4]',
1513                           'Missing space after ,  [whitespace/comma] [3]'])
1514         self.assert_lint('f(a, /* name */ b);', '')
1515         self.assert_lint('f(a, /* name */b);', '')
1516
1517     def test_pointer_reference_marker_location(self):
1518         self.assert_lint('int* b;', '', 'foo.cpp')
1519         self.assert_lint('int *b;',
1520                          'Declaration has space between type name and * in int *b  [whitespace/declaration] [3]',
1521                          'foo.cpp')
1522         self.assert_lint('return *b;', '', 'foo.cpp')
1523         self.assert_lint('int *b;', '', 'foo.c')
1524         self.assert_lint('int* b;',
1525                          'Declaration has space between * and variable name in int* b  [whitespace/declaration] [3]',
1526                          'foo.c')
1527         self.assert_lint('int& b;', '', 'foo.cpp')
1528         self.assert_lint('int &b;',
1529                          'Declaration has space between type name and & in int &b  [whitespace/declaration] [3]',
1530                          'foo.cpp')
1531         self.assert_lint('return &b;', '', 'foo.cpp')
1532
1533     def test_indent(self):
1534         self.assert_lint('static int noindent;', '')
1535         self.assert_lint('    int fourSpaceIndent;', '')
1536         self.assert_lint(' int oneSpaceIndent;',
1537                          'Weird number of spaces at line-start.  '
1538                          'Are you using a 4-space indent?  [whitespace/indent] [3]')
1539         self.assert_lint('   int threeSpaceIndent;',
1540                          'Weird number of spaces at line-start.  '
1541                          'Are you using a 4-space indent?  [whitespace/indent] [3]')
1542         self.assert_lint(' char* oneSpaceIndent = "public:";',
1543                          'Weird number of spaces at line-start.  '
1544                          'Are you using a 4-space indent?  [whitespace/indent] [3]')
1545         self.assert_lint(' public:', '')
1546         self.assert_lint('  public:', '')
1547         self.assert_lint('   public:', '')
1548
1549     def test_label(self):
1550         self.assert_lint('public:',
1551                          'Labels should always be indented at least one space.  '
1552                          'If this is a member-initializer list in a constructor, '
1553                          'the colon should be on the line after the definition '
1554                          'header.  [whitespace/labels] [4]')
1555         self.assert_lint('  public:', '')
1556         self.assert_lint('   public:', '')
1557         self.assert_lint(' public:', '')
1558         self.assert_lint('  public:', '')
1559         self.assert_lint('   public:', '')
1560
1561     def test_not_alabel(self):
1562         self.assert_lint('MyVeryLongNamespace::MyVeryLongClassName::', '')
1563
1564     def test_tab(self):
1565         self.assert_lint('\tint a;',
1566                          'Tab found; better to use spaces  [whitespace/tab] [1]')
1567         self.assert_lint('int a = 5;\t// set a to 5',
1568                          'Tab found; better to use spaces  [whitespace/tab] [1]')
1569
1570     def test_parse_arguments(self):
1571         old_usage = cpp_style._USAGE
1572         old_style_categories = cpp_style._STYLE_CATEGORIES
1573         old_webkit_filter_rules = cpp_style._WEBKIT_FILTER_RULES
1574         old_output_format = cpp_style._cpp_style_state.output_format
1575         old_verbose_level = cpp_style._cpp_style_state.verbose_level
1576         old_filters = cpp_style._cpp_style_state.filters
1577         try:
1578             # Don't print usage during the tests, or filter categories
1579             cpp_style._USAGE = ''
1580             cpp_style._STYLE_CATEGORIES = []
1581             cpp_style._WEBKIT_FILTER_RULES = []
1582
1583             self.assertRaises(SystemExit, cpp_style.parse_arguments, ['--badopt'])
1584             self.assertRaises(SystemExit, cpp_style.parse_arguments, ['--help'])
1585             self.assertRaises(SystemExit, cpp_style.parse_arguments, ['--filter='])
1586             # This is illegal because all filters must start with + or -
1587             self.assertRaises(ValueError, cpp_style.parse_arguments, ['--filter=foo'])
1588             self.assertRaises(ValueError, cpp_style.parse_arguments,
1589                               ['--filter=+a,b,-c'])
1590
1591             self.assertEquals((['foo.cpp'], {}), cpp_style.parse_arguments(['foo.cpp']))
1592             self.assertEquals(old_output_format, cpp_style._cpp_style_state.output_format)
1593             self.assertEquals(old_verbose_level, cpp_style._cpp_style_state.verbose_level)
1594
1595             self.assertEquals(([], {}), cpp_style.parse_arguments([]))
1596             self.assertEquals(([], {}), cpp_style.parse_arguments(['--v=0']))
1597
1598             self.assertEquals((['foo.cpp'], {}),
1599                               cpp_style.parse_arguments(['--v=1', 'foo.cpp']))
1600             self.assertEquals(1, cpp_style._cpp_style_state.verbose_level)
1601             self.assertEquals((['foo.h'], {}),
1602                               cpp_style.parse_arguments(['--v=3', 'foo.h']))
1603             self.assertEquals(3, cpp_style._cpp_style_state.verbose_level)
1604             self.assertEquals((['foo.cpp'], {}),
1605                               cpp_style.parse_arguments(['--verbose=5', 'foo.cpp']))
1606             self.assertEquals(5, cpp_style._cpp_style_state.verbose_level)
1607             self.assertRaises(ValueError,
1608                               cpp_style.parse_arguments, ['--v=f', 'foo.cpp'])
1609
1610             self.assertEquals((['foo.cpp'], {}),
1611                               cpp_style.parse_arguments(['--output=emacs', 'foo.cpp']))
1612             self.assertEquals('emacs', cpp_style._cpp_style_state.output_format)
1613             self.assertEquals((['foo.h'], {}),
1614                               cpp_style.parse_arguments(['--output=vs7', 'foo.h']))
1615             self.assertEquals('vs7', cpp_style._cpp_style_state.output_format)
1616             self.assertRaises(SystemExit,
1617                               cpp_style.parse_arguments, ['--output=blah', 'foo.cpp'])
1618
1619             filt = '-,+whitespace,-whitespace/indent'
1620             self.assertEquals((['foo.h'], {}),
1621                               cpp_style.parse_arguments(['--filter='+filt, 'foo.h']))
1622             self.assertEquals(['-', '+whitespace', '-whitespace/indent'],
1623                               cpp_style._cpp_style_state.filters)
1624
1625             self.assertEquals((['foo.cpp', 'foo.h'], {}),
1626                               cpp_style.parse_arguments(['foo.cpp', 'foo.h']))
1627
1628             self.assertEquals((['foo.cpp'], {'--foo': ''}),
1629                               cpp_style.parse_arguments(['--foo', 'foo.cpp'], ['foo']))
1630             self.assertEquals((['foo.cpp'], {'--foo': 'bar'}),
1631                               cpp_style.parse_arguments(['--foo=bar', 'foo.cpp'], ['foo=']))
1632             self.assertEquals((['foo.cpp'], {}),
1633                               cpp_style.parse_arguments(['foo.cpp'], ['foo=']))
1634             self.assertRaises(SystemExit,
1635                               cpp_style.parse_arguments,
1636                               ['--footypo=bar', 'foo.cpp'], ['foo='])
1637         finally:
1638             cpp_style._USAGE = old_usage
1639             cpp_style._STYLE_CATEGORIES = old_style_categories
1640             cpp_style._WEBKIT_FILTER_RULES = old_webkit_filter_rules
1641             cpp_style._cpp_style_state.output_format = old_output_format
1642             cpp_style._cpp_style_state.verbose_level = old_verbose_level
1643             cpp_style._cpp_style_state.filters = old_filters
1644
1645     def test_filter(self):
1646         old_filters = cpp_style._cpp_style_state.filters
1647         try:
1648             cpp_style._cpp_style_state.set_filters('-,+whitespace,-whitespace/indent')
1649             self.assert_lint(
1650                 '// Hello there ',
1651                 'Line ends in whitespace.  Consider deleting these extra spaces.'
1652                 '  [whitespace/end_of_line] [4]')
1653             self.assert_lint('int a = (int)1.0;', '')
1654             self.assert_lint(' weird opening space', '')
1655         finally:
1656             cpp_style._cpp_style_state.filters = old_filters
1657
1658     def test_default_filter(self):
1659         default_filter_rules = cpp_style._DEFAULT_FILTER_RULES
1660         old_filters = cpp_style._cpp_style_state.filters
1661         cpp_style._DEFAULT_FILTER_RULES = [ '-whitespace' ]
1662         try:
1663             # Reset filters
1664             cpp_style._cpp_style_state.set_filters('')
1665             self.assert_lint('// Hello there ', '')
1666             cpp_style._cpp_style_state.set_filters('+whitespace/end_of_line')
1667             self.assert_lint(
1668                 '// Hello there ',
1669                 'Line ends in whitespace.  Consider deleting these extra spaces.'
1670                 '  [whitespace/end_of_line] [4]')
1671             self.assert_lint(' weird opening space', '')
1672         finally:
1673             cpp_style._cpp_style_state.filters = old_filters
1674             cpp_style._DEFAULT_FILTER_RULES = default_filter_rules
1675
1676     def test_unnamed_namespaces_in_headers(self):
1677         self.assert_language_rules_check(
1678             'foo.h', 'namespace {',
1679             'Do not use unnamed namespaces in header files.  See'
1680             ' http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
1681             ' for more information.  [build/namespaces] [4]')
1682         # namespace registration macros are OK.
1683         self.assert_language_rules_check('foo.h', 'namespace {  \\', '')
1684         # named namespaces are OK.
1685         self.assert_language_rules_check('foo.h', 'namespace foo {', '')
1686         self.assert_language_rules_check('foo.h', 'namespace foonamespace {', '')
1687         self.assert_language_rules_check('foo.cpp', 'namespace {', '')
1688         self.assert_language_rules_check('foo.cpp', 'namespace foo {', '')
1689
1690     def test_build_class(self):
1691         # Test that the linter can parse to the end of class definitions,
1692         # and that it will report when it can't.
1693         # Use multi-line linter because it performs the ClassState check.
1694         self.assert_multi_line_lint(
1695             'class Foo {',
1696             'Failed to find complete declaration of class Foo'
1697             '  [build/class] [5]')
1698         # Don't warn on forward declarations of various types.
1699         self.assert_multi_line_lint(
1700             'class Foo;',
1701             '')
1702         self.assert_multi_line_lint(
1703             '''struct Foo*
1704                  foo = NewFoo();''',
1705             '')
1706         # Here is an example where the linter gets confused, even though
1707         # the code doesn't violate the style guide.
1708         self.assert_multi_line_lint(
1709             '''class Foo
1710             #ifdef DERIVE_FROM_GOO
1711               : public Goo {
1712             #else
1713               : public Hoo {
1714             #endif
1715               };''',
1716             'Failed to find complete declaration of class Foo'
1717             '  [build/class] [5]')
1718
1719     def test_build_end_comment(self):
1720         # The crosstool compiler we currently use will fail to compile the
1721         # code in this test, so we might consider removing the lint check.
1722         self.assert_lint('#endif Not a comment',
1723                          'Uncommented text after #endif is non-standard.'
1724                          '  Use a comment.'
1725                          '  [build/endif_comment] [5]')
1726
1727     def test_build_forward_decl(self):
1728         # The crosstool compiler we currently use will fail to compile the
1729         # code in this test, so we might consider removing the lint check.
1730         self.assert_lint('class Foo::Goo;',
1731                          'Inner-style forward declarations are invalid.'
1732                          '  Remove this line.'
1733                          '  [build/forward_decl] [5]')
1734
1735     def test_build_header_guard(self):
1736         file_path = 'mydir/Foo.h'
1737
1738         # We can't rely on our internal stuff to get a sane path on the open source
1739         # side of things, so just parse out the suggested header guard. This
1740         # doesn't allow us to test the suggested header guard, but it does let us
1741         # test all the other header tests.
1742         error_collector = ErrorCollector(self.assert_)
1743         cpp_style.process_file_data(file_path, 'h', [], error_collector)
1744         expected_guard = ''
1745         matcher = re.compile(
1746             'No \#ifndef header guard found\, suggested CPP variable is\: ([A-Za-z_0-9]+) ')
1747         for error in error_collector.result_list():
1748             matches = matcher.match(error)
1749             if matches:
1750                 expected_guard = matches.group(1)
1751                 break
1752
1753         # Make sure we extracted something for our header guard.
1754         self.assertNotEqual(expected_guard, '')
1755
1756         # Wrong guard
1757         error_collector = ErrorCollector(self.assert_)
1758         cpp_style.process_file_data(file_path, 'h',
1759                                     ['#ifndef FOO_H', '#define FOO_H'], error_collector)
1760         self.assertEquals(
1761             1,
1762             error_collector.result_list().count(
1763                 '#ifndef header guard has wrong style, please use: %s'
1764                 '  [build/header_guard] [5]' % expected_guard),
1765             error_collector.result_list())
1766
1767         # No define
1768         error_collector = ErrorCollector(self.assert_)
1769         cpp_style.process_file_data(file_path, 'h',
1770                                     ['#ifndef %s' % expected_guard], error_collector)
1771         self.assertEquals(
1772             1,
1773             error_collector.result_list().count(
1774                 'No #ifndef header guard found, suggested CPP variable is: %s'
1775                 '  [build/header_guard] [5]' % expected_guard),
1776             error_collector.result_list())
1777
1778         # Mismatched define
1779         error_collector = ErrorCollector(self.assert_)
1780         cpp_style.process_file_data(file_path, 'h',
1781                                     ['#ifndef %s' % expected_guard,
1782                                      '#define FOO_H'],
1783                                     error_collector)
1784         self.assertEquals(
1785             1,
1786             error_collector.result_list().count(
1787                 'No #ifndef header guard found, suggested CPP variable is: %s'
1788                 '  [build/header_guard] [5]' % expected_guard),
1789             error_collector.result_list())
1790
1791         # No endif
1792         error_collector = ErrorCollector(self.assert_)
1793         cpp_style.process_file_data(file_path, 'h',
1794                                     ['#ifndef %s' % expected_guard,
1795                                      '#define %s' % expected_guard],
1796                                     error_collector)
1797         self.assertEquals(
1798             1,
1799             error_collector.result_list().count(
1800                 '#endif line should be "#endif // %s"'
1801                 '  [build/header_guard] [5]' % expected_guard),
1802             error_collector.result_list())
1803
1804         # Commentless endif
1805         error_collector = ErrorCollector(self.assert_)
1806         cpp_style.process_file_data(file_path, 'h',
1807                                     ['#ifndef %s' % expected_guard,
1808                                      '#define %s' % expected_guard,
1809                                      '#endif'],
1810                                     error_collector)
1811         self.assertEquals(
1812             1,
1813             error_collector.result_list().count(
1814                 '#endif line should be "#endif // %s"'
1815                 '  [build/header_guard] [5]' % expected_guard),
1816             error_collector.result_list())
1817
1818         # Commentless endif for old-style guard
1819         error_collector = ErrorCollector(self.assert_)
1820         cpp_style.process_file_data(file_path, 'h',
1821                                     ['#ifndef %s_' % expected_guard,
1822                                      '#define %s_' % expected_guard,
1823                                      '#endif'],
1824                                     error_collector)
1825         self.assertEquals(
1826             1,
1827             error_collector.result_list().count(
1828                 '#endif line should be "#endif // %s"'
1829                 '  [build/header_guard] [5]' % expected_guard),
1830             error_collector.result_list())
1831
1832         # No header guard errors
1833         error_collector = ErrorCollector(self.assert_)
1834         cpp_style.process_file_data(file_path, 'h',
1835                                     ['#ifndef %s' % expected_guard,
1836                                      '#define %s' % expected_guard,
1837                                      '#endif // %s' % expected_guard],
1838                                     error_collector)
1839         for line in error_collector.result_list():
1840             if line.find('build/header_guard') != -1:
1841                 self.fail('Unexpected error: %s' % line)
1842
1843         # Completely incorrect header guard
1844         error_collector = ErrorCollector(self.assert_)
1845         cpp_style.process_file_data(file_path, 'h',
1846                                     ['#ifndef FOO',
1847                                      '#define FOO',
1848                                      '#endif  // FOO'],
1849                                     error_collector)
1850         self.assertEquals(
1851             1,
1852             error_collector.result_list().count(
1853                 '#ifndef header guard has wrong style, please use: %s'
1854                 '  [build/header_guard] [5]' % expected_guard),
1855             error_collector.result_list())
1856         self.assertEquals(
1857             1,
1858             error_collector.result_list().count(
1859                 '#endif line should be "#endif // %s"'
1860                 '  [build/header_guard] [5]' % expected_guard),
1861             error_collector.result_list())
1862
1863     def test_build_printf_format(self):
1864         self.assert_lint(
1865             r'printf("\%%d", value);',
1866             '%, [, (, and { are undefined character escapes.  Unescape them.'
1867             '  [build/printf_format] [3]')
1868
1869         self.assert_lint(
1870             r'snprintf(buffer, sizeof(buffer), "\[%d", value);',
1871             '%, [, (, and { are undefined character escapes.  Unescape them.'
1872             '  [build/printf_format] [3]')
1873
1874         self.assert_lint(
1875             r'fprintf(file, "\(%d", value);',
1876             '%, [, (, and { are undefined character escapes.  Unescape them.'
1877             '  [build/printf_format] [3]')
1878
1879         self.assert_lint(
1880             r'vsnprintf(buffer, sizeof(buffer), "\\\{%d", ap);',
1881             '%, [, (, and { are undefined character escapes.  Unescape them.'
1882             '  [build/printf_format] [3]')
1883
1884         # Don't warn if double-slash precedes the symbol
1885         self.assert_lint(r'printf("\\%%%d", value);',
1886                          '')
1887
1888     def test_runtime_printf_format(self):
1889         self.assert_lint(
1890             r'fprintf(file, "%q", value);',
1891             '%q in format strings is deprecated.  Use %ll instead.'
1892             '  [runtime/printf_format] [3]')
1893
1894         self.assert_lint(
1895             r'aprintf(file, "The number is %12q", value);',
1896             '%q in format strings is deprecated.  Use %ll instead.'
1897             '  [runtime/printf_format] [3]')
1898
1899         self.assert_lint(
1900             r'printf(file, "The number is" "%-12q", value);',
1901             '%q in format strings is deprecated.  Use %ll instead.'
1902             '  [runtime/printf_format] [3]')
1903
1904         self.assert_lint(
1905             r'printf(file, "The number is" "%+12q", value);',
1906             '%q in format strings is deprecated.  Use %ll instead.'
1907             '  [runtime/printf_format] [3]')
1908
1909         self.assert_lint(
1910             r'printf(file, "The number is" "% 12q", value);',
1911             '%q in format strings is deprecated.  Use %ll instead.'
1912             '  [runtime/printf_format] [3]')
1913
1914         self.assert_lint(
1915             r'snprintf(file, "Never mix %d and %1$d parmaeters!", value);',
1916             '%N$ formats are unconventional.  Try rewriting to avoid them.'
1917             '  [runtime/printf_format] [2]')
1918
1919     def assert_lintLogCodeOnError(self, code, expected_message):
1920         # Special assert_lint which logs the input code on error.
1921         result = self.perform_single_line_lint(code, 'foo.cpp')
1922         if result != expected_message:
1923             self.fail('For code: "%s"\nGot: "%s"\nExpected: "%s"'
1924                       % (code, result, expected_message))
1925
1926     def test_build_storage_class(self):
1927         qualifiers = [None, 'const', 'volatile']
1928         signs = [None, 'signed', 'unsigned']
1929         types = ['void', 'char', 'int', 'float', 'double',
1930                  'schar', 'int8', 'uint8', 'int16', 'uint16',
1931                  'int32', 'uint32', 'int64', 'uint64']
1932         storage_classes = ['auto', 'extern', 'register', 'static', 'typedef']
1933
1934         build_storage_class_error_message = (
1935             'Storage class (static, extern, typedef, etc) should be first.'
1936             '  [build/storage_class] [5]')
1937
1938         # Some explicit cases. Legal in C++, deprecated in C99.
1939         self.assert_lint('const int static foo = 5;',
1940                          build_storage_class_error_message)
1941
1942         self.assert_lint('char static foo;',
1943                          build_storage_class_error_message)
1944
1945         self.assert_lint('double const static foo = 2.0;',
1946                          build_storage_class_error_message)
1947
1948         self.assert_lint('uint64 typedef unsignedLongLong;',
1949                          build_storage_class_error_message)
1950
1951         self.assert_lint('int register foo = 0;',
1952                          build_storage_class_error_message)
1953
1954         # Since there are a very large number of possibilities, randomly
1955         # construct declarations.
1956         # Make sure that the declaration is logged if there's an error.
1957         # Seed generator with an integer for absolute reproducibility.
1958         random.seed(25)
1959         for unused_i in range(10):
1960             # Build up random list of non-storage-class declaration specs.
1961             other_decl_specs = [random.choice(qualifiers), random.choice(signs),
1962                                 random.choice(types)]
1963             # remove None
1964             other_decl_specs = filter(lambda x: x is not None, other_decl_specs)
1965
1966             # shuffle
1967             random.shuffle(other_decl_specs)
1968
1969             # insert storage class after the first
1970             storage_class = random.choice(storage_classes)
1971             insertion_point = random.randint(1, len(other_decl_specs))
1972             decl_specs = (other_decl_specs[0:insertion_point]
1973                           + [storage_class]
1974                           + other_decl_specs[insertion_point:])
1975
1976             self.assert_lintLogCodeOnError(
1977                 ' '.join(decl_specs) + ';',
1978                 build_storage_class_error_message)
1979
1980             # but no error if storage class is first
1981             self.assert_lintLogCodeOnError(
1982                 storage_class + ' ' + ' '.join(other_decl_specs),
1983                 '')
1984
1985     def test_legal_copyright(self):
1986         legal_copyright_message = (
1987             'No copyright message found.  '
1988             'You should have a line: "Copyright [year] <Copyright Owner>"'
1989             '  [legal/copyright] [5]')
1990
1991         copyright_line = '// Copyright 2008 Google Inc. All Rights Reserved.'
1992
1993         file_path = 'mydir/googleclient/foo.cpp'
1994
1995         # There should be a copyright message in the first 10 lines
1996         error_collector = ErrorCollector(self.assert_)
1997         cpp_style.process_file_data(file_path, 'cpp', [], error_collector)
1998         self.assertEquals(
1999             1,
2000             error_collector.result_list().count(legal_copyright_message))
2001
2002         error_collector = ErrorCollector(self.assert_)
2003         cpp_style.process_file_data(
2004             file_path, 'cpp',
2005             ['' for unused_i in range(10)] + [copyright_line],
2006             error_collector)
2007         self.assertEquals(
2008             1,
2009             error_collector.result_list().count(legal_copyright_message))
2010
2011         # Test that warning isn't issued if Copyright line appears early enough.
2012         error_collector = ErrorCollector(self.assert_)
2013         cpp_style.process_file_data(file_path, 'cpp', [copyright_line], error_collector)
2014         for message in error_collector.result_list():
2015             if message.find('legal/copyright') != -1:
2016                 self.fail('Unexpected error: %s' % message)
2017
2018         error_collector = ErrorCollector(self.assert_)
2019         cpp_style.process_file_data(
2020             file_path, 'cpp',
2021             ['' for unused_i in range(9)] + [copyright_line],
2022             error_collector)
2023         for message in error_collector.result_list():
2024             if message.find('legal/copyright') != -1:
2025                 self.fail('Unexpected error: %s' % message)
2026
2027     def test_invalid_increment(self):
2028         self.assert_lint('*count++;',
2029                          'Changing pointer instead of value (or unused value of '
2030                          'operator*).  [runtime/invalid_increment] [5]')
2031
2032
2033 class CleansedLinesTest(unittest.TestCase):
2034     def test_init(self):
2035         lines = ['Line 1',
2036                  'Line 2',
2037                  'Line 3 // Comment test',
2038                  'Line 4 "foo"']
2039
2040         clean_lines = cpp_style.CleansedLines(lines)
2041         self.assertEquals(lines, clean_lines.raw_lines)
2042         self.assertEquals(4, clean_lines.num_lines())
2043
2044         self.assertEquals(['Line 1',
2045                            'Line 2',
2046                            'Line 3 ',
2047                            'Line 4 "foo"'],
2048                           clean_lines.lines)
2049
2050         self.assertEquals(['Line 1',
2051                            'Line 2',
2052                            'Line 3 ',
2053                            'Line 4 ""'],
2054                           clean_lines.elided)
2055
2056     def test_init_empty(self):
2057         clean_lines = cpp_style.CleansedLines([])
2058         self.assertEquals([], clean_lines.raw_lines)
2059         self.assertEquals(0, clean_lines.num_lines())
2060
2061     def test_collapse_strings(self):
2062         collapse = cpp_style.CleansedLines.collapse_strings
2063         self.assertEquals('""', collapse('""'))             # ""     (empty)
2064         self.assertEquals('"""', collapse('"""'))           # """    (bad)
2065         self.assertEquals('""', collapse('"xyz"'))          # "xyz"  (string)
2066         self.assertEquals('""', collapse('"\\\""'))         # "\""   (string)
2067         self.assertEquals('""', collapse('"\'"'))           # "'"    (string)
2068         self.assertEquals('"\"', collapse('"\"'))           # "\"    (bad)
2069         self.assertEquals('""', collapse('"\\\\"'))         # "\\"   (string)
2070         self.assertEquals('"', collapse('"\\\\\\"'))        # "\\\"  (bad)
2071         self.assertEquals('""', collapse('"\\\\\\\\"'))     # "\\\\" (string)
2072
2073         self.assertEquals('\'\'', collapse('\'\''))         # ''     (empty)
2074         self.assertEquals('\'\'', collapse('\'a\''))        # 'a'    (char)
2075         self.assertEquals('\'\'', collapse('\'\\\'\''))     # '\''   (char)
2076         self.assertEquals('\'', collapse('\'\\\''))         # '\'    (bad)
2077         self.assertEquals('', collapse('\\012'))            # '\012' (char)
2078         self.assertEquals('', collapse('\\xfF0'))           # '\xfF0' (char)
2079         self.assertEquals('', collapse('\\n'))              # '\n' (char)
2080         self.assertEquals('\#', collapse('\\#'))            # '\#' (bad)
2081
2082         self.assertEquals('StringReplace(body, "", "");',
2083                           collapse('StringReplace(body, "\\\\", "\\\\\\\\");'))
2084         self.assertEquals('\'\' ""',
2085                           collapse('\'"\' "foo"'))
2086
2087
2088 class OrderOfIncludesTest(CppStyleTestBase):
2089     def setUp(self):
2090         self.include_state = cpp_style._IncludeState()
2091
2092         # Cheat os.path.abspath called in FileInfo class.
2093         self.os_path_abspath_orig = os.path.abspath
2094         os.path.abspath = lambda value: value
2095
2096     def tearDown(self):
2097         os.path.abspath = self.os_path_abspath_orig
2098
2099     def test_try_drop_common_suffixes(self):
2100         self.assertEqual('foo/foo', cpp_style._drop_common_suffixes('foo/foo-inl.h'))
2101         self.assertEqual('foo/bar/foo',
2102                          cpp_style._drop_common_suffixes('foo/bar/foo_inl.h'))
2103         self.assertEqual('foo/foo', cpp_style._drop_common_suffixes('foo/foo.cpp'))
2104         self.assertEqual('foo/foo_unusualinternal',
2105                          cpp_style._drop_common_suffixes('foo/foo_unusualinternal.h'))
2106         self.assertEqual('',
2107                          cpp_style._drop_common_suffixes('_test.cpp'))
2108         self.assertEqual('test',
2109                          cpp_style._drop_common_suffixes('test.cpp'))
2110
2111
2112 class OrderOfIncludesTest(CppStyleTestBase):
2113     def setUp(self):
2114         self.include_state = cpp_style._IncludeState()
2115
2116         # Cheat os.path.abspath called in FileInfo class.
2117         self.os_path_abspath_orig = os.path.abspath
2118         os.path.abspath = lambda value: value
2119
2120     def tearDown(self):
2121         os.path.abspath = self.os_path_abspath_orig
2122
2123     def test_check_next_include_order__no_config(self):
2124         self.assertEqual('Header file should not contain WebCore config.h.',
2125                          self.include_state.check_next_include_order(cpp_style._CONFIG_HEADER, True))
2126
2127     def test_check_next_include_order__no_self(self):
2128         self.assertEqual('Header file should not contain itself.',
2129                          self.include_state.check_next_include_order(cpp_style._PRIMARY_HEADER, True))
2130         # Test actual code to make sure that header types are correctly assigned.
2131         self.assert_language_rules_check('Foo.h',
2132                                          '#include "Foo.h"\n',
2133                                          'Header file should not contain itself. Should be: alphabetically sorted.'
2134                                          '  [build/include_order] [4]')
2135         self.assert_language_rules_check('FooBar.h',
2136                                          '#include "Foo.h"\n',
2137                                          '')
2138
2139     def test_check_next_include_order__likely_then_config(self):
2140         self.assertEqual('Found header this file implements before WebCore config.h.',
2141                          self.include_state.check_next_include_order(cpp_style._PRIMARY_HEADER, False))
2142         self.assertEqual('Found WebCore config.h after a header this file implements.',
2143                          self.include_state.check_next_include_order(cpp_style._CONFIG_HEADER, False))
2144
2145     def test_check_next_include_order__other_then_config(self):
2146         self.assertEqual('Found other header before WebCore config.h.',
2147                          self.include_state.check_next_include_order(cpp_style._OTHER_HEADER, False))
2148         self.assertEqual('Found WebCore config.h after other header.',
2149                          self.include_state.check_next_include_order(cpp_style._CONFIG_HEADER, False))
2150
2151     def test_check_next_include_order__config_then_other_then_likely(self):
2152         self.assertEqual('', self.include_state.check_next_include_order(cpp_style._CONFIG_HEADER, False))
2153         self.assertEqual('Found other header before a header this file implements.',
2154                          self.include_state.check_next_include_order(cpp_style._OTHER_HEADER, False))
2155         self.assertEqual('Found header this file implements after other header.',
2156                          self.include_state.check_next_include_order(cpp_style._PRIMARY_HEADER, False))
2157
2158     def test_check_alphabetical_include_order(self):
2159         self.assert_language_rules_check('foo.h',
2160                                          '#include "a.h"\n'
2161                                          '#include "c.h"\n'
2162                                          '#include "b.h"\n',
2163                                          'Alphabetical sorting problem.  [build/include_order] [4]')
2164
2165         self.assert_language_rules_check('foo.h',
2166                                          '#include "a.h"\n'
2167                                          '#include "b.h"\n'
2168                                          '#include "c.h"\n',
2169                                          '')
2170
2171         self.assert_language_rules_check('foo.h',
2172                                          '#include <assert.h>\n'
2173                                          '#include "bar.h"\n',
2174                                          'Alphabetical sorting problem.  [build/include_order] [4]')
2175
2176         self.assert_language_rules_check('foo.h',
2177                                          '#include "bar.h"\n'
2178                                          '#include <assert.h>\n',
2179                                          '')
2180
2181     def test_check_line_break_after_own_header(self):
2182         self.assert_language_rules_check('foo.cpp',
2183                                          '#include "config.h"\n'
2184                                          '#include "foo.h"\n'
2185                                          '#include "bar.h"\n',
2186                                          'You should add a blank line after implementation file\'s own header.  [build/include_order] [4]')
2187
2188         self.assert_language_rules_check('foo.cpp',
2189                                          '#include "config.h"\n'
2190                                          '#include "foo.h"\n'
2191                                          '\n'
2192                                          '#include "bar.h"\n',
2193                                          '')
2194
2195     def test_check_preprocessor_in_include_section(self):
2196         self.assert_language_rules_check('foo.cpp',
2197                                          '#include "config.h"\n'
2198                                          '#include "foo.h"\n'
2199                                          '\n'
2200                                          '#ifdef BAZ\n'
2201                                          '#include "baz.h"\n'
2202                                          '#else\n'
2203                                          '#include "foobar.h"\n'
2204                                          '#endif"\n'
2205                                          '#include "bar.h"\n', # No flag because previous is in preprocessor section
2206                                          '')
2207
2208         self.assert_language_rules_check('foo.cpp',
2209                                          '#include "config.h"\n'
2210                                          '#include "foo.h"\n'
2211                                          '\n'
2212                                          '#ifdef BAZ\n'
2213                                          '#include "baz.h"\n'
2214                                          '#endif"\n'
2215                                          '#include "bar.h"\n'
2216                                          '#include "a.h"\n', # Should still flag this.
2217                                          'Alphabetical sorting problem.  [build/include_order] [4]')
2218
2219         self.assert_language_rules_check('foo.cpp',
2220                                          '#include "config.h"\n'
2221                                          '#include "foo.h"\n'
2222                                          '\n'
2223                                          '#ifdef BAZ\n'
2224                                          '#include "baz.h"\n'
2225                                          '#include "bar.h"\n' #Should still flag this
2226                                          '#endif"\n',
2227                                          'Alphabetical sorting problem.  [build/include_order] [4]')
2228
2229         self.assert_language_rules_check('foo.cpp',
2230                                          '#include "config.h"\n'
2231                                          '#include "foo.h"\n'
2232                                          '\n'
2233                                          '#ifdef BAZ\n'
2234                                          '#include "baz.h"\n'
2235                                          '#endif"\n'
2236                                          '#ifdef FOOBAR\n'
2237                                          '#include "foobar.h"\n'
2238                                          '#endif"\n'
2239                                          '#include "bar.h"\n'
2240                                          '#include "a.h"\n', # Should still flag this.
2241                                          'Alphabetical sorting problem.  [build/include_order] [4]')
2242
2243         # Check that after an already included error, the sorting rules still work.
2244         self.assert_language_rules_check('foo.cpp',
2245                                          '#include "config.h"\n'
2246                                          '#include "foo.h"\n'
2247                                          '\n'
2248                                          '#include "foo.h"\n'
2249                                          '#include "g.h"\n',
2250                                          '"foo.h" already included at foo.cpp:1  [build/include] [4]')
2251
2252     def test_check_wtf_includes(self):
2253         self.assert_language_rules_check('foo.cpp',
2254                                          '#include "config.h"\n'
2255                                          '#include "foo.h"\n'
2256                                          '\n'
2257                                          '#include <wtf/Assertions.h>\n',
2258                                          '')
2259         self.assert_language_rules_check('foo.cpp',
2260                                          '#include "config.h"\n'
2261                                          '#include "foo.h"\n'
2262                                          '\n'
2263                                          '#include "wtf/Assertions.h"\n',
2264                                          'wtf includes should be <wtf/file.h> instead of "wtf/file.h".'
2265                                          '  [build/include] [4]')
2266
2267     def test_classify_include(self):
2268         classify_include = cpp_style._classify_include
2269         include_state = cpp_style._IncludeState()
2270         self.assertEqual(cpp_style._CONFIG_HEADER,
2271                          classify_include('foo/foo.cpp',
2272                                           'config.h',
2273                                           False, include_state))
2274         self.assertEqual(cpp_style._PRIMARY_HEADER,
2275                          classify_include('foo/internal/foo.cpp',
2276                                           'foo/public/foo.h',
2277                                           False, include_state))
2278         self.assertEqual(cpp_style._PRIMARY_HEADER,
2279                          classify_include('foo/internal/foo.cpp',
2280                                           'foo/other/public/foo.h',
2281                                           False, include_state))
2282         self.assertEqual(cpp_style._OTHER_HEADER,
2283                          classify_include('foo/internal/foo.cpp',
2284                                           'foo/other/public/foop.h',
2285                                           False, include_state))
2286         self.assertEqual(cpp_style._OTHER_HEADER,
2287                          classify_include('foo/foo.cpp',
2288                                           'string',
2289                                           True, include_state))
2290         self.assertEqual(cpp_style._PRIMARY_HEADER,
2291                          classify_include('fooCustom.cpp',
2292                                           'foo.h',
2293                                           False, include_state))
2294         self.assertEqual(cpp_style._PRIMARY_HEADER,
2295                          classify_include('PrefixFooCustom.cpp',
2296                                           'Foo.h',
2297                                           False, include_state))
2298         # Tricky example where both includes might be classified as primary.
2299         self.assert_language_rules_check('ScrollbarThemeWince.cpp',
2300                                          '#include "config.h"\n'
2301                                          '#include "ScrollbarThemeWince.h"\n'
2302                                          '\n'
2303                                          '#include "Scrollbar.h"\n',
2304                                          '')
2305         self.assert_language_rules_check('ScrollbarThemeWince.cpp',
2306                                          '#include "config.h"\n'
2307                                          '#include "Scrollbar.h"\n'
2308                                          '\n'
2309                                          '#include "ScrollbarThemeWince.h"\n',
2310                                          'Found header this file implements after a header this file implements.'
2311                                          ' Should be: config.h, primary header, blank line, and then alphabetically sorted.'
2312                                          '  [build/include_order] [4]')
2313
2314     def test_try_drop_common_suffixes(self):
2315         self.assertEqual('foo/foo', cpp_style._drop_common_suffixes('foo/foo-inl.h'))
2316         self.assertEqual('foo/bar/foo',
2317                          cpp_style._drop_common_suffixes('foo/bar/foo_inl.h'))
2318         self.assertEqual('foo/foo', cpp_style._drop_common_suffixes('foo/foo.cpp'))
2319         self.assertEqual('foo/foo_unusualinternal',
2320                          cpp_style._drop_common_suffixes('foo/foo_unusualinternal.h'))
2321         self.assertEqual('',
2322                          cpp_style._drop_common_suffixes('_test.cpp'))
2323         self.assertEqual('test',
2324                          cpp_style._drop_common_suffixes('test.cpp'))
2325         self.assertEqual('test',
2326                          cpp_style._drop_common_suffixes('test.cpp'))
2327
2328 class CheckForFunctionLengthsTest(CppStyleTestBase):
2329     def setUp(self):
2330         # Reducing these thresholds for the tests speeds up tests significantly.
2331         self.old_normal_trigger = cpp_style._FunctionState._NORMAL_TRIGGER
2332         self.old_test_trigger = cpp_style._FunctionState._TEST_TRIGGER
2333
2334         cpp_style._FunctionState._NORMAL_TRIGGER = 10
2335         cpp_style._FunctionState._TEST_TRIGGER = 25
2336
2337     def tearDown(self):
2338         cpp_style._FunctionState._NORMAL_TRIGGER = self.old_normal_trigger
2339         cpp_style._FunctionState._TEST_TRIGGER = self.old_test_trigger
2340
2341     def assert_function_lengths_check(self, code, expected_message):
2342         """Check warnings for long function bodies are as expected.
2343
2344         Args:
2345           code: C++ source code expected to generate a warning message.
2346           expected_message: Message expected to be generated by the C++ code.
2347         """
2348         self.assertEquals(expected_message,
2349                           self.perform_function_lengths_check(code))
2350
2351     def trigger_lines(self, error_level):
2352         """Return number of lines needed to trigger a function length warning.
2353
2354         Args:
2355           error_level: --v setting for cpp_style.
2356
2357         Returns:
2358           Number of lines needed to trigger a function length warning.
2359         """
2360         return cpp_style._FunctionState._NORMAL_TRIGGER * 2 ** error_level
2361
2362     def trigger_test_lines(self, error_level):
2363         """Return number of lines needed to trigger a test function length warning.
2364
2365         Args:
2366           error_level: --v setting for cpp_style.
2367
2368         Returns:
2369           Number of lines needed to trigger a test function length warning.
2370         """
2371         return cpp_style._FunctionState._TEST_TRIGGER * 2 ** error_level
2372
2373     def assert_function_length_check_definition(self, lines, error_level):
2374         """Generate long function definition and check warnings are as expected.
2375
2376         Args:
2377           lines: Number of lines to generate.
2378           error_level:  --v setting for cpp_style.
2379         """
2380         trigger_level = self.trigger_lines(cpp_style._verbose_level())
2381         self.assert_function_lengths_check(
2382             'void test(int x)' + self.function_body(lines),
2383             ('Small and focused functions are preferred: '
2384              'test() has %d non-comment lines '
2385              '(error triggered by exceeding %d lines).'
2386              '  [readability/fn_size] [%d]'
2387              % (lines, trigger_level, error_level)))
2388
2389     def assert_function_length_check_definition_ok(self, lines):
2390         """Generate shorter function definition and check no warning is produced.
2391
2392         Args:
2393           lines: Number of lines to generate.
2394         """
2395         self.assert_function_lengths_check(
2396             'void test(int x)' + self.function_body(lines),
2397             '')
2398
2399     def assert_function_length_check_at_error_level(self, error_level):
2400         """Generate and check function at the trigger level for --v setting.
2401
2402         Args:
2403           error_level: --v setting for cpp_style.
2404         """
2405         self.assert_function_length_check_definition(self.trigger_lines(error_level),
2406                                                      error_level)
2407
2408     def assert_function_length_check_below_error_level(self, error_level):
2409         """Generate and check function just below the trigger level for --v setting.
2410
2411         Args:
2412           error_level: --v setting for cpp_style.
2413         """
2414         self.assert_function_length_check_definition(self.trigger_lines(error_level) - 1,
2415                                                      error_level - 1)
2416
2417     def assert_function_length_check_above_error_level(self, error_level):
2418         """Generate and check function just above the trigger level for --v setting.
2419
2420         Args:
2421           error_level: --v setting for cpp_style.
2422         """
2423         self.assert_function_length_check_definition(self.trigger_lines(error_level) + 1,
2424                                                      error_level)
2425
2426     def function_body(self, number_of_lines):
2427         return ' {\n' + '    this_is_just_a_test();\n' * number_of_lines + '}'
2428
2429     def function_body_with_blank_lines(self, number_of_lines):
2430         return ' {\n' + '    this_is_just_a_test();\n\n' * number_of_lines + '}'
2431
2432     def function_body_with_no_lints(self, number_of_lines):
2433         return ' {\n' + '    this_is_just_a_test();  // NOLINT\n' * number_of_lines + '}'
2434
2435     # Test line length checks.
2436     def test_function_length_check_declaration(self):
2437         self.assert_function_lengths_check(
2438             'void test();',  # Not a function definition
2439             '')
2440
2441     def test_function_length_check_declaration_with_block_following(self):
2442         self.assert_function_lengths_check(
2443             ('void test();\n'
2444              + self.function_body(66)),  # Not a function definition
2445             '')
2446
2447     def test_function_length_check_class_definition(self):
2448         self.assert_function_lengths_check(  # Not a function definition
2449             'class Test' + self.function_body(66) + ';',
2450             '')
2451
2452     def test_function_length_check_trivial(self):
2453         self.assert_function_lengths_check(
2454             'void test() {}',  # Not counted
2455             '')
2456
2457     def test_function_length_check_empty(self):
2458         self.assert_function_lengths_check(
2459             'void test() {\n}',
2460             '')
2461
2462     def test_function_length_check_definition_below_severity0(self):
2463         old_verbosity = cpp_style._set_verbose_level(0)
2464         self.assert_function_length_check_definition_ok(self.trigger_lines(0) - 1)
2465         cpp_style._set_verbose_level(old_verbosity)
2466
2467     def test_function_length_check_definition_at_severity0(self):
2468         old_verbosity = cpp_style._set_verbose_level(0)
2469         self.assert_function_length_check_definition_ok(self.trigger_lines(0))
2470         cpp_style._set_verbose_level(old_verbosity)
2471
2472     def test_function_length_check_definition_above_severity0(self):
2473         old_verbosity = cpp_style._set_verbose_level(0)
2474         self.assert_function_length_check_above_error_level(0)
2475         cpp_style._set_verbose_level(old_verbosity)
2476
2477     def test_function_length_check_definition_below_severity1v0(self):
2478         old_verbosity = cpp_style._set_verbose_level(0)
2479         self.assert_function_length_check_below_error_level(1)
2480         cpp_style._set_verbose_level(old_verbosity)
2481
2482     def test_function_length_check_definition_at_severity1v0(self):
2483         old_verbosity = cpp_style._set_verbose_level(0)
2484         self.assert_function_length_check_at_error_level(1)
2485         cpp_style._set_verbose_level(old_verbosity)
2486
2487     def test_function_length_check_definition_below_severity1(self):
2488         self.assert_function_length_check_definition_ok(self.trigger_lines(1) - 1)
2489
2490     def test_function_length_check_definition_at_severity1(self):
2491         self.assert_function_length_check_definition_ok(self.trigger_lines(1))
2492
2493     def test_function_length_check_definition_above_severity1(self):
2494         self.assert_function_length_check_above_error_level(1)
2495
2496     def test_function_length_check_definition_severity1_plus_blanks(self):
2497         error_level = 1
2498         error_lines = self.trigger_lines(error_level) + 1
2499         trigger_level = self.trigger_lines(cpp_style._verbose_level())
2500         self.assert_function_lengths_check(
2501             'void test_blanks(int x)' + self.function_body(error_lines),
2502             ('Small and focused functions are preferred: '
2503              'test_blanks() has %d non-comment lines '
2504              '(error triggered by exceeding %d lines).'
2505              '  [readability/fn_size] [%d]')
2506             % (error_lines, trigger_level, error_level))
2507
2508     def test_function_length_check_complex_definition_severity1(self):
2509         error_level = 1
2510         error_lines = self.trigger_lines(error_level) + 1
2511         trigger_level = self.trigger_lines(cpp_style._verbose_level())
2512         self.assert_function_lengths_check(
2513             ('my_namespace::my_other_namespace::MyVeryLongTypeName*\n'
2514              'my_namespace::my_other_namespace::MyFunction(int arg1, char* arg2)'
2515              + self.function_body(error_lines)),
2516             ('Small and focused functions are preferred: '
2517              'my_namespace::my_other_namespace::MyFunction()'
2518              ' has %d non-comment lines '
2519              '(error triggered by exceeding %d lines).'
2520              '  [readability/fn_size] [%d]')
2521             % (error_lines, trigger_level, error_level))
2522
2523     def test_function_length_check_definition_severity1_for_test(self):
2524         error_level = 1
2525         error_lines = self.trigger_test_lines(error_level) + 1
2526         trigger_level = self.trigger_test_lines(cpp_style._verbose_level())
2527         self.assert_function_lengths_check(
2528             'TEST_F(Test, Mutator)' + self.function_body(error_lines),
2529             ('Small and focused functions are preferred: '
2530              'TEST_F(Test, Mutator) has %d non-comment lines '
2531              '(error triggered by exceeding %d lines).'
2532              '  [readability/fn_size] [%d]')
2533             % (error_lines, trigger_level, error_level))
2534
2535     def test_function_length_check_definition_severity1_for_split_line_test(self):
2536         error_level = 1
2537         error_lines = self.trigger_test_lines(error_level) + 1
2538         trigger_level = self.trigger_test_lines(cpp_style._verbose_level())
2539         self.assert_function_lengths_check(
2540             ('TEST_F(GoogleUpdateRecoveryRegistryProtectedTest,\n'
2541              '    FixGoogleUpdate_AllValues_MachineApp)'  # note: 4 spaces
2542              + self.function_body(error_lines)),
2543             ('Small and focused functions are preferred: '
2544              'TEST_F(GoogleUpdateRecoveryRegistryProtectedTest, '  # 1 space
2545              'FixGoogleUpdate_AllValues_MachineApp) has %d non-comment lines '
2546              '(error triggered by exceeding %d lines).'
2547              '  [readability/fn_size] [%d]')
2548             % (error_lines+1, trigger_level, error_level))
2549
2550     def test_function_length_check_definition_severity1_for_bad_test_doesnt_break(self):
2551         error_level = 1
2552         error_lines = self.trigger_test_lines(error_level) + 1
2553         trigger_level = self.trigger_test_lines(cpp_style._verbose_level())
2554         self.assert_function_lengths_check(
2555             ('TEST_F('
2556              + self.function_body(error_lines)),
2557             ('Small and focused functions are preferred: '
2558              'TEST_F has %d non-comment lines '
2559              '(error triggered by exceeding %d lines).'
2560              '  [readability/fn_size] [%d]')
2561             % (error_lines, trigger_level, error_level))
2562
2563     def test_function_length_check_definition_severity1_with_embedded_no_lints(self):
2564         error_level = 1
2565         error_lines = self.trigger_lines(error_level) + 1
2566         trigger_level = self.trigger_lines(cpp_style._verbose_level())
2567         self.assert_function_lengths_check(
2568             'void test(int x)' + self.function_body_with_no_lints(error_lines),
2569             ('Small and focused functions are preferred: '
2570              'test() has %d non-comment lines '
2571              '(error triggered by exceeding %d lines).'
2572              '  [readability/fn_size] [%d]')
2573             % (error_lines, trigger_level, error_level))
2574
2575     def test_function_length_check_definition_severity1_with_no_lint(self):
2576         self.assert_function_lengths_check(
2577             ('void test(int x)' + self.function_body(self.trigger_lines(1))
2578              + '  // NOLINT -- long function'),
2579             '')
2580
2581     def test_function_length_check_definition_below_severity2(self):
2582         self.assert_function_length_check_below_error_level(2)
2583
2584     def test_function_length_check_definition_severity2(self):
2585         self.assert_function_length_check_at_error_level(2)
2586
2587     def test_function_length_check_definition_above_severity2(self):
2588         self.assert_function_length_check_above_error_level(2)
2589
2590     def test_function_length_check_definition_below_severity3(self):
2591         self.assert_function_length_check_below_error_level(3)
2592
2593     def test_function_length_check_definition_severity3(self):
2594         self.assert_function_length_check_at_error_level(3)
2595
2596     def test_function_length_check_definition_above_severity3(self):
2597         self.assert_function_length_check_above_error_level(3)
2598
2599     def test_function_length_check_definition_below_severity4(self):
2600         self.assert_function_length_check_below_error_level(4)
2601
2602     def test_function_length_check_definition_severity4(self):
2603         self.assert_function_length_check_at_error_level(4)
2604
2605     def test_function_length_check_definition_above_severity4(self):
2606         self.assert_function_length_check_above_error_level(4)
2607
2608     def test_function_length_check_definition_below_severity5(self):
2609         self.assert_function_length_check_below_error_level(5)
2610
2611     def test_function_length_check_definition_at_severity5(self):
2612         self.assert_function_length_check_at_error_level(5)
2613
2614     def test_function_length_check_definition_above_severity5(self):
2615         self.assert_function_length_check_above_error_level(5)
2616
2617     def test_function_length_check_definition_huge_lines(self):
2618         # 5 is the limit
2619         self.assert_function_length_check_definition(self.trigger_lines(10), 5)
2620
2621     def test_function_length_not_determinable(self):
2622         # Macro invocation without terminating semicolon.
2623         self.assert_function_lengths_check(
2624             'MACRO(arg)',
2625             '')
2626
2627         # Macro with underscores
2628         self.assert_function_lengths_check(
2629             'MACRO_WITH_UNDERSCORES(arg1, arg2, arg3)',
2630             '')
2631
2632         self.assert_function_lengths_check(
2633             'NonMacro(arg)',
2634             'Lint failed to find start of function body.'
2635             '  [readability/fn_size] [5]')
2636
2637
2638 class NoNonVirtualDestructorsTest(CppStyleTestBase):
2639
2640     def test_no_error(self):
2641         self.assert_multi_line_lint(
2642             '''class Foo {
2643                    virtual ~Foo();
2644                    virtual void foo();
2645                };''',
2646             '')
2647
2648         self.assert_multi_line_lint(
2649             '''class Foo {
2650                    virtual inline ~Foo();
2651                    virtual void foo();
2652                };''',
2653             '')
2654
2655         self.assert_multi_line_lint(
2656             '''class Foo {
2657                    inline virtual ~Foo();
2658                    virtual void foo();
2659                };''',
2660             '')
2661
2662         self.assert_multi_line_lint(
2663             '''class Foo::Goo {
2664                    virtual ~Goo();
2665                    virtual void goo();
2666                };''',
2667             '')
2668         self.assert_multi_line_lint(
2669             'class Foo { void foo(); };',
2670             'More than one command on the same line  [whitespace/newline] [4]')
2671
2672         self.assert_multi_line_lint(
2673             '''class Qualified::Goo : public Foo {
2674                    virtual void goo();
2675                };''',
2676             '')
2677
2678         self.assert_multi_line_lint(
2679             # Line-ending :
2680             '''class Goo :
2681                public Foo {
2682                     virtual void goo();
2683                };''',
2684             'Labels should always be indented at least one space.  If this is a '
2685             'member-initializer list in a constructor, the colon should be on the '
2686             'line after the definition header.  [whitespace/labels] [4]')
2687
2688     def test_no_destructor_when_virtual_needed(self):
2689         self.assert_multi_line_lint_re(
2690             '''class Foo {
2691                    virtual void foo();
2692                };''',
2693             'The class Foo probably needs a virtual destructor')
2694
2695     def test_destructor_non_virtual_when_virtual_needed(self):
2696         self.assert_multi_line_lint_re(
2697             '''class Foo {
2698                    ~Foo();
2699                    virtual void foo();
2700                };''',
2701             'The class Foo probably needs a virtual destructor')
2702
2703     def test_no_warn_when_derived(self):
2704         self.assert_multi_line_lint(
2705             '''class Foo : public Goo {
2706                    virtual void foo();
2707                };''',
2708             '')
2709
2710     def test_internal_braces(self):
2711         self.assert_multi_line_lint_re(
2712             '''class Foo {
2713                    enum Goo {
2714                        GOO
2715                    };
2716                    virtual void foo();
2717                };''',
2718             'The class Foo probably needs a virtual destructor')
2719
2720     def test_inner_class_needs_virtual_destructor(self):
2721         self.assert_multi_line_lint_re(
2722             '''class Foo {
2723                    class Goo {
2724                        virtual void goo();
2725                    };
2726                };''',
2727             'The class Goo probably needs a virtual destructor')
2728
2729     def test_outer_class_needs_virtual_destructor(self):
2730         self.assert_multi_line_lint_re(
2731             '''class Foo {
2732                    class Goo {
2733                    };
2734                    virtual void foo();
2735                };''',
2736             'The class Foo probably needs a virtual destructor')
2737
2738     def test_qualified_class_needs_virtual_destructor(self):
2739         self.assert_multi_line_lint_re(
2740             '''class Qualified::Foo {
2741                    virtual void foo();
2742                };''',
2743             'The class Qualified::Foo probably needs a virtual destructor')
2744
2745     def test_multi_line_declaration_no_error(self):
2746         self.assert_multi_line_lint_re(
2747             '''class Foo
2748                    : public Goo {
2749                    virtual void foo();
2750                };''',
2751             '')
2752
2753     def test_multi_line_declaration_with_error(self):
2754         self.assert_multi_line_lint(
2755             '''class Foo
2756                {
2757                    virtual void foo();
2758                };''',
2759             ['This { should be at the end of the previous line  '
2760              '[whitespace/braces] [4]',
2761              'The class Foo probably needs a virtual destructor due to having '
2762              'virtual method(s), one declared at line 2.  [runtime/virtual] [4]'])
2763
2764
2765 class CppStyleStateTest(unittest.TestCase):
2766     def test_error_count(self):
2767         self.assertEquals(0, cpp_style.error_count())
2768         cpp_style._cpp_style_state.increment_error_count()
2769         cpp_style._cpp_style_state.increment_error_count()
2770         self.assertEquals(2, cpp_style.error_count())
2771         cpp_style._cpp_style_state.reset_error_count()
2772         self.assertEquals(0, cpp_style.error_count())
2773
2774
2775 class WebKitStyleTest(CppStyleTestBase):
2776
2777     # for http://webkit.org/coding/coding-style.html
2778     def test_indentation(self):
2779         # 1. Use spaces, not tabs. Tabs should only appear in files that
2780         #    require them for semantic meaning, like Makefiles.
2781         self.assert_multi_line_lint(
2782             'class Foo {\n'
2783             '    int goo;\n'
2784             '};',
2785             '')
2786         self.assert_multi_line_lint(
2787             'class Foo {\n'
2788             '\tint goo;\n'
2789             '};',
2790             'Tab found; better to use spaces  [whitespace/tab] [1]')
2791
2792         # 2. The indent size is 4 spaces.
2793         self.assert_multi_line_lint(
2794             'class Foo {\n'
2795             '    int goo;\n'
2796             '};',
2797             '')
2798         self.assert_multi_line_lint(
2799             'class Foo {\n'
2800             '   int goo;\n'
2801             '};',
2802             'Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]')
2803         # FIXME: No tests for 8-spaces.
2804
2805         # 3. In a header, code inside a namespace should not be indented.
2806         self.assert_multi_line_lint(
2807             'namespace WebCore {\n\n'
2808             'class Document {\n'
2809             '    int myVariable;\n'
2810             '};\n'
2811             '}',
2812             '',
2813             'foo.h')
2814         self.assert_multi_line_lint(
2815             'namespace OuterNamespace {\n'
2816             '    namespace InnerNamespace {\n'
2817             '    class Document {\n'
2818             '};\n'
2819             '};\n'
2820             '}',
2821             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2822             'foo.h')
2823         self.assert_multi_line_lint(
2824             'namespace OuterNamespace {\n'
2825             '    class Document {\n'
2826             '    namespace InnerNamespace {\n'
2827             '};\n'
2828             '};\n'
2829             '}',
2830             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2831             'foo.h')
2832         self.assert_multi_line_lint(
2833             'namespace WebCore {\n'
2834             '#if 0\n'
2835             '    class Document {\n'
2836             '};\n'
2837             '#endif\n'
2838             '}',
2839             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2840             'foo.h')
2841         self.assert_multi_line_lint(
2842             'namespace WebCore {\n'
2843             'class Document {\n'
2844             '};\n'
2845             '}',
2846             '',
2847             'foo.h')
2848
2849         # 4. In an implementation file (files with the extension .cpp, .c
2850         #    or .mm), code inside a namespace should not be indented.
2851         self.assert_multi_line_lint(
2852             'namespace WebCore {\n\n'
2853             'Document::Foo()\n'
2854             '    : foo(bar)\n'
2855             '    , boo(far)\n'
2856             '{\n'
2857             '    stuff();\n'
2858             '}',
2859             '',
2860             'foo.cpp')
2861         self.assert_multi_line_lint(
2862             'namespace OuterNamespace {\n'
2863             'namespace InnerNamespace {\n'
2864             'Document::Foo() { }\n'
2865             '    void* p;\n'
2866             '}\n'
2867             '}\n',
2868             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2869             'foo.cpp')
2870         self.assert_multi_line_lint(
2871             'namespace OuterNamespace {\n'
2872             'namespace InnerNamespace {\n'
2873             'Document::Foo() { }\n'
2874             '}\n'
2875             '    void* p;\n'
2876             '}\n',
2877             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2878             'foo.cpp')
2879         self.assert_multi_line_lint(
2880             'namespace WebCore {\n\n'
2881             '    const char* foo = "start:;"\n'
2882             '        "dfsfsfs";\n'
2883             '}\n',
2884             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2885             'foo.cpp')
2886         self.assert_multi_line_lint(
2887             'namespace WebCore {\n\n'
2888             'const char* foo(void* a = ";", // ;\n'
2889             '    void* b);\n'
2890             '    void* p;\n'
2891             '}\n',
2892             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2893             'foo.cpp')
2894         self.assert_multi_line_lint(
2895             'namespace WebCore {\n\n'
2896             'const char* foo[] = {\n'
2897             '    "void* b);", // ;\n'
2898             '    "asfdf",\n'
2899             '    }\n'
2900             '    void* p;\n'
2901             '}\n',
2902             'Code inside a namespace should not be indented.  [whitespace/indent] [4]',
2903             'foo.cpp')
2904         self.assert_multi_line_lint(
2905