]>
git.proxmox.com Git - ceph.git/blob - ceph/src/googletest/googlemock/scripts/generator/cpp/gmock_class_test.py
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."""
20 __author__
= 'nnorwitz@google.com (Neal Norwitz)'
27 # Allow the cpp imports below to work when run as a standalone script.
28 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..'))
31 from cpp
import gmock_class
34 class TestCase(unittest
.TestCase
):
35 """Helper class that adds assert methods."""
37 def StripLeadingWhitespace(self
, lines
):
38 """Strip leading whitespace in each line in 'lines'."""
39 return '\n'.join([s
.lstrip() for s
in lines
.split('\n')])
41 def assertEqualIgnoreLeadingWhitespace(self
, expected_lines
, lines
):
42 """Specialized assert that ignores the indent level."""
43 self
.assertEqual(expected_lines
, self
.StripLeadingWhitespace(lines
))
46 class GenerateMethodsTest(TestCase
):
48 def GenerateMethodSource(self
, cpp_source
):
49 """Convert C++ source to Google Mock output source lines."""
50 method_source_lines
= []
51 # <test> is a pseudo-filename, it is not read or written.
52 builder
= ast
.BuilderFromSource(cpp_source
, '<test>')
53 ast_list
= list(builder
.Generate())
54 gmock_class
._GenerateMethods
(method_source_lines
, cpp_source
, ast_list
[0])
55 return '\n'.join(method_source_lines
)
57 def testSimpleMethod(self
):
64 self
.assertEqualIgnoreLeadingWhitespace(
65 'MOCK_METHOD0(Bar,\nint());',
66 self
.GenerateMethodSource(source
))
68 def testSimpleConstructorsAndDestructor(self
):
77 virtual int Bar() = 0;
80 # The constructors and destructor should be ignored.
81 self
.assertEqualIgnoreLeadingWhitespace(
82 'MOCK_METHOD0(Bar,\nint());',
83 self
.GenerateMethodSource(source
))
85 def testVirtualDestructor(self
):
90 virtual int Bar() = 0;
93 # The destructor should be ignored.
94 self
.assertEqualIgnoreLeadingWhitespace(
95 'MOCK_METHOD0(Bar,\nint());',
96 self
.GenerateMethodSource(source
))
98 def testExplicitlyDefaultedConstructorsAndDestructor(self
):
103 Foo(const Foo& f) = default;
104 Foo(Foo&& f) = default;
106 virtual int Bar() = 0;
109 # The constructors and destructor should be ignored.
110 self
.assertEqualIgnoreLeadingWhitespace(
111 'MOCK_METHOD0(Bar,\nint());',
112 self
.GenerateMethodSource(source
))
114 def testExplicitlyDeletedConstructorsAndDestructor(self
):
119 Foo(const Foo& f) = delete;
120 Foo(Foo&& f) = delete;
122 virtual int Bar() = 0;
125 # The constructors and destructor should be ignored.
126 self
.assertEqualIgnoreLeadingWhitespace(
127 'MOCK_METHOD0(Bar,\nint());',
128 self
.GenerateMethodSource(source
))
130 def testSimpleOverrideMethod(self
):
137 self
.assertEqualIgnoreLeadingWhitespace(
138 'MOCK_METHOD0(Bar,\nint());',
139 self
.GenerateMethodSource(source
))
141 def testSimpleConstMethod(self
):
145 virtual void Bar(bool flag) const;
148 self
.assertEqualIgnoreLeadingWhitespace(
149 'MOCK_CONST_METHOD1(Bar,\nvoid(bool flag));',
150 self
.GenerateMethodSource(source
))
152 def testExplicitVoid(self
):
156 virtual int Bar(void);
159 self
.assertEqualIgnoreLeadingWhitespace(
160 'MOCK_METHOD0(Bar,\nint(void));',
161 self
.GenerateMethodSource(source
))
163 def testStrangeNewlineInParameter(self
):
171 self
.assertEqualIgnoreLeadingWhitespace(
172 'MOCK_METHOD1(Bar,\nvoid(int a));',
173 self
.GenerateMethodSource(source
))
175 def testDefaultParameters(self
):
179 virtual void Bar(int a, char c = 'x') = 0;
182 self
.assertEqualIgnoreLeadingWhitespace(
183 'MOCK_METHOD2(Bar,\nvoid(int, char));',
184 self
.GenerateMethodSource(source
))
186 def testMultipleDefaultParameters(self
):
190 virtual void Bar(int a = 42, char c = 'x') = 0;
193 self
.assertEqualIgnoreLeadingWhitespace(
194 'MOCK_METHOD2(Bar,\nvoid(int, char));',
195 self
.GenerateMethodSource(source
))
197 def testRemovesCommentsWhenDefaultsArePresent(self
):
201 virtual void Bar(int a = 42 /* a comment */,
202 char /* other comment */ c= 'x') = 0;
205 self
.assertEqualIgnoreLeadingWhitespace(
206 'MOCK_METHOD2(Bar,\nvoid(int, char));',
207 self
.GenerateMethodSource(source
))
209 def testDoubleSlashCommentsInParameterListAreRemoved(self
):
213 virtual void Bar(int a, // inline comments should be elided.
214 int b // inline comments should be elided.
218 self
.assertEqualIgnoreLeadingWhitespace(
219 'MOCK_CONST_METHOD2(Bar,\nvoid(int a, int b));',
220 self
.GenerateMethodSource(source
))
222 def testCStyleCommentsInParameterListAreNotRemoved(self
):
223 # NOTE(nnorwitz): I'm not sure if it's the best behavior to keep these
224 # comments. Also note that C style comments after the last parameter
229 virtual const string& Bar(int /* keeper */, int b);
232 self
.assertEqualIgnoreLeadingWhitespace(
233 'MOCK_METHOD2(Bar,\nconst string&(int /* keeper */, int b));',
234 self
.GenerateMethodSource(source
))
236 def testArgsOfTemplateTypes(self
):
240 virtual int Bar(const vector<int>& v, map<int, string>* output);
242 self
.assertEqualIgnoreLeadingWhitespace(
243 'MOCK_METHOD2(Bar,\n'
244 'int(const vector<int>& v, map<int, string>* output));',
245 self
.GenerateMethodSource(source
))
247 def testReturnTypeWithOneTemplateArg(self
):
251 virtual vector<int>* Bar(int n);
253 self
.assertEqualIgnoreLeadingWhitespace(
254 'MOCK_METHOD1(Bar,\nvector<int>*(int n));',
255 self
.GenerateMethodSource(source
))
257 def testReturnTypeWithManyTemplateArgs(self
):
261 virtual map<int, string> Bar();
263 # Comparing the comment text is brittle - we'll think of something
264 # better in case this gets annoying, but for now let's keep it simple.
265 self
.assertEqualIgnoreLeadingWhitespace(
266 '// The following line won\'t really compile, as the return\n'
267 '// type has multiple template arguments. To fix it, use a\n'
268 '// typedef for the return type.\n'
269 'MOCK_METHOD0(Bar,\nmap<int, string>());',
270 self
.GenerateMethodSource(source
))
272 def testSimpleMethodInTemplatedClass(self
):
280 self
.assertEqualIgnoreLeadingWhitespace(
281 'MOCK_METHOD0_T(Bar,\nint());',
282 self
.GenerateMethodSource(source
))
284 def testPointerArgWithoutNames(self
):
290 self
.assertEqualIgnoreLeadingWhitespace(
291 'MOCK_METHOD1(Bar,\nint(C*));',
292 self
.GenerateMethodSource(source
))
294 def testReferenceArgWithoutNames(self
):
300 self
.assertEqualIgnoreLeadingWhitespace(
301 'MOCK_METHOD1(Bar,\nint(C&));',
302 self
.GenerateMethodSource(source
))
304 def testArrayArgWithoutNames(self
):
307 virtual int Bar(C[]);
310 self
.assertEqualIgnoreLeadingWhitespace(
311 'MOCK_METHOD1(Bar,\nint(C[]));',
312 self
.GenerateMethodSource(source
))
315 class GenerateMocksTest(TestCase
):
317 def GenerateMocks(self
, cpp_source
):
318 """Convert C++ source to complete Google Mock output source."""
319 # <test> is a pseudo-filename, it is not read or written.
321 builder
= ast
.BuilderFromSource(cpp_source
, filename
)
322 ast_list
= list(builder
.Generate())
323 lines
= gmock_class
._GenerateMocks
(filename
, cpp_source
, ast_list
, None)
324 return '\n'.join(lines
)
326 def testNamespaces(self
):
329 namespace Bar { class Forward; }
344 class MockTest : public Test {
353 self
.assertEqualIgnoreLeadingWhitespace(
354 expected
, self
.GenerateMocks(source
))
356 def testClassWithStorageSpecifierMacro(self
):
358 class STORAGE_SPECIFIER Test {
364 class MockTest : public Test {
370 self
.assertEqualIgnoreLeadingWhitespace(
371 expected
, self
.GenerateMocks(source
))
373 def testTemplatedForwardDeclaration(self
):
375 template <class T> class Forward; // Forward declaration should be ignored.
382 class MockTest : public Test {
388 self
.assertEqualIgnoreLeadingWhitespace(
389 expected
, self
.GenerateMocks(source
))
391 def testTemplatedClass(self
):
393 template <typename S, typename T>
400 template <typename T0, typename T1>
401 class MockTest : public Test<T0, T1> {
407 self
.assertEqualIgnoreLeadingWhitespace(
408 expected
, self
.GenerateMocks(source
))
410 def testTemplateInATemplateTypedef(self
):
414 typedef std::vector<std::list<int>> FooType;
415 virtual void Bar(const FooType& test_arg);
419 class MockTest : public Test {
422 void(const FooType& test_arg));
425 self
.assertEqualIgnoreLeadingWhitespace(
426 expected
, self
.GenerateMocks(source
))
428 def testTemplateInATemplateTypedefWithComma(self
):
432 typedef std::function<void(
433 const vector<std::list<int>>&, int> FooType;
434 virtual void Bar(const FooType& test_arg);
438 class MockTest : public Test {
441 void(const FooType& test_arg));
444 self
.assertEqualIgnoreLeadingWhitespace(
445 expected
, self
.GenerateMocks(source
))
447 if __name__
== '__main__':