content: # Copyright 2016 Google Inc. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  """Basic tests for yapf.reformatter."""
  
  import sys
  import textwrap
  import unittest
  
  from yapf.yapflib import reformatter
  from yapf.yapflib import style
  
  from yapftests import yapf_test_helper
  
  
  class BasicReformatterTest(yapf_test_helper.YAPFTest):
  
    @classmethod
    def setUpClass(cls):
      style.SetGlobalStyle(style.CreateYapfStyle())
  
    def testSplittingAllArgs(self):
      style.SetGlobalStyle(
          style.CreateStyleFromConfig(
              '{split_all_comma_separated_values: true, column_limit: 40}'))
      unformatted_code = textwrap.dedent("""\
          responseDict = {"timestamp": timestamp, "someValue":   value, "whatever": 120}
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          responseDict = {
              "timestamp": timestamp,
              "someValue": value,
              "whatever": 120
          }
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          yes = { 'yes': 'no', 'no': 'yes', }
      """)
      expected_formatted_code = textwrap.dedent("""\
          yes = {
              'yes': 'no',
              'no': 'yes',
          }
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      unformatted_code = textwrap.dedent("""\
          def foo(long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args):
              pass
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          def foo(long_arg,
                  really_long_arg,
                  really_really_long_arg,
                  cant_keep_all_these_args):
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      unformatted_code = textwrap.dedent("""\
          foo_tuple = [long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          foo_tuple = [
              long_arg,
              really_long_arg,
              really_really_long_arg,
              cant_keep_all_these_args
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      unformatted_code = textwrap.dedent("""\
          foo_tuple = [short, arg]
      """)
      expected_formatted_code = textwrap.dedent("""\
          foo_tuple = [short, arg]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      unformatted_code = textwrap.dedent("""\
          values = [ lambda arg1, arg2: arg1 + arg2 ]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          values = [
              lambda arg1, arg2: arg1 + arg2
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      unformatted_code = textwrap.dedent("""\
          values = [
              (some_arg1, some_arg2) for some_arg1, some_arg2 in values
          ]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          values = [
              (some_arg1,
               some_arg2)
              for some_arg1, some_arg2 in values
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # There is a test for split_all_top_level_comma_separated_values, with
      # different expected value
      unformatted_code = textwrap.dedent("""\
          someLongFunction(this_is_a_very_long_parameter,
              abc=(a, this_will_just_fit_xxxxxxx))
      """)
      expected_formatted_code = textwrap.dedent("""\
          someLongFunction(
              this_is_a_very_long_parameter,
              abc=(a,
                   this_will_just_fit_xxxxxxx))
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testSplittingTopLevelAllArgs(self):
      style_dict = style.CreateStyleFromConfig(
          '{split_all_top_level_comma_separated_values: true, '
          'column_limit: 40}')
      style.SetGlobalStyle(style_dict)
      # Works the same way as split_all_comma_separated_values
      unformatted_code = textwrap.dedent("""\
          responseDict = {"timestamp": timestamp, "someValue":   value, "whatever": 120}
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          responseDict = {
              "timestamp": timestamp,
              "someValue": value,
              "whatever": 120
          }
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # Works the same way as split_all_comma_separated_values
      unformatted_code = textwrap.dedent("""\
          def foo(long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args):
                pass
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          def foo(long_arg,
                  really_long_arg,
                  really_really_long_arg,
                  cant_keep_all_these_args):
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # Works the same way as split_all_comma_separated_values
      unformatted_code = textwrap.dedent("""\
          foo_tuple = [long_arg, really_long_arg, really_really_long_arg, cant_keep_all_these_args]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          foo_tuple = [
              long_arg,
              really_long_arg,
              really_really_long_arg,
              cant_keep_all_these_args
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # Works the same way as split_all_comma_separated_values
      unformatted_code = textwrap.dedent("""\
          foo_tuple = [short, arg]
      """)
      expected_formatted_code = textwrap.dedent("""\
          foo_tuple = [short, arg]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # Works the same way as split_all_comma_separated_values
      unformatted_code = textwrap.dedent("""\
          values = [ lambda arg1, arg2: arg1 + arg2 ]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          values = [
              lambda arg1, arg2: arg1 + arg2
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # There is a test for split_all_comma_separated_values, with different
      # expected value
      unformatted_code = textwrap.dedent("""\
          values = [
              (some_arg1, some_arg2) for some_arg1, some_arg2 in values
          ]
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          values = [
              (some_arg1, some_arg2)
              for some_arg1, some_arg2 in values
          ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
      # There is a test for split_all_comma_separated_values, with different
      # expected value
      unformatted_code = textwrap.dedent("""\
          someLongFunction(this_is_a_very_long_parameter,
              abc=(a, this_will_just_fit_xxxxxxx))
      """)
      expected_formatted_code = textwrap.dedent("""\
          someLongFunction(
              this_is_a_very_long_parameter,
              abc=(a, this_will_just_fit_xxxxxxx))
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      actual_formatted_code = reformatter.Reformat(llines)
      self.assertEqual(40, len(actual_formatted_code.splitlines()[-1]))
      self.assertCodeEqual(expected_formatted_code, actual_formatted_code)
  
      unformatted_code = textwrap.dedent("""\
          someLongFunction(this_is_a_very_long_parameter,
              abc=(a, this_will_not_fit_xxxxxxxxx))
      """)
      expected_formatted_code = textwrap.dedent("""\
          someLongFunction(
              this_is_a_very_long_parameter,
              abc=(a,
                   this_will_not_fit_xxxxxxxxx))
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      # This tests when there is an embedded dictionary that will fit in a line
      original_multiline = style_dict['FORCE_MULTILINE_DICT']
      style_dict['FORCE_MULTILINE_DICT'] = False
      style.SetGlobalStyle(style_dict)
      unformatted_code = textwrap.dedent("""\
            someLongFunction(this_is_a_very_long_parameter,
                abc={a: b, b: c})
            """)
      expected_formatted_code = textwrap.dedent("""\
            someLongFunction(
                this_is_a_very_long_parameter,
                abc={
                    a: b, b: c
                })
            """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      actual_formatted_code = reformatter.Reformat(llines)
      self.assertCodeEqual(expected_formatted_code, actual_formatted_code)
  
      # This tests when there is an embedded dictionary that will fit in a line,
      #  but FORCE_MULTILINE_DICT is set
      style_dict['FORCE_MULTILINE_DICT'] = True
      style.SetGlobalStyle(style_dict)
      unformatted_code = textwrap.dedent("""\
            someLongFunction(this_is_a_very_long_parameter,
                abc={a: b, b: c})
            """)
      expected_formatted_code = textwrap.dedent("""\
            someLongFunction(
                this_is_a_very_long_parameter,
                abc={
                    a: b,
                    b: c
                })
            """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      actual_formatted_code = reformatter.Reformat(llines)
      self.assertCodeEqual(expected_formatted_code, actual_formatted_code)
  
      style_dict['FORCE_MULTILINE_DICT'] = original_multiline
      style.SetGlobalStyle(style_dict)
  
      # Exercise the case where there's no opening bracket (for a, b)
      unformatted_code = textwrap.dedent("""\
          a, b = f(
              a_very_long_parameter, yet_another_one, and_another)
      """)
      expected_formatted_code = textwrap.dedent("""\
          a, b = f(
              a_very_long_parameter, yet_another_one, and_another)
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      # Don't require splitting before comments.
      unformatted_code = textwrap.dedent("""\
          KO = {
              'ABC': Abc, # abc
              'DEF': Def, # def
              'LOL': Lol, # wtf
              'GHI': Ghi,
              'JKL': Jkl,
          }
      """)
      expected_formatted_code = textwrap.dedent("""\
          KO = {
              'ABC': Abc,  # abc
              'DEF': Def,  # def
              'LOL': Lol,  # wtf
              'GHI': Ghi,
              'JKL': Jkl,
          }
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testSimpleFunctionsWithTrailingComments(self):
      unformatted_code = textwrap.dedent("""\
          def g():  # Trailing comment
            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
                xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
              pass
  
          def f(  # Intermediate comment
          ):
            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
                xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
              pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          def g():  # Trailing comment
            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
                xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
              pass
  
  
          def f(  # Intermediate comment
          ):
            if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
                xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
              pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testParamListWithTrailingComments(self):
      unformatted_code = textwrap.dedent("""\
          def f(a,
                b, #
                c):
            pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          def f(a, b,  #
                c):
            pass
      """)
      try:
        style.SetGlobalStyle(
            style.CreateStyleFromConfig(
                '{based_on_style: yapf,'
                ' disable_split_list_with_comment: True}'))
        llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
        self.assertCodeEqual(expected_formatted_code,
                             reformatter.Reformat(llines))
      finally:
        style.SetGlobalStyle(style.CreateYapfStyle())
  
    def testBlankLinesBetweenTopLevelImportsAndVariables(self):
      unformatted_code = textwrap.dedent("""\
          import foo as bar
          VAR = 'baz'
      """)
      expected_formatted_code = textwrap.dedent("""\
          import foo as bar
  
          VAR = 'baz'
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          import foo as bar
  
          VAR = 'baz'
      """)
      expected_formatted_code = textwrap.dedent("""\
          import foo as bar
  
  
          VAR = 'baz'
      """)
      try:
        style.SetGlobalStyle(
            style.CreateStyleFromConfig(
                '{based_on_style: yapf, '
                'blank_lines_between_top_level_imports_and_variables: 2}'))
        llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
        self.assertCodeEqual(expected_formatted_code,
                             reformatter.Reformat(llines))
      finally:
        style.SetGlobalStyle(style.CreateYapfStyle())
  
      unformatted_code = textwrap.dedent("""\
          import foo as bar
          # Some comment
      """)
      expected_formatted_code = textwrap.dedent("""\
          import foo as bar
          # Some comment
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          import foo as bar
          class Baz():
            pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          import foo as bar
  
  
          class Baz():
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          import foo as bar
          def foobar():
            pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          import foo as bar
  
  
          def foobar():
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          def foobar():
            from foo import Bar
            Bar.baz()
      """)
      expected_formatted_code = textwrap.dedent("""\
          def foobar():
            from foo import Bar
            Bar.baz()
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testBlankLinesAtEndOfFile(self):
      unformatted_code = textwrap.dedent("""\
          def foobar(): # foo
           pass
  
  
  
      """)
      expected_formatted_code = textwrap.dedent("""\
          def foobar():  # foo
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      unformatted_code = textwrap.dedent("""\
          x = {  'a':37,'b':42,
  
          'c':927}
  
      """)
      expected_formatted_code = textwrap.dedent("""\
          x = {'a': 37, 'b': 42, 'c': 927}
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testIndentBlankLines(self):
      unformatted_code = textwrap.dedent("""\
          class foo(object):
  
            def foobar(self):
  
              pass
  
            def barfoo(self, x, y):  # bar
  
              if x:
  
                return y
  
  
          def bar():
  
            return 0
      """)
      expected_formatted_code = """\
  class foo(object):\n  \n  def foobar(self):\n    \n    pass\n  \n  def barfoo(self, x, y):  # bar\n    \n    if x:\n      \n      return y\n\n\ndef bar():\n  \n  return 0
  """  # noqa
  
      try:
        style.SetGlobalStyle(
            style.CreateStyleFromConfig(
                '{based_on_style: yapf, indent_blank_lines: true}'))
  
        llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
        self.assertCodeEqual(expected_formatted_code,
                             reformatter.Reformat(llines))
      finally:
        style.SetGlobalStyle(style.CreateYapfStyle())
  
      unformatted_code, expected_formatted_code = (expected_formatted_code,
                                                   unformatted_code)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testMultipleUgliness(self):
      unformatted_code = textwrap.dedent("""\
          x = {  'a':37,'b':42,
  
          'c':927}
  
          y = 'hello ''world'
          z = 'hello '+'world'
          a = 'hello {}'.format('world')
          class foo  (     object  ):
            def f    (self   ):
              return       37*-+2
            def g(self, x,y=42):
                return y
          def f  (   a ) :
            return      37+-+a[42-x :  y**3]
      """)
      expected_formatted_code = textwrap.dedent("""\
          x = {'a': 37, 'b': 42, 'c': 927}
  
          y = 'hello ' 'world'
          z = 'hello ' + 'world'
          a = 'hello {}'.format('world')
  
  
          class foo(object):
  
            def f(self):
              return 37 * -+2
  
            def g(self, x, y=42):
              return y
  
  
          def f(a):
            return 37 + -+a[42 - x:y**3]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testComments(self):
      unformatted_code = textwrap.dedent("""\
          class Foo(object):
            pass
  
          # Attached comment
          class Bar(object):
            pass
  
          global_assignment = 42
  
          # Comment attached to class with decorator.
          # Comment attached to class with decorator.
          @noop
          @noop
          class Baz(object):
            pass
  
          # Intermediate comment
  
          class Qux(object):
            pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          class Foo(object):
            pass
  
  
          # Attached comment
          class Bar(object):
            pass
  
  
          global_assignment = 42
  
  
          # Comment attached to class with decorator.
          # Comment attached to class with decorator.
          @noop
          @noop
          class Baz(object):
            pass
  
  
          # Intermediate comment
  
  
          class Qux(object):
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testSingleComment(self):
      code = textwrap.dedent("""\
          # Thing 1
      """)
      llines = yapf_test_helper.ParseAndUnwrap(code)
      self.assertCodeEqual(code, reformatter.Reformat(llines))
  
    def testCommentsWithTrailingSpaces(self):
      unformatted_code = textwrap.dedent("""\
          # Thing 1    \n# Thing 2    \n""")
      expected_formatted_code = textwrap.dedent("""\
          # Thing 1
          # Thing 2
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testCommentsInDataLiteral(self):
      code = textwrap.dedent("""\
          def f():
            return collections.OrderedDict({
                # First comment.
                'fnord': 37,
  
                # Second comment.
                # Continuation of second comment.
                'bork': 42,
  
                # Ending comment.
            })
      """)
      llines = yapf_test_helper.ParseAndUnwrap(code)
      self.assertCodeEqual(code, reformatter.Reformat(llines))
  
    def testEndingWhitespaceAfterSimpleStatement(self):
      code = textwrap.dedent("""\
          import foo as bar
          # Thing 1
          # Thing 2
      """)
      llines = yapf_test_helper.ParseAndUnwrap(code)
      self.assertCodeEqual(code, reformatter.Reformat(llines))
  
    def testDocstrings(self):
      unformatted_code = textwrap.dedent('''\
          u"""Module-level docstring."""
          import os
          class Foo(object):
  
            """Class-level docstring."""
            # A comment for qux.
            def qux(self):
  
  
              """Function-level docstring.
  
              A multiline function docstring.
              """
              print('hello {}'.format('world'))
              return 42
      ''')
      expected_formatted_code = textwrap.dedent('''\
          u"""Module-level docstring."""
          import os
  
  
          class Foo(object):
            """Class-level docstring."""
  
            # A comment for qux.
            def qux(self):
              """Function-level docstring.
  
              A multiline function docstring.
              """
              print('hello {}'.format('world'))
              return 42
      ''')
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testDocstringAndMultilineComment(self):
      unformatted_code = textwrap.dedent('''\
          """Hello world"""
          # A multiline
          # comment
          class bar(object):
            """class docstring"""
            # class multiline
            # comment
            def foo(self):
              """Another docstring."""
              # Another multiline
              # comment
              pass
      ''')
      expected_formatted_code = textwrap.dedent('''\
          """Hello world"""
  
  
          # A multiline
          # comment
          class bar(object):
            """class docstring"""
  
            # class multiline
            # comment
            def foo(self):
              """Another docstring."""
              # Another multiline
              # comment
              pass
      ''')
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testMultilineDocstringAndMultilineComment(self):
      unformatted_code = textwrap.dedent('''\
          """Hello world
  
          RIP Dennis Richie.
          """
          # A multiline
          # comment
          class bar(object):
            """class docstring
  
            A classy class.
            """
            # class multiline
            # comment
            def foo(self):
              """Another docstring.
  
              A functional function.
              """
              # Another multiline
              # comment
              pass
      ''')
      expected_formatted_code = textwrap.dedent('''\
          """Hello world
  
          RIP Dennis Richie.
          """
  
  
          # A multiline
          # comment
          class bar(object):
            """class docstring
  
            A classy class.
            """
  
            # class multiline
            # comment
            def foo(self):
              """Another docstring.
  
              A functional function.
              """
              # Another multiline
              # comment
              pass
      ''')
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testTupleCommaBeforeLastParen(self):
      unformatted_code = textwrap.dedent("""\
          a = ( 1, )
      """)
      expected_formatted_code = textwrap.dedent("""\
          a = (1,)
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testNoBreakOutsideOfBracket(self):
      # FIXME(morbo): How this is formatted is not correct. But it's syntactically
      # correct.
      unformatted_code = textwrap.dedent("""\
          def f():
            assert port >= minimum, \
  'Unexpected port %d when minimum was %d.' % (port, minimum)
          """)
      expected_formatted_code = textwrap.dedent("""\
          def f():
            assert port >= minimum, 'Unexpected port %d when minimum was %d.' % (port,
                                                                                 minimum)
      """)  # noqa
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testBlankLinesBeforeDecorators(self):
      unformatted_code = textwrap.dedent("""\
          @foo()
          class A(object):
            @bar()
            @baz()
            def x(self):
              pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          @foo()
          class A(object):
  
            @bar()
            @baz()
            def x(self):
              pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testCommentBetweenDecorators(self):
      unformatted_code = textwrap.dedent("""\
          @foo()
          # frob
          @bar
          def x  (self):
              pass
      """)
      expected_formatted_code = textwrap.dedent("""\
          @foo()
          # frob
          @bar
          def x(self):
            pass
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testListComprehension(self):
      unformatted_code = textwrap.dedent("""\
          def given(y):
              [k for k in ()
                if k in y]
      """)
      expected_formatted_code = textwrap.dedent("""\
          def given(y):
            [k for k in () if k in y]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testListComprehensionPreferOneLine(self):
      unformatted_code = textwrap.dedent("""\
          def given(y):
              long_variable_name = [
                  long_var_name + 1
                  for long_var_name in ()
                  if long_var_name == 2]
      """)
      expected_formatted_code = textwrap.dedent("""\
          def given(y):
            long_variable_name = [
                long_var_name + 1 for long_var_name in () if long_var_name == 2
            ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testListComprehensionPreferOneLineOverArithmeticSplit(self):
      unformatted_code = textwrap.dedent("""\
          def given(used_identifiers):
            return (sum(len(identifier)
                        for identifier in used_identifiers) / len(used_identifiers))
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          def given(used_identifiers):
            return (sum(len(identifier) for identifier in used_identifiers) /
                    len(used_identifiers))
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testListComprehensionPreferThreeLinesForLineWrap(self):
      unformatted_code = textwrap.dedent("""\
          def given(y):
              long_variable_name = [
                  long_var_name + 1
                  for long_var_name, number_two in ()
                  if long_var_name == 2 and number_two == 3]
      """)
      expected_formatted_code = textwrap.dedent("""\
          def given(y):
            long_variable_name = [
                long_var_name + 1
                for long_var_name, number_two in ()
                if long_var_name == 2 and number_two == 3
            ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testListComprehensionPreferNoBreakForTrivialExpression(self):
      unformatted_code = textwrap.dedent("""\
          def given(y):
              long_variable_name = [
                  long_var_name
                  for long_var_name, number_two in ()
                  if long_var_name == 2 and number_two == 3]
      """)
      expected_formatted_code = textwrap.dedent("""\
          def given(y):
            long_variable_name = [
                long_var_name for long_var_name, number_two in ()
                if long_var_name == 2 and number_two == 3
            ]
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testOpeningAndClosingBrackets(self):
      unformatted_code = textwrap.dedent("""\
          foo( (1, ) )
          foo( ( 1, 2, 3  ) )
          foo( ( 1, 2, 3, ) )
      """)
      expected_formatted_code = textwrap.dedent("""\
          foo((1,))
          foo((1, 2, 3))
          foo((
              1,
              2,
              3,
          ))
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testSingleLineFunctions(self):
      unformatted_code = textwrap.dedent("""\
          def foo():  return 42
      """)
      expected_formatted_code = textwrap.dedent("""\
          def foo():
            return 42
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testNoQueueSeletionInMiddleOfLine(self):
      # If the queue isn't properly constructed, then a token in the middle of the
      # line may be selected as the one with least penalty. The tokens after that
      # one are then splatted at the end of the line with no formatting.
      unformatted_code = textwrap.dedent("""\
          find_symbol(node.type) + "< " + " ".join(find_pattern(n) for n in node.child) + " >"
      """)  # noqa
      expected_formatted_code = textwrap.dedent("""\
          find_symbol(node.type) + "< " + " ".join(
              find_pattern(n) for n in node.child) + " >"
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testNoSpacesBetweenSubscriptsAndCalls(self):
      unformatted_code = textwrap.dedent("""\
          aaaaaaaaaa = bbbbbbbb.ccccccccc() [42] (a, 2)
      """)
      expected_formatted_code = textwrap.dedent("""\
          aaaaaaaaaa = bbbbbbbb.ccccccccc()[42](a, 2)
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
    def testNoSpacesBetweenOpeningBracketAndStartingOperator(self):
      # Unary operator.
      unformatted_code = textwrap.dedent("""\
          aaaaaaaaaa = bbbbbbbb.ccccccccc[ -1 ]( -42 )
      """)
      expected_formatted_code = textwrap.dedent("""\
          aaaaaaaaaa = bbbbbbbb.ccccccccc[-1](-42)
      """)
      llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
  
      # Varargs and kwargs.
      unformatted_code = textwrap.dedent("""\
          aaaaaaaaaa = bbbbbbbb.ccccccccc( *varargs )
          aaaaaaaaaa = bbbbbbbb.ccccccccc( **kwargs )
      """)
  