OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests for enum_preprocess.py. | 6 """Tests for enum_preprocess.py. |
7 | 7 |
8 This test suite containss various tests for the C++ -> Java enum generator. | 8 This test suite containss various tests for the C++ -> Java enum generator. |
9 """ | 9 """ |
10 | 10 |
11 import collections | 11 import collections |
| 12 import optparse |
| 13 import os |
| 14 import sys |
12 import unittest | 15 import unittest |
| 16 |
13 from java_cpp_enum import EnumDefinition, GenerateOutput, HeaderParser | 17 from java_cpp_enum import EnumDefinition, GenerateOutput, HeaderParser |
14 | 18 |
| 19 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
| 20 from util import build_utils |
| 21 |
15 class TestPreprocess(unittest.TestCase): | 22 class TestPreprocess(unittest.TestCase): |
16 def testOutput(self): | 23 def testOutput(self): |
17 definition = EnumDefinition(class_name='ClassName', | 24 definition = EnumDefinition(class_name='ClassName', |
18 class_package='some.package', | 25 class_package='some.package', |
19 entries=[('E1', 1), ('E2', '2 << 2')]) | 26 entries=[('E1', 1), ('E2', '2 << 2')]) |
20 output = GenerateOutput('path/to/file', definition) | 27 output = GenerateOutput('path/to/file', definition) |
21 expected = """ | 28 expected = """ |
22 // Copyright 2014 The Chromium Authors. All rights reserved. | 29 // Copyright 2014 The Chromium Authors. All rights reserved. |
23 // Use of this source code is governed by a BSD-style license that can be | 30 // Use of this source code is governed by a BSD-style license that can be |
24 // found in the LICENSE file. | 31 // found in the LICENSE file. |
(...skipping 22 matching lines...) Expand all Loading... |
47 """.split('\n') | 54 """.split('\n') |
48 definitions = HeaderParser(test_data).ParseDefinitions() | 55 definitions = HeaderParser(test_data).ParseDefinitions() |
49 self.assertEqual(1, len(definitions)) | 56 self.assertEqual(1, len(definitions)) |
50 definition = definitions[0] | 57 definition = definitions[0] |
51 self.assertEqual('EnumName', definition.class_name) | 58 self.assertEqual('EnumName', definition.class_name) |
52 self.assertEqual('test.namespace', definition.class_package) | 59 self.assertEqual('test.namespace', definition.class_package) |
53 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', 0), | 60 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', 0), |
54 ('VALUE_ONE', 1)]), | 61 ('VALUE_ONE', 1)]), |
55 definition.entries) | 62 definition.entries) |
56 | 63 |
| 64 def testParseClassNameOverride(self): |
| 65 test_data = """ |
| 66 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 67 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName |
| 68 enum EnumName { |
| 69 FOO |
| 70 }; |
| 71 """.split('\n') |
| 72 definitions = HeaderParser(test_data).ParseDefinitions() |
| 73 self.assertEqual(1, len(definitions)) |
| 74 definition = definitions[0] |
| 75 self.assertEqual('OverrideName', definition.class_name) |
| 76 |
57 def testParseTwoEnums(self): | 77 def testParseTwoEnums(self): |
58 test_data = """ | 78 test_data = """ |
59 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace | 79 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
60 enum EnumOne { | 80 enum EnumOne { |
61 ENUM_ONE_A = 1, | 81 ENUM_ONE_A = 1, |
62 // Comment there | 82 // Comment there |
63 ENUM_ONE_B = A, | 83 ENUM_ONE_B = A, |
64 }; | 84 }; |
65 | 85 |
66 enum EnumIgnore { | 86 enum EnumIgnore { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 definition.AppendEntry('B', 'A') | 138 definition.AppendEntry('B', 'A') |
119 definition.AppendEntry('C', None) | 139 definition.AppendEntry('C', None) |
120 definition.AppendEntry('D', 'C') | 140 definition.AppendEntry('D', 'C') |
121 definition.Finalize() | 141 definition.Finalize() |
122 self.assertEqual(collections.OrderedDict([('A', 0), | 142 self.assertEqual(collections.OrderedDict([('A', 0), |
123 ('B', 0), | 143 ('B', 0), |
124 ('C', 1), | 144 ('C', 1), |
125 ('D', 1)]), | 145 ('D', 1)]), |
126 definition.entries) | 146 definition.entries) |
127 | 147 |
| 148 def testEnumValueAssignmentSet(self): |
| 149 definition = EnumDefinition('c', 'p', []) |
| 150 definition.AppendEntry('A', None) |
| 151 definition.AppendEntry('B', '2') |
| 152 definition.AppendEntry('C', None) |
| 153 definition.Finalize() |
| 154 self.assertEqual(collections.OrderedDict([('A', 0), |
| 155 ('B', 2), |
| 156 ('C', 3)]), |
| 157 definition.entries) |
| 158 |
| 159 def testEnumValueAssignmentSetReferences(self): |
| 160 definition = EnumDefinition('c', 'p', []) |
| 161 definition.AppendEntry('A', None) |
| 162 definition.AppendEntry('B', 'A') |
| 163 definition.AppendEntry('C', 'B') |
| 164 definition.AppendEntry('D', None) |
| 165 definition.Finalize() |
| 166 self.assertEqual(collections.OrderedDict([('A', 0), |
| 167 ('B', 0), |
| 168 ('C', 0), |
| 169 ('D', 1)]), |
| 170 definition.entries) |
| 171 |
128 def testEnumValueAssignmentRaises(self): | 172 def testEnumValueAssignmentRaises(self): |
129 definition = EnumDefinition('c', 'p', []) | 173 definition = EnumDefinition('c', 'p', []) |
130 definition.AppendEntry('A', None) | 174 definition.AppendEntry('A', None) |
131 definition.AppendEntry('B', '1') | 175 definition.AppendEntry('B', 'foo') |
132 definition.AppendEntry('C', None) | 176 definition.AppendEntry('C', None) |
133 with self.assertRaises(Exception): | 177 with self.assertRaises(Exception): |
134 definition.Finalize() | 178 definition.Finalize() |
135 | 179 |
136 def testExplicitPrefixStripping(self): | 180 def testExplicitPrefixStripping(self): |
137 definition = EnumDefinition('c', 'p', []) | 181 definition = EnumDefinition('c', 'p', []) |
138 definition.AppendEntry('P_A', None) | 182 definition.AppendEntry('P_A', None) |
139 definition.AppendEntry('B', None) | 183 definition.AppendEntry('B', None) |
140 definition.AppendEntry('P_C', None) | 184 definition.AppendEntry('P_C', None) |
| 185 definition.AppendEntry('P_LAST', 'P_C') |
141 definition.prefix_to_strip = 'P_' | 186 definition.prefix_to_strip = 'P_' |
142 definition.Finalize() | 187 definition.Finalize() |
143 self.assertEqual(['A', 'B', 'C'], definition.entries.keys()) | 188 self.assertEqual(collections.OrderedDict([('A', 0), |
| 189 ('B', 1), |
| 190 ('C', 2), |
| 191 ('LAST', 2)]), |
| 192 definition.entries) |
144 | 193 |
145 def testImplicitPrefixStripping(self): | 194 def testImplicitPrefixStripping(self): |
146 definition = EnumDefinition('ClassName', 'p', []) | 195 definition = EnumDefinition('ClassName', 'p', []) |
147 definition.AppendEntry('CLASS_NAME_A', None) | 196 definition.AppendEntry('CLASS_NAME_A', None) |
148 definition.AppendEntry('CLASS_NAME_B', None) | 197 definition.AppendEntry('CLASS_NAME_B', None) |
149 definition.AppendEntry('CLASS_NAME_C', None) | 198 definition.AppendEntry('CLASS_NAME_C', None) |
| 199 definition.AppendEntry('CLASS_NAME_LAST', 'CLASS_NAME_C') |
150 definition.Finalize() | 200 definition.Finalize() |
151 self.assertEqual(['A', 'B', 'C'], definition.entries.keys()) | 201 self.assertEqual(collections.OrderedDict([('A', 0), |
| 202 ('B', 1), |
| 203 ('C', 2), |
| 204 ('LAST', 2)]), |
| 205 definition.entries) |
152 | 206 |
153 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self): | 207 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self): |
154 definition = EnumDefinition('Name', 'p', []) | 208 definition = EnumDefinition('Name', 'p', []) |
155 definition.AppendEntry('A', None) | 209 definition.AppendEntry('A', None) |
156 definition.AppendEntry('B', None) | 210 definition.AppendEntry('B', None) |
157 definition.AppendEntry('NAME_LAST', None) | 211 definition.AppendEntry('NAME_LAST', None) |
158 definition.Finalize() | 212 definition.Finalize() |
159 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys()) | 213 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys()) |
160 | 214 |
| 215 def main(argv): |
| 216 parser = optparse.OptionParser() |
| 217 parser.add_option("--stamp", help="File to touch on success.") |
| 218 options, _ = parser.parse_args(argv) |
| 219 |
| 220 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) |
| 221 unittest.TextTestRunner().run(suite) |
| 222 |
| 223 if options.stamp: |
| 224 build_utils.Touch(options.stamp) |
161 | 225 |
162 if __name__ == '__main__': | 226 if __name__ == '__main__': |
163 unittest.main() | 227 main(sys.argv[1:]) |
OLD | NEW |