| 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 | 12 import optparse |
| 13 import os | 13 import os |
| 14 import sys | 14 import sys |
| 15 import unittest | 15 import unittest |
| 16 | 16 |
| 17 from java_cpp_enum import EnumDefinition, GenerateOutput, HeaderParser | 17 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName |
| 18 from java_cpp_enum import HeaderParser |
| 18 | 19 |
| 19 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) | 20 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
| 20 from util import build_utils | 21 from util import build_utils |
| 21 | 22 |
| 22 class TestPreprocess(unittest.TestCase): | 23 class TestPreprocess(unittest.TestCase): |
| 23 def testOutput(self): | 24 def testOutput(self): |
| 24 definition = EnumDefinition(original_enum_name='ClassName', | 25 definition = EnumDefinition(original_enum_name='ClassName', |
| 25 enum_package='some.package', | 26 enum_package='some.package', |
| 26 entries=[('E1', 1), ('E2', '2 << 2')]) | 27 entries=[('E1', 1), ('E2', '2 << 2')]) |
| 27 output = GenerateOutput('path/to/file', definition) | 28 output = GenerateOutput('path/to/file', definition) |
| 28 expected = """ | 29 expected = """ |
| 29 // Copyright 2014 The Chromium Authors. All rights reserved. | 30 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 30 // Use of this source code is governed by a BSD-style license that can be | 31 // Use of this source code is governed by a BSD-style license that can be |
| 31 // found in the LICENSE file. | 32 // found in the LICENSE file. |
| 32 | 33 |
| 33 // This file is autogenerated by | 34 // This file is autogenerated by |
| 34 // build/android/gyp/java_cpp_enum_tests.py | 35 // %s |
| 35 // From | 36 // From |
| 36 // path/to/file | 37 // path/to/file |
| 37 | 38 |
| 38 package some.package; | 39 package some.package; |
| 39 | 40 |
| 40 public class ClassName { | 41 public class ClassName { |
| 41 public static final int E1 = 1; | 42 public static final int E1 = 1; |
| 42 public static final int E2 = 2 << 2; | 43 public static final int E2 = 2 << 2; |
| 43 } | 44 } |
| 44 """ | 45 """ |
| 45 self.assertEqual(expected, output) | 46 self.assertEqual(expected % GetScriptName(), output) |
| 46 | 47 |
| 47 def testParseSimpleEnum(self): | 48 def testParseSimpleEnum(self): |
| 48 test_data = """ | 49 test_data = """ |
| 49 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace | 50 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 50 enum EnumName { | 51 enum EnumName { |
| 51 VALUE_ZERO, | 52 VALUE_ZERO, |
| 52 VALUE_ONE, | 53 VALUE_ONE, |
| 53 }; | 54 }; |
| 54 """.split('\n') | 55 """.split('\n') |
| 55 definitions = HeaderParser(test_data).ParseDefinitions() | 56 definitions = HeaderParser(test_data).ParseDefinitions() |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 def testParseThrowsOnUnknownDirective(self): | 144 def testParseThrowsOnUnknownDirective(self): |
| 144 test_data = """ | 145 test_data = """ |
| 145 // GENERATED_JAVA_UNKNOWN: Value | 146 // GENERATED_JAVA_UNKNOWN: Value |
| 146 enum EnumName { | 147 enum EnumName { |
| 147 VALUE_ONE, | 148 VALUE_ONE, |
| 148 }; | 149 }; |
| 149 """.split('\n') | 150 """.split('\n') |
| 150 with self.assertRaises(Exception): | 151 with self.assertRaises(Exception): |
| 151 HeaderParser(test_data).ParseDefinitions() | 152 HeaderParser(test_data).ParseDefinitions() |
| 152 | 153 |
| 154 def testParseEnumClass(self): |
| 155 test_data = """ |
| 156 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 157 enum class Foo { |
| 158 FOO_A, |
| 159 }; |
| 160 """.split('\n') |
| 161 definitions = HeaderParser(test_data).ParseDefinitions() |
| 162 self.assertEqual(1, len(definitions)) |
| 163 definition = definitions[0] |
| 164 self.assertEqual('Foo', definition.class_name) |
| 165 self.assertEqual('test.namespace', definition.enum_package) |
| 166 self.assertEqual(collections.OrderedDict([('A', 0)]), |
| 167 definition.entries) |
| 168 |
| 169 def testParseEnumStruct(self): |
| 170 test_data = """ |
| 171 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 172 enum struct Foo { |
| 173 FOO_A, |
| 174 }; |
| 175 """.split('\n') |
| 176 definitions = HeaderParser(test_data).ParseDefinitions() |
| 177 self.assertEqual(1, len(definitions)) |
| 178 definition = definitions[0] |
| 179 self.assertEqual('Foo', definition.class_name) |
| 180 self.assertEqual('test.namespace', definition.enum_package) |
| 181 self.assertEqual(collections.OrderedDict([('A', 0)]), |
| 182 definition.entries) |
| 183 |
| 184 def testParseFixedTypeEnum(self): |
| 185 test_data = """ |
| 186 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 187 enum Foo : int { |
| 188 FOO_A, |
| 189 }; |
| 190 """.split('\n') |
| 191 definitions = HeaderParser(test_data).ParseDefinitions() |
| 192 self.assertEqual(1, len(definitions)) |
| 193 definition = definitions[0] |
| 194 self.assertEqual('Foo', definition.class_name) |
| 195 self.assertEqual('test.namespace', definition.enum_package) |
| 196 self.assertEqual('int', definition.fixed_type) |
| 197 self.assertEqual(collections.OrderedDict([('A', 0)]), |
| 198 definition.entries) |
| 199 |
| 200 def testParseFixedTypeEnumClass(self): |
| 201 test_data = """ |
| 202 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 203 enum class Foo: unsigned short { |
| 204 FOO_A, |
| 205 }; |
| 206 """.split('\n') |
| 207 definitions = HeaderParser(test_data).ParseDefinitions() |
| 208 self.assertEqual(1, len(definitions)) |
| 209 definition = definitions[0] |
| 210 self.assertEqual('Foo', definition.class_name) |
| 211 self.assertEqual('test.namespace', definition.enum_package) |
| 212 self.assertEqual('unsigned short', definition.fixed_type) |
| 213 self.assertEqual(collections.OrderedDict([('A', 0)]), |
| 214 definition.entries) |
| 215 |
| 216 def testParseUnknownFixedTypeRaises(self): |
| 217 test_data = """ |
| 218 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
| 219 enum class Foo: foo_type { |
| 220 FOO_A, |
| 221 }; |
| 222 """.split('\n') |
| 223 with self.assertRaises(Exception): |
| 224 HeaderParser(test_data).ParseDefinitions() |
| 225 |
| 153 def testEnumValueAssignmentNoneDefined(self): | 226 def testEnumValueAssignmentNoneDefined(self): |
| 154 definition = EnumDefinition(original_enum_name='c', enum_package='p') | 227 definition = EnumDefinition(original_enum_name='c', enum_package='p') |
| 155 definition.AppendEntry('A', None) | 228 definition.AppendEntry('A', None) |
| 156 definition.AppendEntry('B', None) | 229 definition.AppendEntry('B', None) |
| 157 definition.AppendEntry('C', None) | 230 definition.AppendEntry('C', None) |
| 158 definition.Finalize() | 231 definition.Finalize() |
| 159 self.assertEqual(collections.OrderedDict([('A', 0), | 232 self.assertEqual(collections.OrderedDict([('A', 0), |
| 160 ('B', 1), | 233 ('B', 1), |
| 161 ('C', 2)]), | 234 ('C', 2)]), |
| 162 definition.entries) | 235 definition.entries) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 options, _ = parser.parse_args(argv) | 333 options, _ = parser.parse_args(argv) |
| 261 | 334 |
| 262 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) | 335 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) |
| 263 unittest.TextTestRunner(verbosity=0).run(suite) | 336 unittest.TextTestRunner(verbosity=0).run(suite) |
| 264 | 337 |
| 265 if options.stamp: | 338 if options.stamp: |
| 266 build_utils.Touch(options.stamp) | 339 build_utils.Touch(options.stamp) |
| 267 | 340 |
| 268 if __name__ == '__main__': | 341 if __name__ == '__main__': |
| 269 main(sys.argv[1:]) | 342 main(sys.argv[1:]) |
| OLD | NEW |