Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: build/android/gyp/java_cpp_enum_tests.py

Issue 2815103004: Android: convert kEnumName to ENUM_NAME in java_cpp_enum.py. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 ('VALUE_ONE', 1)]), 90 ('VALUE_ONE', 1)]),
91 definition.entries) 91 definition.entries)
92 92
93 def testParseBitShifts(self): 93 def testParseBitShifts(self):
94 test_data = """ 94 test_data = """
95 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace 95 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
96 enum EnumName { 96 enum EnumName {
97 VALUE_ZERO = 1 << 0, 97 VALUE_ZERO = 1 << 0,
98 VALUE_ONE = 1 << 1, 98 VALUE_ONE = 1 << 1,
99 }; 99 };
100
101 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
102 enum EnumName {
103 ENUM_NAME_ZERO = 1 << 0,
104 ENUM_NAME_ONE = 1 << 1,
105 ENUM_NAME_TWO = ENUM_NAME_ZERO | ENUM_NAME_ONE,
106 };
100 """.split('\n') 107 """.split('\n')
101 definitions = HeaderParser(test_data).ParseDefinitions() 108 definitions = HeaderParser(test_data).ParseDefinitions()
102 self.assertEqual(1, len(definitions)) 109 self.assertEqual(2, len(definitions))
103 definition = definitions[0] 110 definition = definitions[0]
104 self.assertEqual('EnumName', definition.class_name) 111 self.assertEqual('EnumName', definition.class_name)
105 self.assertEqual('test.namespace', definition.enum_package) 112 self.assertEqual('test.namespace', definition.enum_package)
106 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', '1 << 0'), 113 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', '1 << 0'),
107 ('VALUE_ONE', '1 << 1')]), 114 ('VALUE_ONE', '1 << 1')]),
108 definition.entries) 115 definition.entries)
109 116
117 definition = definitions[1]
118 expected_entries = collections.OrderedDict([
119 ('ZERO', '1 << 0'),
120 ('ONE', '1 << 1'),
121 ('TWO', 'ZERO | ONE')])
122 self.assertEqual(expected_entries, definition.entries)
123
110 def testParseMultilineEnumEntry(self): 124 def testParseMultilineEnumEntry(self):
111 test_data = """ 125 test_data = """
112 // GENERATED_JAVA_ENUM_PACKAGE: bar.namespace 126 // GENERATED_JAVA_ENUM_PACKAGE: bar.namespace
113 enum Foo { 127 enum Foo {
114 VALUE_ZERO = 1 << 0, 128 VALUE_ZERO = 1 << 0,
115 VALUE_ONE = 129 VALUE_ONE =
116 SymbolKey | FnKey | AltGrKey | MetaKey | AltKey | ControlKey, 130 SymbolKey | FnKey | AltGrKey | MetaKey | AltKey | ControlKey,
117 VALUE_TWO = 1 << 18, 131 VALUE_TWO = 1 << 18,
118 }; 132 };
119 """.split('\n') 133 """.split('\n')
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 enum EnumTwo { P_A, P_B }; 296 enum EnumTwo { P_A, P_B };
283 """.split('\n') 297 """.split('\n')
284 definitions = HeaderParser(test_data).ParseDefinitions() 298 definitions = HeaderParser(test_data).ParseDefinitions()
285 definition = definitions[0] 299 definition = definitions[0]
286 self.assertEqual('EnumTwo', definition.class_name) 300 self.assertEqual('EnumTwo', definition.class_name)
287 self.assertEqual('other.package', definition.enum_package) 301 self.assertEqual('other.package', definition.enum_package)
288 self.assertEqual(collections.OrderedDict([('A', 0), 302 self.assertEqual(collections.OrderedDict([('A', 0),
289 ('B', 1)]), 303 ('B', 1)]),
290 definition.entries) 304 definition.entries)
291 305
306 def testParseWithStrippingAndRelativeReferences(self):
307 test_data = """
308 // GENERATED_JAVA_ENUM_PACKAGE: other.package
309 // GENERATED_JAVA_PREFIX_TO_STRIP: P_
310 enum EnumTwo {
311 P_A = 1,
312 // P_A is old-don't use P_A.
313 P_B = P_A,
314 };
315 """.split('\n')
316 definitions = HeaderParser(test_data).ParseDefinitions()
317 definition = definitions[0]
318 self.assertEqual('EnumTwo', definition.class_name)
319 self.assertEqual('other.package', definition.enum_package)
320 self.assertEqual(collections.OrderedDict([('A', '1'),
321 ('B', 'A')]),
322 definition.entries)
323 self.assertEqual(collections.OrderedDict([('B', 'A is old-don\'t use A.')]),
324 definition.comments)
325
292 def testParseSingleLineAndRegularEnum(self): 326 def testParseSingleLineAndRegularEnum(self):
293 test_data = """ 327 test_data = """
294 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace 328 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
295 enum EnumOne { 329 enum EnumOne {
296 ENUM_ONE_A = 1, 330 ENUM_ONE_A = 1,
297 // Comment there 331 // Comment there
298 ENUM_ONE_B = A, 332 ENUM_ONE_B = A,
299 }; 333 };
300 334
301 // GENERATED_JAVA_ENUM_PACKAGE: other.package 335 // GENERATED_JAVA_ENUM_PACKAGE: other.package
302 enum EnumTwo { P_A, P_B }; 336 enum EnumTwo { P_A, P_B };
303 337
304 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace 338 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
305 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName 339 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName
306 enum EnumName { 340 enum EnumName {
307 ENUM_NAME_FOO 341 ENUM_NAME_FOO
308 }; 342 };
309 """.split('\n') 343 """.split('\n')
310 definitions = HeaderParser(test_data).ParseDefinitions() 344 definitions = HeaderParser(test_data).ParseDefinitions()
311 definition = definitions[0] 345 definition = definitions[0]
312 self.assertEqual( 346 self.assertEqual(
313 collections.OrderedDict([('A', '1'), ('B', 'A')]), definition.entries) 347 collections.OrderedDict([('A', '1'), ('B', 'A')]), definition.entries)
314 self.assertEqual(collections.OrderedDict([('ENUM_ONE_B', 'Comment there')]), 348 self.assertEqual(collections.OrderedDict([('B', 'Comment there')]),
315 definition.comments) 349 definition.comments)
316 350
317 self.assertEqual(3, len(definitions)) 351 self.assertEqual(3, len(definitions))
318 definition = definitions[1] 352 definition = definitions[1]
319 self.assertEqual( 353 self.assertEqual(
320 collections.OrderedDict([('P_A', 0), ('P_B', 1)]), definition.entries) 354 collections.OrderedDict([('P_A', 0), ('P_B', 1)]), definition.entries)
321 355
322 definition = definitions[2] 356 definition = definitions[2]
323 self.assertEqual(collections.OrderedDict([('FOO', 0)]), definition.entries) 357 self.assertEqual(collections.OrderedDict([('FOO', 0)]), definition.entries)
324 358
359 def testParseWithCamelCaseNames(self):
360 test_data = """
361 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
362 enum EnumTest {
363 EnumTestA = 1,
364 // comment for EnumTestB.
365 EnumTestB = 2,
366 };
367
368 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
369 // GENERATED_JAVA_PREFIX_TO_STRIP: Test
370 enum AnEnum {
371 TestHTTPOption,
372 TestHTTPSOption,
373 };
374
375 """.split('\n')
376 definitions = HeaderParser(test_data).ParseDefinitions()
377 definition = definitions[0]
378 self.assertEqual(
379 collections.OrderedDict([('ENUM_TEST_A', '1'), ('ENUM_TEST_B', '2')]),
380 definition.entries)
381 self.assertEqual(
382 collections.OrderedDict([('ENUM_TEST_B', 'comment for ENUM_TEST_B.')]),
383 definition.comments)
384
385 definition = definitions[1]
386 self.assertEqual(
387 collections.OrderedDict([('HTTP_OPTION', 0), ('HTTPS_OPTION', 1)]),
388 definition.entries)
389
390 def testParseWithKCamelCaseNames(self):
391 test_data = """
392 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
393 enum EnumOne {
394 kEnumOne = 1,
395 // comment for kEnumTwo.
396 kEnumTwo = 2,
397 };
398
399 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
400 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName
401 // GENERATED_JAVA_PREFIX_TO_STRIP: kEnumName
402 enum EnumName {
403 kEnumNameFoo,
404 kEnumNameBar
405 };
406
407 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
408 enum EnumName {
409 kEnumNameFoo,
410 kEnumBar,
411 };
412
413 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
414 enum Keys {
415 kSymbolKey = 1 << 0,
416 kAltKey = 1 << 1,
417 kUpKey = 1 << 2,
418 kKeyModifiers = kSymbolKey | kAltKey | kUpKey | kKeyModifiers,
419 };
420
421 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
422 enum Mixed {
423 kTestVal,
424 kCodecMPEG2
425 };
426 """.split('\n')
427 definitions = HeaderParser(test_data).ParseDefinitions()
428 definition = definitions[0]
429 self.assertEqual(
430 collections.OrderedDict([('ENUM_ONE', '1'), ('ENUM_TWO', '2')]),
431 definition.entries)
432 self.assertEqual(
433 collections.OrderedDict([('ENUM_TWO', 'comment for ENUM_TWO.')]),
434 definition.comments)
435
436 definition = definitions[1]
437 self.assertEqual(
438 collections.OrderedDict([('FOO', 0), ('BAR', 1)]),
439 definition.entries)
440
441 definition = definitions[2]
442 self.assertEqual(
443 collections.OrderedDict([('ENUM_NAME_FOO', 0), ('ENUM_BAR', 1)]),
444 definition.entries)
445
446 definition = definitions[3]
447 expected_entries = collections.OrderedDict([
448 ('SYMBOL_KEY', '1 << 0'),
449 ('ALT_KEY', '1 << 1'),
450 ('UP_KEY', '1 << 2'),
451 ('KEY_MODIFIERS', 'SYMBOL_KEY | ALT_KEY | UP_KEY | KEY_MODIFIERS')])
452 self.assertEqual(expected_entries, definition.entries)
453
454 definition = definitions[4]
455 self.assertEqual(
456 collections.OrderedDict([('TEST_VAL', 0), ('CODEC_MPEG2', 1)]),
457 definition.entries)
458
325 def testParseThrowsOnUnknownDirective(self): 459 def testParseThrowsOnUnknownDirective(self):
326 test_data = """ 460 test_data = """
327 // GENERATED_JAVA_UNKNOWN: Value 461 // GENERATED_JAVA_UNKNOWN: Value
328 enum EnumName { 462 enum EnumName {
329 VALUE_ONE, 463 VALUE_ONE,
330 }; 464 };
331 """.split('\n') 465 """.split('\n')
332 with self.assertRaises(Exception): 466 with self.assertRaises(Exception):
333 HeaderParser(test_data).ParseDefinitions() 467 HeaderParser(test_data).ParseDefinitions()
334 468
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 options, _ = parser.parse_args(argv) 753 options, _ = parser.parse_args(argv)
620 754
621 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) 755 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess)
622 unittest.TextTestRunner(verbosity=0).run(suite) 756 unittest.TextTestRunner(verbosity=0).run(suite)
623 757
624 if options.stamp: 758 if options.stamp:
625 build_utils.Touch(options.stamp) 759 build_utils.Touch(options.stamp)
626 760
627 if __name__ == '__main__': 761 if __name__ == '__main__':
628 main(sys.argv[1:]) 762 main(sys.argv[1:])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698