OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Tests for java_cpp_string.py. |
| 8 |
| 9 This test suite contains various tests for the C++ -> Java string generator. |
| 10 """ |
| 11 |
| 12 import collections |
| 13 from datetime import date |
| 14 import optparse |
| 15 import os |
| 16 import sys |
| 17 import unittest |
| 18 |
| 19 import java_cpp_string |
| 20 from java_cpp_string import StringListDefinition, GenerateOutput, GetScriptName |
| 21 from java_cpp_string import CppStringFileParser |
| 22 |
| 23 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) |
| 24 from util import build_utils |
| 25 |
| 26 class TestPreprocess(unittest.TestCase): |
| 27 def testOutput(self): |
| 28 definition = StringListDefinition(class_name='ClassName', |
| 29 string_package='some.package', |
| 30 entries=[('L1', 'hello'), |
| 31 ('L2', 'world')]) |
| 32 output = GenerateOutput('path/to/file', definition) |
| 33 expected = """ |
| 34 // Copyright %d The Chromium Authors. All rights reserved. |
| 35 // Use of this source code is governed by a BSD-style license that can be |
| 36 // found in the LICENSE file. |
| 37 |
| 38 // This file is autogenerated by |
| 39 // %s |
| 40 // From |
| 41 // path/to/file |
| 42 |
| 43 package some.package; |
| 44 |
| 45 public class ClassName { |
| 46 public static final String L1 = "hello"; |
| 47 public static final String L2 = "world"; |
| 48 } |
| 49 """ |
| 50 self.assertEqual(expected % (date.today().year, GetScriptName()), output) |
| 51 |
| 52 def testParseAndGenerate(self): |
| 53 test_data = """ |
| 54 // GENERATED_JAVA_STRING_PACKAGE: some.package |
| 55 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 56 |
| 57 // Name of the book. |
| 58 const char NAME[] = "Mars"; |
| 59 // Serial number of the book. |
| 60 const char SERIAL[] = "xx12345"; |
| 61 const char AUTHOR[] = "Bar"; |
| 62 """.split('\n') |
| 63 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 64 output = GenerateOutput('path/to/file', definition) |
| 65 expected = """ |
| 66 // Copyright %d The Chromium Authors. All rights reserved. |
| 67 // Use of this source code is governed by a BSD-style license that can be |
| 68 // found in the LICENSE file. |
| 69 |
| 70 // This file is autogenerated by |
| 71 // %s |
| 72 // From |
| 73 // path/to/file |
| 74 |
| 75 package some.package; |
| 76 |
| 77 public class ClassName { |
| 78 public static final String NAME = "Mars"; |
| 79 public static final String SERIAL = "xx12345"; |
| 80 public static final String AUTHOR = "Bar"; |
| 81 } |
| 82 """ |
| 83 self.assertEqual(expected % (date.today().year, GetScriptName()), output) |
| 84 |
| 85 def testParseSimpleString(self): |
| 86 test_data = """ |
| 87 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 88 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 89 const char STRING_ONE[] = "hello"; |
| 90 const char STRING_TWO[] = "world"; |
| 91 """.split('\n') |
| 92 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 93 self.assertEqual('ClassName', definition.class_name) |
| 94 self.assertEqual('test.package', definition.string_package) |
| 95 self.assertEqual(collections.OrderedDict([('STRING_ONE', 'hello'), |
| 96 ('STRING_TWO', 'world')]), |
| 97 definition.entries) |
| 98 |
| 99 def testParseMultiLineString(self): |
| 100 test_data = """ |
| 101 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 102 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 103 const char MULTI_LINE_STRING[] = |
| 104 "a_really_long_string"; |
| 105 """.split('\n') |
| 106 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 107 self.assertEqual('ClassName', definition.class_name) |
| 108 self.assertEqual('test.package', definition.string_package) |
| 109 self.assertEqual(collections.OrderedDict([('MULTI_LINE_STRING', |
| 110 'a_really_long_string')]), |
| 111 definition.entries) |
| 112 |
| 113 def testParseComments(self): |
| 114 test_data = """ |
| 115 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 116 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 117 const char NAME[] = "Bar"; |
| 118 // const char NAME_IN_COMMENT[] = "Foo"; |
| 119 """.split('\n') |
| 120 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 121 self.assertEqual(1, len(definition.entries)) |
| 122 self.assertEqual('ClassName', definition.class_name) |
| 123 self.assertEqual('test.package', definition.string_package) |
| 124 self.assertEqual(collections.OrderedDict([('NAME', |
| 125 'Bar')]), |
| 126 definition.entries) |
| 127 |
| 128 def testParseThrowsOnMissingEndOfMultiLineStringDefinition(self): |
| 129 test_data = """ |
| 130 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 131 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 132 const char IMCOMPLETE_STRING[] = |
| 133 // Oops! I forgot to complete the string above. |
| 134 const char NAME[] = "foo"; |
| 135 """.split('\n') |
| 136 with self.assertRaises(Exception): |
| 137 CppStringFileParser(test_data).ParseDefinitions() |
| 138 |
| 139 def testParseThrowsOnUnknownDirective(self): |
| 140 test_data = """ |
| 141 // GENERATED_JAVA_UNKNOWN: Value |
| 142 const char NAME[] = "foo"; |
| 143 """.split('\n') |
| 144 with self.assertRaises(Exception): |
| 145 CppStringFileParser(test_data).ParseDefinitions() |
| 146 |
| 147 def testParseThrowsOnMissingClassDirective(self): |
| 148 test_data = """ |
| 149 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 150 const char NAME[] = "foo"; |
| 151 """.split('\n') |
| 152 with self.assertRaises(Exception): |
| 153 CppStringFileParser(test_data).ParseDefinitions() |
| 154 |
| 155 def testParseSimpleMultiLineDirective(self): |
| 156 test_data = """ |
| 157 // GENERATED_JAVA_STRING_PACKAGE: ( |
| 158 // test.namespace) |
| 159 // GENERATED_JAVA_CLASS_NAME: Bar |
| 160 const char NAME[] = "foo"; |
| 161 """.split('\n') |
| 162 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 163 self.assertEqual('test.namespace', definition.string_package) |
| 164 self.assertEqual('Bar', definition.class_name) |
| 165 |
| 166 def testParseMultiLineDirective(self): |
| 167 test_data = """ |
| 168 // GENERATED_JAVA_STRING_PACKAGE: (te |
| 169 // st.name |
| 170 // space) |
| 171 // GENERATED_JAVA_CLASS_NAME: Bar |
| 172 const char NAME[] = "foo"; |
| 173 """.split('\n') |
| 174 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 175 self.assertEqual('test.namespace', definition.string_package) |
| 176 |
| 177 def testParseTwoMultilineDirectives(self): |
| 178 test_data = """ |
| 179 // GENERATED_JAVA_STRING_PACKAGE: ( |
| 180 // test.namespace) |
| 181 // GENERATED_JAVA_CLASS_NAME: ( |
| 182 // Ba |
| 183 // r |
| 184 // ) |
| 185 const char NAME[] = "foo"; |
| 186 """.split('\n') |
| 187 definition = CppStringFileParser(test_data).ParseDefinitions() |
| 188 self.assertEqual('test.namespace', definition.string_package) |
| 189 self.assertEqual('Bar', definition.class_name) |
| 190 |
| 191 def testParseMalformedMultiLineDirectiveWithOtherDirective(self): |
| 192 test_data = """ |
| 193 // GENERATED_JAVA_STRING_PACKAGE: ( |
| 194 // test.name |
| 195 // space |
| 196 // GENERATED_JAVA_CLASS_NAME: Bar |
| 197 const char NAME[] = "foo"; |
| 198 """.split('\n') |
| 199 with self.assertRaises(Exception): |
| 200 CppStringFileParser(test_data).ParseDefinitions() |
| 201 |
| 202 def testParseMalformedMultiLineDirective(self): |
| 203 test_data = """ |
| 204 // GENERATED_JAVA_STRING_PACKAGE: ( |
| 205 // test.name |
| 206 // space |
| 207 const char NAME[] = "foo"; |
| 208 """.split('\n') |
| 209 with self.assertRaises(Exception): |
| 210 CppStringFileParser(test_data).ParseDefinitions() |
| 211 |
| 212 def testParseThrowsOnMissingDerectivesBeforeStringDefinition(self): |
| 213 test_data = """ |
| 214 const char STRING_BEFORE_DIRECTIVE[] = "foo"; |
| 215 // GENERATED_JAVA_STRING_PACKAGE: test.package |
| 216 // GENERATED_JAVA_CLASS_NAME: ClassName |
| 217 const char STRING_AFTER_DIRECTIVE[] = "bar"; |
| 218 """.split('\n') |
| 219 with self.assertRaises(Exception): |
| 220 CppStringFileParser(test_data).ParseDefinitions() |
| 221 |
| 222 def testParseMalformedMultiLineDirectiveShort(self): |
| 223 test_data = """ |
| 224 // GENERATED_JAVA_STRING_PACKAGE: ( |
| 225 const char NAME[] = "foo"; |
| 226 """.split('\n') |
| 227 with self.assertRaises(Exception): |
| 228 CppStringFileParser(test_data).ParseDefinitions() |
| 229 |
| 230 def testGenerateThrowsOnEmptyInput(self): |
| 231 with self.assertRaises(Exception): |
| 232 original_do_parse = java_cpp_string.DoParseHeaderFile |
| 233 try: |
| 234 java_cpp_string.DoParseHeaderFile = lambda _: [] |
| 235 for _ in java_cpp_string.DoGenerate(['file']): |
| 236 pass |
| 237 finally: |
| 238 java_cpp_string.DoParseHeaderFile = original_do_parse |
| 239 |
| 240 def main(argv): |
| 241 parser = optparse.OptionParser() |
| 242 parser.add_option("--stamp", help="File to touch on success.") |
| 243 options, _ = parser.parse_args(argv) |
| 244 |
| 245 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) |
| 246 unittest.TextTestRunner(verbosity=0).run(suite) |
| 247 |
| 248 if options.stamp: |
| 249 build_utils.Touch(options.stamp) |
| 250 |
| 251 if __name__ == '__main__': |
| 252 main(sys.argv[1:]) |
OLD | NEW |