Beispiel #1
0
 def testConcat(self):
   b = Code()
   (b.Sblock('2')
       .Append('2')
     .Eblock('2')
   )
   c = Code()
   (c.Sblock('1')
       .Concat(b)
       .Append('1')
     .Eblock('1')
   )
   self.assertMultiLineEqual(
     '1\n'
     '  2\n'
     '    2\n'
     '  2\n'
     '  1\n'
     '1',
     c.Render())
   d = Code()
   a = Code()
   a.Concat(d)
   self.assertEquals('', a.Render())
   a.Concat(c)
   self.assertEquals(
     '1\n'
     '  2\n'
     '    2\n'
     '  2\n'
     '  1\n'
     '1',
     a.Render())
Beispiel #2
0
 def testComment(self):
     long_comment = ('This comment is eighty nine characters in longness, '
                     'that is, to use another word, length')
     c = Code()
     c.Comment(long_comment)
     self.assertEquals(
         '// This comment is eighty nine characters '
         'in longness, that is, to use another\n'
         '// word, length', c.Render())
     c = Code()
     c.Sblock('sblock')
     c.Comment(long_comment)
     c.Eblock('eblock')
     c.Comment(long_comment)
     self.assertEquals(
         'sblock\n'
         '  // This comment is eighty nine characters '
         'in longness, that is, to use\n'
         '  // another word, length\n'
         'eblock\n'
         '// This comment is eighty nine characters in '
         'longness, that is, to use another\n'
         '// word, length', c.Render())
     long_word = 'x' * 100
     c = Code()
     c.Comment(long_word)
     self.assertEquals('// ' + 'x' * 77 + '\n' '// ' + 'x' * 23, c.Render())
Beispiel #3
0
 def Write(self):
     """Writes the output."""
     header_file_path = self._out_base_filename + '.h'
     cc_file_path = self._out_base_filename + '.cc'
     substitutions = ({
         'header_file_path':
         header_file_path,
         'header_guard':
         (header_file_path.replace('/', '_').replace('.', '_').upper()),
         'provider_class':
         self._provider_class,
         'source_files':
         str(self._source_files),
         'year':
         str(datetime.now().year)
     })
     if not os.path.exists(self._out_root):
         os.makedirs(self._out_root)
     # Write the .h file.
     with open(os.path.join(self._out_root, header_file_path), 'w') as f:
         header_file = Code()
         header_file.Append(HEADER_FILE_TEMPLATE)
         header_file.Substitute(substitutions)
         f.write(header_file.Render().strip())
     # Write the .cc file.
     with open(os.path.join(self._out_root, cc_file_path), 'w') as f:
         cc_file = Code()
         cc_file.Append(CC_FILE_BEGIN)
         cc_file.Substitute(substitutions)
         cc_file.Concat(self.Render())
         cc_end = Code()
         cc_end.Append(CC_FILE_END)
         cc_end.Substitute(substitutions)
         cc_file.Concat(cc_end)
         f.write(cc_file.Render().strip())
  def Write(self):
    """Writes the output."""
    header_file = self._out_base_filename + '.h'
    cc_file = self._out_base_filename + '.cc'

    include_file_root = self._out_root[len(self._gen_dir_relpath)+1:]
    header_file_path = '%s/%s' % (include_file_root, header_file)
    cc_file_path = '%s/%s' % (include_file_root, cc_file)
    substitutions = ({
        'header_file_path': header_file_path,
        'header_guard': (header_file_path.replace('/', '_').
                             replace('.', '_').upper()),
        'method_name': self._method_name,
        'source_files': str([ToPosixPath(f) for f in self._source_files]),
        'year': str(datetime.now().year)
    })
    if not os.path.exists(self._out_root):
      os.makedirs(self._out_root)
    # Write the .h file.
    with open(os.path.join(self._out_root, header_file), 'w') as f:
      header_file = Code()
      header_file.Append(HEADER_FILE_TEMPLATE)
      header_file.Substitute(substitutions)
      f.write(header_file.Render().strip())
    # Write the .cc file.
    with open(os.path.join(self._out_root, cc_file), 'w') as f:
      cc_file = Code()
      cc_file.Append(CC_FILE_BEGIN)
      cc_file.Substitute(substitutions)
      cc_file.Concat(self.Render())
      cc_end = Code()
      cc_end.Append(CC_FILE_END)
      cc_end.Substitute(substitutions)
      cc_file.Concat(cc_end)
      f.write(cc_file.Render().strip())
Beispiel #5
0
 def testCommentWithSpecialCharacters(self):
     c = Code()
     c.Comment('20% of 80%s')
     c.Substitute({})
     self.assertEquals('// 20% of 80%s', c.Render())
     d = Code()
     d.Append('90')
     d.Concat(c)
     self.assertEquals('90\n' '// 20% of 80%s', d.Render())
Beispiel #6
0
 def testSubstitute(self):
     c = Code()
     c.Append('%(var1)s %(var2)s %(var1)s')
     c.Substitute({'var1': 'one', 'var2': 'two'})
     self.assertEquals('one two one', c.Render())
     c.Append('%(var1)s %(var2)s %(var3)s')
     c.Append('%(var2)s %(var1)s %(var3)s')
     c.Substitute({'var1': 'one', 'var2': 'two', 'var3': 'three'})
     self.assertEquals('one two one\n'
                       'one two three\n'
                       'two one three', c.Render())
Beispiel #7
0
    def Write(self):
        """Writes the output."""
        header_file = self._out_base_filename + '.h'
        cc_file = self._out_base_filename + '.cc'

        include_file_root = self._out_root
        GEN_DIR_PREFIX = 'gen/'
        if include_file_root.startswith(GEN_DIR_PREFIX) and len(
                include_file_root) >= len(GEN_DIR_PREFIX):
            include_file_root = include_file_root[len(GEN_DIR_PREFIX):]
        else:
            include_file_root = ''
        if include_file_root:
            header_file_path = '%s/%s' % (include_file_root, header_file)
        else:
            header_file_path = header_file
        cc_file_path = '%s/%s' % (include_file_root, cc_file)

        substitutions = ({
            'header_file_path':
            header_file_path,
            'header_guard':
            (header_file_path.replace('/', '_').replace('.', '_').upper()),
            'method_name':
            self._method_name,
            'source_files':
            str(self._source_files),
            'year':
            str(datetime.now().year)
        })
        if not os.path.exists(self._out_root):
            os.makedirs(self._out_root)
        # Write the .h file.
        with open(os.path.join(self._out_root, header_file), 'w') as f:
            header_file = Code()
            header_file.Append(HEADER_FILE_TEMPLATE)
            header_file.Substitute(substitutions)
            f.write(header_file.Render().strip())
        # Write the .cc file.
        with open(os.path.join(self._out_root, cc_file), 'w') as f:
            cc_file = Code()
            cc_file.Append(CC_FILE_BEGIN)
            cc_file.Substitute(substitutions)
            cc_file.Concat(self.Render())
            cc_end = Code()
            cc_end.Append(CC_FILE_END)
            cc_end.Substitute(substitutions)
            cc_file.Concat(cc_end)
            f.write(cc_file.Render().strip())
Beispiel #8
0
 def testSameLineAppendConcatComment(self):
   c = Code()
   c.Append('This is a line.')
   c.Append('This too.', new_line=False)
   d = Code()
   d.Append('And this.')
   c.Concat(d, new_line=False)
   self.assertEquals('This is a line.This too.And this.', c.Render())
   c = Code()
   c.Append('This is a')
   c.Comment(' spectacular 80-character line thingy ' +
                 'that fits wonderfully everywhere.',
             comment_prefix='',
             new_line=False)
   self.assertEquals('This is a spectacular 80-character line thingy that ' +
                         'fits wonderfully everywhere.',
                     c.Render())
Beispiel #9
0
 def testSameLineAppendAndConcat(self):
     c = Code()
     c.Append('This is a line.')
     c.Append('This too.', new_line=False)
     d = Code()
     d.Append('And this.')
     c.Concat(d, new_line=False)
     self.assertEquals('This is a line.This too.And this.', c.Render())
Beispiel #10
0
 def testComment(self):
     long_comment = ('This comment is ninety one characters in longness, '
                     'that is, using a different word, length.')
     c = Code()
     c.Comment(long_comment)
     self.assertEqual(
         '// This comment is ninety one characters '
         'in longness, that is, using a different\n'
         '// word, length.', c.Render())
     c = Code()
     c.Sblock('sblock')
     c.Comment(long_comment)
     c.Eblock('eblock')
     c.Comment(long_comment)
     self.assertEqual(
         'sblock\n'
         '  // This comment is ninety one characters '
         'in longness, that is, using a\n'
         '  // different word, length.\n'
         'eblock\n'
         '// This comment is ninety one characters in '
         'longness, that is, using a different\n'
         '// word, length.', c.Render())
     # Words that cannot be broken up are left as too long.
     long_word = 'x' * 100
     c = Code()
     c.Comment('xxx')
     c.Comment(long_word)
     c.Comment('xxx')
     self.assertEqual('// xxx\n'
                      '// ' + 'x' * 100 + '\n'
                      '// xxx', c.Render())
     c = Code(indent_size=2, comment_length=40)
     c.Comment(
         'Pretend this is a Closure Compiler style comment, which should '
         'both wrap and indent',
         comment_prefix=' * ',
         wrap_indent=4)
     self.assertEqual(
         ' * Pretend this is a Closure Compiler\n'
         ' *     style comment, which should both\n'
         ' *     wrap and indent', c.Render())
Beispiel #11
0
 def testBlock(self):
     c = Code()
     (c.Append('line').Sblock('sblock').Append('inner').Append(
         'moreinner').Sblock('moresblock').Append('inner').Eblock(
             'out').Append('inner').Eblock('out'))
     self.assertEquals(
         'line\n'
         'sblock\n'
         '  inner\n'
         '  moreinner\n'
         '  moresblock\n'
         '    inner\n'
         '  out\n'
         '  inner\n'
         'out', c.Render())
Beispiel #12
0
 def testLinePrefixes(self):
     c = Code()
     c.Sblock(line='/**', line_prefix=' * ')
     c.Sblock('@typedef {{')
     c.Append('foo: bar,')
     c.Sblock('baz: {')
     c.Append('x: y')
     c.Eblock('}')
     c.Eblock('}}')
     c.Eblock(line=' */')
     output = c.Render()
     self.assertMultiLineEqual(
         '/**\n'
         ' * @typedef {{\n'
         ' *   foo: bar,\n'
         ' *   baz: {\n'
         ' *     x: y\n'
         ' *   }\n'
         ' * }}\n'
         ' */', output)
Beispiel #13
0
 def testAppend(self):
     c = Code()
     c.Append('line')
     self.assertEquals('line', c.Render())