<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import ast <EOL> import textwrap <EOL> from pants_test . contrib . python . checks . tasks . checkstyle . plugin_test_base import CheckstylePluginTestBase <EOL> from pants . contrib . python . checks . tasks . checkstyle . common import Nit <EOL> from pants . contrib . python . checks . tasks . checkstyle . import_order import ImportOrder , ImportType <EOL> IMPORT_CHUNKS = { <EOL> ImportType . STDLIB : """<STR_LIT>""" , <EOL> ImportType . TWITTER : """<STR_LIT>""" , <EOL> ImportType . GEN : """<STR_LIT>""" , <EOL> ImportType . PACKAGE : """<STR_LIT>""" , <EOL> ImportType . THIRD_PARTY : """<STR_LIT>""" , <EOL> } <EOL> def strip_newline ( stmt ) : <EOL> return textwrap . dedent ( '<STR_LIT:\n>' . join ( filter ( None , stmt . splitlines ( ) ) ) ) <EOL> def stitch_chunks ( newlines , * chunks ) : <EOL> return ( '<STR_LIT:\n>' * newlines ) . join ( [ strip_newline ( IMPORT_CHUNKS . get ( c ) ) for c in chunks ] ) <EOL> class ImportOrderTest ( CheckstylePluginTestBase ) : <EOL> plugin_type = ImportOrder <EOL> def get_import_chunk_types ( self , import_type ) : <EOL> chunks = list ( self . get_plugin ( IMPORT_CHUNKS [ import_type ] ) . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( chunks ) ) <EOL> return tuple ( map ( type , chunks [ <NUM_LIT:0> ] ) ) <EOL> def test_classify_import_chunks ( self ) : <EOL> self . assertEqual ( ( ast . Import , ast . ImportFrom , ast . Import ) , <EOL> self . get_import_chunk_types ( ImportType . STDLIB ) ) <EOL> self . assertEqual ( ( ast . ImportFrom , ast . ImportFrom ) , <EOL> self . get_import_chunk_types ( ImportType . TWITTER ) ) <EOL> self . assertEqual ( ( ast . ImportFrom , ) , <EOL> self . get_import_chunk_types ( ImportType . GEN ) ) <EOL> self . assertEqual ( ( ast . ImportFrom , ) , <EOL> self . get_import_chunk_types ( ImportType . PACKAGE ) ) <EOL> self . assertEqual ( ( ast . ImportFrom , ast . Import ) , <EOL> self . get_import_chunk_types ( ImportType . THIRD_PARTY ) ) <EOL> def test_classify_import ( self ) : <EOL> for import_type , chunk in IMPORT_CHUNKS . items ( ) : <EOL> io = self . get_plugin ( chunk ) <EOL> import_chunks = list ( io . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( import_chunks ) ) <EOL> module_types , chunk_errors = io . classify_imports ( import_chunks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( module_types ) ) <EOL> self . assertEqual ( import_type , module_types . pop ( ) ) <EOL> self . assertEqual ( [ ] , chunk_errors ) <EOL> PAIRS = ( <EOL> ( ImportType . STDLIB , ImportType . TWITTER ) , <EOL> ( ImportType . TWITTER , ImportType . GEN ) , <EOL> ( ImportType . PACKAGE , ImportType . THIRD_PARTY ) , <EOL> ) <EOL> def test_pairwise_classify ( self ) : <EOL> for first , second in self . PAIRS : <EOL> io = self . get_plugin ( stitch_chunks ( <NUM_LIT:1> , first , second ) ) <EOL> import_chunks = list ( io . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( import_chunks ) ) <EOL> module_types , chunk_errors = io . classify_imports ( import_chunks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( module_types ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( chunk_errors ) ) <EOL> self . assertEqual ( first , module_types . pop ( ) ) <EOL> module_types , chunk_errors = io . classify_imports ( import_chunks [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( module_types ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( chunk_errors ) ) <EOL> self . assertEqual ( second , module_types . pop ( ) ) <EOL> for second , first in self . PAIRS : <EOL> io = self . get_plugin ( stitch_chunks ( <NUM_LIT:1> , first , second ) ) <EOL> import_chunks = list ( io . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( import_chunks ) ) <EOL> nits = list ( io . nits ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( nits ) ) <EOL> self . assertEqual ( '<STR_LIT>' , nits [ <NUM_LIT:0> ] . code ) <EOL> self . assertEqual ( Nit . ERROR , nits [ <NUM_LIT:0> ] . severity ) <EOL> def test_multiple_imports_error ( self ) : <EOL> io = self . get_plugin ( stitch_chunks ( <NUM_LIT:0> , ImportType . STDLIB , ImportType . TWITTER ) ) <EOL> import_chunks = list ( io . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( import_chunks ) ) <EOL> module_types , chunk_errors = io . classify_imports ( import_chunks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( chunk_errors ) ) <EOL> self . assertEqual ( '<STR_LIT>' , chunk_errors [ <NUM_LIT:0> ] . code ) <EOL> self . assertEqual ( Nit . ERROR , chunk_errors [ <NUM_LIT:0> ] . severity ) <EOL> self . assertItemsEqual ( [ ImportType . STDLIB , ImportType . TWITTER ] , module_types ) <EOL> io = self . get_plugin ( """<STR_LIT>""" ) <EOL> import_chunks = list ( io . iter_import_chunks ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( import_chunks ) ) <EOL> module_types , chunk_errors = io . classify_imports ( import_chunks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( chunk_errors ) ) <EOL> self . assertItemsEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ chunk_error . code for chunk_error in chunk_errors ] ) <EOL> self . assertItemsEqual ( [ ImportType . STDLIB , ImportType . THIRD_PARTY ] , module_types ) <EOL> def test_import_lexical_order ( self ) : <EOL> imp = """<STR_LIT>""" <EOL> self . assertNit ( imp , '<STR_LIT>' ) <EOL> def test_import_wildcard ( self ) : <EOL> imp = """<STR_LIT>""" <EOL> self . assertNit ( imp , '<STR_LIT>' ) </s>
