<s> """<STR_LIT>""" <EOL> import sys <EOL> import textwrap <EOL> import unittest <EOL> from yapf . yapflib import comment_splicer <EOL> from yapf . yapflib import pytree_unwrapper <EOL> from yapf . yapflib import pytree_utils <EOL> from yapf . yapflib import pytree_visitor <EOL> class PytreeUnwrapperTest ( unittest . TestCase ) : <EOL> def _ParseAndUnwrap ( self , code , dumptree = False ) : <EOL> """<STR_LIT>""" <EOL> tree = pytree_utils . ParseCodeToTree ( code ) <EOL> comment_splicer . SpliceComments ( tree ) <EOL> if dumptree : <EOL> pytree_visitor . DumpPyTree ( tree , target_stream = sys . stderr ) <EOL> return pytree_unwrapper . UnwrapPyTree ( tree ) <EOL> def _CheckUnwrappedLines ( self , uwlines , list_of_expected ) : <EOL> """<STR_LIT>""" <EOL> actual = [ ] <EOL> for uwl in uwlines : <EOL> filtered_values = [ ft . value <EOL> for ft in uwl . tokens <EOL> if ft . name not in pytree_utils . NONSEMANTIC_TOKENS ] <EOL> actual . append ( ( uwl . depth , filtered_values ) ) <EOL> self . assertEqual ( list_of_expected , actual ) <EOL> def testSimpleFileScope ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:2>' ] ) ] ) <EOL> def testSimpleMultilineStatement ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:(>' , '<STR_LIT:1>' , '<STR_LIT:+>' , '<STR_LIT:x>' , '<STR_LIT:)>' ] ) ] ) <EOL> def testFileScopeWithInlineComment ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:2>' ] ) ] ) <EOL> def testSimpleIf ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:2>' ] ) ] ) <EOL> def testSimpleIfWithComments ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:2>' ] ) ] ) <EOL> def testIfWithCommentsInside ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:2>' ] ) ] ) <EOL> def testIfElifElse ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:y>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:y>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:z>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) ] ) <EOL> def testNestedCompoundTwoLevel ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' , '<STR_LIT:t>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:2> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:2> , [ '<STR_LIT>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:k>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) ] ) <EOL> def testSimpleWhile ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT:>>' , '<STR_LIT:1>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:x>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) ] ) <EOL> def testSimpleTry ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) ] ) <EOL> def testSimpleFuncdef ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:(>' , '<STR_LIT:x>' , '<STR_LIT:)>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' , '<STR_LIT:x>' ] ) ] ) <EOL> def testTwoFuncDefs ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:(>' , '<STR_LIT:x>' , '<STR_LIT:)>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' , '<STR_LIT:x>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:bar>' , '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' , '<STR_LIT:x>' ] ) ] ) <EOL> def testSimpleClassDef ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:class>' , '<STR_LIT>' , '<STR_LIT::>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT:p>' , '<STR_LIT:=>' , '<STR_LIT:1>' ] ) ] ) <EOL> def testSingleLineStmtInFunc ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:f>' , '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' , '<STR_LIT>' ] ) ] ) <EOL> def testMultipleComments ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT:f>' , '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT::>' ] ) , <EOL> ( <NUM_LIT:1> , [ '<STR_LIT>' ] ) ] ) <EOL> def testSplitListWithComment ( self ) : <EOL> code = textwrap . dedent ( r"""<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckUnwrappedLines ( uwlines , [ <EOL> ( <NUM_LIT:0> , [ '<STR_LIT:a>' , '<STR_LIT:=>' , '<STR_LIT:[>' , "<STR_LIT>" , '<STR_LIT:U+002C>' , "<STR_LIT>" , '<STR_LIT:U+002C>' , <EOL> "<STR_LIT>" , '<STR_LIT:U+002C>' , '<STR_LIT>' , '<STR_LIT:]>' ] ) ] ) <EOL> class MatchBracketsTest ( unittest . TestCase ) : <EOL> def _ParseAndUnwrap ( self , code , dumptree = False ) : <EOL> """<STR_LIT>""" <EOL> tree = pytree_utils . ParseCodeToTree ( code ) <EOL> comment_splicer . SpliceComments ( tree ) <EOL> if dumptree : <EOL> pytree_visitor . DumpPyTree ( tree , target_stream = sys . stderr ) <EOL> return pytree_unwrapper . UnwrapPyTree ( tree ) <EOL> def _CheckMatchingBrackets ( self , uwlines , list_of_expected ) : <EOL> """<STR_LIT>""" <EOL> actual = [ ] <EOL> for uwl in uwlines : <EOL> filtered_values = [ ( ft , ft . matching_bracket ) <EOL> for ft in uwl . tokens <EOL> if ft . name not in pytree_utils . NONSEMANTIC_TOKENS ] <EOL> if filtered_values : <EOL> actual . append ( filtered_values ) <EOL> for index , bracket_list in enumerate ( list_of_expected ) : <EOL> uwline = actual [ index ] <EOL> if not bracket_list : <EOL> for value in uwline : <EOL> self . assertIsNone ( value [ <NUM_LIT:1> ] ) <EOL> else : <EOL> for open_bracket , close_bracket in bracket_list : <EOL> self . assertEqual ( uwline [ open_bracket ] [ <NUM_LIT:0> ] , uwline [ close_bracket ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( uwline [ close_bracket ] [ <NUM_LIT:0> ] , uwline [ open_bracket ] [ <NUM_LIT:1> ] ) <EOL> def testFunctionDef ( self ) : <EOL> code = textwrap . dedent ( """<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckMatchingBrackets ( uwlines , [ <EOL> [ ( <NUM_LIT:2> , <NUM_LIT> ) , ( <NUM_LIT:7> , <NUM_LIT:15> ) , ( <NUM_LIT:10> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ] , <EOL> [ ] <EOL> ] ) <EOL> def testDecorator ( self ) : <EOL> code = textwrap . dedent ( """<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckMatchingBrackets ( uwlines , [ <EOL> [ ( <NUM_LIT:2> , <NUM_LIT:3> ) ] , <EOL> [ ( <NUM_LIT:2> , <NUM_LIT:8> ) ] , <EOL> [ ] <EOL> ] ) <EOL> def testClassDef ( self ) : <EOL> code = textwrap . dedent ( """<STR_LIT>""" ) <EOL> uwlines = self . _ParseAndUnwrap ( code ) <EOL> self . _CheckMatchingBrackets ( uwlines , [ <EOL> [ ( <NUM_LIT:2> , <NUM_LIT:8> ) ] , <EOL> [ ] <EOL> ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
