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 StringDefinition, GenerateOutput, GetScriptName | |
21 from java_cpp_string import HeaderParser | |
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 = StringDefinition(class_name='ClassName', | |
29 string_package='some.package', | |
30 entries=[('L1', 'hello'), ('L2', 'world')]) | |
31 output = GenerateOutput('path/to/file', definition) | |
32 expected = """ | |
33 // Copyright %d The Chromium Authors. All rights reserved. | |
34 // Use of this source code is governed by a BSD-style license that can be | |
35 // found in the LICENSE file. | |
36 | |
37 // This file is autogenerated by | |
38 // %s | |
39 // From | |
40 // path/to/file | |
41 | |
42 package some.package; | |
43 | |
44 public class ClassName { | |
45 public static final String L1 = "hello"; | |
46 public static final String L2 = "world"; | |
47 } | |
48 """ | |
49 self.assertEqual(expected % (date.today().year, GetScriptName()), output) | |
50 | |
51 def testParseSimpleString(self): | |
52 test_data = """ | |
53 // GENERATED_JAVA_STRING_PACKAGE: test.package | |
54 // GENERATED_JAVA_CLASS_NAME: ClassName | |
55 const char STRING_ONE[] = "hello"; | |
56 const char STRING_TWO[] = "world"; | |
57 """.split('\n') | |
58 definition = HeaderParser(test_data).ParseDefinitions() | |
59 self.assertEqual('ClassName', definition.class_name) | |
60 self.assertEqual('test.package', definition.string_package) | |
61 self.assertEqual(collections.OrderedDict([('STRING_ONE', 'hello'), | |
62 ('STRING_TWO', 'world')]), | |
63 definition.entries) | |
64 | |
65 def testParseMultiLineString(self): | |
66 test_data = """ | |
67 // GENERATED_JAVA_STRING_PACKAGE: test.package | |
68 // GENERATED_JAVA_CLASS_NAME: ClassName | |
69 const char MULTI_LINE_STRING[] = | |
70 "a_really_long_string"; | |
71 """.split('\n') | |
72 definition = HeaderParser(test_data).ParseDefinitions() | |
73 self.assertEqual('ClassName', definition.class_name) | |
74 self.assertEqual('test.package', definition.string_package) | |
75 self.assertEqual(collections.OrderedDict([('MULTI_LINE_STRING', | |
76 'a_really_long_string')]), | |
77 definition.entries) | |
78 | |
79 def testParseThrowsOnUnknownDirective(self): | |
80 test_data = """ | |
81 // GENERATED_JAVA_UNKNOWN: Value | |
82 const char NAME[] = "foo"; | |
83 """.split('\n') | |
84 with self.assertRaises(Exception): | |
85 HeaderParser(test_data).ParseDefinitions() | |
86 | |
87 def testParseThrowsOnEmptyClassName(self): | |
raymes
2016/02/18 02:22:32
nit: testParseThrowsOnMissingClassDirective
lshang
2016/02/29 07:45:03
Done.
| |
88 test_data = """ | |
89 // GENERATED_JAVA_STRING_PACKAGE: test.package | |
90 const char NAME[] = "foo"; | |
91 """.split('\n') | |
92 with self.assertRaises(Exception): | |
93 HeaderParser(test_data).ParseDefinitions() | |
94 | |
95 def testParseSimpleMultiLineDirective(self): | |
96 test_data = """ | |
97 // GENERATED_JAVA_STRING_PACKAGE: ( | |
98 // test.namespace) | |
99 // GENERATED_JAVA_CLASS_NAME: Bar | |
100 const char NAME[] = "foo"; | |
101 """.split('\n') | |
102 definition = HeaderParser(test_data).ParseDefinitions() | |
103 self.assertEqual('test.namespace', definition.string_package) | |
104 self.assertEqual('Bar', definition.class_name) | |
105 | |
106 def testParseMultiLineDirective(self): | |
107 test_data = """ | |
108 // GENERATED_JAVA_STRING_PACKAGE: (te | |
109 // st.name | |
110 // space) | |
111 // GENERATED_JAVA_CLASS_NAME: Bar | |
112 const char NAME[] = "foo"; | |
113 """.split('\n') | |
114 definition = HeaderParser(test_data).ParseDefinitions() | |
115 self.assertEqual('test.namespace', definition.string_package) | |
116 | |
117 def testParseMultiLineDirectiveWithOtherDirective(self): | |
raymes
2016/02/18 02:22:32
nit: testParseTwoMultilineDirectives ?
lshang
2016/02/29 07:45:03
Done.
| |
118 test_data = """ | |
119 // GENERATED_JAVA_STRING_PACKAGE: ( | |
120 // test.namespace) | |
121 // GENERATED_JAVA_CLASS_NAME: ( | |
122 // Ba | |
123 // r | |
124 // ) | |
125 const char NAME[] = "foo"; | |
126 """.split('\n') | |
127 definition = HeaderParser(test_data).ParseDefinitions() | |
128 self.assertEqual('test.namespace', definition.string_package) | |
129 self.assertEqual('Bar', definition.class_name) | |
130 | |
131 def testParseMalformedMultiLineDirectiveWithOtherDirective(self): | |
132 test_data = """ | |
133 // GENERATED_JAVA_STRING_PACKAGE: ( | |
134 // test.name | |
135 // space | |
136 // GENERATED_JAVA_CLASS_NAME: Bar | |
137 const char NAME[] = "foo"; | |
138 """.split('\n') | |
139 with self.assertRaises(Exception): | |
140 HeaderParser(test_data).ParseDefinitions() | |
141 | |
142 def testParseMalformedMultiLineDirective(self): | |
143 test_data = """ | |
144 // GENERATED_JAVA_STRING_PACKAGE: ( | |
145 // test.name | |
146 // space | |
147 const char NAME[] = "foo"; | |
148 """.split('\n') | |
149 with self.assertRaises(Exception): | |
150 HeaderParser(test_data).ParseDefinitions() | |
151 | |
152 def testParseMalformedMultiLineDirectiveShort(self): | |
153 test_data = """ | |
154 // GENERATED_JAVA_STRING_PACKAGE: ( | |
155 const char NAME[] = "foo"; | |
156 """.split('\n') | |
157 with self.assertRaises(Exception): | |
158 HeaderParser(test_data).ParseDefinitions() | |
159 | |
160 def testGenerateThrowsOnEmptyInput(self): | |
161 with self.assertRaises(Exception): | |
162 original_do_parse = java_cpp_string.DoParseHeaderFile | |
163 try: | |
164 java_cpp_string.DoParseHeaderFile = lambda _: [] | |
165 for _ in java_cpp_string.DoGenerate(['file']): | |
166 pass | |
167 finally: | |
168 java_cpp_string.DoParseHeaderFile = original_do_parse | |
169 | |
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.
| |
170 def main(argv): | |
171 parser = optparse.OptionParser() | |
172 parser.add_option("--stamp", help="File to touch on success.") | |
173 options, _ = parser.parse_args(argv) | |
174 | |
175 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) | |
176 unittest.TextTestRunner(verbosity=0).run(suite) | |
177 | |
178 if options.stamp: | |
179 build_utils.Touch(options.stamp) | |
180 | |
181 if __name__ == '__main__': | |
182 main(sys.argv[1:]) | |
OLD | NEW |