Chromium Code Reviews| Index: build/android/gyp/java_cpp_string_tests.py |
| diff --git a/build/android/gyp/java_cpp_string_tests.py b/build/android/gyp/java_cpp_string_tests.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..1468c9a1d3004ef10a6f3d075fdf29d6580b33bc |
| --- /dev/null |
| +++ b/build/android/gyp/java_cpp_string_tests.py |
| @@ -0,0 +1,182 @@ |
| +#!/usr/bin/env python |
| +# |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Tests for java_cpp_string.py. |
| + |
| +This test suite contains various tests for the C++ -> Java string generator. |
| +""" |
| + |
| +import collections |
| +from datetime import date |
| +import optparse |
| +import os |
| +import sys |
| +import unittest |
| + |
| +import java_cpp_string |
| +from java_cpp_string import StringDefinition, GenerateOutput, GetScriptName |
| +from java_cpp_string import HeaderParser |
| + |
| +sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
| +from util import build_utils |
| + |
| +class TestPreprocess(unittest.TestCase): |
| + def testOutput(self): |
| + definition = StringDefinition(class_name='ClassName', |
| + string_package='some.package', |
| + entries=[('L1', 'hello'), ('L2', 'world')]) |
| + output = GenerateOutput('path/to/file', definition) |
| + expected = """ |
| +// Copyright %d The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This file is autogenerated by |
| +// %s |
| +// From |
| +// path/to/file |
| + |
| +package some.package; |
| + |
| +public class ClassName { |
| + public static final String L1 = "hello"; |
| + public static final String L2 = "world"; |
| +} |
| +""" |
| + self.assertEqual(expected % (date.today().year, GetScriptName()), output) |
| + |
| + def testParseSimpleString(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: test.package |
| + // GENERATED_JAVA_CLASS_NAME: ClassName |
| + const char STRING_ONE[] = "hello"; |
| + const char STRING_TWO[] = "world"; |
| + """.split('\n') |
| + definition = HeaderParser(test_data).ParseDefinitions() |
| + self.assertEqual('ClassName', definition.class_name) |
| + self.assertEqual('test.package', definition.string_package) |
| + self.assertEqual(collections.OrderedDict([('STRING_ONE', 'hello'), |
| + ('STRING_TWO', 'world')]), |
| + definition.entries) |
| + |
| + def testParseMultiLineString(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: test.package |
| + // GENERATED_JAVA_CLASS_NAME: ClassName |
| + const char MULTI_LINE_STRING[] = |
| + "a_really_long_string"; |
| + """.split('\n') |
| + definition = HeaderParser(test_data).ParseDefinitions() |
| + self.assertEqual('ClassName', definition.class_name) |
| + self.assertEqual('test.package', definition.string_package) |
| + self.assertEqual(collections.OrderedDict([('MULTI_LINE_STRING', |
| + 'a_really_long_string')]), |
| + definition.entries) |
| + |
| + def testParseThrowsOnUnknownDirective(self): |
| + test_data = """ |
| + // GENERATED_JAVA_UNKNOWN: Value |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + with self.assertRaises(Exception): |
| + HeaderParser(test_data).ParseDefinitions() |
| + |
| + def testParseThrowsOnEmptyClassName(self): |
|
raymes
2016/02/18 02:22:32
nit: testParseThrowsOnMissingClassDirective
lshang
2016/02/29 07:45:03
Done.
|
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: test.package |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + with self.assertRaises(Exception): |
| + HeaderParser(test_data).ParseDefinitions() |
| + |
| + def testParseSimpleMultiLineDirective(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: ( |
| + // test.namespace) |
| + // GENERATED_JAVA_CLASS_NAME: Bar |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + definition = HeaderParser(test_data).ParseDefinitions() |
| + self.assertEqual('test.namespace', definition.string_package) |
| + self.assertEqual('Bar', definition.class_name) |
| + |
| + def testParseMultiLineDirective(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: (te |
| + // st.name |
| + // space) |
| + // GENERATED_JAVA_CLASS_NAME: Bar |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + definition = HeaderParser(test_data).ParseDefinitions() |
| + self.assertEqual('test.namespace', definition.string_package) |
| + |
| + def testParseMultiLineDirectiveWithOtherDirective(self): |
|
raymes
2016/02/18 02:22:32
nit: testParseTwoMultilineDirectives ?
lshang
2016/02/29 07:45:03
Done.
|
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: ( |
| + // test.namespace) |
| + // GENERATED_JAVA_CLASS_NAME: ( |
| + // Ba |
| + // r |
| + // ) |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + definition = HeaderParser(test_data).ParseDefinitions() |
| + self.assertEqual('test.namespace', definition.string_package) |
| + self.assertEqual('Bar', definition.class_name) |
| + |
| + def testParseMalformedMultiLineDirectiveWithOtherDirective(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: ( |
| + // test.name |
| + // space |
| + // GENERATED_JAVA_CLASS_NAME: Bar |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + with self.assertRaises(Exception): |
| + HeaderParser(test_data).ParseDefinitions() |
| + |
| + def testParseMalformedMultiLineDirective(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: ( |
| + // test.name |
| + // space |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + with self.assertRaises(Exception): |
| + HeaderParser(test_data).ParseDefinitions() |
| + |
| + def testParseMalformedMultiLineDirectiveShort(self): |
| + test_data = """ |
| + // GENERATED_JAVA_STRING_PACKAGE: ( |
| + const char NAME[] = "foo"; |
| + """.split('\n') |
| + with self.assertRaises(Exception): |
| + HeaderParser(test_data).ParseDefinitions() |
| + |
| + def testGenerateThrowsOnEmptyInput(self): |
| + with self.assertRaises(Exception): |
| + original_do_parse = java_cpp_string.DoParseHeaderFile |
| + try: |
| + java_cpp_string.DoParseHeaderFile = lambda _: [] |
| + for _ in java_cpp_string.DoGenerate(['file']): |
| + pass |
| + finally: |
| + java_cpp_string.DoParseHeaderFile = original_do_parse |
| + |
|
raymes
2016/02/18 02:22:32
I can think of a few more cases:
1) There are stri
lshang
2016/02/29 07:45:03
Done.
|
| +def main(argv): |
| + parser = optparse.OptionParser() |
| + parser.add_option("--stamp", help="File to touch on success.") |
| + options, _ = parser.parse_args(argv) |
| + |
| + suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) |
| + unittest.TextTestRunner(verbosity=0).run(suite) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| +if __name__ == '__main__': |
| + main(sys.argv[1:]) |