Пример #1
0
  def testSetGlobalStyle(self):
    try:
      style.SetGlobalStyle(style.CreateChromiumStyle())
      unformatted_code = textwrap.dedent(u"""\
          for i in range(5):
           print('bar')
          """)
      expected_formatted_code = textwrap.dedent(u"""\
          for i in range(5):
            print('bar')
          """)
      uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
      self.assertCodeEqual(expected_formatted_code,
                           reformatter.Reformat(uwlines))
    finally:
      style.SetGlobalStyle(style.CreatePEP8Style())
      style.DEFAULT_STYLE = self.current_style

    unformatted_code = textwrap.dedent(u"""\
        for i in range(5):
         print('bar')
        """)
    expected_formatted_code = textwrap.dedent(u"""\
        for i in range(5):
            print('bar')
        """)
    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
Пример #2
0
def _ParseAndUnwrap(code, dumptree=False):
    """Produces unwrapped lines from the given code.

  Parses the code into a tree, performs comment splicing and runs the
  unwrapper.

  Arguments:
    code: code to parse as a string
    dumptree: if True, the parsed pytree (after comment splicing) is dumped
      to stderr. Useful for debugging.

  Returns:
    List of unwrapped lines.
  """
    style.SetGlobalStyle(style.CreateChromiumStyle())
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
    subtype_assigner.AssignSubtypes(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    if dumptree:
        pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)

    uwlines = pytree_unwrapper.UnwrapPyTree(tree)
    for uwl in uwlines:
        uwl.CalculateFormattingInformation()

    return uwlines
 def testB19372573(self):
     code = textwrap.dedent("""\
     def f():
         if a: return 42
         while True:
             if b: continue
             if c: break
         return 0
     """)
     uwlines = yapf_test_helper.ParseAndUnwrap(code)
     try:
         style.SetGlobalStyle(style.CreatePEP8Style())
         self.assertCodeEqual(code, reformatter.Reformat(uwlines))
     finally:
         style.SetGlobalStyle(style.CreateChromiumStyle())
    def testB20016122(self):
        try:
            style.SetGlobalStyle(
                style.CreateStyleFromConfig(
                    '{based_on_style: pep8, split_penalty_import_names: 35}'))
            unformatted_code = textwrap.dedent("""\
          from a_very_long_or_indented_module_name_yada_yada import (long_argument_1,
                                                                     long_argument_2)
          """)
            expected_formatted_code = textwrap.dedent("""\
          from a_very_long_or_indented_module_name_yada_yada import (
              long_argument_1, long_argument_2)
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))
        finally:
            style.SetGlobalStyle(style.CreatePEP8Style())

        try:
            style.SetGlobalStyle(
                style.CreateStyleFromConfig(
                    '{based_on_style: chromium, '
                    'split_before_logical_operator: True}'))
            code = textwrap.dedent("""\
          class foo():

            def __eq__(self, other):
              return (isinstance(other, type(self))
                      and self.xxxxxxxxxxx == other.xxxxxxxxxxx
                      and self.xxxxxxxx == other.xxxxxxxx
                      and self.aaaaaaaaaaaa == other.aaaaaaaaaaaa
                      and self.bbbbbbbbbbb == other.bbbbbbbbbbb
                      and self.ccccccccccccccccc == other.ccccccccccccccccc
                      and self.ddddddddddddddddddddddd == other.ddddddddddddddddddddddd
                      and self.eeeeeeeeeeee == other.eeeeeeeeeeee
                      and self.ffffffffffffff == other.time_completed
                      and self.gggggg == other.gggggg and self.hhh == other.hhh
                      and len(self.iiiiiiii) == len(other.iiiiiiii)
                      and all(jjjjjjj in other.iiiiiiii for jjjjjjj in self.iiiiiiii))
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(code)
            self.assertCodeEqual(code, reformatter.Reformat(uwlines))
        finally:
            style.SetGlobalStyle(style.CreateChromiumStyle())
Пример #5
0
 def setUpClass(cls):
     style.SetGlobalStyle(style.CreateChromiumStyle())
Пример #6
0
 def _Check(self, unformatted_code, expected_formatted_code):
     style.SetGlobalStyle(style.CreateChromiumStyle())
     formatted_code = yapf_api.FormatCode(unformatted_code)
     self.assertEqual(expected_formatted_code, formatted_code)