0
0
mirror of https://github.com/google/googletest.git synced 2025-03-21 03:43:48 +00:00

Googletest export

Merge 65032e28cba171c000accc85ffaf6f1e62921b86 into 8c91ecef292e963d23cd5b25f01ea1579fbe9aaa

Closes #2470

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/googletest/pull/2470 from hermas55:bugfix/default_const_param 65032e28cba171c000accc85ffaf6f1e62921b86
PiperOrigin-RevId: 277118535
This commit is contained in:
mhermas 2019-10-28 15:26:05 -04:00 committed by vslashg
parent 2bee6da24e
commit fff8dabbf6
3 changed files with 423 additions and 377 deletions

View File

@ -1,19 +1,33 @@
#!/usr/bin/env python #!/usr/bin/env python
# #
# Copyright 2007 Neal Norwitz # Copyright 2008, Google Inc.
# Portions Copyright 2007 Google Inc. # All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Redistribution and use in source and binary forms, with or without
# you may not use this file except in compliance with the License. # modification, are permitted provided that the following conditions are
# You may obtain a copy of the License at # met:
# #
# http://www.apache.org/licenses/LICENSE-2.0 # * 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.
# #
# Unless required by applicable law or agreed to in writing, software # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# distributed under the License is distributed on an "AS IS" BASIS, # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# See the License for the specific language governing permissions and # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# limitations under the License. # 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.
"""Generate an Abstract Syntax Tree (AST) for C++.""" """Generate an Abstract Syntax Tree (AST) for C++."""
@ -929,7 +943,10 @@ class AstBuilder(object):
def _GetNextToken(self): def _GetNextToken(self):
if self.token_queue: if self.token_queue:
return self.token_queue.pop() return self.token_queue.pop()
try:
return next(self.tokens) return next(self.tokens)
except StopIteration:
return
def _AddBackToken(self, token): def _AddBackToken(self, token):
if token.whence == tokenize.WHENCE_STREAM: if token.whence == tokenize.WHENCE_STREAM:

View File

@ -53,6 +53,7 @@ try:
_dummy = set _dummy = set
except NameError: except NameError:
import sets import sets
set = sets.Set set = sets.Set
_VERSION = (1, 0, 1) # The version of this script. _VERSION = (1, 0, 1) # The version of this script.
@ -136,22 +137,17 @@ def _GenerateMethods(output_lines, source, class_node):
args = '' args = ''
if node.parameters: if node.parameters:
# Due to the parser limitations, it is impossible to keep comments
# while stripping the default parameters. When defaults are
# present, we choose to strip them and comments (and produce
# compilable code).
# TODO(nnorwitz@google.com): Investigate whether it is possible to
# preserve parameter name when reconstructing parameter text from
# the AST.
if len([param for param in node.parameters if param.default]) > 0:
args = ', '.join(param.type.name for param in node.parameters)
else:
# Get the full text of the parameters from the start # Get the full text of the parameters from the start
# of the first parameter to the end of the last parameter. # of the first parameter to the end of the last parameter.
start = node.parameters[0].start start = node.parameters[0].start
end = node.parameters[-1].end end = node.parameters[-1].end
# Remove // comments. # Remove // comments.
args_strings = re.sub(r'//.*', '', source[start:end]) args_strings = re.sub(r'//.*', '', source[start:end])
# Remove /* comments */.
args_strings = re.sub(r'/\*.*\*/', '', args_strings)
# Remove default arguments.
args_strings = re.sub(r'=.*,', ',', args_strings)
args_strings = re.sub(r'=.*', '', args_strings)
# Condense multiple spaces and eliminate newlines putting the # Condense multiple spaces and eliminate newlines putting the
# parameters together on a single line. Ensure there is a # parameters together on a single line. Ensure there is a
# space in an argument which is split by a newline without # space in an argument which is split by a newline without

View File

@ -45,7 +45,8 @@ from cpp import gmock_class
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
"""Helper class that adds assert methods.""" """Helper class that adds assert methods."""
def StripLeadingWhitespace(self, lines): @staticmethod
def StripLeadingWhitespace(lines):
"""Strip leading whitespace in each line in 'lines'.""" """Strip leading whitespace in each line in 'lines'."""
return '\n'.join([s.lstrip() for s in lines.split('\n')]) return '\n'.join([s.lstrip() for s in lines.split('\n')])
@ -56,7 +57,8 @@ class TestCase(unittest.TestCase):
class GenerateMethodsTest(TestCase): class GenerateMethodsTest(TestCase):
def GenerateMethodSource(self, cpp_source): @staticmethod
def GenerateMethodSource(cpp_source):
"""Convert C++ source to Google Mock output source lines.""" """Convert C++ source to Google Mock output source lines."""
method_source_lines = [] method_source_lines = []
# <test> is a pseudo-filename, it is not read or written. # <test> is a pseudo-filename, it is not read or written.
@ -191,20 +193,49 @@ class Foo {
}; };
""" """
self.assertEqualIgnoreLeadingWhitespace( self.assertEqualIgnoreLeadingWhitespace(
'MOCK_METHOD2(Bar,\nvoid(int, char));', 'MOCK_METHOD2(Bar,\nvoid(int a, char c ));',
self.GenerateMethodSource(source)) self.GenerateMethodSource(source))
def testMultipleDefaultParameters(self): def testMultipleDefaultParameters(self):
source = """ source = """
class Foo { class Foo {
public: public:
virtual void Bar(int a = 42, char c = 'x') = 0; virtual void Bar(
int a = 42,
char c = 'x',
const int* const p = nullptr,
const std::string& s = "42",
char tab[] = {'4','2'},
int const *& rp = aDefaultPointer) = 0;
}; };
""" """
self.assertEqualIgnoreLeadingWhitespace( self.assertEqualIgnoreLeadingWhitespace(
'MOCK_METHOD2(Bar,\nvoid(int, char));', "MOCK_METHOD7(Bar,\n"
"void(int a , char c , const int* const p , const std::string& s , char tab[] , int const *& rp ));",
self.GenerateMethodSource(source)) self.GenerateMethodSource(source))
def testConstDefaultParameter(self):
source = """
class Test {
public:
virtual bool Bar(const int test_arg = 42) = 0;
};
"""
expected = 'MOCK_METHOD1(Bar,\nbool(const int test_arg ));'
self.assertEqualIgnoreLeadingWhitespace(
expected, self.GenerateMethodSource(source))
def testConstRefDefaultParameter(self):
source = """
class Test {
public:
virtual bool Bar(const std::string& test_arg = "42" ) = 0;
};
"""
expected = 'MOCK_METHOD1(Bar,\nbool(const std::string& test_arg ));'
self.assertEqualIgnoreLeadingWhitespace(
expected, self.GenerateMethodSource(source))
def testRemovesCommentsWhenDefaultsArePresent(self): def testRemovesCommentsWhenDefaultsArePresent(self):
source = """ source = """
class Foo { class Foo {
@ -214,7 +245,7 @@ class Foo {
}; };
""" """
self.assertEqualIgnoreLeadingWhitespace( self.assertEqualIgnoreLeadingWhitespace(
'MOCK_METHOD2(Bar,\nvoid(int, char));', 'MOCK_METHOD2(Bar,\nvoid(int a , char c));',
self.GenerateMethodSource(source)) self.GenerateMethodSource(source))
def testDoubleSlashCommentsInParameterListAreRemoved(self): def testDoubleSlashCommentsInParameterListAreRemoved(self):
@ -241,7 +272,7 @@ class Foo {
}; };
""" """
self.assertEqualIgnoreLeadingWhitespace( self.assertEqualIgnoreLeadingWhitespace(
'MOCK_METHOD2(Bar,\nconst string&(int /* keeper */, int b));', 'MOCK_METHOD2(Bar,\nconst string&(int , int b));',
self.GenerateMethodSource(source)) self.GenerateMethodSource(source))
def testArgsOfTemplateTypes(self): def testArgsOfTemplateTypes(self):
@ -325,7 +356,8 @@ class Foo {
class GenerateMocksTest(TestCase): class GenerateMocksTest(TestCase):
def GenerateMocks(self, cpp_source): @staticmethod
def GenerateMocks(cpp_source):
"""Convert C++ source to complete Google Mock output source.""" """Convert C++ source to complete Google Mock output source."""
# <test> is a pseudo-filename, it is not read or written. # <test> is a pseudo-filename, it is not read or written.
filename = '<test>' filename = '<test>'
@ -517,5 +549,6 @@ std::function<int (std::string)>());
self.assertEqualIgnoreLeadingWhitespace( self.assertEqualIgnoreLeadingWhitespace(
expected, self.GenerateMocks(source)) expected, self.GenerateMocks(source))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()