<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from unittest import TestCase <EOL> from subprocess import check_output , Popen , PIPE <EOL> from pyrsistent import pmap <EOL> from . . _bytesjson import dumps <EOL> from . . prettyprint import pretty_format , _CLI_HELP , REQUIRED_FIELDS <EOL> SIMPLE_MESSAGE = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : [ <NUM_LIT> , <NUM_LIT> ] } <EOL> UNTYPED_MESSAGE = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:key>" : <NUM_LIT> , <EOL> "<STR_LIT:abc>" : "<STR_LIT>" } <EOL> class FormattingTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_message ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> pretty_format ( SIMPLE_MESSAGE ) , <EOL> """<STR_LIT>""" ) <EOL> def test_untyped_message ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> pretty_format ( UNTYPED_MESSAGE ) , <EOL> """<STR_LIT>""" ) <EOL> def test_action ( self ) : <EOL> """<STR_LIT>""" <EOL> message = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertEqual ( <EOL> pretty_format ( message ) , <EOL> """<STR_LIT>""" ) <EOL> def test_multi_line ( self ) : <EOL> """<STR_LIT>""" <EOL> message = { "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertEqual ( <EOL> pretty_format ( message ) , <EOL> """<STR_LIT>""" ) <EOL> def test_tabs ( self ) : <EOL> """<STR_LIT>""" <EOL> message = { "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:key>" : "<STR_LIT>" } <EOL> self . assertEqual ( <EOL> pretty_format ( message ) , <EOL> """<STR_LIT>""" ) <EOL> def test_structured ( self ) : <EOL> """<STR_LIT>""" <EOL> message = { "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:key>" : { "<STR_LIT:value>" : <NUM_LIT> , <EOL> "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , { "<STR_LIT>" : "<STR_LIT:data>" } ] } } <EOL> self . assertEqual ( <EOL> pretty_format ( message ) , <EOL> """<STR_LIT>""" ) <EOL> class CommandLineTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_help ( self ) : <EOL> """<STR_LIT>""" <EOL> result = check_output ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertEqual ( result , _CLI_HELP . encode ( "<STR_LIT:utf-8>" ) ) <EOL> def write_and_read ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> process = Popen ( [ b"<STR_LIT>" ] , stdin = PIPE , stdout = PIPE ) <EOL> process . stdin . write ( b"<STR_LIT>" . join ( line + b"<STR_LIT:\n>" for line in lines ) ) <EOL> process . stdin . close ( ) <EOL> result = process . stdout . read ( ) . decode ( "<STR_LIT:utf-8>" ) <EOL> process . stdout . close ( ) <EOL> return result <EOL> def test_output ( self ) : <EOL> """<STR_LIT>""" <EOL> messages = [ SIMPLE_MESSAGE , UNTYPED_MESSAGE , SIMPLE_MESSAGE ] <EOL> stdout = self . write_and_read ( map ( dumps , messages ) ) <EOL> self . assertEqual ( <EOL> stdout , <EOL> "<STR_LIT>" . join ( pretty_format ( message ) + "<STR_LIT:\n>" for message in messages ) ) <EOL> def test_not_json_message ( self ) : <EOL> """<STR_LIT>""" <EOL> not_json = b"<STR_LIT>" <EOL> lines = [ dumps ( SIMPLE_MESSAGE ) , not_json , dumps ( UNTYPED_MESSAGE ) ] <EOL> stdout = self . write_and_read ( lines ) <EOL> self . assertEqual ( <EOL> stdout , <EOL> "<STR_LIT>" . format ( <EOL> pretty_format ( SIMPLE_MESSAGE ) , str ( not_json ) , <EOL> pretty_format ( UNTYPED_MESSAGE ) ) ) <EOL> def test_missing_required_field ( self ) : <EOL> """<STR_LIT>""" <EOL> base = pmap ( SIMPLE_MESSAGE ) <EOL> messages = [ dumps ( dict ( base . remove ( field ) ) ) <EOL> for field in REQUIRED_FIELDS ] + [ dumps ( SIMPLE_MESSAGE ) ] <EOL> stdout = self . write_and_read ( messages ) <EOL> self . assertEqual ( <EOL> stdout , <EOL> "<STR_LIT>" . format ( <EOL> "<STR_LIT>" . join ( "<STR_LIT>" . format ( msg ) <EOL> for msg in messages [ : - <NUM_LIT:1> ] ) , <EOL> pretty_format ( SIMPLE_MESSAGE ) ) ) </s>
