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

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

Issue 153953009: Revert of Update the static_cast style check to check for DEFINE_TYPE_CASTS patterns as well. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 10 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 return self.perform_lint(code, 'test.cpp', basic_error_rules) 293 return self.perform_lint(code, 'test.cpp', basic_error_rules)
294 294
295 # Only include what you use errors. 295 # Only include what you use errors.
296 def perform_include_what_you_use(self, code, filename='foo.h', fs=None): 296 def perform_include_what_you_use(self, code, filename='foo.h', fs=None):
297 basic_error_rules = ('-', 297 basic_error_rules = ('-',
298 '+build/include_what_you_use') 298 '+build/include_what_you_use')
299 return self.perform_lint(code, filename, basic_error_rules, fs) 299 return self.perform_lint(code, filename, basic_error_rules, fs)
300 300
301 def perform_avoid_static_cast_of_objects(self, code, filename='foo.cpp', fs= None): 301 def perform_avoid_static_cast_of_objects(self, code, filename='foo.cpp', fs= None):
302 basic_error_rules = ('-', 302 basic_error_rules = ('-',
303 '+security/casting',
304 '+runtime/casting') 303 '+runtime/casting')
305 return self.perform_lint(code, filename, basic_error_rules, fs) 304 return self.perform_lint(code, filename, basic_error_rules, fs)
306 305
307 # Perform lint and compare the error message with "expected_message". 306 # Perform lint and compare the error message with "expected_message".
308 def assert_lint(self, code, expected_message, file_name='foo.cpp'): 307 def assert_lint(self, code, expected_message, file_name='foo.cpp'):
309 self.assertEqual(expected_message, self.perform_single_line_lint(code, f ile_name)) 308 self.assertEqual(expected_message, self.perform_single_line_lint(code, f ile_name))
310 309
311 def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_ name='foo.cpp'): 310 def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_ name='foo.cpp'):
312 messages = self.perform_single_line_lint(code, file_name) 311 messages = self.perform_single_line_lint(code, file_name)
313 for message in messages: 312 for message in messages:
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 statement = 'int* x = dynamic_cast<int*>(&foo);' 751 statement = 'int* x = dynamic_cast<int*>(&foo);'
753 error_message = ( 752 error_message = (
754 'Do not use dynamic_cast<>. If you need to cast within a class ' 753 'Do not use dynamic_cast<>. If you need to cast within a class '
755 'hierarchy, use static_cast<> to upcast. Google doesn\'t support ' 754 'hierarchy, use static_cast<> to upcast. Google doesn\'t support '
756 'RTTI. [runtime/rtti] [5]') 755 'RTTI. [runtime/rtti] [5]')
757 # dynamic_cast is disallowed in most files. 756 # dynamic_cast is disallowed in most files.
758 self.assert_language_rules_check('foo.cpp', statement, error_message) 757 self.assert_language_rules_check('foo.cpp', statement, error_message)
759 self.assert_language_rules_check('foo.h', statement, error_message) 758 self.assert_language_rules_check('foo.h', statement, error_message)
760 759
761 # Tests for static_cast readability. 760 # Tests for static_cast readability.
762 # FIXME: Add cases for testing the DEFINE_TYPE_CASTS usecases also.
763 def test_static_cast_on_objects_with_toFoo(self): 761 def test_static_cast_on_objects_with_toFoo(self):
764 mock_header_contents = ['inline Foo* toFoo(Bar* bar)'] 762 mock_header_contents = ['inline Foo* toFoo(Bar* bar)']
765 fs = FileSystem() 763 fs = FileSystem()
766 orig_read_text_file_fn = fs.read_text_file 764 orig_read_text_file_fn = fs.read_text_file
767 765
768 def mock_read_text_file_fn(path): 766 def mock_read_text_file_fn(path):
769 return mock_header_contents 767 return mock_header_contents
770 768
771 try: 769 try:
772 fs.read_text_file = mock_read_text_file_fn 770 fs.read_text_file = mock_read_text_file_fn
773 message = self.perform_avoid_static_cast_of_objects( 771 message = self.perform_avoid_static_cast_of_objects(
774 'Foo* x = static_cast<Foo*>(bar);', 772 'Foo* x = static_cast<Foo*>(bar);',
775 filename='casting.cpp', 773 filename='casting.cpp',
776 fs=fs) 774 fs=fs)
777 self.assertEqual(message, 'static_cast of class objects is not allow ed. Use toFoo defined in Foo.h.' 775 self.assertEqual(message, 'static_cast of class objects is not allow ed. Use toFoo defined in Foo.h.'
778 ' [security/casting] [4]') 776 ' [runtime/casting] [4]')
779 finally: 777 finally:
780 fs.read_text_file = orig_read_text_file_fn 778 fs.read_text_file = orig_read_text_file_fn
781 779
782 def test_static_cast_on_objects_without_toFoo(self): 780 def test_static_cast_on_objects_without_toFoo(self):
783 mock_header_contents = ['inline FooBar* toFooBar(Bar* bar)'] 781 mock_header_contents = ['inline FooBar* toFooBar(Bar* bar)']
784 fs = FileSystem() 782 fs = FileSystem()
785 orig_read_text_file_fn = fs.read_text_file 783 orig_read_text_file_fn = fs.read_text_file
786 784
787 def mock_read_text_file_fn(path): 785 def mock_read_text_file_fn(path):
788 return mock_header_contents 786 return mock_header_contents
789 787
790 try: 788 try:
791 fs.read_text_file = mock_read_text_file_fn 789 fs.read_text_file = mock_read_text_file_fn
792 message = self.perform_avoid_static_cast_of_objects( 790 message = self.perform_avoid_static_cast_of_objects(
793 'Foo* x = static_cast<Foo*>(bar);', 791 'Foo* x = static_cast<Foo*>(bar);',
794 filename='casting.cpp', 792 filename='casting.cpp',
795 fs=fs) 793 fs=fs)
796 self.assertEqual(message, 'static_cast of class objects is not allow ed. Define cast macro DEFINE_TYPE_CASTS(Foo) in Foo.h and use it.' 794 self.assertEqual(message, 'static_cast of class objects is not allow ed. Add toFoo in Foo.h and use it instead.'
797 ' [runtime/casting] [4]') 795 ' [runtime/casting] [4]')
798 finally: 796 finally:
799 fs.read_text_file = orig_read_text_file_fn 797 fs.read_text_file = orig_read_text_file_fn
800 798
801 # We cannot test this functionality because of difference of 799 # We cannot test this functionality because of difference of
802 # function definitions. Anyway, we may never enable this. 800 # function definitions. Anyway, we may never enable this.
803 # 801 #
804 # # Test for unnamed arguments in a method. 802 # # Test for unnamed arguments in a method.
805 # def test_check_for_unnamed_params(self): 803 # def test_check_for_unnamed_params(self):
806 # message = ('All parameters should be named in a function' 804 # message = ('All parameters should be named in a function'
(...skipping 4380 matching lines...) Expand 10 before | Expand all | Expand 10 after
5187 def test_ne(self): 5185 def test_ne(self):
5188 """Test __ne__ inequality function.""" 5186 """Test __ne__ inequality function."""
5189 checker1 = self._checker() 5187 checker1 = self._checker()
5190 checker2 = self._checker() 5188 checker2 = self._checker()
5191 5189
5192 # != calls __ne__. 5190 # != calls __ne__.
5193 # By default, __ne__ always returns true on different objects. 5191 # By default, __ne__ always returns true on different objects.
5194 # Thus, just check the distinguishing case to verify that the 5192 # Thus, just check the distinguishing case to verify that the
5195 # code defines __ne__. 5193 # code defines __ne__.
5196 self.assertFalse(checker1 != checker2) 5194 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