| OLD | NEW |
| (Empty) | |
| 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 |
| 18 """Tests for gmock.scripts.generator.cpp.gmock_class.""" |
| 19 |
| 20 __author__ = 'nnorwitz@google.com (Neal Norwitz)' |
| 21 |
| 22 |
| 23 import os |
| 24 import sys |
| 25 import unittest |
| 26 |
| 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__), '..')) |
| 29 |
| 30 from cpp import ast |
| 31 from cpp import gmock_class |
| 32 |
| 33 |
| 34 class TestCase(unittest.TestCase): |
| 35 """Helper class that adds assert methods.""" |
| 36 |
| 37 def assertEqualIgnoreLeadingWhitespace(self, expected_lines, lines): |
| 38 """Specialized assert that ignores the indent level.""" |
| 39 stripped_lines = '\n'.join([s.lstrip() for s in lines.split('\n')]) |
| 40 self.assertEqual(expected_lines, stripped_lines) |
| 41 |
| 42 |
| 43 class GenerateMethodsTest(TestCase): |
| 44 |
| 45 def GenerateMethodSource(self, cpp_source): |
| 46 """Helper method to convert C++ source to gMock output source lines.""" |
| 47 method_source_lines = [] |
| 48 # <test> is a pseudo-filename, it is not read or written. |
| 49 builder = ast.BuilderFromSource(cpp_source, '<test>') |
| 50 ast_list = list(builder.Generate()) |
| 51 gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0]) |
| 52 return ''.join(method_source_lines) |
| 53 |
| 54 def testStrangeNewlineInParameter(self): |
| 55 source = """ |
| 56 class Foo { |
| 57 public: |
| 58 virtual void Bar(int |
| 59 a) = 0; |
| 60 }; |
| 61 """ |
| 62 self.assertEqualIgnoreLeadingWhitespace( |
| 63 'MOCK_METHOD1(Bar,\nvoid(int a));', |
| 64 self.GenerateMethodSource(source)) |
| 65 |
| 66 def testDoubleSlashCommentsInParameterListAreRemoved(self): |
| 67 source = """ |
| 68 class Foo { |
| 69 public: |
| 70 virtual void Bar(int a, // inline comments should be elided. |
| 71 int b // inline comments should be elided. |
| 72 ) const = 0; |
| 73 }; |
| 74 """ |
| 75 self.assertEqualIgnoreLeadingWhitespace( |
| 76 'MOCK_CONST_METHOD2(Bar,\nvoid(int a, int b));', |
| 77 self.GenerateMethodSource(source)) |
| 78 |
| 79 def testCStyleCommentsInParameterListAreNotRemoved(self): |
| 80 # NOTE(nnorwitz): I'm not sure if it's the best behavior to keep these |
| 81 # comments. Also note that C style comments after the last parameter |
| 82 # are still elided. |
| 83 source = """ |
| 84 class Foo { |
| 85 public: |
| 86 virtual const string& Bar(int /* keeper */, int b); |
| 87 }; |
| 88 """ |
| 89 self.assertEqualIgnoreLeadingWhitespace( |
| 90 'MOCK_METHOD2(Bar,\nconst string&(int /* keeper */, int b));', |
| 91 self.GenerateMethodSource(source)) |
| 92 |
| 93 |
| 94 class GenerateMocksTest(TestCase): |
| 95 |
| 96 def GenerateMocks(self, cpp_source): |
| 97 """Helper method to convert C++ source to complete gMock output source.""" |
| 98 # <test> is a pseudo-filename, it is not read or written. |
| 99 filename = '<test>' |
| 100 builder = ast.BuilderFromSource(cpp_source, filename) |
| 101 ast_list = list(builder.Generate()) |
| 102 lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None) |
| 103 return '\n'.join(lines) |
| 104 |
| 105 def testNamespaces(self): |
| 106 source = """ |
| 107 namespace Foo { |
| 108 namespace Bar { class Forward; } |
| 109 namespace Baz { |
| 110 |
| 111 class Test { |
| 112 public: |
| 113 virtual void Foo(); |
| 114 }; |
| 115 |
| 116 } // namespace Baz |
| 117 } // namespace Foo |
| 118 """ |
| 119 expected = """\ |
| 120 namespace Foo { |
| 121 namespace Baz { |
| 122 |
| 123 class MockTest : public Test { |
| 124 public: |
| 125 MOCK_METHOD0(Foo, |
| 126 void()); |
| 127 }; |
| 128 |
| 129 } // namespace Baz |
| 130 } // namespace Foo |
| 131 """ |
| 132 self.assertEqualIgnoreLeadingWhitespace( |
| 133 expected, self.GenerateMocks(source)) |
| 134 |
| 135 |
| 136 if __name__ == '__main__': |
| 137 unittest.main() |
| OLD | NEW |