Move core/forwarders to internal.
[chromium-blink-merge.git] / build / android / gyp / java_cpp_enum_tests.py
blob44f9766c82c98d6c6bab67764d89ee4bc6640282
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.
6 """Tests for enum_preprocess.py.
8 This test suite containss various tests for the C++ -> Java enum generator.
9 """
11 import collections
12 import optparse
13 import os
14 import sys
15 import unittest
17 import java_cpp_enum
18 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName
19 from java_cpp_enum import HeaderParser
21 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp"))
22 from util import build_utils
24 class TestPreprocess(unittest.TestCase):
25 def testOutput(self):
26 definition = EnumDefinition(original_enum_name='ClassName',
27 enum_package='some.package',
28 entries=[('E1', 1), ('E2', '2 << 2')])
29 output = GenerateOutput('path/to/file', definition)
30 expected = """
31 // Copyright 2014 The Chromium Authors. All rights reserved.
32 // Use of this source code is governed by a BSD-style license that can be
33 // found in the LICENSE file.
35 // This file is autogenerated by
36 // %s
37 // From
38 // path/to/file
40 package some.package;
42 public class ClassName {
43 public static final int E1 = 1;
44 public static final int E2 = 2 << 2;
46 """
47 self.assertEqual(expected % GetScriptName(), output)
49 def testParseSimpleEnum(self):
50 test_data = """
51 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
52 enum EnumName {
53 VALUE_ZERO,
54 VALUE_ONE,
56 """.split('\n')
57 definitions = HeaderParser(test_data).ParseDefinitions()
58 self.assertEqual(1, len(definitions))
59 definition = definitions[0]
60 self.assertEqual('EnumName', definition.class_name)
61 self.assertEqual('test.namespace', definition.enum_package)
62 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', 0),
63 ('VALUE_ONE', 1)]),
64 definition.entries)
66 def testParseBitShifts(self):
67 test_data = """
68 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
69 enum EnumName {
70 VALUE_ZERO = 1 << 0,
71 VALUE_ONE = 1 << 1,
73 """.split('\n')
74 definitions = HeaderParser(test_data).ParseDefinitions()
75 self.assertEqual(1, len(definitions))
76 definition = definitions[0]
77 self.assertEqual('EnumName', definition.class_name)
78 self.assertEqual('test.namespace', definition.enum_package)
79 self.assertEqual(collections.OrderedDict([('VALUE_ZERO', '1 << 0'),
80 ('VALUE_ONE', '1 << 1')]),
81 definition.entries)
83 def testParseClassNameOverride(self):
84 test_data = """
85 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
86 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName
87 enum EnumName {
88 FOO
91 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
92 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OtherOverride
93 enum PrefixTest {
94 PREFIX_TEST_A,
95 PREFIX_TEST_B,
97 """.split('\n')
98 definitions = HeaderParser(test_data).ParseDefinitions()
99 self.assertEqual(2, len(definitions))
100 definition = definitions[0]
101 self.assertEqual('OverrideName', definition.class_name)
103 definition = definitions[1]
104 self.assertEqual('OtherOverride', definition.class_name)
105 self.assertEqual(collections.OrderedDict([('A', 0),
106 ('B', 1)]),
107 definition.entries)
109 def testParseTwoEnums(self):
110 test_data = """
111 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
112 enum EnumOne {
113 ENUM_ONE_A = 1,
114 // Comment there
115 ENUM_ONE_B = A,
118 enum EnumIgnore {
119 C, D, E
122 // GENERATED_JAVA_ENUM_PACKAGE: other.package
123 // GENERATED_JAVA_PREFIX_TO_STRIP: P_
124 enum EnumTwo {
125 P_A,
128 """.split('\n')
129 definitions = HeaderParser(test_data).ParseDefinitions()
130 self.assertEqual(2, len(definitions))
131 definition = definitions[0]
132 self.assertEqual('EnumOne', definition.class_name)
133 self.assertEqual('test.namespace', definition.enum_package)
134 self.assertEqual(collections.OrderedDict([('A', '1'),
135 ('B', 'A')]),
136 definition.entries)
138 definition = definitions[1]
139 self.assertEqual('EnumTwo', definition.class_name)
140 self.assertEqual('other.package', definition.enum_package)
141 self.assertEqual(collections.OrderedDict([('A', 0),
142 ('B', 1)]),
143 definition.entries)
145 def testParseThrowsOnUnknownDirective(self):
146 test_data = """
147 // GENERATED_JAVA_UNKNOWN: Value
148 enum EnumName {
149 VALUE_ONE,
151 """.split('\n')
152 with self.assertRaises(Exception):
153 HeaderParser(test_data).ParseDefinitions()
155 def testParseReturnsEmptyListWithoutDirectives(self):
156 test_data = """
157 enum EnumName {
158 VALUE_ONE,
160 """.split('\n')
161 self.assertEqual([], HeaderParser(test_data).ParseDefinitions())
163 def testParseEnumClass(self):
164 test_data = """
165 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
166 enum class Foo {
167 FOO_A,
169 """.split('\n')
170 definitions = HeaderParser(test_data).ParseDefinitions()
171 self.assertEqual(1, len(definitions))
172 definition = definitions[0]
173 self.assertEqual('Foo', definition.class_name)
174 self.assertEqual('test.namespace', definition.enum_package)
175 self.assertEqual(collections.OrderedDict([('A', 0)]),
176 definition.entries)
178 def testParseEnumStruct(self):
179 test_data = """
180 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
181 enum struct Foo {
182 FOO_A,
184 """.split('\n')
185 definitions = HeaderParser(test_data).ParseDefinitions()
186 self.assertEqual(1, len(definitions))
187 definition = definitions[0]
188 self.assertEqual('Foo', definition.class_name)
189 self.assertEqual('test.namespace', definition.enum_package)
190 self.assertEqual(collections.OrderedDict([('A', 0)]),
191 definition.entries)
193 def testParseFixedTypeEnum(self):
194 test_data = """
195 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
196 enum Foo : int {
197 FOO_A,
199 """.split('\n')
200 definitions = HeaderParser(test_data).ParseDefinitions()
201 self.assertEqual(1, len(definitions))
202 definition = definitions[0]
203 self.assertEqual('Foo', definition.class_name)
204 self.assertEqual('test.namespace', definition.enum_package)
205 self.assertEqual('int', definition.fixed_type)
206 self.assertEqual(collections.OrderedDict([('A', 0)]),
207 definition.entries)
209 def testParseFixedTypeEnumClass(self):
210 test_data = """
211 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
212 enum class Foo: unsigned short {
213 FOO_A,
215 """.split('\n')
216 definitions = HeaderParser(test_data).ParseDefinitions()
217 self.assertEqual(1, len(definitions))
218 definition = definitions[0]
219 self.assertEqual('Foo', definition.class_name)
220 self.assertEqual('test.namespace', definition.enum_package)
221 self.assertEqual('unsigned short', definition.fixed_type)
222 self.assertEqual(collections.OrderedDict([('A', 0)]),
223 definition.entries)
225 def testParseUnknownFixedTypeRaises(self):
226 test_data = """
227 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
228 enum class Foo: foo_type {
229 FOO_A,
231 """.split('\n')
232 with self.assertRaises(Exception):
233 HeaderParser(test_data).ParseDefinitions()
235 def testParseSimpleMultiLineDirective(self):
236 test_data = """
237 // GENERATED_JAVA_ENUM_PACKAGE: (
238 // test.namespace)
239 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: Bar
240 enum Foo {
241 FOO_A,
243 """.split('\n')
244 definitions = HeaderParser(test_data).ParseDefinitions()
245 self.assertEqual('test.namespace', definitions[0].enum_package)
246 self.assertEqual('Bar', definitions[0].class_name)
248 def testParseMultiLineDirective(self):
249 test_data = """
250 // GENERATED_JAVA_ENUM_PACKAGE: (te
251 // st.name
252 // space)
253 enum Foo {
254 FOO_A,
256 """.split('\n')
257 definitions = HeaderParser(test_data).ParseDefinitions()
258 self.assertEqual('test.namespace', definitions[0].enum_package)
260 def testParseMultiLineDirectiveWithOtherDirective(self):
261 test_data = """
262 // GENERATED_JAVA_ENUM_PACKAGE: (
263 // test.namespace)
264 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: (
265 // Ba
266 // r
267 // )
268 enum Foo {
269 FOO_A,
271 """.split('\n')
272 definitions = HeaderParser(test_data).ParseDefinitions()
273 self.assertEqual('test.namespace', definitions[0].enum_package)
274 self.assertEqual('Bar', definitions[0].class_name)
276 def testParseMalformedMultiLineDirectiveWithOtherDirective(self):
277 test_data = """
278 // GENERATED_JAVA_ENUM_PACKAGE: (
279 // test.name
280 // space
281 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: Bar
282 enum Foo {
283 FOO_A,
285 """.split('\n')
286 with self.assertRaises(Exception):
287 HeaderParser(test_data).ParseDefinitions()
289 def testParseMalformedMultiLineDirective(self):
290 test_data = """
291 // GENERATED_JAVA_ENUM_PACKAGE: (
292 // test.name
293 // space
294 enum Foo {
295 FOO_A,
297 """.split('\n')
298 with self.assertRaises(Exception):
299 HeaderParser(test_data).ParseDefinitions()
301 def testParseMalformedMultiLineDirectiveShort(self):
302 test_data = """
303 // GENERATED_JAVA_ENUM_PACKAGE: (
304 enum Foo {
305 FOO_A,
307 """.split('\n')
308 with self.assertRaises(Exception):
309 HeaderParser(test_data).ParseDefinitions()
311 def testEnumValueAssignmentNoneDefined(self):
312 definition = EnumDefinition(original_enum_name='c', enum_package='p')
313 definition.AppendEntry('A', None)
314 definition.AppendEntry('B', None)
315 definition.AppendEntry('C', None)
316 definition.Finalize()
317 self.assertEqual(collections.OrderedDict([('A', 0),
318 ('B', 1),
319 ('C', 2)]),
320 definition.entries)
322 def testEnumValueAssignmentAllDefined(self):
323 definition = EnumDefinition(original_enum_name='c', enum_package='p')
324 definition.AppendEntry('A', '1')
325 definition.AppendEntry('B', '2')
326 definition.AppendEntry('C', '3')
327 definition.Finalize()
328 self.assertEqual(collections.OrderedDict([('A', '1'),
329 ('B', '2'),
330 ('C', '3')]),
331 definition.entries)
333 def testEnumValueAssignmentReferences(self):
334 definition = EnumDefinition(original_enum_name='c', enum_package='p')
335 definition.AppendEntry('A', None)
336 definition.AppendEntry('B', 'A')
337 definition.AppendEntry('C', None)
338 definition.AppendEntry('D', 'C')
339 definition.Finalize()
340 self.assertEqual(collections.OrderedDict([('A', 0),
341 ('B', 0),
342 ('C', 1),
343 ('D', 1)]),
344 definition.entries)
346 def testEnumValueAssignmentSet(self):
347 definition = EnumDefinition(original_enum_name='c', enum_package='p')
348 definition.AppendEntry('A', None)
349 definition.AppendEntry('B', '2')
350 definition.AppendEntry('C', None)
351 definition.Finalize()
352 self.assertEqual(collections.OrderedDict([('A', 0),
353 ('B', 2),
354 ('C', 3)]),
355 definition.entries)
357 def testEnumValueAssignmentSetReferences(self):
358 definition = EnumDefinition(original_enum_name='c', enum_package='p')
359 definition.AppendEntry('A', None)
360 definition.AppendEntry('B', 'A')
361 definition.AppendEntry('C', 'B')
362 definition.AppendEntry('D', None)
363 definition.Finalize()
364 self.assertEqual(collections.OrderedDict([('A', 0),
365 ('B', 0),
366 ('C', 0),
367 ('D', 1)]),
368 definition.entries)
370 def testEnumValueAssignmentRaises(self):
371 definition = EnumDefinition(original_enum_name='c', enum_package='p')
372 definition.AppendEntry('A', None)
373 definition.AppendEntry('B', 'foo')
374 definition.AppendEntry('C', None)
375 with self.assertRaises(Exception):
376 definition.Finalize()
378 def testExplicitPrefixStripping(self):
379 definition = EnumDefinition(original_enum_name='c', enum_package='p')
380 definition.AppendEntry('P_A', None)
381 definition.AppendEntry('B', None)
382 definition.AppendEntry('P_C', None)
383 definition.AppendEntry('P_LAST', 'P_C')
384 definition.prefix_to_strip = 'P_'
385 definition.Finalize()
386 self.assertEqual(collections.OrderedDict([('A', 0),
387 ('B', 1),
388 ('C', 2),
389 ('LAST', 2)]),
390 definition.entries)
392 def testImplicitPrefixStripping(self):
393 definition = EnumDefinition(original_enum_name='ClassName',
394 enum_package='p')
395 definition.AppendEntry('CLASS_NAME_A', None)
396 definition.AppendEntry('CLASS_NAME_B', None)
397 definition.AppendEntry('CLASS_NAME_C', None)
398 definition.AppendEntry('CLASS_NAME_LAST', 'CLASS_NAME_C')
399 definition.Finalize()
400 self.assertEqual(collections.OrderedDict([('A', 0),
401 ('B', 1),
402 ('C', 2),
403 ('LAST', 2)]),
404 definition.entries)
406 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self):
407 definition = EnumDefinition(original_enum_name='Name',
408 enum_package='p')
409 definition.AppendEntry('A', None)
410 definition.AppendEntry('B', None)
411 definition.AppendEntry('NAME_LAST', None)
412 definition.Finalize()
413 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys())
415 def testGenerateThrowsOnEmptyInput(self):
416 with self.assertRaises(Exception):
417 original_do_parse = java_cpp_enum.DoParseHeaderFile
418 try:
419 java_cpp_enum.DoParseHeaderFile = lambda _: []
420 java_cpp_enum.DoGenerate('dir', ['file'])
421 finally:
422 java_cpp_enum.DoParseHeaderFile = original_do_parse
424 def main(argv):
425 parser = optparse.OptionParser()
426 parser.add_option("--stamp", help="File to touch on success.")
427 options, _ = parser.parse_args(argv)
429 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess)
430 unittest.TextTestRunner(verbosity=0).run(suite)
432 if options.stamp:
433 build_utils.Touch(options.stamp)
435 if __name__ == '__main__':
436 main(sys.argv[1:])