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 import java_cpp_enum |
17 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName | 18 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName |
18 from java_cpp_enum import HeaderParser | 19 from java_cpp_enum import HeaderParser |
19 | 20 |
20 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) | 21 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
21 from util import build_utils | 22 from util import build_utils |
22 | 23 |
23 class TestPreprocess(unittest.TestCase): | 24 class TestPreprocess(unittest.TestCase): |
24 def testOutput(self): | 25 def testOutput(self): |
25 definition = EnumDefinition(original_enum_name='ClassName', | 26 definition = EnumDefinition(original_enum_name='ClassName', |
26 enum_package='some.package', | 27 enum_package='some.package', |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 def testParseThrowsOnUnknownDirective(self): | 145 def testParseThrowsOnUnknownDirective(self): |
145 test_data = """ | 146 test_data = """ |
146 // GENERATED_JAVA_UNKNOWN: Value | 147 // GENERATED_JAVA_UNKNOWN: Value |
147 enum EnumName { | 148 enum EnumName { |
148 VALUE_ONE, | 149 VALUE_ONE, |
149 }; | 150 }; |
150 """.split('\n') | 151 """.split('\n') |
151 with self.assertRaises(Exception): | 152 with self.assertRaises(Exception): |
152 HeaderParser(test_data).ParseDefinitions() | 153 HeaderParser(test_data).ParseDefinitions() |
153 | 154 |
| 155 def testParseReturnsEmptyListWithoutDirectives(self): |
| 156 test_data = """ |
| 157 enum EnumName { |
| 158 VALUE_ONE, |
| 159 }; |
| 160 """.split('\n') |
| 161 self.assertEqual([], HeaderParser(test_data).ParseDefinitions()) |
| 162 |
154 def testParseEnumClass(self): | 163 def testParseEnumClass(self): |
155 test_data = """ | 164 test_data = """ |
156 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace | 165 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace |
157 enum class Foo { | 166 enum class Foo { |
158 FOO_A, | 167 FOO_A, |
159 }; | 168 }; |
160 """.split('\n') | 169 """.split('\n') |
161 definitions = HeaderParser(test_data).ParseDefinitions() | 170 definitions = HeaderParser(test_data).ParseDefinitions() |
162 self.assertEqual(1, len(definitions)) | 171 self.assertEqual(1, len(definitions)) |
163 definition = definitions[0] | 172 definition = definitions[0] |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 | 405 |
397 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self): | 406 def testImplicitPrefixStrippingRequiresAllConstantsToBePrefixed(self): |
398 definition = EnumDefinition(original_enum_name='Name', | 407 definition = EnumDefinition(original_enum_name='Name', |
399 enum_package='p') | 408 enum_package='p') |
400 definition.AppendEntry('A', None) | 409 definition.AppendEntry('A', None) |
401 definition.AppendEntry('B', None) | 410 definition.AppendEntry('B', None) |
402 definition.AppendEntry('NAME_LAST', None) | 411 definition.AppendEntry('NAME_LAST', None) |
403 definition.Finalize() | 412 definition.Finalize() |
404 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys()) | 413 self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys()) |
405 | 414 |
| 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 |
| 423 |
406 def main(argv): | 424 def main(argv): |
407 parser = optparse.OptionParser() | 425 parser = optparse.OptionParser() |
408 parser.add_option("--stamp", help="File to touch on success.") | 426 parser.add_option("--stamp", help="File to touch on success.") |
409 options, _ = parser.parse_args(argv) | 427 options, _ = parser.parse_args(argv) |
410 | 428 |
411 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) | 429 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) |
412 unittest.TextTestRunner(verbosity=0).run(suite) | 430 unittest.TextTestRunner(verbosity=0).run(suite) |
413 | 431 |
414 if options.stamp: | 432 if options.stamp: |
415 build_utils.Touch(options.stamp) | 433 build_utils.Touch(options.stamp) |
416 | 434 |
417 if __name__ == '__main__': | 435 if __name__ == '__main__': |
418 main(sys.argv[1:]) | 436 main(sys.argv[1:]) |
OLD | NEW |