
    ?d                         d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  G d de
      Zed        Zed	        Z G d
 de	j                        Z G d de	j                        Zy)zTests for yapf.__init__.main.    )contextmanagerN)	py3compat)yapf_test_helperc                   8    e Zd ZdZ G d de      Zd Zd Zd Zy)IOzIO is a thin wrapper around StringIO.

  This is strictly to wrap the Python 3 StringIO object so that it can supply a
  "buffer" attribute.
  c                       e Zd Zd Zd Zd Zy)	IO.Bufferc                 6    t        j                         | _        y N)r   StringIO	string_ioselfs    5/usr/lib/python3/dist-packages/yapftests/main_test.py__init__zIO.Buffer.__init__$   s     ))+dn    c                     t         j                  rt        |t              rt	        |d      }| j
                  j                  |       y )Nzutf-8)r   PY3
isinstancebytesstrr   writer   ss     r   r   zIO.Buffer.write'   s/    	:a/7O
nn1r   c                 6    | j                   j                         S r   )r   getvaluer   s    r   r   zIO.Buffer.getvalue,   s    ^^$$&&r   N)__name__
__module____qualname__r   r   r    r   r   Bufferr	   "   s    ,
'r   r!   c                 .    | j                         | _        y r   )r!   bufferr   s    r   r   zIO.__init__/   s    ++-DKr   c                 :    | j                   j                  |       y r   )r#   r   r   s     r   r   zIO.write2   s    KKar   c                 6    | j                   j                         S r   )r#   r   r   s    r   r   zIO.getvalue5   s    ;;!!r   N)	r   r   r   __doc__objectr!   r   r   r   r    r   r   r   r      s"    'v ' "r   r   c               #   N  K   t               t               }} t        j                  t        j                  }}	 | |ct        _        t        _        t        j                  t        j                  f ||ct        _        t        _        y # ||ct        _        t        _        w xY wwr   )r   sysstdoutstderr)new_outnew_errold_outold_errs       r   captured_outputr0   9   sl     T247'ZZ7'.$gCJ

**cjj
  $gCJ
WgCJ
s   5B%9B	 1B%	B""B%c              #       K    fd} |       fd}	 t         j                  j                  }|t         j                  _        d |t         j                  _        y# t         j                  _        w xY ww)z6Monkey patch code as though it were coming from stdin.c               3   R   K   j                         D ]  } |   t               wr   )
splitlinesEOFError)linecodes    r   lineszpatched_input.<locals>.linesH   s*     ! j
*s   $'c                     t        |       S r   )next)r7   s    r   patch_raw_inputz&patched_input.<locals>.patch_raw_inputM   s    ;r   N)yapfr   	raw_input)r6   r7   r:   orig_raw_imports   `   r   patched_inputr>   D   sS     
 "G /nn..O.DNN	.DNNDNNs   A73A A7A44A7c                       e Zd Zd Zy)RunMainTestc                 b   d}ddgt         _        t               5 \  }}| j                  t              5  t        j                          ddd       | j                  |j                         d       | j                  |j                         |       ddd       y# 1 sw Y   RxY w# 1 sw Y   yxY w)z1run_main should handle YapfError and sys.exit(1).z5yapf: input filenames did not match any python files
r;   foo.cN )	r)   argvr0   assertRaises
SystemExitr;   run_mainassertEqualr   )r   expected_messageouterrs       r   testShouldHandleYapfErrorz%RunMainTest.testShouldHandleYapfErrorZ   s    O CH		 9jsCZ( 
s||~r*
s||~'78	9 9 9 9s#   B%BAB%B"	B%%B.N)r   r   r   rL   r    r   r   r@   r@   X   s    9r   r@   c                   *    e Zd Zd Zd Zd Zd Zd Zy)MainTestc                     | j                  t        j                  j                  d      5  t        j                  ddg       d d d        y # 1 sw Y   y xY w)Nzdid not match any python filesr;   rB   )assertRaisesRegexr;   errors	YapfErrormainr   s    r   testNoPythonFilesMatchedz!MainTest.testNoPythonFilesMatchedg   sF    			 5 5 @
B #
ii!"# # #s   AAc                    d}t        |      5  t               5 \  }}t        j                  g       }| j	                  |d       | j	                  |j                         |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nza = 1
b = 2
r   r>   r0   r;   rS   rH   r   )r   r6   rJ   _rets        r   testEchoInputzMainTest.testEchoInputl   s}    D	t	 / /aiima .// // // /s#   BAA5$B5A>	:BB
c                 "   d}d}t        |      5  t               5 \  }}t        j                  ddg      }| j	                  |d       | j	                  |j                         |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nzdef f(a = 1

):
    return 2*a
zdef f(a=1):
  return 2 * a
-z--style=yapfr   rV   )r   r6   	yapf_coderJ   rW   rX   s         r   testEchoInputWithStylezMainTest.testEchoInputWithStylet   s    0D/I	t	 4 4aiin-.a 344 44 44 4s#   BAA9(B9B	>BBc                 4   d}t        |      5  t               5 \  }}| j                  t        j                  j
                  d      5  t        j                  g        d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nz  a = 1
zunexpected indent)r>   r0   rP   r;   rQ   rR   rS   )r   
bad_syntaxrW   s      r   testEchoBadInputzMainTest.testEchoBadInput}   s    J	z	"  A##DKK$9$9;NO 	
))B-	 	 	  s:   B.BA6B%B6A?;BB	BBc                    t               5 \  }}t        j                  g d      }| j                  |d       |j	                         }| j                  d|       | j                  d|       d d d        y # 1 sw Y   y xY w)N)r[   z--style-helpz--style=pep8r   zindent_width=4z8The number of spaces required before a trailing comment.)r0   r;   rS   rH   r   assertIn)r   rJ   rW   rX   help_messages        r   testHelpzMainTest.testHelp   sn    		 "hsAII;<c
sA\\^l
mm$l3
mmN "" " "s   A!A55A>N)r   r   r   rT   rY   r]   r`   rd   r    r   r   rN   rN   e   s    #
/4"r   rN   )r&   
contextlibr   r)   unittestr;   yapf.yapflibr   	yapftestsr   r'   r   r0   r>   YAPFTestr@   rN   r    r   r   <module>rj      sv    $ % 
   " &" "< . . / /&
9"++ 
9&"(( &"r   