PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/external/googletest/googlemock/scripts/generator/cpp/gmock_class_test.py

https://github.com/gromacs/gromacs
Python | 570 lines | 544 code | 7 blank | 19 comment | 1 complexity | e53d7117e1e64cfebcc582179d65c646 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009 Neal Norwitz All Rights Reserved.
  4. # Portions Copyright 2009 Google Inc. All Rights Reserved.
  5. #
  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
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  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.
  17. """Tests for gmock.scripts.generator.cpp.gmock_class."""
  18. import os
  19. import sys
  20. import unittest
  21. # Allow the cpp imports below to work when run as a standalone script.
  22. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  23. from cpp import ast
  24. from cpp import gmock_class
  25. class TestCase(unittest.TestCase):
  26. """Helper class that adds assert methods."""
  27. @staticmethod
  28. def StripLeadingWhitespace(lines):
  29. """Strip leading whitespace in each line in 'lines'."""
  30. return '\n'.join([s.lstrip() for s in lines.split('\n')])
  31. def assertEqualIgnoreLeadingWhitespace(self, expected_lines, lines):
  32. """Specialized assert that ignores the indent level."""
  33. self.assertEqual(expected_lines, self.StripLeadingWhitespace(lines))
  34. class GenerateMethodsTest(TestCase):
  35. @staticmethod
  36. def GenerateMethodSource(cpp_source):
  37. """Convert C++ source to Google Mock output source lines."""
  38. method_source_lines = []
  39. # <test> is a pseudo-filename, it is not read or written.
  40. builder = ast.BuilderFromSource(cpp_source, '<test>')
  41. ast_list = list(builder.Generate())
  42. gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0])
  43. return '\n'.join(method_source_lines)
  44. def testSimpleMethod(self):
  45. source = """
  46. class Foo {
  47. public:
  48. virtual int Bar();
  49. };
  50. """
  51. self.assertEqualIgnoreLeadingWhitespace(
  52. 'MOCK_METHOD(int, Bar, (), (override));',
  53. self.GenerateMethodSource(source))
  54. def testSimpleConstructorsAndDestructor(self):
  55. source = """
  56. class Foo {
  57. public:
  58. Foo();
  59. Foo(int x);
  60. Foo(const Foo& f);
  61. Foo(Foo&& f);
  62. ~Foo();
  63. virtual int Bar() = 0;
  64. };
  65. """
  66. # The constructors and destructor should be ignored.
  67. self.assertEqualIgnoreLeadingWhitespace(
  68. 'MOCK_METHOD(int, Bar, (), (override));',
  69. self.GenerateMethodSource(source))
  70. def testVirtualDestructor(self):
  71. source = """
  72. class Foo {
  73. public:
  74. virtual ~Foo();
  75. virtual int Bar() = 0;
  76. };
  77. """
  78. # The destructor should be ignored.
  79. self.assertEqualIgnoreLeadingWhitespace(
  80. 'MOCK_METHOD(int, Bar, (), (override));',
  81. self.GenerateMethodSource(source))
  82. def testExplicitlyDefaultedConstructorsAndDestructor(self):
  83. source = """
  84. class Foo {
  85. public:
  86. Foo() = default;
  87. Foo(const Foo& f) = default;
  88. Foo(Foo&& f) = default;
  89. ~Foo() = default;
  90. virtual int Bar() = 0;
  91. };
  92. """
  93. # The constructors and destructor should be ignored.
  94. self.assertEqualIgnoreLeadingWhitespace(
  95. 'MOCK_METHOD(int, Bar, (), (override));',
  96. self.GenerateMethodSource(source))
  97. def testExplicitlyDeletedConstructorsAndDestructor(self):
  98. source = """
  99. class Foo {
  100. public:
  101. Foo() = delete;
  102. Foo(const Foo& f) = delete;
  103. Foo(Foo&& f) = delete;
  104. ~Foo() = delete;
  105. virtual int Bar() = 0;
  106. };
  107. """
  108. # The constructors and destructor should be ignored.
  109. self.assertEqualIgnoreLeadingWhitespace(
  110. 'MOCK_METHOD(int, Bar, (), (override));',
  111. self.GenerateMethodSource(source))
  112. def testSimpleOverrideMethod(self):
  113. source = """
  114. class Foo {
  115. public:
  116. int Bar() override;
  117. };
  118. """
  119. self.assertEqualIgnoreLeadingWhitespace(
  120. 'MOCK_METHOD(int, Bar, (), (override));',
  121. self.GenerateMethodSource(source))
  122. def testSimpleConstMethod(self):
  123. source = """
  124. class Foo {
  125. public:
  126. virtual void Bar(bool flag) const;
  127. };
  128. """
  129. self.assertEqualIgnoreLeadingWhitespace(
  130. 'MOCK_METHOD(void, Bar, (bool flag), (const, override));',
  131. self.GenerateMethodSource(source))
  132. def testExplicitVoid(self):
  133. source = """
  134. class Foo {
  135. public:
  136. virtual int Bar(void);
  137. };
  138. """
  139. self.assertEqualIgnoreLeadingWhitespace(
  140. 'MOCK_METHOD(int, Bar, (), (override));',
  141. self.GenerateMethodSource(source))
  142. def testStrangeNewlineInParameter(self):
  143. source = """
  144. class Foo {
  145. public:
  146. virtual void Bar(int
  147. a) = 0;
  148. };
  149. """
  150. self.assertEqualIgnoreLeadingWhitespace(
  151. 'MOCK_METHOD(void, Bar, (int a), (override));',
  152. self.GenerateMethodSource(source))
  153. def testDefaultParameters(self):
  154. source = """
  155. class Foo {
  156. public:
  157. virtual void Bar(int a, char c = 'x') = 0;
  158. };
  159. """
  160. self.assertEqualIgnoreLeadingWhitespace(
  161. 'MOCK_METHOD(void, Bar, (int a, char c), (override));',
  162. self.GenerateMethodSource(source))
  163. def testMultipleDefaultParameters(self):
  164. source = """
  165. class Foo {
  166. public:
  167. virtual void Bar(
  168. int a = 42,
  169. char c = 'x',
  170. const int* const p = nullptr,
  171. const std::string& s = "42",
  172. char tab[] = {'4','2'},
  173. int const *& rp = aDefaultPointer) = 0;
  174. };
  175. """
  176. self.assertEqualIgnoreLeadingWhitespace(
  177. 'MOCK_METHOD(void, Bar, '
  178. '(int a, char c, const int* const p, const std::string& s, char tab[], int const *& rp), '
  179. '(override));', self.GenerateMethodSource(source))
  180. def testMultipleSingleLineDefaultParameters(self):
  181. source = """
  182. class Foo {
  183. public:
  184. virtual void Bar(int a = 42, int b = 43, int c = 44) = 0;
  185. };
  186. """
  187. self.assertEqualIgnoreLeadingWhitespace(
  188. 'MOCK_METHOD(void, Bar, (int a, int b, int c), (override));',
  189. self.GenerateMethodSource(source))
  190. def testConstDefaultParameter(self):
  191. source = """
  192. class Test {
  193. public:
  194. virtual bool Bar(const int test_arg = 42) = 0;
  195. };
  196. """
  197. self.assertEqualIgnoreLeadingWhitespace(
  198. 'MOCK_METHOD(bool, Bar, (const int test_arg), (override));',
  199. self.GenerateMethodSource(source))
  200. def testConstRefDefaultParameter(self):
  201. source = """
  202. class Test {
  203. public:
  204. virtual bool Bar(const std::string& test_arg = "42" ) = 0;
  205. };
  206. """
  207. self.assertEqualIgnoreLeadingWhitespace(
  208. 'MOCK_METHOD(bool, Bar, (const std::string& test_arg), (override));',
  209. self.GenerateMethodSource(source))
  210. def testRemovesCommentsWhenDefaultsArePresent(self):
  211. source = """
  212. class Foo {
  213. public:
  214. virtual void Bar(int a = 42 /* a comment */,
  215. char /* other comment */ c= 'x') = 0;
  216. };
  217. """
  218. self.assertEqualIgnoreLeadingWhitespace(
  219. 'MOCK_METHOD(void, Bar, (int a, char c), (override));',
  220. self.GenerateMethodSource(source))
  221. def testDoubleSlashCommentsInParameterListAreRemoved(self):
  222. source = """
  223. class Foo {
  224. public:
  225. virtual void Bar(int a, // inline comments should be elided.
  226. int b // inline comments should be elided.
  227. ) const = 0;
  228. };
  229. """
  230. self.assertEqualIgnoreLeadingWhitespace(
  231. 'MOCK_METHOD(void, Bar, (int a, int b), (const, override));',
  232. self.GenerateMethodSource(source))
  233. def testCStyleCommentsInParameterListAreNotRemoved(self):
  234. # NOTE(nnorwitz): I'm not sure if it's the best behavior to keep these
  235. # comments. Also note that C style comments after the last parameter
  236. # are still elided.
  237. source = """
  238. class Foo {
  239. public:
  240. virtual const string& Bar(int /* keeper */, int b);
  241. };
  242. """
  243. self.assertEqualIgnoreLeadingWhitespace(
  244. 'MOCK_METHOD(const string&, Bar, (int, int b), (override));',
  245. self.GenerateMethodSource(source))
  246. def testArgsOfTemplateTypes(self):
  247. source = """
  248. class Foo {
  249. public:
  250. virtual int Bar(const vector<int>& v, map<int, string>* output);
  251. };"""
  252. self.assertEqualIgnoreLeadingWhitespace(
  253. 'MOCK_METHOD(int, Bar, (const vector<int>& v, (map<int, string>* output)), (override));',
  254. self.GenerateMethodSource(source))
  255. def testReturnTypeWithOneTemplateArg(self):
  256. source = """
  257. class Foo {
  258. public:
  259. virtual vector<int>* Bar(int n);
  260. };"""
  261. self.assertEqualIgnoreLeadingWhitespace(
  262. 'MOCK_METHOD(vector<int>*, Bar, (int n), (override));',
  263. self.GenerateMethodSource(source))
  264. def testReturnTypeWithManyTemplateArgs(self):
  265. source = """
  266. class Foo {
  267. public:
  268. virtual map<int, string> Bar();
  269. };"""
  270. self.assertEqualIgnoreLeadingWhitespace(
  271. 'MOCK_METHOD((map<int, string>), Bar, (), (override));',
  272. self.GenerateMethodSource(source))
  273. def testSimpleMethodInTemplatedClass(self):
  274. source = """
  275. template<class T>
  276. class Foo {
  277. public:
  278. virtual int Bar();
  279. };
  280. """
  281. self.assertEqualIgnoreLeadingWhitespace(
  282. 'MOCK_METHOD(int, Bar, (), (override));',
  283. self.GenerateMethodSource(source))
  284. def testPointerArgWithoutNames(self):
  285. source = """
  286. class Foo {
  287. virtual int Bar(C*);
  288. };
  289. """
  290. self.assertEqualIgnoreLeadingWhitespace(
  291. 'MOCK_METHOD(int, Bar, (C*), (override));',
  292. self.GenerateMethodSource(source))
  293. def testReferenceArgWithoutNames(self):
  294. source = """
  295. class Foo {
  296. virtual int Bar(C&);
  297. };
  298. """
  299. self.assertEqualIgnoreLeadingWhitespace(
  300. 'MOCK_METHOD(int, Bar, (C&), (override));',
  301. self.GenerateMethodSource(source))
  302. def testArrayArgWithoutNames(self):
  303. source = """
  304. class Foo {
  305. virtual int Bar(C[]);
  306. };
  307. """
  308. self.assertEqualIgnoreLeadingWhitespace(
  309. 'MOCK_METHOD(int, Bar, (C[]), (override));',
  310. self.GenerateMethodSource(source))
  311. class GenerateMocksTest(TestCase):
  312. @staticmethod
  313. def GenerateMocks(cpp_source):
  314. """Convert C++ source to complete Google Mock output source."""
  315. # <test> is a pseudo-filename, it is not read or written.
  316. filename = '<test>'
  317. builder = ast.BuilderFromSource(cpp_source, filename)
  318. ast_list = list(builder.Generate())
  319. lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None)
  320. return '\n'.join(lines)
  321. def testNamespaces(self):
  322. source = """
  323. namespace Foo {
  324. namespace Bar { class Forward; }
  325. namespace Baz::Qux {
  326. class Test {
  327. public:
  328. virtual void Foo();
  329. };
  330. } // namespace Baz::Qux
  331. } // namespace Foo
  332. """
  333. expected = """\
  334. namespace Foo {
  335. namespace Baz::Qux {
  336. class MockTest : public Test {
  337. public:
  338. MOCK_METHOD(void, Foo, (), (override));
  339. };
  340. } // namespace Baz::Qux
  341. } // namespace Foo
  342. """
  343. self.assertEqualIgnoreLeadingWhitespace(expected,
  344. self.GenerateMocks(source))
  345. def testClassWithStorageSpecifierMacro(self):
  346. source = """
  347. class STORAGE_SPECIFIER Test {
  348. public:
  349. virtual void Foo();
  350. };
  351. """
  352. expected = """\
  353. class MockTest : public Test {
  354. public:
  355. MOCK_METHOD(void, Foo, (), (override));
  356. };
  357. """
  358. self.assertEqualIgnoreLeadingWhitespace(expected,
  359. self.GenerateMocks(source))
  360. def testTemplatedForwardDeclaration(self):
  361. source = """
  362. template <class T> class Forward; // Forward declaration should be ignored.
  363. class Test {
  364. public:
  365. virtual void Foo();
  366. };
  367. """
  368. expected = """\
  369. class MockTest : public Test {
  370. public:
  371. MOCK_METHOD(void, Foo, (), (override));
  372. };
  373. """
  374. self.assertEqualIgnoreLeadingWhitespace(expected,
  375. self.GenerateMocks(source))
  376. def testTemplatedClass(self):
  377. source = """
  378. template <typename S, typename T>
  379. class Test {
  380. public:
  381. virtual void Foo();
  382. };
  383. """
  384. expected = """\
  385. template <typename S, typename T>
  386. class MockTest : public Test<S, T> {
  387. public:
  388. MOCK_METHOD(void, Foo, (), (override));
  389. };
  390. """
  391. self.assertEqualIgnoreLeadingWhitespace(expected,
  392. self.GenerateMocks(source))
  393. def testTemplateInATemplateTypedef(self):
  394. source = """
  395. class Test {
  396. public:
  397. typedef std::vector<std::list<int>> FooType;
  398. virtual void Bar(const FooType& test_arg);
  399. };
  400. """
  401. expected = """\
  402. class MockTest : public Test {
  403. public:
  404. MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
  405. };
  406. """
  407. self.assertEqualIgnoreLeadingWhitespace(expected,
  408. self.GenerateMocks(source))
  409. def testTemplatedClassWithTemplatedArguments(self):
  410. source = """
  411. template <typename S, typename T, typename U, typename V, typename W>
  412. class Test {
  413. public:
  414. virtual U Foo(T some_arg);
  415. };
  416. """
  417. expected = """\
  418. template <typename S, typename T, typename U, typename V, typename W>
  419. class MockTest : public Test<S, T, U, V, W> {
  420. public:
  421. MOCK_METHOD(U, Foo, (T some_arg), (override));
  422. };
  423. """
  424. self.assertEqualIgnoreLeadingWhitespace(expected,
  425. self.GenerateMocks(source))
  426. def testTemplateInATemplateTypedefWithComma(self):
  427. source = """
  428. class Test {
  429. public:
  430. typedef std::function<void(
  431. const vector<std::list<int>>&, int> FooType;
  432. virtual void Bar(const FooType& test_arg);
  433. };
  434. """
  435. expected = """\
  436. class MockTest : public Test {
  437. public:
  438. MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
  439. };
  440. """
  441. self.assertEqualIgnoreLeadingWhitespace(expected,
  442. self.GenerateMocks(source))
  443. def testParenthesizedCommaInArg(self):
  444. source = """
  445. class Test {
  446. public:
  447. virtual void Bar(std::function<void(int, int)> f);
  448. };
  449. """
  450. expected = """\
  451. class MockTest : public Test {
  452. public:
  453. MOCK_METHOD(void, Bar, (std::function<void(int, int)> f), (override));
  454. };
  455. """
  456. self.assertEqualIgnoreLeadingWhitespace(expected,
  457. self.GenerateMocks(source))
  458. def testEnumType(self):
  459. source = """
  460. class Test {
  461. public:
  462. enum Bar {
  463. BAZ, QUX, QUUX, QUUUX
  464. };
  465. virtual void Foo();
  466. };
  467. """
  468. expected = """\
  469. class MockTest : public Test {
  470. public:
  471. MOCK_METHOD(void, Foo, (), (override));
  472. };
  473. """
  474. self.assertEqualIgnoreLeadingWhitespace(expected,
  475. self.GenerateMocks(source))
  476. def testEnumClassType(self):
  477. source = """
  478. class Test {
  479. public:
  480. enum class Bar {
  481. BAZ, QUX, QUUX, QUUUX
  482. };
  483. virtual void Foo();
  484. };
  485. """
  486. expected = """\
  487. class MockTest : public Test {
  488. public:
  489. MOCK_METHOD(void, Foo, (), (override));
  490. };
  491. """
  492. self.assertEqualIgnoreLeadingWhitespace(expected,
  493. self.GenerateMocks(source))
  494. def testStdFunction(self):
  495. source = """
  496. class Test {
  497. public:
  498. Test(std::function<int(std::string)> foo) : foo_(foo) {}
  499. virtual std::function<int(std::string)> foo();
  500. private:
  501. std::function<int(std::string)> foo_;
  502. };
  503. """
  504. expected = """\
  505. class MockTest : public Test {
  506. public:
  507. MOCK_METHOD(std::function<int (std::string)>, foo, (), (override));
  508. };
  509. """
  510. self.assertEqualIgnoreLeadingWhitespace(expected,
  511. self.GenerateMocks(source))
  512. if __name__ == '__main__':
  513. unittest.main()