2009-06-02 20:41:21 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2019-10-29 19:13:11 +00:00
|
|
|
# Copyright 2009 Neal Norwitz All Rights Reserved.
|
|
|
|
# Portions Copyright 2009 Google Inc. All Rights Reserved.
|
2009-06-02 20:41:21 +00:00
|
|
|
#
|
2019-10-29 19:13:11 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2009-06-02 20:41:21 +00:00
|
|
|
#
|
2019-10-29 19:13:11 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-02 20:41:21 +00:00
|
|
|
#
|
2019-10-29 19:13:11 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2009-06-02 20:41:21 +00:00
|
|
|
|
|
|
|
"""Tests for gmock.scripts.generator.cpp.gmock_class."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
# Allow the cpp imports below to work when run as a standalone script.
|
2009-06-04 05:48:20 +00:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
|
|
|
from cpp import ast
|
|
|
|
from cpp import gmock_class
|
|
|
|
|
|
|
|
|
|
|
|
class TestCase(unittest.TestCase):
|
2019-10-28 19:26:05 +00:00
|
|
|
"""Helper class that adds assert methods."""
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def StripLeadingWhitespace(lines):
|
|
|
|
"""Strip leading whitespace in each line in 'lines'."""
|
|
|
|
return '\n'.join([s.lstrip() for s in lines.split('\n')])
|
2010-10-05 19:21:38 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def assertEqualIgnoreLeadingWhitespace(self, expected_lines, lines):
|
|
|
|
"""Specialized assert that ignores the indent level."""
|
|
|
|
self.assertEqual(expected_lines, self.StripLeadingWhitespace(lines))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenerateMethodsTest(TestCase):
|
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def GenerateMethodSource(cpp_source):
|
|
|
|
"""Convert C++ source to Google Mock output source lines."""
|
|
|
|
method_source_lines = []
|
|
|
|
# <test> is a pseudo-filename, it is not read or written.
|
|
|
|
builder = ast.BuilderFromSource(cpp_source, '<test>')
|
|
|
|
ast_list = list(builder.Generate())
|
|
|
|
gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0])
|
|
|
|
return '\n'.join(method_source_lines)
|
|
|
|
|
|
|
|
def testSimpleMethod(self):
|
|
|
|
source = """
|
2010-10-05 19:21:38 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual int Bar();
|
|
|
|
};
|
2014-03-12 23:27:35 +00:00
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-03-12 23:27:35 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testSimpleConstructorsAndDestructor(self):
|
|
|
|
source = """
|
2014-11-17 02:49:22 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
Foo();
|
|
|
|
Foo(int x);
|
|
|
|
Foo(const Foo& f);
|
|
|
|
Foo(Foo&& f);
|
|
|
|
~Foo();
|
|
|
|
virtual int Bar() = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
# The constructors and destructor should be ignored.
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:49:22 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testVirtualDestructor(self):
|
|
|
|
source = """
|
2014-11-17 02:49:22 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual ~Foo();
|
|
|
|
virtual int Bar() = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
# The destructor should be ignored.
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:49:22 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testExplicitlyDefaultedConstructorsAndDestructor(self):
|
|
|
|
source = """
|
2014-11-17 02:49:22 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
Foo() = default;
|
|
|
|
Foo(const Foo& f) = default;
|
|
|
|
Foo(Foo&& f) = default;
|
|
|
|
~Foo() = default;
|
|
|
|
virtual int Bar() = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
# The constructors and destructor should be ignored.
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:49:22 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testExplicitlyDeletedConstructorsAndDestructor(self):
|
|
|
|
source = """
|
2014-11-17 02:49:22 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
Foo() = delete;
|
|
|
|
Foo(const Foo& f) = delete;
|
|
|
|
Foo(Foo&& f) = delete;
|
|
|
|
~Foo() = delete;
|
|
|
|
virtual int Bar() = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
# The constructors and destructor should be ignored.
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:49:22 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testSimpleOverrideMethod(self):
|
|
|
|
source = """
|
2014-03-12 23:27:35 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
int Bar() override;
|
|
|
|
};
|
2010-10-05 19:21:38 +00:00
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2010-10-05 19:21:38 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testSimpleConstMethod(self):
|
|
|
|
source = """
|
2010-10-05 19:21:38 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual void Bar(bool flag) const;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_CONST_METHOD1(Bar,\nvoid(bool flag));',
|
|
|
|
self.GenerateMethodSource(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testExplicitVoid(self):
|
|
|
|
source = """
|
2011-05-20 21:44:14 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual int Bar(void);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0(Bar,\nint(void));',
|
|
|
|
self.GenerateMethodSource(source))
|
2011-05-20 21:44:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testStrangeNewlineInParameter(self):
|
|
|
|
source = """
|
2009-06-02 20:41:21 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual void Bar(int
|
|
|
|
a) = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD1(Bar,\nvoid(int a));',
|
|
|
|
self.GenerateMethodSource(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testDefaultParameters(self):
|
|
|
|
source = """
|
2011-05-20 21:44:14 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual void Bar(int a, char c = 'x') = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD2(Bar,\nvoid(int a, char c ));',
|
|
|
|
self.GenerateMethodSource(source))
|
2011-05-20 21:44:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testMultipleDefaultParameters(self):
|
|
|
|
source = """
|
2011-05-20 21:44:14 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
2019-10-28 19:26:05 +00:00
|
|
|
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(
|
|
|
|
"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))
|
|
|
|
|
|
|
|
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;
|
2011-05-20 21:44:14 +00:00
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = 'MOCK_METHOD1(Bar,\nbool(const std::string& test_arg ));'
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMethodSource(source))
|
2011-05-20 21:44:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testRemovesCommentsWhenDefaultsArePresent(self):
|
|
|
|
source = """
|
2011-05-20 21:44:14 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual void Bar(int a = 42 /* a comment */,
|
|
|
|
char /* other comment */ c= 'x') = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD2(Bar,\nvoid(int a , char c));',
|
|
|
|
self.GenerateMethodSource(source))
|
2011-05-20 21:44:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testDoubleSlashCommentsInParameterListAreRemoved(self):
|
|
|
|
source = """
|
2009-06-02 20:41:21 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual void Bar(int a, // inline comments should be elided.
|
|
|
|
int b // inline comments should be elided.
|
|
|
|
) const = 0;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_CONST_METHOD2(Bar,\nvoid(int a, int b));',
|
|
|
|
self.GenerateMethodSource(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testCStyleCommentsInParameterListAreNotRemoved(self):
|
|
|
|
# NOTE(nnorwitz): I'm not sure if it's the best behavior to keep these
|
|
|
|
# comments. Also note that C style comments after the last parameter
|
|
|
|
# are still elided.
|
|
|
|
source = """
|
2009-06-02 20:41:21 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual const string& Bar(int /* keeper */, int b);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD2(Bar,\nconst string&(int , int b));',
|
|
|
|
self.GenerateMethodSource(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testArgsOfTemplateTypes(self):
|
|
|
|
source = """
|
2010-10-05 19:21:38 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual int Bar(const vector<int>& v, map<int, string>* output);
|
|
|
|
};"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD2(Bar,\n'
|
|
|
|
'int(const vector<int>& v, map<int, string>* output));',
|
|
|
|
self.GenerateMethodSource(source))
|
2010-10-05 19:21:38 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testReturnTypeWithOneTemplateArg(self):
|
|
|
|
source = """
|
2010-10-05 19:21:38 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual vector<int>* Bar(int n);
|
|
|
|
};"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD1(Bar,\nvector<int>*(int n));',
|
|
|
|
self.GenerateMethodSource(source))
|
2010-10-05 19:21:38 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testReturnTypeWithManyTemplateArgs(self):
|
|
|
|
source = """
|
2010-10-05 19:21:38 +00:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual map<int, string> Bar();
|
|
|
|
};"""
|
2019-10-28 19:26:05 +00:00
|
|
|
# Comparing the comment text is brittle - we'll think of something
|
|
|
|
# better in case this gets annoying, but for now let's keep it simple.
|
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'// The following line won\'t really compile, as the return\n'
|
|
|
|
'// type has multiple template arguments. To fix it, use a\n'
|
|
|
|
'// typedef for the return type.\n'
|
|
|
|
'MOCK_METHOD0(Bar,\nmap<int, string>());',
|
|
|
|
self.GenerateMethodSource(source))
|
|
|
|
|
|
|
|
def testSimpleMethodInTemplatedClass(self):
|
|
|
|
source = """
|
2013-09-06 22:52:14 +00:00
|
|
|
template<class T>
|
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
virtual int Bar();
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD0_T(Bar,\nint());',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:42:33 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testPointerArgWithoutNames(self):
|
|
|
|
source = """
|
2014-11-17 02:42:33 +00:00
|
|
|
class Foo {
|
|
|
|
virtual int Bar(C*);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD1(Bar,\nint(C*));',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:42:33 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testReferenceArgWithoutNames(self):
|
|
|
|
source = """
|
2014-11-17 02:42:33 +00:00
|
|
|
class Foo {
|
|
|
|
virtual int Bar(C&);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD1(Bar,\nint(C&));',
|
|
|
|
self.GenerateMethodSource(source))
|
2014-11-17 02:42:33 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testArrayArgWithoutNames(self):
|
|
|
|
source = """
|
2014-11-17 02:42:33 +00:00
|
|
|
class Foo {
|
|
|
|
virtual int Bar(C[]);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
'MOCK_METHOD1(Bar,\nint(C[]));',
|
|
|
|
self.GenerateMethodSource(source))
|
2013-09-06 22:52:14 +00:00
|
|
|
|
2009-06-02 20:41:21 +00:00
|
|
|
|
|
|
|
class GenerateMocksTest(TestCase):
|
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def GenerateMocks(cpp_source):
|
|
|
|
"""Convert C++ source to complete Google Mock output source."""
|
|
|
|
# <test> is a pseudo-filename, it is not read or written.
|
|
|
|
filename = '<test>'
|
|
|
|
builder = ast.BuilderFromSource(cpp_source, filename)
|
|
|
|
ast_list = list(builder.Generate())
|
|
|
|
lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None)
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
|
|
def testNamespaces(self):
|
|
|
|
source = """
|
2009-06-02 20:41:21 +00:00
|
|
|
namespace Foo {
|
|
|
|
namespace Bar { class Forward; }
|
|
|
|
namespace Baz {
|
|
|
|
|
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
virtual void Foo();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Baz
|
|
|
|
} // namespace Foo
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2009-06-02 20:41:21 +00:00
|
|
|
namespace Foo {
|
|
|
|
namespace Baz {
|
|
|
|
|
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Baz
|
|
|
|
} // namespace Foo
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testClassWithStorageSpecifierMacro(self):
|
|
|
|
source = """
|
2011-02-01 00:00:03 +00:00
|
|
|
class STORAGE_SPECIFIER Test {
|
|
|
|
public:
|
|
|
|
virtual void Foo();
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2011-02-01 00:00:03 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2009-06-02 20:41:21 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testTemplatedForwardDeclaration(self):
|
|
|
|
source = """
|
2013-09-06 22:52:14 +00:00
|
|
|
template <class T> class Forward; // Forward declaration should be ignored.
|
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
virtual void Foo();
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2013-09-06 22:52:14 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2013-09-06 22:52:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testTemplatedClass(self):
|
|
|
|
source = """
|
2013-09-06 22:52:14 +00:00
|
|
|
template <typename S, typename T>
|
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
virtual void Foo();
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2013-09-06 22:52:14 +00:00
|
|
|
template <typename T0, typename T1>
|
|
|
|
class MockTest : public Test<T0, T1> {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0_T(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2013-09-06 22:52:14 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testTemplateInATemplateTypedef(self):
|
|
|
|
source = """
|
2015-01-08 02:48:08 +00:00
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
typedef std::vector<std::list<int>> FooType;
|
|
|
|
virtual void Bar(const FooType& test_arg);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2015-01-08 02:48:08 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD1(Bar,
|
|
|
|
void(const FooType& test_arg));
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2015-01-08 02:48:08 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testTemplateInATemplateTypedefWithComma(self):
|
|
|
|
source = """
|
2015-01-08 02:48:08 +00:00
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
typedef std::function<void(
|
|
|
|
const vector<std::list<int>>&, int> FooType;
|
|
|
|
virtual void Bar(const FooType& test_arg);
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2015-01-08 02:48:08 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD1(Bar,
|
|
|
|
void(const FooType& test_arg));
|
|
|
|
};
|
2017-08-10 16:58:57 +00:00
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2017-08-10 16:58:57 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testEnumType(self):
|
|
|
|
source = """
|
2017-08-10 16:58:57 +00:00
|
|
|
class Test {
|
|
|
|
public:
|
2019-10-23 18:09:41 +00:00
|
|
|
enum Bar {
|
|
|
|
BAZ, QUX, QUUX, QUUUX
|
|
|
|
};
|
|
|
|
virtual void Foo();
|
2017-08-10 16:58:57 +00:00
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2017-08-10 16:58:57 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
2019-10-23 18:09:41 +00:00
|
|
|
MOCK_METHOD0(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2019-10-23 18:09:41 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testEnumClassType(self):
|
|
|
|
source = """
|
2019-10-23 18:09:41 +00:00
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
enum class Bar {
|
|
|
|
BAZ, QUX, QUUX, QUUUX
|
|
|
|
};
|
|
|
|
virtual void Foo();
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2019-10-23 18:09:41 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0(Foo,
|
|
|
|
void());
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
2019-10-23 18:09:41 +00:00
|
|
|
|
2019-10-28 19:26:05 +00:00
|
|
|
def testStdFunction(self):
|
|
|
|
source = """
|
2019-10-23 18:09:41 +00:00
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
Test(std::function<int(std::string)> foo) : foo_(foo) {}
|
|
|
|
|
|
|
|
virtual std::function<int(std::string)> foo();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<int(std::string)> foo_;
|
|
|
|
};
|
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
expected = """\
|
2019-10-23 18:09:41 +00:00
|
|
|
class MockTest : public Test {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD0(foo,
|
|
|
|
std::function<int (std::string)>());
|
2017-08-10 16:58:57 +00:00
|
|
|
};
|
2015-01-08 02:48:08 +00:00
|
|
|
"""
|
2019-10-28 19:26:05 +00:00
|
|
|
self.assertEqualIgnoreLeadingWhitespace(
|
|
|
|
expected, self.GenerateMocks(source))
|
|
|
|
|
2013-09-06 22:52:14 +00:00
|
|
|
|
2009-06-02 20:41:21 +00:00
|
|
|
if __name__ == '__main__':
|
2019-10-28 19:26:05 +00:00
|
|
|
unittest.main()
|