Example #1
0
 def test_write_encoded_to_stdout(self):
   s = '\ufeff# -*- coding: utf-8 -*-\nresult = "passed"\n'  # pylint: disable=anomalous-unicode-escape-in-string
   stream = BufferedByteStream() if py3compat.PY3 else py3compat.StringIO()
   with utils.stdout_redirector(stream):
     file_resources.WriteReformattedCode(
         None, s, in_place=False, encoding='utf-8')
   self.assertEqual(stream.getvalue(), s)
Example #2
0
def ParseCode(unformatted_source, filename='<unknown>'):
    """Parse a string of Python code into logical lines.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.

  Returns:
    A list of LogicalLines.

  Raises:
    An exception is raised if there's an error during AST parsing.
  """
    if not unformatted_source.endswith(os.linesep):
        unformatted_source += os.linesep

    try:
        ast_tree = ast.parse(unformatted_source, filename)
        readline = py3compat.StringIO(unformatted_source).readline
        tokens = tokenize.generate_tokens(readline)
    except Exception:
        raise

    logical_lines = _CreateLogicalLines(tokens)

    # Process the logical lines.
    split_penalty_visitor.SplitPenalty(logical_lines).visit(ast_tree)
    return logical_lines
Example #3
0
 def test_write_to_stdout(self):
   s = u'foobar'
   stream = BufferedByteStream() if py3compat.PY3 else py3compat.StringIO()
   with utils.stdout_redirector(stream):
     file_resources.WriteReformattedCode(
         None, s, in_place=False, encoding='utf-8')
   self.assertEqual(stream.getvalue(), s)
Example #4
0
 def testNoFile(self):
   stream = py3compat.StringIO()
   handler = logging.StreamHandler(stream)
   logger = logging.getLogger('mylogger')
   logger.addHandler(handler)
   self.assertRaises(
       IOError, yapf_api.FormatFile, 'not_a_file.py', logger=logger.error)
   self.assertEqual(stream.getvalue(),
                    "[Errno 2] No such file or directory: 'not_a_file.py'\n")
Example #5
0
    def testDumpPyTree(self):
        # Similar sanity checking for the convenience wrapper DumpPyTree
        tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)
        stream = py3compat.StringIO()
        pytree_visitor.DumpPyTree(tree, target_stream=stream)

        dump_output = stream.getvalue()
        self.assertIn('file_input [3 children]', dump_output)
        self.assertIn("NAME(Leaf(1, 'foo'))", dump_output)
        self.assertIn("EQUAL(Leaf(22, '='))", dump_output)
Example #6
0
 def testWriteEncodedToStdout(self):
     s = '\ufeff# -*- coding: utf-8 -*-\nresult = "passed"\n'
     stream = BufferedByteStream() if py3compat.PY3 else py3compat.StringIO(
     )
     with stdout_redirector(stream):
         file_resources.WriteReformattedCode(None,
                                             s,
                                             in_place=False,
                                             encoding='utf-8')
     self.assertEqual(stream.getvalue(), s)
Example #7
0
    def testDumper(self):
        # PyTreeDumper is mainly a debugging utility, so only do basic sanity
        # checking.
        tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)
        stream = py3compat.StringIO()
        pytree_visitor.PyTreeDumper(target_stream=stream).Visit(tree)

        dump_output = stream.getvalue()
        self.assertIn('file_input [3 children]', dump_output)
        self.assertIn("NAME(Leaf(1, 'foo'))", dump_output)
        self.assertIn("EQUAL(Leaf(22, '='))", dump_output)
 def __init__(self):
   self.string_io = py3compat.StringIO()
Example #9
0
 def testWriteToStdout(self):
     s = u'foobar'
     stream = py3compat.StringIO()
     with stdout_redirector(stream):
         file_resources.WriteReformattedCode(None, s, in_place=False)
     self.assertEqual(stream.getvalue(), s)