3 # Copyright 2009 Neal Norwitz All Rights Reserved.
4 # Portions Copyright 2009 Google Inc. All Rights Reserved.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 """Tests for gmock.scripts.generator.cpp.gmock_class."""
24 # Allow the cpp imports below to work when run as a standalone script.
25 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
28 from cpp import gmock_class
31 class TestCase(unittest.TestCase):
32 """Helper class that adds assert methods."""
35 def StripLeadingWhitespace(lines):
36 """Strip leading whitespace in each line in 'lines'."""
37 return '\n'.join([s.lstrip() for s in lines.split('\n')])
39 def assertEqualIgnoreLeadingWhitespace(self, expected_lines, lines):
40 """Specialized assert that ignores the indent level."""
41 self.assertEqual(expected_lines, self.StripLeadingWhitespace(lines))
44 class GenerateMethodsTest(TestCase):
47 def GenerateMethodSource(cpp_source):
48 """Convert C++ source to Google Mock output source lines."""
49 method_source_lines = []
50 # <test> is a pseudo-filename, it is not read or written.
51 builder = ast.BuilderFromSource(cpp_source, '<test>')
52 ast_list = list(builder.Generate())
53 gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0])
54 return '\n'.join(method_source_lines)
56 def testSimpleMethod(self):
63 self.assertEqualIgnoreLeadingWhitespace(
64 'MOCK_METHOD(int, Bar, (), (override));',
65 self.GenerateMethodSource(source))
67 def testSimpleConstructorsAndDestructor(self):
76 virtual int Bar() = 0;
79 # The constructors and destructor should be ignored.
80 self.assertEqualIgnoreLeadingWhitespace(
81 'MOCK_METHOD(int, Bar, (), (override));',
82 self.GenerateMethodSource(source))
84 def testVirtualDestructor(self):
89 virtual int Bar() = 0;
92 # The destructor should be ignored.
93 self.assertEqualIgnoreLeadingWhitespace(
94 'MOCK_METHOD(int, Bar, (), (override));',
95 self.GenerateMethodSource(source))
97 def testExplicitlyDefaultedConstructorsAndDestructor(self):
102 Foo(const Foo& f) = default;
103 Foo(Foo&& f) = default;
105 virtual int Bar() = 0;
108 # The constructors and destructor should be ignored.
109 self.assertEqualIgnoreLeadingWhitespace(
110 'MOCK_METHOD(int, Bar, (), (override));',
111 self.GenerateMethodSource(source))
113 def testExplicitlyDeletedConstructorsAndDestructor(self):
118 Foo(const Foo& f) = delete;
119 Foo(Foo&& f) = delete;
121 virtual int Bar() = 0;
124 # The constructors and destructor should be ignored.
125 self.assertEqualIgnoreLeadingWhitespace(
126 'MOCK_METHOD(int, Bar, (), (override));',
127 self.GenerateMethodSource(source))
129 def testSimpleOverrideMethod(self):
136 self.assertEqualIgnoreLeadingWhitespace(
137 'MOCK_METHOD(int, Bar, (), (override));',
138 self.GenerateMethodSource(source))
140 def testSimpleConstMethod(self):
144 virtual void Bar(bool flag) const;
147 self.assertEqualIgnoreLeadingWhitespace(
148 'MOCK_METHOD(void, Bar, (bool flag), (const, override));',
149 self.GenerateMethodSource(source))
151 def testExplicitVoid(self):
155 virtual int Bar(void);
158 self.assertEqualIgnoreLeadingWhitespace(
159 'MOCK_METHOD(int, Bar, (), (override));',
160 self.GenerateMethodSource(source))
162 def testStrangeNewlineInParameter(self):
170 self.assertEqualIgnoreLeadingWhitespace(
171 'MOCK_METHOD(void, Bar, (int a), (override));',
172 self.GenerateMethodSource(source))
174 def testDefaultParameters(self):
178 virtual void Bar(int a, char c = 'x') = 0;
181 self.assertEqualIgnoreLeadingWhitespace(
182 'MOCK_METHOD(void, Bar, (int a, char c), (override));',
183 self.GenerateMethodSource(source))
185 def testMultipleDefaultParameters(self):
192 const int* const p = nullptr,
193 const std::string& s = "42",
194 char tab[] = {'4','2'},
195 int const *& rp = aDefaultPointer) = 0;
198 self.assertEqualIgnoreLeadingWhitespace(
199 'MOCK_METHOD(void, Bar, '
200 '(int a, char c, const int* const p, const std::string& s, char tab[], int const *& rp), '
201 '(override));', self.GenerateMethodSource(source))
203 def testMultipleSingleLineDefaultParameters(self):
207 virtual void Bar(int a = 42, int b = 43, int c = 44) = 0;
210 self.assertEqualIgnoreLeadingWhitespace(
211 'MOCK_METHOD(void, Bar, (int a, int b, int c), (override));',
212 self.GenerateMethodSource(source))
214 def testConstDefaultParameter(self):
218 virtual bool Bar(const int test_arg = 42) = 0;
221 self.assertEqualIgnoreLeadingWhitespace(
222 'MOCK_METHOD(bool, Bar, (const int test_arg), (override));',
223 self.GenerateMethodSource(source))
225 def testConstRefDefaultParameter(self):
229 virtual bool Bar(const std::string& test_arg = "42" ) = 0;
232 self.assertEqualIgnoreLeadingWhitespace(
233 'MOCK_METHOD(bool, Bar, (const std::string& test_arg), (override));',
234 self.GenerateMethodSource(source))
236 def testRemovesCommentsWhenDefaultsArePresent(self):
240 virtual void Bar(int a = 42 /* a comment */,
241 char /* other comment */ c= 'x') = 0;
244 self.assertEqualIgnoreLeadingWhitespace(
245 'MOCK_METHOD(void, Bar, (int a, char c), (override));',
246 self.GenerateMethodSource(source))
248 def testDoubleSlashCommentsInParameterListAreRemoved(self):
252 virtual void Bar(int a, // inline comments should be elided.
253 int b // inline comments should be elided.
257 self.assertEqualIgnoreLeadingWhitespace(
258 'MOCK_METHOD(void, Bar, (int a, int b), (const, override));',
259 self.GenerateMethodSource(source))
261 def testCStyleCommentsInParameterListAreNotRemoved(self):
262 # NOTE(nnorwitz): I'm not sure if it's the best behavior to keep these
263 # comments. Also note that C style comments after the last parameter
268 virtual const string& Bar(int /* keeper */, int b);
271 self.assertEqualIgnoreLeadingWhitespace(
272 'MOCK_METHOD(const string&, Bar, (int, int b), (override));',
273 self.GenerateMethodSource(source))
275 def testArgsOfTemplateTypes(self):
279 virtual int Bar(const vector<int>& v, map<int, string>* output);
281 self.assertEqualIgnoreLeadingWhitespace(
282 'MOCK_METHOD(int, Bar, (const vector<int>& v, (map<int, string>* output)), (override));',
283 self.GenerateMethodSource(source))
285 def testReturnTypeWithOneTemplateArg(self):
289 virtual vector<int>* Bar(int n);
291 self.assertEqualIgnoreLeadingWhitespace(
292 'MOCK_METHOD(vector<int>*, Bar, (int n), (override));',
293 self.GenerateMethodSource(source))
295 def testReturnTypeWithManyTemplateArgs(self):
299 virtual map<int, string> Bar();
301 self.assertEqualIgnoreLeadingWhitespace(
302 'MOCK_METHOD((map<int, string>), Bar, (), (override));',
303 self.GenerateMethodSource(source))
305 def testSimpleMethodInTemplatedClass(self):
313 self.assertEqualIgnoreLeadingWhitespace(
314 'MOCK_METHOD(int, Bar, (), (override));',
315 self.GenerateMethodSource(source))
317 def testPointerArgWithoutNames(self):
323 self.assertEqualIgnoreLeadingWhitespace(
324 'MOCK_METHOD(int, Bar, (C*), (override));',
325 self.GenerateMethodSource(source))
327 def testReferenceArgWithoutNames(self):
333 self.assertEqualIgnoreLeadingWhitespace(
334 'MOCK_METHOD(int, Bar, (C&), (override));',
335 self.GenerateMethodSource(source))
337 def testArrayArgWithoutNames(self):
340 virtual int Bar(C[]);
343 self.assertEqualIgnoreLeadingWhitespace(
344 'MOCK_METHOD(int, Bar, (C[]), (override));',
345 self.GenerateMethodSource(source))
348 class GenerateMocksTest(TestCase):
351 def GenerateMocks(cpp_source):
352 """Convert C++ source to complete Google Mock output source."""
353 # <test> is a pseudo-filename, it is not read or written.
355 builder = ast.BuilderFromSource(cpp_source, filename)
356 ast_list = list(builder.Generate())
357 lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None)
358 return '\n'.join(lines)
360 def testNamespaces(self):
363 namespace Bar { class Forward; }
371 } // namespace Baz::Qux
378 class MockTest : public Test {
380 MOCK_METHOD(void, Foo, (), (override));
383 } // namespace Baz::Qux
386 self.assertEqualIgnoreLeadingWhitespace(expected,
387 self.GenerateMocks(source))
389 def testClassWithStorageSpecifierMacro(self):
391 class STORAGE_SPECIFIER Test {
397 class MockTest : public Test {
399 MOCK_METHOD(void, Foo, (), (override));
402 self.assertEqualIgnoreLeadingWhitespace(expected,
403 self.GenerateMocks(source))
405 def testTemplatedForwardDeclaration(self):
407 template <class T> class Forward; // Forward declaration should be ignored.
414 class MockTest : public Test {
416 MOCK_METHOD(void, Foo, (), (override));
419 self.assertEqualIgnoreLeadingWhitespace(expected,
420 self.GenerateMocks(source))
422 def testTemplatedClass(self):
424 template <typename S, typename T>
431 template <typename S, typename T>
432 class MockTest : public Test<S, T> {
434 MOCK_METHOD(void, Foo, (), (override));
437 self.assertEqualIgnoreLeadingWhitespace(expected,
438 self.GenerateMocks(source))
440 def testTemplateInATemplateTypedef(self):
444 typedef std::vector<std::list<int>> FooType;
445 virtual void Bar(const FooType& test_arg);
449 class MockTest : public Test {
451 MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
454 self.assertEqualIgnoreLeadingWhitespace(expected,
455 self.GenerateMocks(source))
457 def testTemplatedClassWithTemplatedArguments(self):
459 template <typename S, typename T, typename U, typename V, typename W>
462 virtual U Foo(T some_arg);
466 template <typename S, typename T, typename U, typename V, typename W>
467 class MockTest : public Test<S, T, U, V, W> {
469 MOCK_METHOD(U, Foo, (T some_arg), (override));
472 self.assertEqualIgnoreLeadingWhitespace(expected,
473 self.GenerateMocks(source))
475 def testTemplateInATemplateTypedefWithComma(self):
479 typedef std::function<void(
480 const vector<std::list<int>>&, int> FooType;
481 virtual void Bar(const FooType& test_arg);
485 class MockTest : public Test {
487 MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
490 self.assertEqualIgnoreLeadingWhitespace(expected,
491 self.GenerateMocks(source))
493 def testParenthesizedCommaInArg(self):
497 virtual void Bar(std::function<void(int, int)> f);
501 class MockTest : public Test {
503 MOCK_METHOD(void, Bar, (std::function<void(int, int)> f), (override));
506 self.assertEqualIgnoreLeadingWhitespace(expected,
507 self.GenerateMocks(source))
509 def testEnumType(self):
514 BAZ, QUX, QUUX, QUUUX
520 class MockTest : public Test {
522 MOCK_METHOD(void, Foo, (), (override));
525 self.assertEqualIgnoreLeadingWhitespace(expected,
526 self.GenerateMocks(source))
528 def testEnumClassType(self):
533 BAZ, QUX, QUUX, QUUUX
539 class MockTest : public Test {
541 MOCK_METHOD(void, Foo, (), (override));
544 self.assertEqualIgnoreLeadingWhitespace(expected,
545 self.GenerateMocks(source))
547 def testStdFunction(self):
551 Test(std::function<int(std::string)> foo) : foo_(foo) {}
553 virtual std::function<int(std::string)> foo();
556 std::function<int(std::string)> foo_;
560 class MockTest : public Test {
562 MOCK_METHOD(std::function<int (std::string)>, foo, (), (override));
565 self.assertEqualIgnoreLeadingWhitespace(expected,
566 self.GenerateMocks(source))
569 if __name__ == '__main__':