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.
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
):
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
)
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
42 public class ClassName {
43 public static final int E1 = 1;
44 public static final int E2 = 2 << 2;
47 self
.assertEqual(expected
% GetScriptName(), output
)
49 def testParseSimpleEnum(self
):
51 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
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),
66 def testParseBitShifts(self
):
68 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
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')]),
83 def testParseClassNameOverride(self
):
85 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
86 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OverrideName
91 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
92 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: OtherOverride
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),
109 def testParseTwoEnums(self
):
111 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
122 // GENERATED_JAVA_ENUM_PACKAGE: other.package
123 // GENERATED_JAVA_PREFIX_TO_STRIP: P_
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'),
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),
145 def testParseThrowsOnUnknownDirective(self
):
147 // GENERATED_JAVA_UNKNOWN: Value
152 with self
.assertRaises(Exception):
153 HeaderParser(test_data
).ParseDefinitions()
155 def testParseReturnsEmptyListWithoutDirectives(self
):
161 self
.assertEqual([], HeaderParser(test_data
).ParseDefinitions())
163 def testParseEnumClass(self
):
165 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
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)]),
178 def testParseEnumStruct(self
):
180 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
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)]),
193 def testParseFixedTypeEnum(self
):
195 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
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)]),
209 def testParseFixedTypeEnumClass(self
):
211 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
212 enum class Foo: unsigned short {
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)]),
225 def testParseUnknownFixedTypeRaises(self
):
227 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
228 enum class Foo: foo_type {
232 with self
.assertRaises(Exception):
233 HeaderParser(test_data
).ParseDefinitions()
235 def testParseSimpleMultiLineDirective(self
):
237 // GENERATED_JAVA_ENUM_PACKAGE: (
239 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: Bar
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
):
250 // GENERATED_JAVA_ENUM_PACKAGE: (te
257 definitions
= HeaderParser(test_data
).ParseDefinitions()
258 self
.assertEqual('test.namespace', definitions
[0].enum_package
)
260 def testParseMultiLineDirectiveWithOtherDirective(self
):
262 // GENERATED_JAVA_ENUM_PACKAGE: (
264 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: (
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
):
278 // GENERATED_JAVA_ENUM_PACKAGE: (
281 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: Bar
286 with self
.assertRaises(Exception):
287 HeaderParser(test_data
).ParseDefinitions()
289 def testParseMalformedMultiLineDirective(self
):
291 // GENERATED_JAVA_ENUM_PACKAGE: (
298 with self
.assertRaises(Exception):
299 HeaderParser(test_data
).ParseDefinitions()
301 def testParseMalformedMultiLineDirectiveShort(self
):
303 // GENERATED_JAVA_ENUM_PACKAGE: (
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),
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'),
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),
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),
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),
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),
392 def testImplicitPrefixStripping(self
):
393 definition
= EnumDefinition(original_enum_name
='ClassName',
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),
406 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self
):
407 definition
= EnumDefinition(original_enum_name
='Name',
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
419 java_cpp_enum
.DoParseHeaderFile
= lambda _
: []
420 java_cpp_enum
.DoGenerate('dir', ['file'])
422 java_cpp_enum
.DoParseHeaderFile
= original_do_parse
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
)
433 build_utils
.Touch(options
.stamp
)
435 if __name__
== '__main__':