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

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

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