<s> import unittest <EOL> import optfunc <EOL> from StringIO import StringIO <EOL> class TestOptFunc ( unittest . TestCase ) : <EOL> def test_three_positional_args ( self ) : <EOL> has_run = [ False ] <EOL> def func ( one , two , three ) : <EOL> has_run [ <NUM_LIT:0> ] = True <EOL> parser , required_args = optfunc . func_to_optionparser ( func ) <EOL> self . assertEqual ( len ( parser . option_list ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( str ( parser . option_list [ <NUM_LIT:0> ] ) , '<STR_LIT>' ) <EOL> self . assertEqual ( required_args , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for argv in ( <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ) : <EOL> e = StringIO ( ) <EOL> optfunc . run ( func , argv , stderr = e ) <EOL> self . assert_ ( '<STR_LIT>' in e . getvalue ( ) , e . getvalue ( ) ) <EOL> self . assertEqual ( has_run [ <NUM_LIT:0> ] , False ) <EOL> e = StringIO ( ) <EOL> optfunc . run ( func , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( has_run [ <NUM_LIT:0> ] , True ) <EOL> def test_one_arg_one_option ( self ) : <EOL> has_run = [ False ] <EOL> def func ( one , option = '<STR_LIT>' ) : <EOL> has_run [ <NUM_LIT:0> ] = ( one , option ) <EOL> parser , required_args = optfunc . func_to_optionparser ( func ) <EOL> self . assertEqual ( len ( parser . option_list ) , <NUM_LIT:2> ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assert_ ( '<STR_LIT>' in strs ) <EOL> self . assert_ ( '<STR_LIT>' in strs ) <EOL> self . assertEqual ( required_args , [ '<STR_LIT>' ] ) <EOL> self . assert_ ( not has_run [ <NUM_LIT:0> ] ) <EOL> optfunc . run ( func , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_ ( has_run [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( has_run [ <NUM_LIT:0> ] , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> has_run [ <NUM_LIT:0> ] = False <EOL> optfunc . run ( func , [ '<STR_LIT>' ] ) <EOL> self . assert_ ( has_run [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( has_run [ <NUM_LIT:0> ] , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_options_are_correctly_named ( self ) : <EOL> def func1 ( one , option = '<STR_LIT>' , verbose = False ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( func1 ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assertEqual ( strs , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_option_with_hyphens ( self ) : <EOL> def func2 ( option_with_hyphens = True ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( func2 ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assertEqual ( strs , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_options_with_same_inital_use_next_letter ( self ) : <EOL> def func1 ( one , version = '<STR_LIT>' , verbose = False ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( func1 ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assertEqual ( strs , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def func2 ( one , host = '<STR_LIT>' ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( func2 ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assertEqual ( strs , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_short_option_can_be_named_explicitly ( self ) : <EOL> def func1 ( one , option = '<STR_LIT>' , q_verbose = False ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( func1 ) <EOL> strs = [ str ( o ) for o in parser . option_list ] <EOL> self . assertEqual ( strs , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> e = StringIO ( ) <EOL> optfunc . run ( func1 , [ '<STR_LIT>' , '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> def test_notstrict ( self ) : <EOL> "<STR_LIT>" <EOL> def strict_func ( one ) : <EOL> pass <EOL> e = StringIO ( ) <EOL> optfunc . run ( strict_func , [ ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> @ optfunc . notstrict <EOL> def notstrict_func ( one ) : <EOL> pass <EOL> e = StringIO ( ) <EOL> optfunc . run ( notstrict_func , [ ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> def test_arghelp ( self ) : <EOL> "<STR_LIT>" <EOL> @ optfunc . arghelp ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> def foo ( foo = False ) : <EOL> pass <EOL> parser , required_args = optfunc . func_to_optionparser ( foo ) <EOL> opt = parser . option_list [ <NUM_LIT:1> ] <EOL> self . assertEqual ( str ( opt ) , '<STR_LIT>' ) <EOL> self . assertEqual ( opt . help , '<STR_LIT>' ) <EOL> def test_multiple_invalid_subcommand ( self ) : <EOL> "<STR_LIT>" <EOL> def one ( arg ) : <EOL> pass <EOL> def two ( arg ) : <EOL> pass <EOL> def three ( arg ) : <EOL> pass <EOL> e = StringIO ( ) <EOL> optfunc . run ( [ one , two ] , [ '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( <EOL> e . getvalue ( ) . strip ( ) , "<STR_LIT>" <EOL> ) <EOL> e = StringIO ( ) <EOL> optfunc . run ( [ one , two , three ] , [ '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( <EOL> e . getvalue ( ) . strip ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> e = StringIO ( ) <EOL> optfunc . run ( [ one , two , three ] , [ ] , stderr = e ) <EOL> self . assertEqual ( <EOL> e . getvalue ( ) . strip ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_multiple_valid_subcommand_invalid_argument ( self ) : <EOL> "<STR_LIT>" <EOL> def one ( arg ) : <EOL> executed . append ( ( '<STR_LIT>' , arg ) ) <EOL> def two ( arg ) : <EOL> executed . append ( ( '<STR_LIT>' , arg ) ) <EOL> e = StringIO ( ) <EOL> executed = [ ] <EOL> optfunc . run ( [ one , two ] , [ '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( <EOL> e . getvalue ( ) . strip ( ) , '<STR_LIT>' <EOL> ) <EOL> def test_multiple_valid_subcommand_valid_argument ( self ) : <EOL> "<STR_LIT>" <EOL> def one ( arg ) : <EOL> executed . append ( ( '<STR_LIT>' , arg ) ) <EOL> def two ( arg ) : <EOL> executed . append ( ( '<STR_LIT>' , arg ) ) <EOL> e = StringIO ( ) <EOL> executed = [ ] <EOL> optfunc . run ( [ one , two ] , [ '<STR_LIT>' , '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( executed , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> def test_run_class ( self ) : <EOL> class Class : <EOL> def __init__ ( self , one , option = '<STR_LIT>' ) : <EOL> self . has_run = [ ( one , option ) ] <EOL> class NoInitClass : <EOL> pass <EOL> e = StringIO ( ) <EOL> c = optfunc . run ( Class , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> self . assert_ ( c . has_run [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( c . has_run [ <NUM_LIT:0> ] , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> c = None <EOL> e = StringIO ( ) <EOL> c = optfunc . run ( Class , [ '<STR_LIT>' ] , stderr = e ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> self . assert_ ( c . has_run [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( c . has_run [ <NUM_LIT:0> ] , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> c = None <EOL> e = StringIO ( ) <EOL> c = optfunc . run ( NoInitClass , [ ] , stderr = e ) <EOL> self . assert_ ( c ) <EOL> self . assertEqual ( e . getvalue ( ) . strip ( ) , '<STR_LIT>' ) <EOL> def test_stdin_special_argument ( self ) : <EOL> consumed = [ ] <EOL> def func ( stdin ) : <EOL> consumed . append ( stdin . read ( ) ) <EOL> class FakeStdin ( object ) : <EOL> def read ( self ) : <EOL> return "<STR_LIT:hello>" <EOL> optfunc . run ( func , stdin = FakeStdin ( ) ) <EOL> self . assertEqual ( consumed , [ '<STR_LIT:hello>' ] ) <EOL> def test_stdout_special_argument ( self ) : <EOL> def upper ( stdin , stdout ) : <EOL> stdout . write ( stdin . read ( ) . upper ( ) ) <EOL> class FakeStdin ( object ) : <EOL> def read ( self ) : <EOL> return "<STR_LIT:hello>" <EOL> class FakeStdout ( object ) : <EOL> written = '<STR_LIT>' <EOL> def write ( self , w ) : <EOL> self . written = w <EOL> stdout = FakeStdout ( ) <EOL> self . assertEqual ( stdout . written , '<STR_LIT>' ) <EOL> optfunc . run ( upper , stdin = FakeStdin ( ) , stdout = stdout ) <EOL> self . assertEqual ( stdout . written , '<STR_LIT>' ) <EOL> def test_stderr_special_argument ( self ) : <EOL> def upper ( stderr ) : <EOL> stderr . write ( '<STR_LIT>' ) <EOL> class FakeStderr ( object ) : <EOL> written = '<STR_LIT>' <EOL> def write ( self , w ) : <EOL> self . written = w <EOL> stderr = FakeStderr ( ) <EOL> self . assertEqual ( stderr . written , '<STR_LIT>' ) <EOL> optfunc . run ( upper , stderr = stderr ) <EOL> self . assertEqual ( stderr . written , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
