File indexing completed on 2025-08-09 08:19:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 """Tests for gmock.scripts.generator.cpp.gmock_class."""
0019
0020 __author__ = 'nnorwitz@google.com (Neal Norwitz)'
0021
0022
0023 import os
0024 import sys
0025 import unittest
0026
0027
0028 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
0029
0030 from cpp import ast
0031 from cpp import gmock_class
0032
0033
0034 class TestCase(unittest.TestCase):
0035 """Helper class that adds assert methods."""
0036
0037 def StripLeadingWhitespace(self, lines):
0038 """Strip leading whitespace in each line in 'lines'."""
0039 return '\n'.join([s.lstrip() for s in lines.split('\n')])
0040
0041 def assertEqualIgnoreLeadingWhitespace(self, expected_lines, lines):
0042 """Specialized assert that ignores the indent level."""
0043 self.assertEqual(expected_lines, self.StripLeadingWhitespace(lines))
0044
0045
0046 class GenerateMethodsTest(TestCase):
0047
0048 def GenerateMethodSource(self, cpp_source):
0049 """Convert C++ source to Google Mock output source lines."""
0050 method_source_lines = []
0051
0052 builder = ast.BuilderFromSource(cpp_source, '<test>')
0053 ast_list = list(builder.Generate())
0054 gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0])
0055 return '\n'.join(method_source_lines)
0056
0057 def testSimpleMethod(self):
0058 source = """
0059 class Foo {
0060 public:
0061 virtual int Bar();
0062 };
0063 """
0064 self.assertEqualIgnoreLeadingWhitespace(
0065 'MOCK_METHOD0(Bar,\nint());',
0066 self.GenerateMethodSource(source))
0067
0068 def testSimpleConstructorsAndDestructor(self):
0069 source = """
0070 class Foo {
0071 public:
0072 Foo();
0073 Foo(int x);
0074 Foo(const Foo& f);
0075 Foo(Foo&& f);
0076 ~Foo();
0077 virtual int Bar() = 0;
0078 };
0079 """
0080
0081 self.assertEqualIgnoreLeadingWhitespace(
0082 'MOCK_METHOD0(Bar,\nint());',
0083 self.GenerateMethodSource(source))
0084
0085 def testVirtualDestructor(self):
0086 source = """
0087 class Foo {
0088 public:
0089 virtual ~Foo();
0090 virtual int Bar() = 0;
0091 };
0092 """
0093
0094 self.assertEqualIgnoreLeadingWhitespace(
0095 'MOCK_METHOD0(Bar,\nint());',
0096 self.GenerateMethodSource(source))
0097
0098 def testExplicitlyDefaultedConstructorsAndDestructor(self):
0099 source = """
0100 class Foo {
0101 public:
0102 Foo() = default;
0103 Foo(const Foo& f) = default;
0104 Foo(Foo&& f) = default;
0105 ~Foo() = default;
0106 virtual int Bar() = 0;
0107 };
0108 """
0109
0110 self.assertEqualIgnoreLeadingWhitespace(
0111 'MOCK_METHOD0(Bar,\nint());',
0112 self.GenerateMethodSource(source))
0113
0114 def testExplicitlyDeletedConstructorsAndDestructor(self):
0115 source = """
0116 class Foo {
0117 public:
0118 Foo() = delete;
0119 Foo(const Foo& f) = delete;
0120 Foo(Foo&& f) = delete;
0121 ~Foo() = delete;
0122 virtual int Bar() = 0;
0123 };
0124 """
0125
0126 self.assertEqualIgnoreLeadingWhitespace(
0127 'MOCK_METHOD0(Bar,\nint());',
0128 self.GenerateMethodSource(source))
0129
0130 def testSimpleOverrideMethod(self):
0131 source = """
0132 class Foo {
0133 public:
0134 int Bar() override;
0135 };
0136 """
0137 self.assertEqualIgnoreLeadingWhitespace(
0138 'MOCK_METHOD0(Bar,\nint());',
0139 self.GenerateMethodSource(source))
0140
0141 def testSimpleConstMethod(self):
0142 source = """
0143 class Foo {
0144 public:
0145 virtual void Bar(bool flag) const;
0146 };
0147 """
0148 self.assertEqualIgnoreLeadingWhitespace(
0149 'MOCK_CONST_METHOD1(Bar,\nvoid(bool flag));',
0150 self.GenerateMethodSource(source))
0151
0152 def testExplicitVoid(self):
0153 source = """
0154 class Foo {
0155 public:
0156 virtual int Bar(void);
0157 };
0158 """
0159 self.assertEqualIgnoreLeadingWhitespace(
0160 'MOCK_METHOD0(Bar,\nint(void));',
0161 self.GenerateMethodSource(source))
0162
0163 def testStrangeNewlineInParameter(self):
0164 source = """
0165 class Foo {
0166 public:
0167 virtual void Bar(int
0168 a) = 0;
0169 };
0170 """
0171 self.assertEqualIgnoreLeadingWhitespace(
0172 'MOCK_METHOD1(Bar,\nvoid(int a));',
0173 self.GenerateMethodSource(source))
0174
0175 def testDefaultParameters(self):
0176 source = """
0177 class Foo {
0178 public:
0179 virtual void Bar(int a, char c = 'x') = 0;
0180 };
0181 """
0182 self.assertEqualIgnoreLeadingWhitespace(
0183 'MOCK_METHOD2(Bar,\nvoid(int, char));',
0184 self.GenerateMethodSource(source))
0185
0186 def testMultipleDefaultParameters(self):
0187 source = """
0188 class Foo {
0189 public:
0190 virtual void Bar(int a = 42, char c = 'x') = 0;
0191 };
0192 """
0193 self.assertEqualIgnoreLeadingWhitespace(
0194 'MOCK_METHOD2(Bar,\nvoid(int, char));',
0195 self.GenerateMethodSource(source))
0196
0197 def testRemovesCommentsWhenDefaultsArePresent(self):
0198 source = """
0199 class Foo {
0200 public:
0201 virtual void Bar(int a = 42 /* a comment */,
0202 char /* other comment */ c= 'x') = 0;
0203 };
0204 """
0205 self.assertEqualIgnoreLeadingWhitespace(
0206 'MOCK_METHOD2(Bar,\nvoid(int, char));',
0207 self.GenerateMethodSource(source))
0208
0209 def testDoubleSlashCommentsInParameterListAreRemoved(self):
0210 source = """
0211 class Foo {
0212 public:
0213 virtual void Bar(int a, // inline comments should be elided.
0214 int b // inline comments should be elided.
0215 ) const = 0;
0216 };
0217 """
0218 self.assertEqualIgnoreLeadingWhitespace(
0219 'MOCK_CONST_METHOD2(Bar,\nvoid(int a, int b));',
0220 self.GenerateMethodSource(source))
0221
0222 def testCStyleCommentsInParameterListAreNotRemoved(self):
0223
0224
0225
0226 source = """
0227 class Foo {
0228 public:
0229 virtual const string& Bar(int /* keeper */, int b);
0230 };
0231 """
0232 self.assertEqualIgnoreLeadingWhitespace(
0233 'MOCK_METHOD2(Bar,\nconst string&(int /* keeper */, int b));',
0234 self.GenerateMethodSource(source))
0235
0236 def testArgsOfTemplateTypes(self):
0237 source = """
0238 class Foo {
0239 public:
0240 virtual int Bar(const vector<int>& v, map<int, string>* output);
0241 };"""
0242 self.assertEqualIgnoreLeadingWhitespace(
0243 'MOCK_METHOD2(Bar,\n'
0244 'int(const vector<int>& v, map<int, string>* output));',
0245 self.GenerateMethodSource(source))
0246
0247 def testReturnTypeWithOneTemplateArg(self):
0248 source = """
0249 class Foo {
0250 public:
0251 virtual vector<int>* Bar(int n);
0252 };"""
0253 self.assertEqualIgnoreLeadingWhitespace(
0254 'MOCK_METHOD1(Bar,\nvector<int>*(int n));',
0255 self.GenerateMethodSource(source))
0256
0257 def testReturnTypeWithManyTemplateArgs(self):
0258 source = """
0259 class Foo {
0260 public:
0261 virtual map<int, string> Bar();
0262 };"""
0263
0264
0265 self.assertEqualIgnoreLeadingWhitespace(
0266 '// The following line won\'t really compile, as the return\n'
0267 '// type has multiple template arguments. To fix it, use a\n'
0268 '// typedef for the return type.\n'
0269 'MOCK_METHOD0(Bar,\nmap<int, string>());',
0270 self.GenerateMethodSource(source))
0271
0272 def testSimpleMethodInTemplatedClass(self):
0273 source = """
0274 template<class T>
0275 class Foo {
0276 public:
0277 virtual int Bar();
0278 };
0279 """
0280 self.assertEqualIgnoreLeadingWhitespace(
0281 'MOCK_METHOD0_T(Bar,\nint());',
0282 self.GenerateMethodSource(source))
0283
0284 def testPointerArgWithoutNames(self):
0285 source = """
0286 class Foo {
0287 virtual int Bar(C*);
0288 };
0289 """
0290 self.assertEqualIgnoreLeadingWhitespace(
0291 'MOCK_METHOD1(Bar,\nint(C*));',
0292 self.GenerateMethodSource(source))
0293
0294 def testReferenceArgWithoutNames(self):
0295 source = """
0296 class Foo {
0297 virtual int Bar(C&);
0298 };
0299 """
0300 self.assertEqualIgnoreLeadingWhitespace(
0301 'MOCK_METHOD1(Bar,\nint(C&));',
0302 self.GenerateMethodSource(source))
0303
0304 def testArrayArgWithoutNames(self):
0305 source = """
0306 class Foo {
0307 virtual int Bar(C[]);
0308 };
0309 """
0310 self.assertEqualIgnoreLeadingWhitespace(
0311 'MOCK_METHOD1(Bar,\nint(C[]));',
0312 self.GenerateMethodSource(source))
0313
0314
0315 class GenerateMocksTest(TestCase):
0316
0317 def GenerateMocks(self, cpp_source):
0318 """Convert C++ source to complete Google Mock output source."""
0319
0320 filename = '<test>'
0321 builder = ast.BuilderFromSource(cpp_source, filename)
0322 ast_list = list(builder.Generate())
0323 lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None)
0324 return '\n'.join(lines)
0325
0326 def testNamespaces(self):
0327 source = """
0328 namespace Foo {
0329 namespace Bar { class Forward; }
0330 namespace Baz {
0331
0332 class Test {
0333 public:
0334 virtual void Foo();
0335 };
0336
0337 } // namespace Baz
0338 } // namespace Foo
0339 """
0340 expected = """\
0341 namespace Foo {
0342 namespace Baz {
0343
0344 class MockTest : public Test {
0345 public:
0346 MOCK_METHOD0(Foo,
0347 void());
0348 };
0349
0350 } // namespace Baz
0351 } // namespace Foo
0352 """
0353 self.assertEqualIgnoreLeadingWhitespace(
0354 expected, self.GenerateMocks(source))
0355
0356 def testClassWithStorageSpecifierMacro(self):
0357 source = """
0358 class STORAGE_SPECIFIER Test {
0359 public:
0360 virtual void Foo();
0361 };
0362 """
0363 expected = """\
0364 class MockTest : public Test {
0365 public:
0366 MOCK_METHOD0(Foo,
0367 void());
0368 };
0369 """
0370 self.assertEqualIgnoreLeadingWhitespace(
0371 expected, self.GenerateMocks(source))
0372
0373 def testTemplatedForwardDeclaration(self):
0374 source = """
0375 template <class T> class Forward; // Forward declaration should be ignored.
0376 class Test {
0377 public:
0378 virtual void Foo();
0379 };
0380 """
0381 expected = """\
0382 class MockTest : public Test {
0383 public:
0384 MOCK_METHOD0(Foo,
0385 void());
0386 };
0387 """
0388 self.assertEqualIgnoreLeadingWhitespace(
0389 expected, self.GenerateMocks(source))
0390
0391 def testTemplatedClass(self):
0392 source = """
0393 template <typename S, typename T>
0394 class Test {
0395 public:
0396 virtual void Foo();
0397 };
0398 """
0399 expected = """\
0400 template <typename T0, typename T1>
0401 class MockTest : public Test<T0, T1> {
0402 public:
0403 MOCK_METHOD0_T(Foo,
0404 void());
0405 };
0406 """
0407 self.assertEqualIgnoreLeadingWhitespace(
0408 expected, self.GenerateMocks(source))
0409
0410 def testTemplateInATemplateTypedef(self):
0411 source = """
0412 class Test {
0413 public:
0414 typedef std::vector<std::list<int>> FooType;
0415 virtual void Bar(const FooType& test_arg);
0416 };
0417 """
0418 expected = """\
0419 class MockTest : public Test {
0420 public:
0421 MOCK_METHOD1(Bar,
0422 void(const FooType& test_arg));
0423 };
0424 """
0425 self.assertEqualIgnoreLeadingWhitespace(
0426 expected, self.GenerateMocks(source))
0427
0428 def testTemplateInATemplateTypedefWithComma(self):
0429 source = """
0430 class Test {
0431 public:
0432 typedef std::function<void(
0433 const vector<std::list<int>>&, int> FooType;
0434 virtual void Bar(const FooType& test_arg);
0435 };
0436 """
0437 expected = """\
0438 class MockTest : public Test {
0439 public:
0440 MOCK_METHOD1(Bar,
0441 void(const FooType& test_arg));
0442 };
0443 """
0444 self.assertEqualIgnoreLeadingWhitespace(
0445 expected, self.GenerateMocks(source))
0446
0447 if __name__ == '__main__':
0448 unittest.main()