Пример #1
0
 def testCoalesceBrackets(self):
     unformatted_code = textwrap.dedent("""\
    some_long_function_name_foo(
        {
            'first_argument_of_the_thing': id,
            'second_argument_of_the_thing': "some thing"
        }
    )""")
     expected_formatted_code = textwrap.dedent("""\
    some_long_function_name_foo({
        'first_argument_of_the_thing': id,
        'second_argument_of_the_thing': "some thing"
    })
    """)
     with utils.NamedTempFile(dirname=self.test_tmpdir,
                              mode='w') as (f, name):
         f.write(
             textwrap.dedent(u'''\
       [style]
       column_limit=82
       coalesce_brackets = True
       '''))
         f.flush()
         self.assertYapfReformats(
             unformatted_code,
             expected_formatted_code,
             extra_options=['--style={0}'.format(name)])
Пример #2
0
  def test_write_to_file(self):
    s = u'foobar\n'
    with utils.NamedTempFile(dirname=self.test_tmpdir) as (f, fname):
      file_resources.WriteReformattedCode(
          fname, s, in_place=True, encoding='utf-8')
      f.flush()

      with open(fname) as f2:
        self.assertEqual(f2.read(), s)
Пример #3
0
 def testUnicodeEncodingPipedToFile(self):
   unformatted_code = textwrap.dedent(u"""\
       def foo():
           print('⇒')
       """)
   with utils.NamedTempFile(
       dirname=self.test_tmpdir, suffix='.py') as (out, _):
     with utils.TempFileContents(
         self.test_tmpdir, unformatted_code, suffix='.py') as filepath:
       subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
Пример #4
0
  def testEncodingVerification(self):
    unformatted_code = textwrap.dedent(u"""\
        '''The module docstring.'''
        # -*- coding: utf-8 -*-
        def f():
            x = 37
        """)

    with utils.NamedTempFile(
        suffix='.py', dirname=self.test_tmpdir) as (out, _):
      with utils.TempFileContents(
          self.test_tmpdir, unformatted_code, suffix='.py') as filepath:
        subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
Пример #5
0
  def testEncodingVerification(self):
    unformatted_code = textwrap.dedent(u"""\
        '''The module docstring.'''
        # -*- coding: utf-8 -*-
        def f():
            x = 37
        """)

    with utils.NamedTempFile(
        suffix='.py', dirname=self.test_tmpdir) as (out, _):
      with utils.TempFileContents(
          self.test_tmpdir, unformatted_code, suffix='.py') as filepath:
        try:
          subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
        except subprocess.CalledProcessError as e:
          self.assertEqual(e.returncode, 1)  # Indicates the text changed.
Пример #6
0
    def testStyleOutputRoundTrip(self):
        unformatted_code = textwrap.dedent("""\
        def foo_function():
          pass
        """)
        expected_formatted_code = textwrap.dedent("""\
        def foo_function():
            pass
        """)

        with utils.NamedTempFile(dirname=self.test_tmpdir) as (stylefile,
                                                               stylepath):
            p = subprocess.Popen(YAPF_BINARY + ['--style-help'],
                                 stdout=stylefile,
                                 stdin=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            _, stderrdata = p.communicate()
            self.assertEqual(stderrdata, b'')
            self.assertYapfReformats(
                unformatted_code,
                expected_formatted_code,
                extra_options=['--style={0}'.format(stylepath)])