Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # -*- coding: utf-8; -*- | 1 # -*- coding: utf-8; -*- |
| 2 # | 2 # |
| 3 # Copyright (C) 2011 Google Inc. All rights reserved. | 3 # Copyright (C) 2011 Google Inc. All rights reserved. |
| 4 # Copyright (C) 2009 Torch Mobile Inc. | 4 # Copyright (C) 2009 Torch Mobile Inc. |
| 5 # Copyright (C) 2009 Apple Inc. All rights reserved. | 5 # Copyright (C) 2009 Apple Inc. All rights reserved. |
| 6 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 6 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
| 7 # | 7 # |
| 8 # Redistribution and use in source and binary forms, with or without | 8 # Redistribution and use in source and binary forms, with or without |
| 9 # modification, are permitted provided that the following conditions are | 9 # modification, are permitted provided that the following conditions are |
| 10 # met: | 10 # met: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 from webkitpy.common.system.filesystem import FileSystem | 43 from webkitpy.common.system.filesystem import FileSystem |
| 44 from webkitpy.style.checkers import cpp as cpp_style | 44 from webkitpy.style.checkers import cpp as cpp_style |
| 45 from webkitpy.style.checkers.cpp import CppChecker | 45 from webkitpy.style.checkers.cpp import CppChecker |
| 46 from webkitpy.style.filter import FilterConfiguration | 46 from webkitpy.style.filter import FilterConfiguration |
| 47 | 47 |
| 48 # This class works as an error collector and replaces cpp_style.Error | 48 # This class works as an error collector and replaces cpp_style.Error |
| 49 # function for the unit tests. We also verify each category we see | 49 # function for the unit tests. We also verify each category we see |
| 50 # is in STYLE_CATEGORIES, to help keep that list up to date. | 50 # is in STYLE_CATEGORIES, to help keep that list up to date. |
| 51 | 51 |
| 52 | 52 |
| 53 class ErrorCollector: | 53 class ErrorCollector(object): |
| 54 _all_style_categories = CppChecker.categories | |
| 55 # This is a list including all categories seen in any unit test. | |
| 56 _seen_style_categories = {} | |
| 57 | 54 |
| 58 def __init__(self, assert_fn, filter=None, lines_to_check=None): | 55 def __init__(self, assert_fn, filter=None, lines_to_check=None): |
| 59 """assert_fn: a function to call when we notice a problem. | 56 """assert_fn: a function to call when we notice a problem. |
| 60 filter: filters the errors that we are concerned about. | 57 filter: filters the errors that we are concerned about. |
| 61 """ | 58 """ |
| 62 self._assert_fn = assert_fn | 59 self._assert_fn = assert_fn |
| 63 self._errors = [] | 60 self._errors = [] |
| 64 self._lines_to_check = lines_to_check | 61 self._lines_to_check = lines_to_check |
| 62 self._all_style_categories = CppChecker.categories | |
| 65 if not filter: | 63 if not filter: |
| 66 filter = FilterConfiguration() | 64 filter = FilterConfiguration() |
| 67 self._filter = filter | 65 self._filter = filter |
| 68 | 66 |
| 69 def __call__(self, line_number, category, confidence, message): | 67 def __call__(self, line_number, category, confidence, message): |
| 70 self._assert_fn(category in self._all_style_categories, | 68 self._assert_fn(category in self._all_style_categories, |
| 71 'Message "%s" has category "%s",' | 69 'Message "%s" has category "%s",' |
| 72 ' which is not in STYLE_CATEGORIES' % (message, category )) | 70 ' which is not in STYLE_CATEGORIES' % (message, category )) |
| 73 | 71 |
| 74 if self._lines_to_check and not line_number in self._lines_to_check: | 72 if self._lines_to_check and not line_number in self._lines_to_check: |
| 75 return False | 73 return False |
| 76 | 74 |
| 77 if self._filter.should_check(category, ""): | 75 if self._filter.should_check(category, ""): |
| 78 self._seen_style_categories[category] = 1 | |
| 79 self._errors.append('%s [%s] [%d]' % (message, category, confidence )) | 76 self._errors.append('%s [%s] [%d]' % (message, category, confidence )) |
| 80 return True | 77 return True |
| 81 | 78 |
| 82 def results(self): | 79 def results(self): |
| 83 if len(self._errors) < 2: | 80 if len(self._errors) < 2: |
| 84 return ''.join(self._errors) # Most tests expect to have a string. | 81 return ''.join(self._errors) # Most tests expect to have a string. |
| 85 else: | 82 else: |
| 86 return self._errors # Let's give a list if there is more than one. | 83 return self._errors # Let's give a list if there is more than one. |
| 87 | 84 |
| 88 def result_list(self): | 85 def result_list(self): |
| 89 return self._errors | 86 return self._errors |
| 90 | 87 |
| 91 def verify_all_categories_are_seen(self): | |
| 92 """Fails if there's a category in _all_style_categories - _seen_style_ca tegories. | |
| 93 | |
| 94 This should only be called after all tests are run, so | |
| 95 _seen_style_categories has had a chance to fully populate. Since | |
| 96 this isn't called from within the normal unittest framework, we | |
| 97 can't use the normal unittest assert macros. Instead we just exit | |
| 98 when we see an error. Good thing this test is always run last! | |
| 99 """ | |
| 100 for category in self._all_style_categories: | |
| 101 if category not in self._seen_style_categories: | |
| 102 import sys | |
| 103 sys.exit('FATAL ERROR: There are no tests for category "%s"' % c ategory) | |
| 104 | |
| 105 | 88 |
| 106 class CppFunctionsTest(unittest.TestCase): | 89 class CppFunctionsTest(unittest.TestCase): |
| 107 | 90 |
| 108 """Supports testing functions that do not need CppStyleTestBase.""" | 91 """Supports testing functions that do not need CppStyleTestBase.""" |
| 109 | 92 |
| 110 def test_convert_to_lower_with_underscores(self): | 93 def test_convert_to_lower_with_underscores(self): |
| 111 self.assertEqual(cpp_style._convert_to_lower_with_underscores('ABC'), 'a bc') | 94 self.assertEqual(cpp_style._convert_to_lower_with_underscores('ABC'), 'a bc') |
| 112 self.assertEqual(cpp_style._convert_to_lower_with_underscores('aB'), 'a_ b') | 95 self.assertEqual(cpp_style._convert_to_lower_with_underscores('aB'), 'a_ b') |
| 113 self.assertEqual(cpp_style._convert_to_lower_with_underscores('isAName') , 'is_a_name') | 96 self.assertEqual(cpp_style._convert_to_lower_with_underscores('isAName') , 'is_a_name') |
| 114 self.assertEqual(cpp_style._convert_to_lower_with_underscores('AnotherTe st'), 'another_test') | 97 self.assertEqual(cpp_style._convert_to_lower_with_underscores('AnotherTe st'), 'another_test') |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 619 'parameter_list': | 602 'parameter_list': |
| 620 ({'type': 'PassRefPtr<MyClass>', 'name': 'paramName', 'row': 2} , | 603 ({'type': 'PassRefPtr<MyClass>', 'name': 'paramName', 'row': 2} , |
| 621 {'type': 'const Other1Class&', 'name': 'foo', 'row': 3}, | 604 {'type': 'const Other1Class&', 'name': 'foo', 'row': 3}, |
| 622 {'type': 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const *', 'name': 'param', 'row': 4}, | 605 {'type': 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const *', 'name': 'param', 'row': 4}, |
| 623 {'type': 'int*', 'name': 'myCount', 'row': 5})}, | 606 {'type': 'int*', 'name': 'myCount', 'row': 5})}, |
| 624 detection_line=2) | 607 detection_line=2) |
| 625 | 608 |
| 626 | 609 |
| 627 class CppStyleTest(CppStyleTestBase): | 610 class CppStyleTest(CppStyleTestBase): |
| 628 | 611 |
| 612 def tearDownClass(self): | |
| 613 | |
|
dcheng
2017/03/14 04:45:54
Why does this need an empty tearDownClass?
qyearsley
2017/03/14 16:11:00
It doesn't, thanks for pointing this out :-)
When
| |
| 614 | |
| 629 def test_asm_lines_ignored(self): | 615 def test_asm_lines_ignored(self): |
| 630 self.assert_lint( | 616 self.assert_lint( |
| 631 '__asm mov [registration], eax', | 617 '__asm mov [registration], eax', |
| 632 '') | 618 '') |
| 633 | 619 |
| 634 # Test get line width. | 620 # Test get line width. |
| 635 def test_get_line_width(self): | 621 def test_get_line_width(self): |
| 636 self.assertEqual(0, cpp_style.get_line_width('')) | 622 self.assertEqual(0, cpp_style.get_line_width('')) |
| 637 self.assertEqual(10, cpp_style.get_line_width(u'x' * 10)) | 623 self.assertEqual(10, cpp_style.get_line_width(u'x' * 10)) |
| 638 self.assertEqual(16, cpp_style.get_line_width(u'都|道|府|県|支庁')) | 624 self.assertEqual(16, cpp_style.get_line_width(u'都|道|府|県|支庁')) |
| (...skipping 1896 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2535 | 2521 |
| 2536 Args: | 2522 Args: |
| 2537 error_level: --v setting for cpp_style. | 2523 error_level: --v setting for cpp_style. |
| 2538 """ | 2524 """ |
| 2539 self.assert_function_length_check_definition(self.trigger_lines(error_le vel) + 1, | 2525 self.assert_function_length_check_definition(self.trigger_lines(error_le vel) + 1, |
| 2540 error_level) | 2526 error_level) |
| 2541 | 2527 |
| 2542 def function_body(self, number_of_lines): | 2528 def function_body(self, number_of_lines): |
| 2543 return ' {\n' + ' this_is_just_a_test();\n' * number_of_lines + '}' | 2529 return ' {\n' + ' this_is_just_a_test();\n' * number_of_lines + '}' |
| 2544 | 2530 |
| 2545 def function_body_with_blank_lines(self, number_of_lines): | |
| 2546 return ' {\n' + ' this_is_just_a_test();\n\n' * number_of_lines + '}' | |
| 2547 | |
| 2548 def function_body_with_no_lints(self, number_of_lines): | 2531 def function_body_with_no_lints(self, number_of_lines): |
| 2549 return ' {\n' + ' this_is_just_a_test(); // NOLINT\n' * number_of_line s + '}' | 2532 return ' {\n' + ' this_is_just_a_test(); // NOLINT\n' * number_of_line s + '}' |
| 2550 | 2533 |
| 2551 # Test line length checks. | 2534 # Test line length checks. |
| 2552 def test_function_length_check_declaration(self): | 2535 def test_function_length_check_declaration(self): |
| 2553 self.assert_function_lengths_check( | 2536 self.assert_function_lengths_check( |
| 2554 'void test();', # Not a function definition | 2537 'void test();', # Not a function definition |
| 2555 '') | 2538 '') |
| 2556 | 2539 |
| 2557 def test_function_length_check_declaration_with_block_following(self): | 2540 def test_function_length_check_declaration_with_block_following(self): |
| (...skipping 1577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4135 def test_ne(self): | 4118 def test_ne(self): |
| 4136 """Test __ne__ inequality function.""" | 4119 """Test __ne__ inequality function.""" |
| 4137 checker1 = self._checker() | 4120 checker1 = self._checker() |
| 4138 checker2 = self._checker() | 4121 checker2 = self._checker() |
| 4139 | 4122 |
| 4140 # != calls __ne__. | 4123 # != calls __ne__. |
| 4141 # By default, __ne__ always returns true on different objects. | 4124 # By default, __ne__ always returns true on different objects. |
| 4142 # Thus, just check the distinguishing case to verify that the | 4125 # Thus, just check the distinguishing case to verify that the |
| 4143 # code defines __ne__. | 4126 # code defines __ne__. |
| 4144 self.assertFalse(checker1 != checker2) | 4127 self.assertFalse(checker1 != checker2) |
| OLD | NEW |