Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: build/android/gyp/java_cpp_enum_tests.py

Issue 2210633002: Modify java_cpp_enum.py to preserve comments (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/gyp/java_cpp_enum.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 from datetime import date 12 from datetime import date
13 import optparse 13 import optparse
14 import os 14 import os
15 import sys 15 import sys
16 import unittest 16 import unittest
17 17
18 import java_cpp_enum 18 import java_cpp_enum
19 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName 19 from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName
20 from java_cpp_enum import HeaderParser 20 from java_cpp_enum import HeaderParser
21 21
22 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp")) 22 sys.path.append(os.path.join(os.path.dirname(__file__), "gyp"))
23 from util import build_utils 23 from util import build_utils
24 24
25 class TestPreprocess(unittest.TestCase): 25 class TestPreprocess(unittest.TestCase):
26 def testOutput(self): 26 def testOutput(self):
27 definition = EnumDefinition(original_enum_name='ClassName', 27 definition = EnumDefinition(original_enum_name='ClassName',
28 enum_package='some.package', 28 enum_package='some.package',
29 entries=[('E1', 1), ('E2', '2 << 2')]) 29 entries=[('E1', 1), ('E2', '2 << 2')],
30 comments=[('E2', 'This is a comment.'),
31 ('E1', 'This is a multiple line '
32 'comment that is really long. '
33 'This is a multiple line '
34 'comment that is really '
35 'really long.')])
30 output = GenerateOutput('path/to/file', definition) 36 output = GenerateOutput('path/to/file', definition)
31 expected = """ 37 expected = """
32 // Copyright %d The Chromium Authors. All rights reserved. 38 // Copyright %d The Chromium Authors. All rights reserved.
33 // Use of this source code is governed by a BSD-style license that can be 39 // Use of this source code is governed by a BSD-style license that can be
34 // found in the LICENSE file. 40 // found in the LICENSE file.
35 41
36 // This file is autogenerated by 42 // This file is autogenerated by
37 // %s 43 // %s
38 // From 44 // From
39 // path/to/file 45 // path/to/file
40 46
41 package some.package; 47 package some.package;
42 48
43 import android.support.annotation.IntDef; 49 import android.support.annotation.IntDef;
44 50
45 import java.lang.annotation.Retention; 51 import java.lang.annotation.Retention;
46 import java.lang.annotation.RetentionPolicy; 52 import java.lang.annotation.RetentionPolicy;
47 53
48 public class ClassName { 54 public class ClassName {
49 @IntDef({ 55 @IntDef({
50 E1, E2 56 E1, E2
51 }) 57 })
52 @Retention(RetentionPolicy.SOURCE) 58 @Retention(RetentionPolicy.SOURCE)
53 public @interface ClassNameEnum {} 59 public @interface ClassNameEnum {}
60 /**
61 * This is a multiple line comment that is really long. This is a multiple lin e comment that is
xunjieli 2016/08/04 14:19:31 Is there a way to bypass the "Found lines longer t
agrieve 2016/08/04 16:39:39 Another option would be to use %s%s or str.format(
xunjieli 2016/08/04 19:34:02 Done. I couldn't get pylint:disable to work for th
62 * really really long.
63 */
54 public static final int E1 = 1; 64 public static final int E1 = 1;
65 /**
66 * This is a comment.
67 */
55 public static final int E2 = 2 << 2; 68 public static final int E2 = 2 << 2;
56 } 69 }
57 """ 70 """
58 self.assertEqual(expected % (date.today().year, GetScriptName()), output) 71 self.assertEqual(expected % (date.today().year, GetScriptName()), output)
59 72
60 def testParseSimpleEnum(self): 73 def testParseSimpleEnum(self):
61 test_data = """ 74 test_data = """
62 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace 75 // GENERATED_JAVA_ENUM_PACKAGE: test.namespace
63 enum EnumName { 76 enum EnumName {
64 VALUE_ZERO, 77 VALUE_ZERO,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 }; 140 };
128 141
129 enum EnumIgnore { 142 enum EnumIgnore {
130 C, D, E 143 C, D, E
131 }; 144 };
132 145
133 // GENERATED_JAVA_ENUM_PACKAGE: other.package 146 // GENERATED_JAVA_ENUM_PACKAGE: other.package
134 // GENERATED_JAVA_PREFIX_TO_STRIP: P_ 147 // GENERATED_JAVA_PREFIX_TO_STRIP: P_
135 enum EnumTwo { 148 enum EnumTwo {
136 P_A, 149 P_A,
150 // This comment spans
151 // two lines.
137 P_B 152 P_B
138 }; 153 };
139 """.split('\n') 154 """.split('\n')
140 definitions = HeaderParser(test_data).ParseDefinitions() 155 definitions = HeaderParser(test_data).ParseDefinitions()
141 self.assertEqual(2, len(definitions)) 156 self.assertEqual(2, len(definitions))
142 definition = definitions[0] 157 definition = definitions[0]
143 self.assertEqual('EnumOne', definition.class_name) 158 self.assertEqual('EnumOne', definition.class_name)
144 self.assertEqual('test.namespace', definition.enum_package) 159 self.assertEqual('test.namespace', definition.enum_package)
145 self.assertEqual(collections.OrderedDict([('A', '1'), 160 self.assertEqual(collections.OrderedDict([('A', '1'),
146 ('B', 'A')]), 161 ('B', 'A')]),
147 definition.entries) 162 definition.entries)
148 163 self.assertEqual(collections.OrderedDict([('ENUM_ONE_B', 'Comment there')]),
164 definition.comments)
149 definition = definitions[1] 165 definition = definitions[1]
150 self.assertEqual('EnumTwo', definition.class_name) 166 self.assertEqual('EnumTwo', definition.class_name)
151 self.assertEqual('other.package', definition.enum_package) 167 self.assertEqual('other.package', definition.enum_package)
168 self.assertEqual(collections.OrderedDict(
169 [('P_B', 'This comment spans two lines.')]), definition.comments)
152 self.assertEqual(collections.OrderedDict([('A', 0), 170 self.assertEqual(collections.OrderedDict([('A', 0),
153 ('B', 1)]), 171 ('B', 1)]),
154 definition.entries) 172 definition.entries)
155 173
156 def testParseThrowsOnUnknownDirective(self): 174 def testParseThrowsOnUnknownDirective(self):
157 test_data = """ 175 test_data = """
158 // GENERATED_JAVA_UNKNOWN: Value 176 // GENERATED_JAVA_UNKNOWN: Value
159 enum EnumName { 177 enum EnumName {
160 VALUE_ONE, 178 VALUE_ONE,
161 }; 179 };
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 options, _ = parser.parse_args(argv) 457 options, _ = parser.parse_args(argv)
440 458
441 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess) 459 suite = unittest.TestLoader().loadTestsFromTestCase(TestPreprocess)
442 unittest.TextTestRunner(verbosity=0).run(suite) 460 unittest.TextTestRunner(verbosity=0).run(suite)
443 461
444 if options.stamp: 462 if options.stamp:
445 build_utils.Touch(options.stamp) 463 build_utils.Touch(options.stamp)
446 464
447 if __name__ == '__main__': 465 if __name__ == '__main__':
448 main(sys.argv[1:]) 466 main(sys.argv[1:])
OLDNEW
« no previous file with comments | « build/android/gyp/java_cpp_enum.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698