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

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

Issue 24406002: Add code style check error for using static_cast instead of toFoo function. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Corrected base_dir to Source to avoid searching header file in LayoutTests Created 7 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
« no previous file with comments | « Tools/Scripts/webkitpy/style/checkers/cpp.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 '+runtime/leaky_pattern') 303 '+runtime/leaky_pattern')
304 return self.perform_lint(code, 'test.cpp', basic_error_rules) 304 return self.perform_lint(code, 'test.cpp', basic_error_rules)
305 305
306 # Only include what you use errors. 306 # Only include what you use errors.
307 def perform_include_what_you_use(self, code, filename='foo.h', io=codecs): 307 def perform_include_what_you_use(self, code, filename='foo.h', io=codecs):
308 basic_error_rules = ('-', 308 basic_error_rules = ('-',
309 '+build/include_what_you_use') 309 '+build/include_what_you_use')
310 unit_test_config = {cpp_style.INCLUDE_IO_INJECTION_KEY: io} 310 unit_test_config = {cpp_style.INCLUDE_IO_INJECTION_KEY: io}
311 return self.perform_lint(code, filename, basic_error_rules, unit_test_co nfig) 311 return self.perform_lint(code, filename, basic_error_rules, unit_test_co nfig)
312 312
313 def perform_avoid_static_cast_of_objects(self, code, filename='foo.cpp', io= codecs):
314 basic_error_rules = ('-',
315 '+runtime/casting')
316 unit_test_config = {cpp_style.INCLUDE_IO_INJECTION_KEY: io}
317 return self.perform_lint(code, filename, basic_error_rules, unit_test_co nfig)
318
313 # Perform lint and compare the error message with "expected_message". 319 # Perform lint and compare the error message with "expected_message".
314 def assert_lint(self, code, expected_message, file_name='foo.cpp'): 320 def assert_lint(self, code, expected_message, file_name='foo.cpp'):
315 self.assertEqual(expected_message, self.perform_single_line_lint(code, f ile_name)) 321 self.assertEqual(expected_message, self.perform_single_line_lint(code, f ile_name))
316 322
317 def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_ name='foo.cpp'): 323 def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_ name='foo.cpp'):
318 messages = self.perform_single_line_lint(code, file_name) 324 messages = self.perform_single_line_lint(code, file_name)
319 for message in messages: 325 for message in messages:
320 if re.search(expected_message_re, message): 326 if re.search(expected_message_re, message):
321 return 327 return
322 328
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 def test_runtime_rtti(self): 763 def test_runtime_rtti(self):
758 statement = 'int* x = dynamic_cast<int*>(&foo);' 764 statement = 'int* x = dynamic_cast<int*>(&foo);'
759 error_message = ( 765 error_message = (
760 'Do not use dynamic_cast<>. If you need to cast within a class ' 766 'Do not use dynamic_cast<>. If you need to cast within a class '
761 'hierarchy, use static_cast<> to upcast. Google doesn\'t support ' 767 'hierarchy, use static_cast<> to upcast. Google doesn\'t support '
762 'RTTI. [runtime/rtti] [5]') 768 'RTTI. [runtime/rtti] [5]')
763 # dynamic_cast is disallowed in most files. 769 # dynamic_cast is disallowed in most files.
764 self.assert_language_rules_check('foo.cpp', statement, error_message) 770 self.assert_language_rules_check('foo.cpp', statement, error_message)
765 self.assert_language_rules_check('foo.h', statement, error_message) 771 self.assert_language_rules_check('foo.h', statement, error_message)
766 772
767 # Test for static_cast readability. 773 # Tests for static_cast readability.
768 def test_static_cast_readability(self): 774 def test_static_cast_on_objects_with_toFoo(self):
769 self.assert_lint( 775 mock_header_contents = ['inline Foo* toFoo(Bar* bar)']
770 'Text* x = static_cast<Text*>(foo);', 776 message = self.perform_avoid_static_cast_of_objects(
771 'Consider using toText helper function in WebCore/dom/Text.h ' 777 'Foo* x = static_cast<Foo*>(bar);',
772 'instead of static_cast<Text*>' 778 filename='casting.cpp',
773 ' [readability/check] [4]') 779 io=MockIo(mock_header_contents))
780 self.assertEqual(message, 'static_cast of class objects is not allowed. Use toFoo defined in Foo.h.'
781 ' [runtime/casting] [4]')
782
783 def test_static_cast_on_objects_without_toFoo(self):
784 mock_header_contents = ['inline FooBar* toFooBar(Bar* bar)']
785 message = self.perform_avoid_static_cast_of_objects(
786 'Foo* x = static_cast<Foo*>(bar);',
787 filename='casting.cpp',
788 io=MockIo(mock_header_contents))
789 self.assertEqual(message, 'static_cast of class objects is not allowed. Add toFoo in Foo.h and use it instead.'
790 ' [runtime/casting] [4]')
774 791
775 # We cannot test this functionality because of difference of 792 # We cannot test this functionality because of difference of
776 # function definitions. Anyway, we may never enable this. 793 # function definitions. Anyway, we may never enable this.
777 # 794 #
778 # # Test for unnamed arguments in a method. 795 # # Test for unnamed arguments in a method.
779 # def test_check_for_unnamed_params(self): 796 # def test_check_for_unnamed_params(self):
780 # message = ('All parameters should be named in a function' 797 # message = ('All parameters should be named in a function'
781 # ' [readability/function] [3]') 798 # ' [readability/function] [3]')
782 # self.assert_lint('virtual void A(int*) const;', message) 799 # self.assert_lint('virtual void A(int*) const;', message)
783 # self.assert_lint('virtual void B(void (*fn)(int*));', message) 800 # self.assert_lint('virtual void B(void (*fn)(int*));', message)
(...skipping 4367 matching lines...) Expand 10 before | Expand all | Expand 10 after
5151 def test_ne(self): 5168 def test_ne(self):
5152 """Test __ne__ inequality function.""" 5169 """Test __ne__ inequality function."""
5153 checker1 = self._checker() 5170 checker1 = self._checker()
5154 checker2 = self._checker() 5171 checker2 = self._checker()
5155 5172
5156 # != calls __ne__. 5173 # != calls __ne__.
5157 # By default, __ne__ always returns true on different objects. 5174 # By default, __ne__ always returns true on different objects.
5158 # Thus, just check the distinguishing case to verify that the 5175 # Thus, just check the distinguishing case to verify that the
5159 # code defines __ne__. 5176 # code defines __ne__.
5160 self.assertFalse(checker1 != checker2) 5177 self.assertFalse(checker1 != checker2)
OLDNEW
« no previous file with comments | « Tools/Scripts/webkitpy/style/checkers/cpp.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698