OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Tests for enum_preprocess.py. |
| 7 |
| 8 This test suite containss various tests for the C++ -> Java enum generator. |
| 9 """ |
| 10 |
| 11 import collections |
| 12 import unittest |
| 13 from java_cpp_enum import EnumDefinition, GenerateOutput, HeaderParser |
| 14 |
| 15 class TestPreprocess(unittest.TestCase): |
| 16 def testOutput(self): |
| 17 definition = EnumDefinition(class_name='ClassName', |
| 18 class_package='some.package', |
| 19 entries=[('E1', 1), ('E2', '2 << 2')]) |
| 20 output = GenerateOutput('path/to/file', definition) |
| 21 expected = """ |
| 22 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 23 // Use of this source code is governed by a BSD-style license that can be |
| 24 // found in the LICENSE file. |
| 25 |
| 26 // This file is autogenerated by |
| 27 // build/android/gyp/java_cpp_enum_tests.py |
| 28 // From |
| 29 // path/to/file |
| 30 |
| 31 package some.package; |
| 32 |
| 33 public class ClassName { |
| 34 public static final int E1 = 1; |
| 35 public static final int E2 = 2 << 2; |
| 36 } |
| 37 """ |
| 38 self.assertEqual(expected, output) |
| 39 |
| 40 def testParseSimpleEnum(self): |
| 41 test_data = """ |
| 42 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 43 enum EnumName { |
| 44 VALUE_ZERO, |
| 45 VALUE_ONE, |
| 46 }; |
| 47 """.split('\n') |
| 48 definitions = HeaderParser(test_data).ParseDefinitions() |
| 49 self.assertEqual(1, len(definitions)) |
| 50 definition = definitions[0] |
| 51 self.assertEqual('EnumName', definition.class_name) |
| 52 self.assertEqual('test.namespace', definition.class_package) |
| 53 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', 0), |
| 54 ('VALUE_ONE', 1)]), |
| 55 definition.entries) |
| 56 |
| 57 def testParseTwoEnums(self): |
| 58 test_data = """ |
| 59 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 60 enum EnumOne { |
| 61 ENUM_ONE_A = 1, |
| 62 // Comment there |
| 63 ENUM_ONE_B = A, |
| 64 }; |
| 65 |
| 66 enum EnumIgnore { |
| 67 C, D, E |
| 68 }; |
| 69 |
| 70 // GENERATED_JAVA_ENUM_PACKAGE: other.package |
| 71 // GENERATED_JAVA_PREFIX_TO_STRIP: P_ |
| 72 enum EnumTwo { |
| 73 P_A, |
| 74 P_B |
| 75 }; |
| 76 """.split('\n') |
| 77 definitions = HeaderParser(test_data).ParseDefinitions() |
| 78 self.assertEqual(2, len(definitions)) |
| 79 definition = definitions[0] |
| 80 self.assertEqual('EnumOne', definition.class_name) |
| 81 self.assertEqual('test.namespace', definition.class_package) |
| 82 self.assertEqual(collections.OrderedDict([('A', '1'), |
| 83 ('B', 'A')]), |
| 84 definition.entries) |
| 85 |
| 86 definition = definitions[1] |
| 87 self.assertEqual('EnumTwo', definition.class_name) |
| 88 self.assertEqual('other.package', definition.class_package) |
| 89 self.assertEqual(collections.OrderedDict([('A', 0), |
| 90 ('B', 1)]), |
| 91 definition.entries) |
| 92 |
| 93 def testEnumValueAssignmentNoneDefined(self): |
| 94 definition = EnumDefinition('c', 'p', []) |
| 95 definition.AppendEntry('A', None) |
| 96 definition.AppendEntry('B', None) |
| 97 definition.AppendEntry('C', None) |
| 98 definition.Finalize() |
| 99 self.assertEqual(collections.OrderedDict([('A', 0), |
| 100 ('B', 1), |
| 101 ('C', 2)]), |
| 102 definition.entries) |
| 103 |
| 104 def testEnumValueAssignmentAllDefined(self): |
| 105 definition = EnumDefinition('c', 'p', []) |
| 106 definition.AppendEntry('A', '1') |
| 107 definition.AppendEntry('B', '2') |
| 108 definition.AppendEntry('C', '3') |
| 109 definition.Finalize() |
| 110 self.assertEqual(collections.OrderedDict([('A', '1'), |
| 111 ('B', '2'), |
| 112 ('C', '3')]), |
| 113 definition.entries) |
| 114 |
| 115 def testEnumValueAssignmentReferences(self): |
| 116 definition = EnumDefinition('c', 'p', []) |
| 117 definition.AppendEntry('A', None) |
| 118 definition.AppendEntry('B', 'A') |
| 119 definition.AppendEntry('C', None) |
| 120 definition.AppendEntry('D', 'C') |
| 121 definition.Finalize() |
| 122 self.assertEqual(collections.OrderedDict([('A', 0), |
| 123 ('B', 0), |
| 124 ('C', 1), |
| 125 ('D', 1)]), |
| 126 definition.entries) |
| 127 |
| 128 def testEnumValueAssignmentRaises(self): |
| 129 definition = EnumDefinition('c', 'p', []) |
| 130 definition.AppendEntry('A', None) |
| 131 definition.AppendEntry('B', '1') |
| 132 definition.AppendEntry('C', None) |
| 133 with self.assertRaises(Exception): |
| 134 definition.Finalize() |
| 135 |
| 136 def testExplicitPrefixStripping(self): |
| 137 definition = EnumDefinition('c', 'p', []) |
| 138 definition.AppendEntry('P_A', None) |
| 139 definition.AppendEntry('B', None) |
| 140 definition.AppendEntry('P_C', None) |
| 141 definition.prefix_to_strip = 'P_' |
| 142 definition.Finalize() |
| 143 self.assertEqual(['A', 'B', 'C'], definition.entries.keys()) |
| 144 |
| 145 def testImplicitPrefixStripping(self): |
| 146 definition = EnumDefinition('ClassName', 'p', []) |
| 147 definition.AppendEntry('CLASS_NAME_A', None) |
| 148 definition.AppendEntry('CLASS_NAME_B', None) |
| 149 definition.AppendEntry('CLASS_NAME_C', None) |
| 150 definition.Finalize() |
| 151 self.assertEqual(['A', 'B', 'C'], definition.entries.keys()) |
| 152 |
| 153 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self): |
| 154 definition = EnumDefinition('Name', 'p', []) |
| 155 definition.AppendEntry('A', None) |
| 156 definition.AppendEntry('B', None) |
| 157 definition.AppendEntry('NAME_LAST', None) |
| 158 definition.Finalize() |
| 159 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys()) |
| 160 |
| 161 |
| 162 if __name__ == '__main__': |
| 163 unittest.main() |
OLD | NEW |