def testOutput(self): definition = EnumDefinition(original_enum_name='ClassName', enum_package='some.package', entries=[('E1', 1), ('E2', '2 << 2')], comments=[('E2', 'This is a comment.'), ('E1', 'This is a multiple line ' 'comment that is really long. ' 'This is a multiple line ' 'comment that is really ' 'really long.')]) 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; import android.support.annotation.IntDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @IntDef({ ClassName.E1, ClassName.E2 }) @Retention(RetentionPolicy.SOURCE) public @interface ClassName { /** * %s * really really long. */ int E1 = 1; /** * This is a comment. */ int E2 = 2 << 2; } """ long_comment = ('This is a multiple line comment that is really long. ' 'This is a multiple line comment that is') self.assertEqual( expected % (date.today().year, java_cpp_utils.GetScriptName(), long_comment), output)
def _GenerateOutput(template, source_paths, template_path, strings): description_template = """ // This following string constants were inserted by // {SCRIPT_NAME} // From // {SOURCE_PATHS} // Into // {TEMPLATE_PATH} """ values = { 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), 'SOURCE_PATHS': ',\n // '.join(source_paths), 'TEMPLATE_PATH': template_path, } description = description_template.format(**values) native_strings = '\n\n'.join(x.Format() for x in strings) values = { 'NATIVE_STRINGS': description + native_strings, } return template.format(**values)
def GenerateOutput(source_path, enum_definition): template = Template(""" // Copyright ${YEAR} 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 // ${SCRIPT_NAME} // From // ${SOURCE_PATH} package ${PACKAGE}; import androidx.annotation.IntDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @IntDef({ ${INT_DEF} }) @Retention(RetentionPolicy.SOURCE) public @interface ${CLASS_NAME} { ${ENUM_ENTRIES} } """) enum_template = Template(' int ${NAME} = ${VALUE};') enum_entries_string = [] enum_names = [] for enum_name, enum_value in enum_definition.entries.items(): values = { 'NAME': enum_name, 'VALUE': enum_value, } enum_comments = enum_definition.comments.get(enum_name) if enum_comments: enum_comments_indent = ' * ' comments_line_wrapper = textwrap.TextWrapper( initial_indent=enum_comments_indent, subsequent_indent=enum_comments_indent, width=100) enum_entries_string.append(' /**') enum_entries_string.append('\n'.join( comments_line_wrapper.wrap(enum_comments))) enum_entries_string.append(' */') enum_entries_string.append(enum_template.substitute(values)) if enum_name != "NUM_ENTRIES": enum_names.append(enum_definition.class_name + '.' + enum_name) enum_entries_string = '\n'.join(enum_entries_string) enum_names_indent = ' ' * 4 wrapper = textwrap.TextWrapper(initial_indent=enum_names_indent, subsequent_indent=enum_names_indent, width=100) enum_names_string = '\n'.join(wrapper.wrap(', '.join(enum_names))) values = { 'CLASS_NAME': enum_definition.class_name, 'ENUM_ENTRIES': enum_entries_string, 'PACKAGE': enum_definition.enum_package, 'INT_DEF': enum_names_string, 'SCRIPT_NAME': java_cpp_utils.GetScriptName(), 'SOURCE_PATH': source_path, 'YEAR': str(date.today().year) } return template.substitute(values)