/webview/native/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py
Python | 4802 lines | 4706 code | 31 blank | 65 comment | 121 complexity | d4f3d466cdc362dc9d69e0d04700e462 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- #!/usr/bin/python
- # -*- coding: utf-8; -*-
- #
- # Copyright (C) 2011 Google Inc. All rights reserved.
- # Copyright (C) 2009 Torch Mobile Inc.
- # Copyright (C) 2009 Apple Inc. All rights reserved.
- # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are
- # met:
- #
- # * Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # * Redistributions in binary form must reproduce the above
- # copyright notice, this list of conditions and the following disclaimer
- # in the documentation and/or other materials provided with the
- # distribution.
- # * Neither the name of Google Inc. nor the names of its
- # contributors may be used to endorse or promote products derived from
- # this software without specific prior written permission.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- """Unit test for cpp_style.py."""
- # FIXME: Add a good test that tests UpdateIncludeState.
- import codecs
- import os
- import random
- import re
- import unittest
- import cpp as cpp_style
- from cpp import CppChecker
- from ..filter import FilterConfiguration
- # This class works as an error collector and replaces cpp_style.Error
- # function for the unit tests. We also verify each category we see
- # is in STYLE_CATEGORIES, to help keep that list up to date.
- class ErrorCollector:
- _all_style_categories = CppChecker.categories
- # This is a list including all categories seen in any unit test.
- _seen_style_categories = {}
- def __init__(self, assert_fn, filter=None, lines_to_check=None):
- """assert_fn: a function to call when we notice a problem.
- filter: filters the errors that we are concerned about."""
- self._assert_fn = assert_fn
- self._errors = []
- self._lines_to_check = lines_to_check
- if not filter:
- filter = FilterConfiguration()
- self._filter = filter
- def __call__(self, line_number, category, confidence, message):
- self._assert_fn(category in self._all_style_categories,
- 'Message "%s" has category "%s",'
- ' which is not in STYLE_CATEGORIES' % (message, category))
- if self._lines_to_check and not line_number in self._lines_to_check:
- return False
- if self._filter.should_check(category, ""):
- self._seen_style_categories[category] = 1
- self._errors.append('%s [%s] [%d]' % (message, category, confidence))
- return True
- def results(self):
- if len(self._errors) < 2:
- return ''.join(self._errors) # Most tests expect to have a string.
- else:
- return self._errors # Let's give a list if there is more than one.
- def result_list(self):
- return self._errors
- def verify_all_categories_are_seen(self):
- """Fails if there's a category in _all_style_categories - _seen_style_categories.
- This should only be called after all tests are run, so
- _seen_style_categories has had a chance to fully populate. Since
- this isn't called from within the normal unittest framework, we
- can't use the normal unittest assert macros. Instead we just exit
- when we see an error. Good thing this test is always run last!
- """
- for category in self._all_style_categories:
- if category not in self._seen_style_categories:
- import sys
- sys.exit('FATAL ERROR: There are no tests for category "%s"' % category)
- # This class is a lame mock of codecs. We do not verify filename, mode, or
- # encoding, but for the current use case it is not needed.
- class MockIo:
- def __init__(self, mock_file):
- self.mock_file = mock_file
- def open(self, unused_filename, unused_mode, unused_encoding, _): # NOLINT
- # (lint doesn't like open as a method name)
- return self.mock_file
- class CppFunctionsTest(unittest.TestCase):
- """Supports testing functions that do not need CppStyleTestBase."""
- def test_convert_to_lower_with_underscores(self):
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('ABC'), 'abc')
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('aB'), 'a_b')
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('isAName'), 'is_a_name')
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('AnotherTest'), 'another_test')
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('PassRefPtr<MyClass>'), 'pass_ref_ptr<my_class>')
- self.assertEquals(cpp_style._convert_to_lower_with_underscores('_ABC'), '_abc')
- def test_create_acronym(self):
- self.assertEquals(cpp_style._create_acronym('ABC'), 'ABC')
- self.assertEquals(cpp_style._create_acronym('IsAName'), 'IAN')
- self.assertEquals(cpp_style._create_acronym('PassRefPtr<MyClass>'), 'PRP<MC>')
- def test_is_c_or_objective_c(self):
- clean_lines = cpp_style.CleansedLines([''])
- clean_objc_lines = cpp_style.CleansedLines(['#import "header.h"'])
- self.assertTrue(cpp_style._FileState(clean_lines, 'c').is_c_or_objective_c())
- self.assertTrue(cpp_style._FileState(clean_lines, 'm').is_c_or_objective_c())
- self.assertFalse(cpp_style._FileState(clean_lines, 'cpp').is_c_or_objective_c())
- self.assertFalse(cpp_style._FileState(clean_lines, 'cc').is_c_or_objective_c())
- self.assertFalse(cpp_style._FileState(clean_lines, 'h').is_c_or_objective_c())
- self.assertTrue(cpp_style._FileState(clean_objc_lines, 'h').is_c_or_objective_c())
- def test_parameter(self):
- # Test type.
- parameter = cpp_style.Parameter('ExceptionCode', 13, 1)
- self.assertEquals(parameter.type, 'ExceptionCode')
- self.assertEquals(parameter.name, '')
- self.assertEquals(parameter.row, 1)
- # Test type and name.
- parameter = cpp_style.Parameter('PassRefPtr<MyClass> parent', 19, 1)
- self.assertEquals(parameter.type, 'PassRefPtr<MyClass>')
- self.assertEquals(parameter.name, 'parent')
- self.assertEquals(parameter.row, 1)
- # Test type, no name, with default value.
- parameter = cpp_style.Parameter('MyClass = 0', 7, 0)
- self.assertEquals(parameter.type, 'MyClass')
- self.assertEquals(parameter.name, '')
- self.assertEquals(parameter.row, 0)
- # Test type, name, and default value.
- parameter = cpp_style.Parameter('MyClass a = 0', 7, 0)
- self.assertEquals(parameter.type, 'MyClass')
- self.assertEquals(parameter.name, 'a')
- self.assertEquals(parameter.row, 0)
- def test_single_line_view(self):
- start_position = cpp_style.Position(row=1, column=1)
- end_position = cpp_style.Position(row=3, column=1)
- single_line_view = cpp_style.SingleLineView(['0', 'abcde', 'fgh', 'i'], start_position, end_position)
- self.assertEquals(single_line_view.single_line, 'bcde fgh i')
- self.assertEquals(single_line_view.convert_column_to_row(0), 1)
- self.assertEquals(single_line_view.convert_column_to_row(4), 1)
- self.assertEquals(single_line_view.convert_column_to_row(5), 2)
- self.assertEquals(single_line_view.convert_column_to_row(8), 2)
- self.assertEquals(single_line_view.convert_column_to_row(9), 3)
- self.assertEquals(single_line_view.convert_column_to_row(100), 3)
- start_position = cpp_style.Position(row=0, column=3)
- end_position = cpp_style.Position(row=0, column=4)
- single_line_view = cpp_style.SingleLineView(['abcdef'], start_position, end_position)
- self.assertEquals(single_line_view.single_line, 'd')
- def test_create_skeleton_parameters(self):
- self.assertEquals(cpp_style.create_skeleton_parameters(''), '')
- self.assertEquals(cpp_style.create_skeleton_parameters(' '), ' ')
- self.assertEquals(cpp_style.create_skeleton_parameters('long'), 'long,')
- self.assertEquals(cpp_style.create_skeleton_parameters('const unsigned long int'), ' int,')
- self.assertEquals(cpp_style.create_skeleton_parameters('long int*'), ' int ,')
- self.assertEquals(cpp_style.create_skeleton_parameters('PassRefPtr<Foo> a'), 'PassRefPtr a,')
- self.assertEquals(cpp_style.create_skeleton_parameters(
- 'ComplexTemplate<NestedTemplate1<MyClass1, MyClass2>, NestedTemplate1<MyClass1, MyClass2> > param, int second'),
- 'ComplexTemplate param, int second,')
- self.assertEquals(cpp_style.create_skeleton_parameters('int = 0, Namespace::Type& a'), 'int , Type a,')
- # Create skeleton parameters is a bit too aggressive with function variables, but
- # it allows for parsing other parameters and declarations like this are rare.
- self.assertEquals(cpp_style.create_skeleton_parameters('void (*fn)(int a, int b), Namespace::Type& a'),
- 'void , Type a,')
- # This doesn't look like functions declarations but the simplifications help to eliminate false positives.
- self.assertEquals(cpp_style.create_skeleton_parameters('b{d}'), 'b ,')
- def test_find_parameter_name_index(self):
- self.assertEquals(cpp_style.find_parameter_name_index(' int a '), 5)
- self.assertEquals(cpp_style.find_parameter_name_index(' PassRefPtr '), 16)
- self.assertEquals(cpp_style.find_parameter_name_index('double'), 6)
- def test_parameter_list(self):
- elided_lines = ['int blah(PassRefPtr<MyClass> paramName,',
- 'const Other1Class& foo,',
- 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const * param = new ComplexTemplate<Class1, NestedTemplate<P1, P2> >(34, 42),',
- 'int* myCount = 0);']
- start_position = cpp_style.Position(row=0, column=8)
- end_position = cpp_style.Position(row=3, column=16)
- expected_parameters = ({'type': 'PassRefPtr<MyClass>', 'name': 'paramName', 'row': 0},
- {'type': 'const Other1Class&', 'name': 'foo', 'row': 1},
- {'type': 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const *', 'name': 'param', 'row': 2},
- {'type': 'int*', 'name': 'myCount', 'row': 3})
- index = 0
- for parameter in cpp_style.parameter_list(elided_lines, start_position, end_position):
- expected_parameter = expected_parameters[index]
- self.assertEquals(parameter.type, expected_parameter['type'])
- self.assertEquals(parameter.name, expected_parameter['name'])
- self.assertEquals(parameter.row, expected_parameter['row'])
- index += 1
- self.assertEquals(index, len(expected_parameters))
- def test_check_parameter_against_text(self):
- error_collector = ErrorCollector(self.assert_)
- parameter = cpp_style.Parameter('FooF ooF', 4, 1)
- self.assertFalse(cpp_style._check_parameter_name_against_text(parameter, 'FooF', error_collector))
- self.assertEquals(error_collector.results(),
- 'The parameter name "ooF" adds no information, so it should be removed. [readability/parameter_name] [5]')
- class CppStyleTestBase(unittest.TestCase):
- """Provides some useful helper functions for cpp_style tests.
- Attributes:
- min_confidence: An integer that is the current minimum confidence
- level for the tests.
- """
- # FIXME: Refactor the unit tests so the confidence level is passed
- # explicitly, just like it is in the real code.
- min_confidence = 1;
- # Helper function to avoid needing to explicitly pass confidence
- # in all the unit test calls to cpp_style.process_file_data().
- def process_file_data(self, filename, file_extension, lines, error, unit_test_config={}):
- """Call cpp_style.process_file_data() with the min_confidence."""
- return cpp_style.process_file_data(filename, file_extension, lines,
- error, self.min_confidence, unit_test_config)
- def perform_lint(self, code, filename, basic_error_rules, unit_test_config={}, lines_to_check=None):
- error_collector = ErrorCollector(self.assert_, FilterConfiguration(basic_error_rules), lines_to_check)
- lines = code.split('\n')
- extension = filename.split('.')[1]
- self.process_file_data(filename, extension, lines, error_collector, unit_test_config)
- return error_collector.results()
- # Perform lint on single line of input and return the error message.
- def perform_single_line_lint(self, code, filename):
- basic_error_rules = ('-build/header_guard',
- '-legal/copyright',
- '-readability/fn_size',
- '-readability/parameter_name',
- '-readability/pass_ptr',
- '-whitespace/ending_newline')
- return self.perform_lint(code, filename, basic_error_rules)
- # Perform lint over multiple lines and return the error message.
- def perform_multi_line_lint(self, code, file_extension):
- basic_error_rules = ('-build/header_guard',
- '-legal/copyright',
- '-readability/parameter_name',
- '-whitespace/ending_newline')
- return self.perform_lint(code, 'test.' + file_extension, basic_error_rules)
- # Only keep some errors related to includes, namespaces and rtti.
- def perform_language_rules_check(self, filename, code, lines_to_check=None):
- basic_error_rules = ('-',
- '+build/include',
- '+build/include_order',
- '+build/namespaces',
- '+runtime/rtti')
- return self.perform_lint(code, filename, basic_error_rules, lines_to_check=lines_to_check)
- # Only keep function length errors.
- def perform_function_lengths_check(self, code):
- basic_error_rules = ('-',
- '+readability/fn_size')
- return self.perform_lint(code, 'test.cpp', basic_error_rules)
- # Only keep pass ptr errors.
- def perform_pass_ptr_check(self, code):
- basic_error_rules = ('-',
- '+readability/pass_ptr')
- return self.perform_lint(code, 'test.cpp', basic_error_rules)
- # Only keep leaky pattern errors.
- def perform_leaky_pattern_check(self, code):
- basic_error_rules = ('-',
- '+runtime/leaky_pattern')
- return self.perform_lint(code, 'test.cpp', basic_error_rules)
- # Only include what you use errors.
- def perform_include_what_you_use(self, code, filename='foo.h', io=codecs):
- basic_error_rules = ('-',
- '+build/include_what_you_use')
- unit_test_config = {cpp_style.INCLUDE_IO_INJECTION_KEY: io}
- return self.perform_lint(code, filename, basic_error_rules, unit_test_config)
- # Perform lint and compare the error message with "expected_message".
- def assert_lint(self, code, expected_message, file_name='foo.cpp'):
- self.assertEquals(expected_message, self.perform_single_line_lint(code, file_name))
- def assert_lint_one_of_many_errors_re(self, code, expected_message_re, file_name='foo.cpp'):
- messages = self.perform_single_line_lint(code, file_name)
- for message in messages:
- if re.search(expected_message_re, message):
- return
- self.assertEquals(expected_message_re, messages)
- def assert_multi_line_lint(self, code, expected_message, file_name='foo.h'):
- file_extension = file_name[file_name.rfind('.') + 1:]
- self.assertEquals(expected_message, self.perform_multi_line_lint(code, file_extension))
- def assert_multi_line_lint_re(self, code, expected_message_re, file_name='foo.h'):
- file_extension = file_name[file_name.rfind('.') + 1:]
- message = self.perform_multi_line_lint(code, file_extension)
- if not re.search(expected_message_re, message):
- self.fail('Message was:\n' + message + 'Expected match to "' + expected_message_re + '"')
- def assert_language_rules_check(self, file_name, code, expected_message, lines_to_check=None):
- self.assertEquals(expected_message,
- self.perform_language_rules_check(file_name, code, lines_to_check))
- def assert_include_what_you_use(self, code, expected_message):
- self.assertEquals(expected_message,
- self.perform_include_what_you_use(code))
- def assert_blank_lines_check(self, lines, start_errors, end_errors):
- error_collector = ErrorCollector(self.assert_)
- self.process_file_data('foo.cpp', 'cpp', lines, error_collector)
- self.assertEquals(
- start_errors,
- error_collector.results().count(
- 'Blank line at the start of a code block. Is this needed?'
- ' [whitespace/blank_line] [2]'))
- self.assertEquals(
- end_errors,
- error_collector.results().count(
- 'Blank line at the end of a code block. Is this needed?'
- ' [whitespace/blank_line] [3]'))
- def assert_positions_equal(self, position, tuple_position):
- """Checks if the two positions are equal.
- position: a cpp_style.Position object.
- tuple_position: a tuple (row, column) to compare against."""
- self.assertEquals(position, cpp_style.Position(tuple_position[0], tuple_position[1]),
- 'position %s, tuple_position %s' % (position, tuple_position))
- class FunctionDetectionTest(CppStyleTestBase):
- def perform_function_detection(self, lines, function_information, detection_line=0):
- clean_lines = cpp_style.CleansedLines(lines)
- function_state = cpp_style._FunctionState(5)
- error_collector = ErrorCollector(self.assert_)
- cpp_style.detect_functions(clean_lines, detection_line, function_state, error_collector)
- if not function_information:
- self.assertEquals(function_state.in_a_function, False)
- return
- self.assertEquals(function_state.in_a_function, True)
- self.assertEquals(function_state.current_function, function_information['name'] + '()')
- self.assertEquals(function_state.modifiers_and_return_type(), function_information['modifiers_and_return_type'])
- self.assertEquals(function_state.is_pure, function_information['is_pure'])
- self.assertEquals(function_state.is_declaration, function_information['is_declaration'])
- self.assert_positions_equal(function_state.function_name_start_position, function_information['function_name_start_position'])
- self.assert_positions_equal(function_state.parameter_start_position, function_information['parameter_start_position'])
- self.assert_positions_equal(function_state.parameter_end_position, function_information['parameter_end_position'])
- self.assert_positions_equal(function_state.body_start_position, function_information['body_start_position'])
- self.assert_positions_equal(function_state.end_position, function_information['end_position'])
- expected_parameters = function_information.get('parameter_list')
- if expected_parameters:
- actual_parameters = function_state.parameter_list()
- self.assertEquals(len(actual_parameters), len(expected_parameters))
- for index in range(len(expected_parameters)):
- actual_parameter = actual_parameters[index]
- expected_parameter = expected_parameters[index]
- self.assertEquals(actual_parameter.type, expected_parameter['type'])
- self.assertEquals(actual_parameter.name, expected_parameter['name'])
- self.assertEquals(actual_parameter.row, expected_parameter['row'])
- def test_basic_function_detection(self):
- self.perform_function_detection(
- ['void theTestFunctionName(int) {',
- '}'],
- {'name': 'theTestFunctionName',
- 'modifiers_and_return_type': 'void',
- 'function_name_start_position': (0, 5),
- 'parameter_start_position': (0, 24),
- 'parameter_end_position': (0, 29),
- 'body_start_position': (0, 30),
- 'end_position': (1, 1),
- 'is_pure': False,
- 'is_declaration': False})
- def test_function_declaration_detection(self):
- self.perform_function_detection(
- ['void aFunctionName(int);'],
- {'name': 'aFunctionName',
- 'modifiers_and_return_type': 'void',
- 'function_name_start_position': (0, 5),
- 'parameter_start_position': (0, 18),
- 'parameter_end_position': (0, 23),
- 'body_start_position': (0, 23),
- 'end_position': (0, 24),
- 'is_pure': False,
- 'is_declaration': True})
- self.perform_function_detection(
- ['CheckedInt<T> operator /(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs);'],
- {'name': 'operator /',
- 'modifiers_and_return_type': 'CheckedInt<T>',
- 'function_name_start_position': (0, 14),
- 'parameter_start_position': (0, 24),
- 'parameter_end_position': (0, 76),
- 'body_start_position': (0, 76),
- 'end_position': (0, 77),
- 'is_pure': False,
- 'is_declaration': True})
- self.perform_function_detection(
- ['CheckedInt<T> operator -(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs);'],
- {'name': 'operator -',
- 'modifiers_and_return_type': 'CheckedInt<T>',
- 'function_name_start_position': (0, 14),
- 'parameter_start_position': (0, 24),
- 'parameter_end_position': (0, 76),
- 'body_start_position': (0, 76),
- 'end_position': (0, 77),
- 'is_pure': False,
- 'is_declaration': True})
- self.perform_function_detection(
- ['CheckedInt<T> operator !=(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs);'],
- {'name': 'operator !=',
- 'modifiers_and_return_type': 'CheckedInt<T>',
- 'function_name_start_position': (0, 14),
- 'parameter_start_position': (0, 25),
- 'parameter_end_position': (0, 77),
- 'body_start_position': (0, 77),
- 'end_position': (0, 78),
- 'is_pure': False,
- 'is_declaration': True})
- self.perform_function_detection(
- ['CheckedInt<T> operator +(const CheckedInt<T> &lhs, const CheckedInt<T> &rhs);'],
- {'name': 'operator +',
- 'modifiers_and_return_type': 'CheckedInt<T>',
- 'function_name_start_position': (0, 14),
- 'parameter_start_position': (0, 24),
- 'parameter_end_position': (0, 76),
- 'body_start_position': (0, 76),
- 'end_position': (0, 77),
- 'is_pure': False,
- 'is_declaration': True})
- def test_pure_function_detection(self):
- self.perform_function_detection(
- ['virtual void theTestFunctionName(int = 0);'],
- {'name': 'theTestFunctionName',
- 'modifiers_and_return_type': 'virtual void',
- 'function_name_start_position': (0, 13),
- 'parameter_start_position': (0, 32),
- 'parameter_end_position': (0, 41),
- 'body_start_position': (0, 41),
- 'end_position': (0, 42),
- 'is_pure': False,
- 'is_declaration': True})
- self.perform_function_detection(
- ['virtual void theTestFunctionName(int) = 0;'],
- {'name': 'theTestFunctionName',
- 'modifiers_and_return_type': 'virtual void',
- 'function_name_start_position': (0, 13),
- 'parameter_start_position': (0, 32),
- 'parameter_end_position': (0, 37),
- 'body_start_position': (0, 41),
- 'end_position': (0, 42),
- 'is_pure': True,
- 'is_declaration': True})
- # Hopefully, no one writes code like this but it is a tricky case.
- self.perform_function_detection(
- ['virtual void theTestFunctionName(int)',
- ' = ',
- ' 0 ;'],
- {'name': 'theTestFunctionName',
- 'modifiers_and_return_type': 'virtual void',
- 'function_name_start_position': (0, 13),
- 'parameter_start_position': (0, 32),
- 'parameter_end_position': (0, 37),
- 'body_start_position': (2, 3),
- 'end_position': (2, 4),
- 'is_pure': True,
- 'is_declaration': True})
- def test_ignore_macros(self):
- self.perform_function_detection(['void aFunctionName(int); \\'], None)
- def test_non_functions(self):
- # This case exposed an error because the open brace was in quotes.
- self.perform_function_detection(
- ['asm(',
- ' "stmdb sp!, {r1-r3}" "\n"',
- ');'],
- # This isn't a function but it looks like one to our simple
- # algorithm and that is ok.
- {'name': 'asm',
- 'modifiers_and_return_type': '',
- 'function_name_start_position': (0, 0),
- 'parameter_start_position': (0, 3),
- 'parameter_end_position': (2, 1),
- 'body_start_position': (2, 1),
- 'end_position': (2, 2),
- 'is_pure': False,
- 'is_declaration': True})
- # Simple test case with something that is not a function.
- self.perform_function_detection(['class Stuff;'], None)
- def test_parameter_list(self):
- # A function with no arguments.
- function_state = self.perform_function_detection(
- ['void functionName();'],
- {'name': 'functionName',
- 'modifiers_and_return_type': 'void',
- 'function_name_start_position': (0, 5),
- 'parameter_start_position': (0, 17),
- 'parameter_end_position': (0, 19),
- 'body_start_position': (0, 19),
- 'end_position': (0, 20),
- 'is_pure': False,
- 'is_declaration': True,
- 'parameter_list': ()})
- # A function with one argument.
- function_state = self.perform_function_detection(
- ['void functionName(int);'],
- {'name': 'functionName',
- 'modifiers_and_return_type': 'void',
- 'function_name_start_position': (0, 5),
- 'parameter_start_position': (0, 17),
- 'parameter_end_position': (0, 22),
- 'body_start_position': (0, 22),
- 'end_position': (0, 23),
- 'is_pure': False,
- 'is_declaration': True,
- 'parameter_list':
- ({'type': 'int', 'name': '', 'row': 0},)})
- # A function with unsigned and short arguments
- function_state = self.perform_function_detection(
- ['void functionName(unsigned a, short b, long c, long long short unsigned int);'],
- {'name': 'functionName',
- 'modifiers_and_return_type': 'void',
- 'function_name_start_position': (0, 5),
- 'parameter_start_position': (0, 17),
- 'parameter_end_position': (0, 76),
- 'body_start_position': (0, 76),
- 'end_position': (0, 77),
- 'is_pure': False,
- 'is_declaration': True,
- 'parameter_list':
- ({'type': 'unsigned', 'name': 'a', 'row': 0},
- {'type': 'short', 'name': 'b', 'row': 0},
- {'type': 'long', 'name': 'c', 'row': 0},
- {'type': 'long long short unsigned int', 'name': '', 'row': 0})})
- # Some parameter type with modifiers and no parameter names.
- function_state = self.perform_function_detection(
- ['virtual void determineARIADropEffects(Vector<String>*&, const unsigned long int*&, const MediaPlayer::Preload, Other<Other2, Other3<P1, P2> >, int);'],
- {'name': 'determineARIADropEffects',
- 'modifiers_and_return_type': 'virtual void',
- 'parameter_start_position': (0, 37),
- 'function_name_start_position': (0, 13),
- 'parameter_end_position': (0, 147),
- 'body_start_position': (0, 147),
- 'end_position': (0, 148),
- 'is_pure': False,
- 'is_declaration': True,
- 'parameter_list':
- ({'type': 'Vector<String>*&', 'name': '', 'row': 0},
- {'type': 'const unsigned long int*&', 'name': '', 'row': 0},
- {'type': 'const MediaPlayer::Preload', 'name': '', 'row': 0},
- {'type': 'Other<Other2, Other3<P1, P2> >', 'name': '', 'row': 0},
- {'type': 'int', 'name': '', 'row': 0})})
- # Try parsing a function with a very complex definition.
- function_state = self.perform_function_detection(
- ['#define MyMacro(a) a',
- 'virtual',
- 'AnotherTemplate<Class1, Class2> aFunctionName(PassRefPtr<MyClass> paramName,',
- 'const Other1Class& foo,',
- 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const * param = new ComplexTemplate<Class1, NestedTemplate<P1, P2> >(34, 42),',
- 'int* myCount = 0);'],
- {'name': 'aFunctionName',
- 'modifiers_and_return_type': 'virtual AnotherTemplate<Class1, Class2>',
- 'function_name_start_position': (2, 32),
- 'parameter_start_position': (2, 45),
- 'parameter_end_position': (5, 17),
- 'body_start_position': (5, 17),
- 'end_position': (5, 18),
- 'is_pure': False,
- 'is_declaration': True,
- 'parameter_list':
- ({'type': 'PassRefPtr<MyClass>', 'name': 'paramName', 'row': 2},
- {'type': 'const Other1Class&', 'name': 'foo', 'row': 3},
- {'type': 'const ComplexTemplate<Class1, NestedTemplate<P1, P2> >* const *', 'name': 'param', 'row': 4},
- {'type': 'int*', 'name': 'myCount', 'row': 5})},
- detection_line=2)
- class CppStyleTest(CppStyleTestBase):
- def test_asm_lines_ignored(self):
- self.assert_lint(
- '__asm mov [registration], eax',
- '')
- # Test get line width.
- def test_get_line_width(self):
- self.assertEquals(0, cpp_style.get_line_width(''))
- self.assertEquals(10, cpp_style.get_line_width(u'x' * 10))
- self.assertEquals(16, cpp_style.get_line_width(u'都|道|府|県|支庁'))
- def test_find_next_multi_line_comment_start(self):
- self.assertEquals(1, cpp_style.find_next_multi_line_comment_start([''], 0))
- lines = ['a', 'b', '/* c']
- self.assertEquals(2, cpp_style.find_next_multi_line_comment_start(lines, 0))
- lines = ['char a[] = "/*";'] # not recognized as comment.
- self.assertEquals(1, cpp_style.find_next_multi_line_comment_start(lines, 0))
- def test_find_next_multi_line_comment_end(self):
- self.assertEquals(1, cpp_style.find_next_multi_line_comment_end([''], 0))
- lines = ['a', 'b', ' c */']
- self.assertEquals(2, cpp_style.find_next_multi_line_comment_end(lines, 0))
- def test_remove_multi_line_comments_from_range(self):
- lines = ['a', ' /* comment ', ' * still comment', ' comment */ ', 'b']
- cpp_style.remove_multi_line_comments_from_range(lines, 1, 4)
- self.assertEquals(['a', '// dummy', '// dummy', '// dummy', 'b'], lines)
- def test_position(self):
- position = cpp_style.Position(3, 4)
- self.assert_positions_equal(position, (3, 4))
- self.assertEquals(position.row, 3)
- self.assertTrue(position > cpp_style.Position(position.row - 1, position.column + 1))
- self.assertTrue(position > cpp_style.Position(position.row, position.column - 1))
- self.assertTrue(position < cpp_style.Position(position.row, position.column + 1))
- self.assertTrue(position < cpp_style.Position(position.row + 1, position.column - 1))
- self.assertEquals(position.__str__(), '(3, 4)')
- def test_rfind_in_lines(self):
- not_found_position = cpp_style.Position(10, 11)
- start_position = cpp_style.Position(2, 2)
- lines = ['ab', 'ace', 'test']
- self.assertEquals(not_found_position, cpp_style._rfind_in_lines('st', lines, start_position, not_found_position))
- self.assertTrue(cpp_style.Position(1, 1) == cpp_style._rfind_in_lines('a', lines, start_position, not_found_position))
- self.assertEquals(cpp_style.Position(2, 2), cpp_style._rfind_in_lines('(te|a)', lines, start_position, not_found_position))
- def test_close_expression(self):
- self.assertEquals(cpp_style.Position(1, -1), cpp_style.close_expression([')('], cpp_style.Position(0, 1)))
- self.assertEquals(cpp_style.Position(1, -1), cpp_style.close_expression([') ()'], cpp_style.Position(0, 1)))
- self.assertEquals(cpp_style.Position(0, 4), cpp_style.close_expression([')[)]'], cpp_style.Position(0, 1)))
- self.assertEquals(cpp_style.Position(0, 5), cpp_style.close_expression(['}{}{}'], cpp_style.Position(0, 3)))
- self.assertEquals(cpp_style.Position(1, 1), cpp_style.close_expression(['}{}{', '}'], cpp_style.Position(0, 3)))
- self.assertEquals(cpp_style.Position(2, -1), cpp_style.close_expression(['][][', ' '], cpp_style.Position(0, 3)))
- def test_spaces_at_end_of_line(self):
- self.assert_lint(
- '// Hello there ',
- 'Line ends in whitespace. Consider deleting these extra spaces.'
- ' [whitespace/end_of_line] [4]')
- # Test C-style cast cases.
- def test_cstyle_cast(self):
- self.assert_lint(
- 'int a = (int)1.0;',
- 'Using C-style cast. Use static_cast<int>(...) instead'
- ' [readability/casting] [4]')
- self.assert_lint(
- 'int *a = (int *)DEFINED_VALUE;',
- 'Using C-style cast. Use reinterpret_cast<int *>(...) instead'
- ' [readability/casting] [4]', 'foo.c')
- self.assert_lint(
- 'uint16 a = (uint16)1.0;',
- 'Using C-style cast. Use static_cast<uint16>(...) instead'
- ' [readability/casting] [4]')
- self.assert_lint(
- 'int32 a = (int32)1.0;',
- 'Using C-style cast. Use static_cast<int32>(...) instead'
- ' [readability/casting] [4]')
- self.assert_lint(
- 'uint64 a = (uint64)1.0;',
- 'Using C-style cast. Use static_cast<uint64>(...) instead'
- ' [readability/casting] [4]')
- # Test taking address of casts (runtime/casting)
- def test_runtime_casting(self):
- self.assert_lint(
- 'int* x = &static_cast<int*>(foo);',
- 'Are you taking an address of a cast? '
- 'This is dangerous: could be a temp var. '
- 'Take the address before doing the cast, rather than after'
- ' [runtime/casting] [4]')
- self.assert_lint(
- 'int* x = &dynamic_cast<int *>(foo);',
- ['Are you taking an address of a cast? '
- 'This is dangerous: could be a temp var. '
- 'Take the address before doing the cast, rather than after'
- ' [runtime/casting] [4]',
- 'Do not use dynamic_cast<>. If you need to cast within a class '
- 'hierarchy, use static_cast<> to upcast. Google doesn\'t support '
- 'RTTI. [runtime/rtti] [5]'])
- self.assert_lint(
- 'int* x = &reinterpret_cast<int *>(foo);',
- 'Are you taking an address of a cast? '
- 'This is dangerous: could be a temp var. '
- 'Take the address before doing the cast, rather than after'
- ' [runtime/casting] [4]')
- # It's OK to cast an address.
- self.assert_lint(
- 'int* x = reinterpret_cast<int *>(&foo);',
- '')
- def test_runtime_selfinit(self):
- self.assert_lint(
- 'Foo::Foo(Bar r, Bel l) : r_(r_), l_(l_) { }',
- 'You seem to be initializing a member variable with itself.'
- ' [runtime/init] [4]')
- self.assert_lint(
- 'Foo::Foo(Bar r, Bel l) : r_(r), l_(l) { }',
- '')
- self.assert_lint(
- 'Foo::Foo(Bar r) : r_(r), l_(r_), ll_(l_) { }',
- '')
- def test_runtime_rtti(self):
- statement = 'int* x = dynamic_cast<int*>(&foo);'
- error_message = (
- 'Do not use dynamic_cast<>. If you need to cast within a class '
- 'hierarchy, use static_cast<> to upcast. Google doesn\'t support '
- 'RTTI. [runtime/rtti] [5]')
- # dynamic_cast is disallowed in most files.
- self.assert_language_rules_check('foo.cpp', statement, error_message)
- self.assert_language_rules_check('foo.h', statement, error_message)
- # Test for static_cast readability.
- def test_static_cast_readability(self):
- self.assert_lint(
- 'Text* x = static_cast<Text*>(foo);',
- 'Consider using toText helper function in WebCore/dom/Text.h '
- 'instead of static_cast<Text*>'
- ' [readability/check] [4]')
- # We cannot test this functionality because of difference of
- # function definitions. Anyway, we may never enable this.
- #
- # # Test for unnamed arguments in a method.
- # def test_check_for_unnamed_params(self):
- # message = ('All parameters should be named in a function'
- # ' [readability/function] [3]')
- # self.assert_lint('virtual void A(int*) const;', message)
- # self.assert_lint('virtual void B(void (*fn)(int*));', message)
- # self.assert_lint('virtual void C(int*);', message)
- # self.assert_lint('void *(*f)(void *) = x;', message)
- # self.assert_lint('void Method(char*) {', message)
- # self.assert_lint('void Method(char*);', message)
- # self.assert_lint('void Method(char* /*x*/);', message)
- # self.assert_lint('typedef void (*Method)(int32);', message)
- # self.assert_lint('static void operator delete[](void*) throw();', message)
- #
- # self.assert_lint('virtual void D(int* p);', '')
- # self.assert_lint('void operator delete(void* x) throw();', '')
- # self.assert_lint('void Method(char* x)\n{', '')
- # self.assert_lint('void Method(char* /*x*/)\n{', '')
- # self.assert_lint('void Method(char* x);', '')
- # self.assert_lint('typedef void (*Method)(int32 x);', '')
- # self.assert_lint('static void operator delete[](void* x) throw();', '')
- # self.assert_lint('static void operator delete[](void* /*x*/) throw();', '')
- #
- # # This one should technically warn, but doesn't because the function
- # # pointer is confusing.
- # self.assert_lint('virtual void E(void (*fn)(int* p));', '')
- # Test deprecated casts such as int(d)
- def test_deprecated_cast(self):
- self.assert_lint(
- 'int a = int(2.2);',
- 'Using deprecated casting style. '
- 'Use static_cast<int>(...) instead'
- ' [readability/casting] [4]')
- # Checks for false positives...
- self.assert_lint(
- 'int a = int(); // Constructor, o.k.',
- '')
- self.assert_lint(
- 'X::X() : a(int()) { } // default Constructor, o.k.',
- '')
- self.assert_lint(
- 'operator bool(); // Conversion operator, o.k.',
- '')
- # The second parameter to a gMock method definition is a function signature
- # that often looks like a bad cast but should not picked up by lint.
- def test_mock_method(self):
- self.assert_lint(
- 'MOCK_METHOD0(method, int());',
- '')
- self.assert_lint(
- 'MOCK_CONST_METHOD1(method, float(string));',
- '')
- self.assert_lint(
- 'MOCK_CONST_METHOD2_T(method, double(float, float));',
- '')
- # Test sizeof(type) cases.
- def test_sizeof_type(self):
- self.assert_lint(
- 'sizeof(int);',
- 'Using sizeof(type). Use sizeof(varname) instead if possible'
- ' [runtime/sizeof] [1]')
- self.assert_lint(
- 'sizeof(int *);',
- 'Using sizeof(type). Use sizeof(varname) instead if possible'
- ' [runtime/sizeof] [1]')
- # Test typedef cases. There was a bug that cpp_style misidentified
- # typedef for pointer to function as C-style cast and produced
- # false-positive error messages.
- def test_typedef_for_pointer_to_function(self):
- self.assert_lint(
- 'typedef void (*Func)(int x);',
- '')
- self.assert_lint(
- 'typedef void (*Func)(int *x);',
- '')
- self.assert_lint(
- 'typedef void Func(int x);',
- '')
- self.assert_lint(
- 'typedef void Func(int *x);',
- '')
- def test_include_what_you_use_no_implementation_files(self):
- code = 'std::vector<int> foo;'
- self.assertEquals('Add #include <vector> for vector<>'
- ' [build/include_what_you_use] [4]',
- self.perform_include_what_you_use(code, 'foo.h'))
- self.assertEquals('',
- self.perform_include_what_you_use(code, 'foo.cpp'))
- def test_include_what_you_use(self):
- self.assert_include_what_you_use(
- '''#include <vector>
- std::vector<int> foo;
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <map>
- std::pair<int,int> foo;
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <multimap>
- std::pair<int,int> foo;
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <hash_map>
- std::pair<int,int> foo;
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <utility>
- std::pair<int,int> foo;
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <vector>
- DECLARE_string(foobar);
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <vector>
- DEFINE_string(foobar, "", "");
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <vector>
- std::pair<int,int> foo;
- ''',
- 'Add #include <utility> for pair<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- std::vector<int> foo;
- ''',
- 'Add #include <vector> for vector<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include <vector>
- std::set<int> foo;
- ''',
- 'Add #include <set> for set<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- hash_map<int, int> foobar;
- ''',
- 'Add #include <hash_map> for hash_map<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- bool foobar = std::less<int>(0,1);
- ''',
- 'Add #include <functional> for less<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- bool foobar = min<int>(0,1);
- ''',
- 'Add #include <algorithm> for min [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- 'void a(const string &foobar);',
- 'Add #include <string> for string [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- bool foobar = swap(0,1);
- ''',
- 'Add #include <algorithm> for swap [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- bool foobar = transform(a.begin(), a.end(), b.start(), Foo);
- ''',
- 'Add #include <algorithm> for transform '
- '[build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include "base/foobar.h"
- bool foobar = min_element(a.begin(), a.end());
- ''',
- 'Add #include <algorithm> for min_element '
- '[build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''foo->swap(0,1);
- foo.swap(0,1);
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include <string>
- void a(const std::multimap<int,string> &foobar);
- ''',
- 'Add #include <map> for multimap<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include <queue>
- void a(const std::priority_queue<int> &foobar);
- ''',
- '')
- self.assert_include_what_you_use(
- '''#include "base/basictypes.h"
- #include "base/port.h"
- #include <assert.h>
- #include <string>
- #include <vector>
- vector<string> hajoa;''', '')
- self.assert_include_what_you_use(
- '''#include <string>
- int i = numeric_limits<int>::max()
- ''',
- 'Add #include <limits> for numeric_limits<>'
- ' [build/include_what_you_use] [4]')
- self.assert_include_what_you_use(
- '''#include <limits>
- int i = numeric_limits<int>::max()
- ''',
- '')
- # Test the UpdateIncludeState code path.
- mock_header_contents = ['#include "blah/foo.h"', '#include "blah/bar.h"']
- message = self.perform_include_what_you_use(
- '#include "config.h"\n'
- '#include "blah/a.h"\n',
- filename='blah/a.cpp',
- io=MockIo(mock_header_contents))
- self.assertEquals(message, '')
- mock_header_contents = ['#include <set>']
- message = self.perform_include_what_you_use(
- '''#include "config.h"
- #include "blah/a.h"
- std::set<int> foo;''',
- filename='blah/a.cpp',
- io=MockIo(mock_header_contents))
- self.assertEquals(message, '')
- # If there's just a .cpp and the header can't be found then it's ok.
- message = self.perform_include_what_you_use(
- '''#include "config.h"
- #include "blah/a.h"
- std::set<int> foo;''',
- filename='blah/a.cpp')
- self.assertEquals(message, '')
- # Make sure we find the headers with relative paths.
- mock_header_contents = ['']
- message = self.perform_include_what_you_use(
- '''#include "config.h"
- #include "%s%sa.h"
- std::set<int> foo;''' % (os.path.basename(os.getcwd()), os.path.sep),
- filename='a.cpp',
- io=MockIo(mock_header_contents))
- self.assertEquals(message, 'Add #include <set> for set<> '
- '[build/include_what_you_use] [4]')
- def test_files_belong_to_same_module(self):
- f = cpp_style.files_belong_to_same_module
- self.assertEquals((True, ''), f('a.cpp', 'a.h'))
- self.assertEquals((True, ''), f('base/google.cpp', 'base/google.h'))
- self.assertEquals((True, ''), f('base/google_test.cpp', 'base/google.h'))
- self.assertEquals((True, ''),
- f('base/google_unittest.cpp', 'base/google.h'))
- self.assertEquals((True, ''),
- f('base/internal/google_unittest.cpp',
- 'base/public/google.h'))
- self.assertEquals((True, 'xxx/yyy/'),
- f('xxx/yyy/base/internal/google_unittest.cpp',
- 'base/public/google.h'))
- self.assertEquals((True, 'xxx/yyy/'),
- f('xxx/yyy/base/google_unittest.cpp',
- 'base/public/google.h'))
- self.assertEquals((True, ''),
- f('base/google_unittest.cpp', 'base/google-inl.h'))
- self.assertEquals((True, '/home/build/google3/'),
- …
Large files files are truncated, but you can click here to view the full file