Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py

Issue 2384943002: Trim more unneeded stuff from Blink's C++ style checker script. (Closed)
Patch Set: . Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 self.fail('Message was:\n' + message + 'Expected match to "' + expec ted_message_re + '"') 339 self.fail('Message was:\n' + message + 'Expected match to "' + expec ted_message_re + '"')
340 340
341 def assert_language_rules_check(self, file_name, code, expected_message, lin es_to_check=None): 341 def assert_language_rules_check(self, file_name, code, expected_message, lin es_to_check=None):
342 self.assertEqual(expected_message, 342 self.assertEqual(expected_message,
343 self.perform_language_rules_check(file_name, code, line s_to_check)) 343 self.perform_language_rules_check(file_name, code, line s_to_check))
344 344
345 def assert_include_what_you_use(self, code, expected_message): 345 def assert_include_what_you_use(self, code, expected_message):
346 self.assertEqual(expected_message, 346 self.assertEqual(expected_message,
347 self.perform_include_what_you_use(code)) 347 self.perform_include_what_you_use(code))
348 348
349 def assert_blank_lines_check(self, lines, start_errors, end_errors):
350 error_collector = ErrorCollector(self.assertTrue)
351 self.process_file_data('foo.cpp', 'cpp', lines, error_collector)
352 self.assertEqual(
353 start_errors,
354 error_collector.results().count(
355 'Blank line at the start of a code block. Is this needed?'
356 ' [whitespace/blank_line] [2]'))
357 self.assertEqual(
358 end_errors,
359 error_collector.results().count(
360 'Blank line at the end of a code block. Is this needed?'
361 ' [whitespace/blank_line] [3]'))
362
363 def assert_positions_equal(self, position, tuple_position): 349 def assert_positions_equal(self, position, tuple_position):
364 """Checks if the two positions are equal. 350 """Checks if the two positions are equal.
365 351
366 position: a cpp_style.Position object. 352 position: a cpp_style.Position object.
367 tuple_position: a tuple (row, column) to compare against. 353 tuple_position: a tuple (row, column) to compare against.
368 """ 354 """
369 self.assertEqual(position, cpp_style.Position(tuple_position[0], tuple_p osition[1]), 355 self.assertEqual(position, cpp_style.Position(tuple_position[0], tuple_p osition[1]),
370 'position %s, tuple_position %s' % (position, tuple_pos ition)) 356 'position %s, tuple_position %s' % (position, tuple_pos ition))
371 357
372 358
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 self.assertEqual(cpp_style.Position(2, 2), cpp_style._rfind_in_lines('(t e|a)', lines, start_position, not_found_position)) 675 self.assertEqual(cpp_style.Position(2, 2), cpp_style._rfind_in_lines('(t e|a)', lines, start_position, not_found_position))
690 676
691 def test_close_expression(self): 677 def test_close_expression(self):
692 self.assertEqual(cpp_style.Position(1, -1), cpp_style.close_expression([ ')('], cpp_style.Position(0, 1))) 678 self.assertEqual(cpp_style.Position(1, -1), cpp_style.close_expression([ ')('], cpp_style.Position(0, 1)))
693 self.assertEqual(cpp_style.Position(1, -1), cpp_style.close_expression([ ') ()'], cpp_style.Position(0, 1))) 679 self.assertEqual(cpp_style.Position(1, -1), cpp_style.close_expression([ ') ()'], cpp_style.Position(0, 1)))
694 self.assertEqual(cpp_style.Position(0, 4), cpp_style.close_expression([' )[)]'], cpp_style.Position(0, 1))) 680 self.assertEqual(cpp_style.Position(0, 4), cpp_style.close_expression([' )[)]'], cpp_style.Position(0, 1)))
695 self.assertEqual(cpp_style.Position(0, 5), cpp_style.close_expression([' }{}{}'], cpp_style.Position(0, 3))) 681 self.assertEqual(cpp_style.Position(0, 5), cpp_style.close_expression([' }{}{}'], cpp_style.Position(0, 3)))
696 self.assertEqual(cpp_style.Position(1, 1), cpp_style.close_expression([' }{}{', '}'], cpp_style.Position(0, 3))) 682 self.assertEqual(cpp_style.Position(1, 1), cpp_style.close_expression([' }{}{', '}'], cpp_style.Position(0, 3)))
697 self.assertEqual(cpp_style.Position(2, -1), cpp_style.close_expression([ '][][', ' '], cpp_style.Position(0, 3))) 683 self.assertEqual(cpp_style.Position(2, -1), cpp_style.close_expression([ '][][', ' '], cpp_style.Position(0, 3)))
698 684
699 def test_spaces_at_end_of_line(self):
700 self.assert_lint(
701 '// Hello there ',
702 'Line ends in whitespace. Consider deleting these extra spaces.'
703 ' [whitespace/end_of_line] [4]')
704
705 # Test C-style cast cases. 685 # Test C-style cast cases.
706 def test_cstyle_cast(self): 686 def test_cstyle_cast(self):
707 self.assert_lint( 687 self.assert_lint(
708 'int a = (int)1.0;', 688 'int a = (int)1.0;',
709 'Using C-style cast. Use static_cast<int>(...) instead' 689 'Using C-style cast. Use static_cast<int>(...) instead'
710 ' [readability/casting] [4]') 690 ' [readability/casting] [4]')
711 self.assert_lint( 691 self.assert_lint(
712 'int *a = (int *)DEFINED_VALUE;', 692 'int *a = (int *)DEFINED_VALUE;',
713 'Using C-style cast. Use reinterpret_cast<int *>(...) instead' 693 'Using C-style cast. Use reinterpret_cast<int *>(...) instead'
714 ' [readability/casting] [4]', 'foo.c') 694 ' [readability/casting] [4]', 'foo.c')
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after
1575 self.assert_lint('CHECK(x>42)', 1555 self.assert_lint('CHECK(x>42)',
1576 'Consider using CHECK_GT instead of CHECK(a > b)' 1556 'Consider using CHECK_GT instead of CHECK(a > b)'
1577 ' [readability/check] [2]') 1557 ' [readability/check] [2]')
1578 1558
1579 self.assert_lint( 1559 self.assert_lint(
1580 ' EXPECT_TRUE(42 < x) // Random comment.', 1560 ' EXPECT_TRUE(42 < x) // Random comment.',
1581 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)' 1561 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
1582 ' [readability/check] [2]') 1562 ' [readability/check] [2]')
1583 self.assert_lint( 1563 self.assert_lint(
1584 'EXPECT_TRUE( 42 < x )', 1564 'EXPECT_TRUE( 42 < x )',
1585 ['Extra space after ( in function call' 1565 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
1586 ' [whitespace/parens] [4]', 1566 ' [readability/check] [2]')
1587 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
1588 ' [readability/check] [2]'])
1589 self.assert_lint( 1567 self.assert_lint(
1590 'CHECK("foo" == "foo")', 1568 'CHECK("foo" == "foo")',
1591 'Consider using CHECK_EQ instead of CHECK(a == b)' 1569 'Consider using CHECK_EQ instead of CHECK(a == b)'
1592 ' [readability/check] [2]') 1570 ' [readability/check] [2]')
1593 1571
1594 self.assert_lint('CHECK_EQ("foo", "foo")', '') 1572 self.assert_lint('CHECK_EQ("foo", "foo")', '')
1595 1573
1596 def test_check_deprecated_macros(self): 1574 def test_check_deprecated_macros(self):
1597 self.assert_lint('ASSERT(foo)', 'ASSERT is deprecated. Use DCHECK or ' 1575 self.assert_lint('ASSERT(foo)', 'ASSERT is deprecated. Use DCHECK or '
1598 'its variants instead. [build/deprecated] [5]') 1576 'its variants instead. [build/deprecated] [5]')
1599 self.assert_lint(' ASSERT_UNUSED(foo, foo)', 'ASSERT_UNUSED is ' 1577 self.assert_lint(' ASSERT_UNUSED(foo, foo)', 'ASSERT_UNUSED is '
1600 'deprecated. Use DCHECK or its variants instead. ' 1578 'deprecated. Use DCHECK or its variants instead. '
1601 '[build/deprecated] [5]') 1579 '[build/deprecated] [5]')
1602 self.assert_lint('ASSERT_NOT_REACHED()', 'ASSERT_NOT_REACHED is ' 1580 self.assert_lint('ASSERT_NOT_REACHED()', 'ASSERT_NOT_REACHED is '
1603 'deprecated. Use NOTREACHED instead. ' 1581 'deprecated. Use NOTREACHED instead. '
1604 '[build/deprecated] [5]') 1582 '[build/deprecated] [5]')
1605 self.assert_lint('ASSERT_WITH_SECURITY_IMPLICATION(foo)', 1583 self.assert_lint('ASSERT_WITH_SECURITY_IMPLICATION(foo)',
1606 'ASSERT_WITH_SECURITY_IMPLICATION is deprecated. Use ' 1584 'ASSERT_WITH_SECURITY_IMPLICATION is deprecated. Use '
1607 'SECURITY_DCHECK instead. [build/deprecated] [5]') 1585 'SECURITY_DCHECK instead. [build/deprecated] [5]')
1608 self.assert_lint('WTF_LOG(foo)', 'WTF_LOG is deprecated. Use DVLOG ' 1586 self.assert_lint('WTF_LOG(foo)', 'WTF_LOG is deprecated. Use DVLOG '
1609 'instead. [build/deprecated] [5]') 1587 'instead. [build/deprecated] [5]')
1610 1588
1611 self.assert_lint('FOO_BAR_ASSERT()', '') 1589 self.assert_lint('FOO_BAR_ASSERT()', '')
1612 self.assert_lint('ASSERT_NO_EXCEPTIONS', '') 1590 self.assert_lint('ASSERT_NO_EXCEPTIONS', '')
1613 1591
1614 def test_brace_at_begin_of_line(self):
1615 self.assert_multi_line_lint(
1616 '#endif\n'
1617 '{\n'
1618 '}\n',
1619 '')
1620 self.assert_multi_line_lint(
1621 'if (condition) {',
1622 '')
1623 self.assert_multi_line_lint(
1624 ' MACRO1(macroArg) {',
1625 '')
1626 self.assert_multi_line_lint(
1627 'int foo() const\n'
1628 '{\n'
1629 '}\n',
1630 '')
1631 self.assert_multi_line_lint(
1632 'int foo() override\n'
1633 '{\n'
1634 '}\n',
1635 '')
1636 self.assert_multi_line_lint(
1637 'int foo() final\n'
1638 '{\n'
1639 '}\n',
1640 '')
1641 self.assert_multi_line_lint(
1642 'if (condition\n'
1643 ' && condition2\n'
1644 ' && condition3) {\n'
1645 '}\n',
1646 '')
1647 self.assert_multi_line_lint(
1648 'if (condition) {\n'
1649 ' {\n'
1650 ' }\n',
1651 '')
1652 self.assert_multi_line_lint(
1653 'int foo()\n'
1654 '{\n'
1655 ' {\n'
1656 ' }\n'
1657 '}\n',
1658 '')
1659 self.assert_multi_line_lint(
1660 'auto foo() -> int\n'
1661 '{\n'
1662 '}\n',
1663 '')
1664 self.assert_multi_line_lint(
1665 'auto foo() -> T<U, V>\n'
1666 '{\n'
1667 '}\n',
1668 '')
1669
1670 def test_mismatching_spaces_in_parens(self):
1671 self.assert_lint('if (foo ) {', 'Extra space before ) in if'
1672 ' [whitespace/parens] [5]')
1673 self.assert_lint('switch ( foo) {', 'Extra space after ( in switch'
1674 ' [whitespace/parens] [5]')
1675 self.assert_lint('for (foo; ba; bar ) {', 'Extra space before ) in for'
1676 ' [whitespace/parens] [5]')
1677 self.assert_lint('for ((foo); (ba); (bar) ) {', 'Extra space before ) in for'
1678 ' [whitespace/parens] [5]')
1679 self.assert_lint('for (; foo; bar) {', '')
1680 self.assert_lint('for (; (foo); (bar)) {', '')
1681 self.assert_lint('for ( ; foo; bar) {', '')
1682 self.assert_lint('for ( ; (foo); (bar)) {', '')
1683 self.assert_lint('for ( ; foo; bar ) {', 'Extra space before ) in for'
1684 ' [whitespace/parens] [5]')
1685 self.assert_lint('for ( ; (foo); (bar) ) {', 'Extra space before ) in fo r'
1686 ' [whitespace/parens] [5]')
1687 self.assert_lint('for (foo; bar; ) {', '')
1688 self.assert_lint('for ((foo); (bar); ) {', '')
1689 self.assert_lint('foreach (foo, foos ) {', 'Extra space before ) in fore ach'
1690 ' [whitespace/parens] [5]')
1691 self.assert_lint('foreach ( foo, foos) {', 'Extra space after ( in forea ch'
1692 ' [whitespace/parens] [5]')
1693 self.assert_lint('while ( foo) {', 'Extra space after ( in while'
1694 ' [whitespace/parens] [5]')
1695
1696 def test_spacing_for_fncall(self):
1697 self.assert_lint('if (foo) {', '')
1698 self.assert_lint('for (foo;bar;baz) {', '')
1699 self.assert_lint('foreach (foo, foos) {', '')
1700 self.assert_lint('while (foo) {', '')
1701 self.assert_lint('switch (foo) {', '')
1702 self.assert_lint('new (RenderArena()) RenderInline(document())', '')
1703 self.assert_lint('foo( bar)', 'Extra space after ( in function call'
1704 ' [whitespace/parens] [4]')
1705 self.assert_lint('foobar( \\', '')
1706 self.assert_lint('foobar( \\', '')
1707 self.assert_lint('( a + b)', 'Extra space after ('
1708 ' [whitespace/parens] [2]')
1709 self.assert_lint('((a+b))', '')
1710 self.assert_lint('#elif (foo(bar))', '')
1711 self.assert_lint('#elif (foo(bar) && foo(baz))', '')
1712 self.assert_lint('typedef foo (*foo)(foo)', '')
1713 self.assert_lint('typedef foo (*foo12bar_)(foo)', '')
1714 self.assert_lint('typedef foo (Foo::*bar)(foo)', '')
1715 self.assert_lint('typedef foo (Foo::*bar)(', '')
1716 self.assert_lint('(foo)(bar)', '')
1717 self.assert_lint('Foo (*foo)(bar)', '')
1718 self.assert_lint('Foo (*foo)(Bar bar,', '')
1719 self.assert_lint('char (*p)[sizeof(foo)] = &foo', '')
1720 self.assert_lint('char (&ref)[sizeof(foo)] = &foo', '')
1721 self.assert_lint('const char32 (*table[])[6];', '')
1722
1723 def test_spacing_before_braces(self):
1724 self.assert_lint('for {', '')
1725 self.assert_lint('EXPECT_DEBUG_DEATH({', '')
1726
1727 def test_spacing_between_braces(self):
1728 self.assert_lint(' { }', '')
1729 self.assert_lint(' {}', '')
1730 self.assert_lint(' { }', 'Too many spaces inside { }. [whitespace/ braces] [5]')
1731
1732 def test_spacing_around_else(self):
1733 self.assert_lint('}else {', 'Missing space before else'
1734 ' [whitespace/braces] [5]')
1735 self.assert_lint('} else {', '')
1736 self.assert_lint('} else if', '')
1737
1738 def test_operator_methods(self):
1739 self.assert_lint('String operator+(const String&, const String&);', '')
1740 self.assert_lint('String operator/(const String&, const String&);', '')
1741 self.assert_lint('bool operator==(const String&, const String&);', '')
1742 self.assert_lint('String& operator-=(const String&, const String&);', '' )
1743 self.assert_lint('String& operator+=(const String&, const String&);', '' )
1744 self.assert_lint('String& operator*=(const String&, const String&);', '' )
1745 self.assert_lint('String& operator%=(const String&, const String&);', '' )
1746 self.assert_lint('String& operator&=(const String&, const String&);', '' )
1747 self.assert_lint('String& operator<<=(const String&, const String&);', ' ')
1748 self.assert_lint('String& operator>>=(const String&, const String&);', ' ')
1749 self.assert_lint('String& operator|=(const String&, const String&);', '' )
1750 self.assert_lint('String& operator^=(const String&, const String&);', '' )
1751
1752 def test_spacing_before_last_semicolon(self): 1592 def test_spacing_before_last_semicolon(self):
1753 self.assert_lint('call_function() ;',
1754 'Extra space before last semicolon. If this should be a n '
1755 'empty statement, use { } instead.'
1756 ' [whitespace/semicolon] [5]')
1757 self.assert_lint('while (true) ;',
1758 'Extra space before last semicolon. If this should be a n '
1759 'empty statement, use { } instead.'
1760 ' [whitespace/semicolon] [5]')
1761 self.assert_lint('default:;', 1593 self.assert_lint('default:;',
1762 'Semicolon defining empty statement. Use { } instead.' 1594 'Semicolon defining empty statement. Use { } instead.'
1763 ' [whitespace/semicolon] [5]') 1595 ' [whitespace/semicolon] [5]')
1764 self.assert_lint(' ;', 1596 self.assert_lint(' ;',
1765 'Line contains only semicolon. If this should be an emp ty ' 1597 'Line contains only semicolon. If this should be an emp ty '
1766 'statement, use { } instead.' 1598 'statement, use { } instead.'
1767 ' [whitespace/semicolon] [5]') 1599 ' [whitespace/semicolon] [5]')
1768 self.assert_lint('for (int i = 0; ;', '') 1600 self.assert_lint('for (int i = 0; ;', '')
1769 1601
1770 # Static or global STL strings. 1602 # Static or global STL strings.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1815 '}\n', '') 1647 '}\n', '')
1816 self.assert_lint('string Class<Type>::Method(\n' 1648 self.assert_lint('string Class<Type>::Method(\n'
1817 ' int arg) const\n' 1649 ' int arg) const\n'
1818 '{\n' 1650 '{\n'
1819 ' return "";\n' 1651 ' return "";\n'
1820 '}\n', '') 1652 '}\n', '')
1821 1653
1822 def test_no_spaces_in_function_calls(self): 1654 def test_no_spaces_in_function_calls(self):
1823 self.assert_lint('TellStory(1, 3);', 1655 self.assert_lint('TellStory(1, 3);',
1824 '') 1656 '')
1825 self.assert_lint('TellStory(1, 3 );',
1826 'Extra space before )'
1827 ' [whitespace/parens] [2]')
1828 self.assert_lint('TellStory(1 /* wolf */, 3 /* pigs */);', 1657 self.assert_lint('TellStory(1 /* wolf */, 3 /* pigs */);',
1829 '') 1658 '')
1830 self.assert_multi_line_lint('#endif\n );', 1659 self.assert_multi_line_lint('#endif\n );',
1831 '') 1660 '')
1832 1661
1833 def test_line_ending_in_whitespace(self):
1834 self.assert_lint('int a; // This is a sentence.',
1835 '')
1836 self.assert_lint('int a; // This is a sentence. ',
1837 'Line ends in whitespace. Consider deleting these extr a spaces. [whitespace/end_of_line] [4]')
1838
1839 def test_newline_at_eof(self):
1840 def do_test(self, data, is_missing_eof):
1841 error_collector = ErrorCollector(self.assertTrue)
1842 self.process_file_data('foo.cpp', 'cpp', data.split('\n'),
1843 error_collector)
1844 # The warning appears only once.
1845 self.assertEqual(
1846 int(is_missing_eof),
1847 error_collector.results().count(
1848 'Could not find a newline character at the end of the file.'
1849 ' [whitespace/ending_newline] [5]'))
1850
1851 do_test(self, '// Newline\n// at EOF\n', False)
1852 do_test(self, '// No newline\n// at EOF', True)
1853
1854 def test_invalid_utf8(self): 1662 def test_invalid_utf8(self):
1855 def do_test(self, raw_bytes, has_invalid_utf8): 1663 def do_test(self, raw_bytes, has_invalid_utf8):
1856 error_collector = ErrorCollector(self.assertTrue) 1664 error_collector = ErrorCollector(self.assertTrue)
1857 self.process_file_data('foo.cpp', 'cpp', 1665 self.process_file_data('foo.cpp', 'cpp',
1858 unicode(raw_bytes, 'utf8', 'replace').split(' \n'), 1666 unicode(raw_bytes, 'utf8', 'replace').split(' \n'),
1859 error_collector) 1667 error_collector)
1860 # The warning appears only once. 1668 # The warning appears only once.
1861 self.assertEqual( 1669 self.assertEqual(
1862 int(has_invalid_utf8), 1670 int(has_invalid_utf8),
1863 error_collector.results().count( 1671 error_collector.results().count(
1864 'Line contains invalid UTF-8' 1672 'Line contains invalid UTF-8'
1865 ' (or Unicode replacement character).' 1673 ' (or Unicode replacement character).'
1866 ' [readability/utf8] [5]')) 1674 ' [readability/utf8] [5]'))
1867 1675
1868 do_test(self, 'Hello world\n', False) 1676 do_test(self, 'Hello world\n', False)
1869 do_test(self, '\xe9\x8e\xbd\n', False) 1677 do_test(self, '\xe9\x8e\xbd\n', False)
1870 do_test(self, '\xe9x\x8e\xbd\n', True) 1678 do_test(self, '\xe9x\x8e\xbd\n', True)
1871 # This is the encoding of the replacement character itself (which 1679 # This is the encoding of the replacement character itself (which
1872 # you can see by evaluating codecs.getencoder('utf8')(u'\ufffd')). 1680 # you can see by evaluating codecs.getencoder('utf8')(u'\ufffd')).
1873 do_test(self, '\xef\xbf\xbd\n', True) 1681 do_test(self, '\xef\xbf\xbd\n', True)
1874 1682
1875 def test_is_blank_line(self): 1683 def test_is_blank_line(self):
1876 self.assertTrue(cpp_style.is_blank_line('')) 1684 self.assertTrue(cpp_style.is_blank_line(''))
1877 self.assertTrue(cpp_style.is_blank_line(' ')) 1685 self.assertTrue(cpp_style.is_blank_line(' '))
1878 self.assertTrue(cpp_style.is_blank_line(' \t\r\n')) 1686 self.assertTrue(cpp_style.is_blank_line(' \t\r\n'))
1879 self.assertTrue(not cpp_style.is_blank_line('int a;')) 1687 self.assertTrue(not cpp_style.is_blank_line('int a;'))
1880 self.assertTrue(not cpp_style.is_blank_line('{')) 1688 self.assertTrue(not cpp_style.is_blank_line('{'))
1881 1689
1882 def test_blank_lines_check(self):
1883 self.assert_blank_lines_check(['{\n', '\n', '\n', '}\n'], 1, 1)
1884 self.assert_blank_lines_check([' if (foo) {\n', '\n', ' }\n'], 1, 1)
1885 self.assert_blank_lines_check(
1886 ['\n', '// {\n', '\n', '\n', '// Comment\n', '{\n', '}\n'], 0, 0)
1887 self.assert_blank_lines_check(['\n', 'run("{");\n', '\n'], 0, 0)
1888 self.assert_blank_lines_check(['\n', ' if (foo) { return 0; }\n', '\n'] , 0, 0)
1889
1890 def test_allow_blank_line_before_closing_namespace(self):
1891 error_collector = ErrorCollector(self.assertTrue)
1892 self.process_file_data('foo.cpp', 'cpp',
1893 ['namespace {', '', '} // namespace'],
1894 error_collector)
1895 self.assertEqual(0, error_collector.results().count(
1896 'Blank line at the end of a code block. Is this needed?'
1897 ' [whitespace/blank_line] [3]'))
1898
1899 def test_allow_blank_line_before_if_else_chain(self):
1900 error_collector = ErrorCollector(self.assertTrue)
1901 self.process_file_data('foo.cpp', 'cpp',
1902 ['if (hoge) {',
1903 '', # No warning
1904 '} else if (piyo) {',
1905 '', # No warning
1906 '} else if (piyopiyo) {',
1907 ' hoge = true;', # No warning
1908 '} else {',
1909 '', # Warning on this line
1910 '}'],
1911 error_collector)
1912 self.assertEqual(1, error_collector.results().count(
1913 'Blank line at the end of a code block. Is this needed?'
1914 ' [whitespace/blank_line] [3]'))
1915
1916 def test_else_on_same_line_as_closing_braces(self):
1917 error_collector = ErrorCollector(self.assertTrue)
1918 self.process_file_data('foo.cpp', 'cpp',
1919 ['if (hoge) {',
1920 '',
1921 '}',
1922 ' else {' # Warning on this line
1923 '',
1924 '}'],
1925 error_collector)
1926
1927 def test_else_clause_not_on_same_line_as_else(self):
1928 self.assert_lint(' else if (blah) {', '')
1929 self.assert_lint(' variable_ends_in_else = true;', '')
1930
1931 def test_comma(self):
1932 self.assert_lint('a = f(1,2);',
1933 'Missing space after , [whitespace/comma] [3]')
1934 self.assert_lint('int tmp=a,a=b,b=tmp;',
1935 ['Missing spaces around = [whitespace/operators] [4]',
1936 'Missing space after , [whitespace/comma] [3]'])
1937 self.assert_lint('f(a, /* name */ b);', '')
1938 self.assert_lint('f(a, /* name */b);', '')
1939
1940 def test_declaration(self):
1941 self.assert_lint('int a;', '')
1942 self.assert_lint('int a;', 'Extra space between int and a [whitespace /declaration] [3]')
1943 self.assert_lint('int* a;', 'Extra space between int* and a [whitespac e/declaration] [3]')
1944 self.assert_lint('else if { }', '')
1945 self.assert_lint('else if { }', 'Extra space between else and if [whi tespace/declaration] [3]')
1946
1947 def test_pointer_reference_marker_location(self):
1948 self.assert_lint('int* b;', '', 'foo.cpp')
1949 self.assert_lint('int *b;',
1950 'Declaration has space between type name and * in int * b [whitespace/declaration] [3]',
1951 'foo.cpp')
1952 self.assert_lint('return *b;', '', 'foo.cpp')
1953 self.assert_lint('delete *b;', '', 'foo.cpp')
1954 self.assert_lint('int *b;', '', 'foo.c')
1955 self.assert_lint('int* b;',
1956 'Declaration has space between * and variable name in i nt* b [whitespace/declaration] [3]',
1957 'foo.c')
1958 self.assert_lint('int& b;', '', 'foo.cpp')
1959 self.assert_lint('int &b;',
1960 'Declaration has space between type name and & in int & b [whitespace/declaration] [3]',
1961 'foo.cpp')
1962 self.assert_lint('return &b;', '', 'foo.cpp')
1963
1964 def test_not_alabel(self): 1690 def test_not_alabel(self):
1965 self.assert_lint('MyVeryLongNamespace::MyVeryLongClassName::', '') 1691 self.assert_lint('MyVeryLongNamespace::MyVeryLongClassName::', '')
1966 1692
1967 def test_tab(self):
1968 self.assert_lint('\tint a;',
1969 'Tab found; better to use spaces [whitespace/tab] [1]' )
1970 self.assert_lint('int a = 5;\t// set a to 5',
1971 'Tab found; better to use spaces [whitespace/tab] [1]' )
1972
1973 def test_unnamed_namespaces_in_headers(self): 1693 def test_unnamed_namespaces_in_headers(self):
1974 self.assert_language_rules_check( 1694 self.assert_language_rules_check(
1975 'foo.h', 'namespace {', 1695 'foo.h', 'namespace {',
1976 'Do not use unnamed namespaces in header files. See' 1696 'Do not use unnamed namespaces in header files. See'
1977 ' http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Nam espaces' 1697 ' http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Nam espaces'
1978 ' for more information. [build/namespaces] [4]') 1698 ' for more information. [build/namespaces] [4]')
1979 # namespace registration macros are OK. 1699 # namespace registration macros are OK.
1980 self.assert_language_rules_check('foo.h', 'namespace { \\', '') 1700 self.assert_language_rules_check('foo.h', 'namespace { \\', '')
1981 # named namespaces are OK. 1701 # named namespaces are OK.
1982 self.assert_language_rules_check('foo.h', 'namespace foo {', '') 1702 self.assert_language_rules_check('foo.h', 'namespace foo {', '')
(...skipping 1512 matching lines...) Expand 10 before | Expand all | Expand 10 after
3495 'HDC dc2 = CreateCompatibleDC(dc);', 3215 'HDC dc2 = CreateCompatibleDC(dc);',
3496 'Use adoptPtr and OwnPtr<HDC> when calling CreateCompatibleDC to avo id potential ' 3216 'Use adoptPtr and OwnPtr<HDC> when calling CreateCompatibleDC to avo id potential '
3497 'memory leaks. [runtime/leaky_pattern] [5]') 3217 'memory leaks. [runtime/leaky_pattern] [5]')
3498 self.assert_leaky_pattern_check( 3218 self.assert_leaky_pattern_check(
3499 'adoptPtr(CreateCompatibleDC(dc));', 3219 'adoptPtr(CreateCompatibleDC(dc));',
3500 '') 3220 '')
3501 3221
3502 3222
3503 class WebKitStyleTest(CppStyleTestBase): 3223 class WebKitStyleTest(CppStyleTestBase):
3504 3224
3505 # for http://webkit.org/coding/coding-style.html 3225 # for https://www.chromium.org/blink/coding-style
3506 def test_indentation(self):
3507 # 1. Use spaces, not tabs. Tabs should only appear in files that
3508 # require them for semantic meaning, like Makefiles.
3509 self.assert_multi_line_lint(
3510 'class Foo {\n'
3511 ' int goo;\n'
3512 '};',
3513 '')
3514 self.assert_multi_line_lint(
3515 'class Foo {\n'
3516 '\tint goo;\n'
3517 '};',
3518 'Tab found; better to use spaces [whitespace/tab] [1]')
3519
3520 # 2. The indent size is 4 spaces.
3521 self.assert_multi_line_lint(
3522 'class Foo {\n'
3523 ' int goo;\n'
3524 '};',
3525 '')
3526
3527 # 3. In a header, code inside a namespace should not be indented.
3528 self.assert_multi_line_lint(
3529 'namespace WebCore {\n\n'
3530 'class Document {\n'
3531 ' int myVariable;\n'
3532 '};\n'
3533 '}',
3534 '',
3535 'foo.h')
3536 self.assert_multi_line_lint(
3537 'namespace WebCore {\n'
3538 'class Document {\n'
3539 '};\n'
3540 '}',
3541 '',
3542 'foo.h')
3543
3544 # 4. In an implementation file (files with the extension .cpp, .c
3545 # or .mm), code inside a namespace should not be indented.
3546 self.assert_multi_line_lint(
3547 'namespace WebCore {\n\n'
3548 'Document::Foo()\n'
3549 ' : foo(bar)\n'
3550 ' , boo(far)\n'
3551 '{\n'
3552 ' stuff();\n'
3553 '}',
3554 '',
3555 'foo.cpp')
3556 self.assert_multi_line_lint(
3557 'namespace WebCore {\n\n'
3558 'const char* foo[] = {\n'
3559 ' "void* b);", // }\n'
3560 ' "asfdf",\n'
3561 ' }\n'
3562 '}\n',
3563 '',
3564 'foo.cpp')
3565 self.assert_multi_line_lint(
3566 ' namespace WebCore {\n\n'
3567 ' void Document::Foo()\n'
3568 ' {\n'
3569 'start: // infinite loops are fun!\n'
3570 ' goto start;\n'
3571 ' }',
3572 'namespace should never be indented. [whitespace/indent] [4]',
3573 'foo.cpp')
3574 self.assert_multi_line_lint(
3575 'namespace WebCore {\n'
3576 '#define abc(x) x; \\\n'
3577 ' x\n'
3578 '}',
3579 '',
3580 'foo.cpp')
3581
3582 # 5. A case label should line up with its switch statement. The
3583 # case statement is indented.
3584 self.assert_multi_line_lint(
3585 ' switch (condition) {\n'
3586 ' case fooCondition:\n'
3587 ' case barCondition:\n'
3588 ' i++;\n'
3589 ' break;\n'
3590 ' default:\n'
3591 ' i--;\n'
3592 ' }\n',
3593 '')
3594 self.assert_multi_line_lint(
3595 ' switch (condition) {\n'
3596 ' case fooCondition:\n'
3597 ' switch (otherCondition) {\n'
3598 ' default:\n'
3599 ' return;\n'
3600 ' }\n'
3601 ' default:\n'
3602 ' i--;\n'
3603 ' }\n',
3604 '')
3605 self.assert_multi_line_lint(
3606 ' switch (condition) {\n'
3607 ' case fooCondition: break;\n'
3608 ' default: return;\n'
3609 ' }\n',
3610 '')
3611
3612 # 6. Boolean expressions at the same nesting level that span
3613 # multiple lines should have their operators on the left side of
3614 # the line instead of the right side.
3615 self.assert_multi_line_lint(
3616 ' return attr->name() == srcAttr\n'
3617 ' || attr->name() == lowsrcAttr;\n',
3618 '')
3619
3620 def test_spacing(self):
3621 # 1. Do not place spaces around unary operators.
3622 self.assert_multi_line_lint(
3623 'i++;',
3624 '')
3625 self.assert_multi_line_lint(
3626 'i ++;',
3627 'Extra space for operator ++; [whitespace/operators] [4]')
3628
3629 # 2. Do place spaces around binary and ternary operators.
3630 self.assert_multi_line_lint(
3631 'y = m * x + b;',
3632 '')
3633 self.assert_multi_line_lint(
3634 'f(a, b);',
3635 '')
3636 self.assert_multi_line_lint(
3637 'c = a | b;',
3638 '')
3639 self.assert_multi_line_lint(
3640 'return condition ? 1 : 0;',
3641 '')
3642 self.assert_multi_line_lint(
3643 'y=m*x+b;',
3644 'Missing spaces around = [whitespace/operators] [4]')
3645 self.assert_multi_line_lint(
3646 'f(a,b);',
3647 'Missing space after , [whitespace/comma] [3]')
3648 # FIXME: We cannot catch this lint error.
3649 # self.assert_multi_line_lint(
3650 # 'return condition ? 1:0;',
3651 # '')
3652
3653 # 3. Place spaces between control statements and their parentheses.
3654 self.assert_multi_line_lint(
3655 ' if (condition)\n'
3656 ' doIt();\n',
3657 '')
3658 self.assert_multi_line_lint(
3659 ' if(condition)\n'
3660 ' doIt();\n',
3661 'Missing space before ( in if( [whitespace/parens] [5]')
3662
3663 # 4. Do not place spaces between a function and its parentheses,
3664 # or between a parenthesis and its content.
3665 self.assert_multi_line_lint(
3666 'f(a, b);',
3667 '')
3668 self.assert_multi_line_lint(
3669 'f( a, b );',
3670 ['Extra space after ( in function call [whitespace/parens] [4]',
3671 'Extra space before ) [whitespace/parens] [2]'])
3672 3226
3673 def test_line_breaking(self): 3227 def test_line_breaking(self):
3674 # 1. Each statement should get its own line.
3675 self.assert_multi_line_lint(
3676 ' x++;\n'
3677 ' y++;\n'
3678 ' if (condition);\n'
3679 ' doIt();\n',
3680 '')
3681 self.assert_multi_line_lint(
3682 ' if (condition) \\\n'
3683 ' doIt();\n',
3684 '')
3685 self.assert_multi_line_lint(
3686 ' if (condition) doIt();\n',
3687 'More than one command on the same line in if [whitespace/parens] [ 4]')
3688 # Ignore preprocessor if's.
3689 self.assert_multi_line_lint(
3690 '#if (condition) || (condition2)\n',
3691 '')
3692
3693 # 2. An else statement should go on the same line as a preceding 3228 # 2. An else statement should go on the same line as a preceding
3694 # close brace if one is present, else it should line up with the 3229 # close brace if one is present, else it should line up with the
3695 # if statement. 3230 # if statement.
3696 self.assert_multi_line_lint( 3231 self.assert_multi_line_lint(
3697 'if (condition) {\n' 3232 'if (condition) {\n'
3698 ' doSomething();\n' 3233 ' doSomething();\n'
3699 ' doSomethingAgain();\n' 3234 ' doSomethingAgain();\n'
3700 '} else {\n' 3235 '} else {\n'
3701 ' doSomethingElse();\n' 3236 ' doSomethingElse();\n'
3702 ' doSomethingElseAgain();\n' 3237 ' doSomethingElseAgain();\n'
(...skipping 11 matching lines...) Expand all
3714 '} else {\n' 3249 '} else {\n'
3715 ' doSomethingElse();\n' 3250 ' doSomethingElse();\n'
3716 ' doSomethingElseAgain();\n' 3251 ' doSomethingElseAgain();\n'
3717 '}\n', 3252 '}\n',
3718 '') 3253 '')
3719 self.assert_multi_line_lint( 3254 self.assert_multi_line_lint(
3720 '#define TEST_ASSERT(expression) do { if (!(expression)) { ' 3255 '#define TEST_ASSERT(expression) do { if (!(expression)) { '
3721 'TestsController::shared().testFailed(__FILE__, __LINE__, #expressio n); ' 3256 'TestsController::shared().testFailed(__FILE__, __LINE__, #expressio n); '
3722 'return; } } while (0)\n', 3257 'return; } } while (0)\n',
3723 '') 3258 '')
3724 self.assert_multi_line_lint(
3725 '#define TEST_ASSERT(expression) do { if ( !(expression)) { '
3726 'TestsController::shared().testFailed(__FILE__, __LINE__, #expressio n); '
3727 'return; } } while (0)\n',
3728 'Extra space after ( in if [whitespace/parens] [5]')
3729 # FIXME: currently we only check first conditional, so we cannot detect errors in next ones. 3259 # FIXME: currently we only check first conditional, so we cannot detect errors in next ones.
3730 self.assert_multi_line_lint( 3260 self.assert_multi_line_lint(
3731 'WTF_MAKE_NONCOPYABLE(ClassName); WTF_MAKE_FAST_ALLOCATED;\n', 3261 'WTF_MAKE_NONCOPYABLE(ClassName); WTF_MAKE_FAST_ALLOCATED;\n',
3732 '') 3262 '')
3733 self.assert_multi_line_lint( 3263 self.assert_multi_line_lint(
3734 'if (condition) doSomething(); else doSomethingElse();\n',
3735 'More than one command on the same line in if [whitespace/parens] [ 4]')
3736 self.assert_multi_line_lint(
3737 'if (condition) doSomething(); else {\n' 3264 'if (condition) doSomething(); else {\n'
3738 ' doSomethingElse();\n' 3265 ' doSomethingElse();\n'
3739 '}\n', 3266 '}\n',
3740 ['More than one command on the same line in if [whitespace/parens] [4]', 3267 'If one part of an if-else statement uses curly braces, the other pa rt must too. [whitespace/braces] [4]')
3741 'If one part of an if-else statement uses curly braces, the other p art must too. [whitespace/braces] [4]'])
3742 self.assert_multi_line_lint( 3268 self.assert_multi_line_lint(
3743 'void func()\n' 3269 'void func()\n'
3744 '{\n' 3270 '{\n'
3745 ' while (condition) { }\n' 3271 ' while (condition) { }\n'
3746 ' return 0;\n' 3272 ' return 0;\n'
3747 '}\n', 3273 '}\n',
3748 '') 3274 '')
3749 self.assert_multi_line_lint(
3750 'void func()\n'
3751 '{\n'
3752 ' for (i = 0; i < 42; i++) { foobar(); }\n'
3753 ' return 0;\n'
3754 '}\n',
3755 'More than one command on the same line in for [whitespace/parens] [4]')
3756 3275
3757 # 3. An else if statement should be written as an if statement 3276 # 3. An else if statement should be written as an if statement
3758 # when the prior if concludes with a return statement. 3277 # when the prior if concludes with a return statement.
3759 self.assert_multi_line_lint( 3278 self.assert_multi_line_lint(
3760 'if (motivated) {\n' 3279 'if (motivated) {\n'
3761 ' if (liquid)\n' 3280 ' if (liquid)\n'
3762 ' return money;\n' 3281 ' return money;\n'
3763 '} else if (tired) {\n' 3282 '} else if (tired) {\n'
3764 ' break;\n' 3283 ' break;\n'
3765 '}', 3284 '}',
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
3864 'else {\n' 3383 'else {\n'
3865 ' prepare();\n' 3384 ' prepare();\n'
3866 ' continue;\n' 3385 ' continue;\n'
3867 '}\n', 3386 '}\n',
3868 ['If one part of an if-else statement uses curly braces, the other p art must too. [whitespace/braces] [4]', 3387 ['If one part of an if-else statement uses curly braces, the other p art must too. [whitespace/braces] [4]',
3869 'An else statement can be removed when the prior "if" concludes ' 3388 'An else statement can be removed when the prior "if" concludes '
3870 'with a return, break, continue or goto statement.' 3389 'with a return, break, continue or goto statement.'
3871 ' [readability/control_flow] [4]']) 3390 ' [readability/control_flow] [4]'])
3872 3391
3873 def test_braces(self): 3392 def test_braces(self):
3874 # 1. Function definitions: place each brace on its own line.
3875 self.assert_multi_line_lint(
3876 'int main()\n'
3877 '{\n'
3878 ' doSomething();\n'
3879 '}\n',
3880 '')
3881
3882 # 2. Other braces: place the open brace on the line preceding the
3883 # code block; place the close brace on its own line.
3884 self.assert_multi_line_lint(
3885 'class MyClass {\n'
3886 ' int foo;\n'
3887 '};\n',
3888 '')
3889 self.assert_multi_line_lint(
3890 'namespace WebCore {\n'
3891 'int foo;\n'
3892 '};\n',
3893 '')
3894 self.assert_multi_line_lint(
3895 'for (int i = 0; i < 10; i++) {\n'
3896 ' DoSomething();\n'
3897 '};\n',
3898 '')
3899
3900 # 3. Curly braces are not required for single-line conditionals and 3393 # 3. Curly braces are not required for single-line conditionals and
3901 # loop bodies, but are required for single-statement bodies that 3394 # loop bodies, but are required for single-statement bodies that
3902 # span multiple lines. 3395 # span multiple lines.
3903 3396
3904 # 3397 #
3905 # Positive tests 3398 # Positive tests
3906 # 3399 #
3907 self.assert_multi_line_lint( 3400 self.assert_multi_line_lint(
3908 'if (condition1)\n' 3401 'if (condition1)\n'
3909 ' statement1();\n' 3402 ' statement1();\n'
(...skipping 856 matching lines...) Expand 10 before | Expand all | Expand 10 after
4766 def test_ne(self): 4259 def test_ne(self):
4767 """Test __ne__ inequality function.""" 4260 """Test __ne__ inequality function."""
4768 checker1 = self._checker() 4261 checker1 = self._checker()
4769 checker2 = self._checker() 4262 checker2 = self._checker()
4770 4263
4771 # != calls __ne__. 4264 # != calls __ne__.
4772 # By default, __ne__ always returns true on different objects. 4265 # By default, __ne__ always returns true on different objects.
4773 # Thus, just check the distinguishing case to verify that the 4266 # Thus, just check the distinguishing case to verify that the
4774 # code defines __ne__. 4267 # code defines __ne__.
4775 self.assertFalse(checker1 != checker2) 4268 self.assertFalse(checker1 != checker2)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698