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..458d1d641730d57bb16fb076903389e07cd3c78f |
--- /dev/null |
+++ b/build/android/gyp/java_cpp_string_tests.py |
@@ -0,0 +1,252 @@ |
+#!/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 StringListDefinition, GenerateOutput, GetScriptName |
+from java_cpp_string import CppStringFileParser |
+ |
+sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
+from util import build_utils |
+ |
+class TestPreprocess(unittest.TestCase): |
+ def testOutput(self): |
+ definition = StringListDefinition(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 testParseAndGenerate(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: some.package |
+ // GENERATED_JAVA_CLASS_NAME: ClassName |
+ |
+ // Name of the book. |
+ const char NAME[] = "Mars"; |
+ // Serial number of the book. |
+ const char SERIAL[] = "xx12345"; |
+ const char AUTHOR[] = "Bar"; |
+ """.split('\n') |
+ definition = CppStringFileParser(test_data).ParseDefinitions() |
+ 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 NAME = "Mars"; |
+ public static final String SERIAL = "xx12345"; |
+ public static final String AUTHOR = "Bar"; |
+} |
+""" |
+ 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 = CppStringFileParser(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 = CppStringFileParser(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 testParseComments(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: test.package |
+ // GENERATED_JAVA_CLASS_NAME: ClassName |
+ const char NAME[] = "Bar"; |
+ // const char NAME_IN_COMMENT[] = "Foo"; |
+ """.split('\n') |
+ definition = CppStringFileParser(test_data).ParseDefinitions() |
+ self.assertEqual(1, len(definition.entries)) |
+ self.assertEqual('ClassName', definition.class_name) |
+ self.assertEqual('test.package', definition.string_package) |
+ self.assertEqual(collections.OrderedDict([('NAME', |
+ 'Bar')]), |
+ definition.entries) |
+ |
+ def testParseThrowsOnMissingEndOfMultiLineStringDefinition(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: test.package |
+ // GENERATED_JAVA_CLASS_NAME: ClassName |
+ const char IMCOMPLETE_STRING[] = |
+ // Oops! I forgot to complete the string above. |
+ const char NAME[] = "foo"; |
+ """.split('\n') |
+ with self.assertRaises(Exception): |
+ CppStringFileParser(test_data).ParseDefinitions() |
+ |
+ def testParseThrowsOnUnknownDirective(self): |
+ test_data = """ |
+ // GENERATED_JAVA_UNKNOWN: Value |
+ const char NAME[] = "foo"; |
+ """.split('\n') |
+ with self.assertRaises(Exception): |
+ CppStringFileParser(test_data).ParseDefinitions() |
+ |
+ def testParseThrowsOnMissingClassDirective(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: test.package |
+ const char NAME[] = "foo"; |
+ """.split('\n') |
+ with self.assertRaises(Exception): |
+ CppStringFileParser(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 = CppStringFileParser(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 = CppStringFileParser(test_data).ParseDefinitions() |
+ self.assertEqual('test.namespace', definition.string_package) |
+ |
+ def testParseTwoMultilineDirectives(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: ( |
+ // test.namespace) |
+ // GENERATED_JAVA_CLASS_NAME: ( |
+ // Ba |
+ // r |
+ // ) |
+ const char NAME[] = "foo"; |
+ """.split('\n') |
+ definition = CppStringFileParser(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): |
+ CppStringFileParser(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): |
+ CppStringFileParser(test_data).ParseDefinitions() |
+ |
+ def testParseThrowsOnMissingDerectivesBeforeStringDefinition(self): |
+ test_data = """ |
+ const char STRING_BEFORE_DIRECTIVE[] = "foo"; |
+ // GENERATED_JAVA_STRING_PACKAGE: test.package |
+ // GENERATED_JAVA_CLASS_NAME: ClassName |
+ const char STRING_AFTER_DIRECTIVE[] = "bar"; |
+ """.split('\n') |
+ with self.assertRaises(Exception): |
+ CppStringFileParser(test_data).ParseDefinitions() |
+ |
+ def testParseMalformedMultiLineDirectiveShort(self): |
+ test_data = """ |
+ // GENERATED_JAVA_STRING_PACKAGE: ( |
+ const char NAME[] = "foo"; |
+ """.split('\n') |
+ with self.assertRaises(Exception): |
+ CppStringFileParser(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 |
+ |
+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:]) |