<s> import pytest <EOL> from jip . options import Option , Options , TYPE_OUTPUT , TYPE_INPUT , TYPE_OPTION <EOL> from jip . options import ParserException <EOL> def test_boolean_option ( ) : <EOL> o = Option ( "<STR_LIT:t>" , short = "<STR_LIT>" , default = False ) <EOL> assert o . name == "<STR_LIT:t>" <EOL> assert o . short == "<STR_LIT>" <EOL> assert not o . default <EOL> assert o . value == [ False ] <EOL> assert str ( o ) == "<STR_LIT>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> o . value = True <EOL> assert str ( o ) == "<STR_LIT>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> def test_single_string_option ( ) : <EOL> o = Option ( "<STR_LIT:t>" , short = "<STR_LIT>" , nargs = <NUM_LIT:1> ) <EOL> assert str ( o ) == "<STR_LIT>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> o . value = "<STR_LIT:test>" <EOL> assert str ( o ) == "<STR_LIT:test>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> def test_list_string_option ( ) : <EOL> o = Option ( "<STR_LIT:t>" , short = "<STR_LIT>" , nargs = "<STR_LIT:*>" ) <EOL> assert str ( o ) == "<STR_LIT>" <EOL> o . value = "<STR_LIT:test>" <EOL> assert str ( o ) == "<STR_LIT:test>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> o . value = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert str ( o ) == "<STR_LIT>" <EOL> assert o . to_cmd ( ) == "<STR_LIT>" <EOL> def test_hidden_option ( ) : <EOL> o = Option ( "<STR_LIT>" , short = "<STR_LIT>" , hidden = True , value = '<STR_LIT>' ) <EOL> assert o . to_cmd ( ) == '<STR_LIT>' <EOL> def test_argparse_parser ( ) : <EOL> from argparse import ArgumentParser <EOL> p = ArgumentParser ( ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , nargs = "<STR_LIT:*>" , help = "<STR_LIT>" , <EOL> required = True ) <EOL> opts = Options . from_argparse ( p ) <EOL> assert len ( opts ) == <NUM_LIT:4> <EOL> assert opts [ '<STR_LIT:input>' ] is not None <EOL> assert opts [ '<STR_LIT:input>' ] . nargs == <NUM_LIT:1> <EOL> assert opts [ '<STR_LIT:test>' ] is not None <EOL> assert opts [ '<STR_LIT:test>' ] . nargs == <NUM_LIT:0> <EOL> assert opts [ '<STR_LIT>' ] is not None <EOL> assert opts [ '<STR_LIT>' ] . nargs == "<STR_LIT:*>" <EOL> assert opts [ '<STR_LIT>' ] . required <EOL> def test_docopt_parser_shorts ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert len ( opts ) == <NUM_LIT:3> <EOL> assert opts [ '<STR_LIT:input>' ] is not None <EOL> assert opts [ '<STR_LIT:input>' ] . nargs == "<STR_LIT:*>" <EOL> assert not opts [ '<STR_LIT:input>' ] . required <EOL> assert opts [ '<STR_LIT:t>' ] is not None <EOL> assert opts [ '<STR_LIT:t>' ] . nargs == <NUM_LIT:0> <EOL> assert not opts [ '<STR_LIT:t>' ] . required <EOL> assert opts [ '<STR_LIT>' ] is not None <EOL> assert opts [ '<STR_LIT>' ] . nargs == <NUM_LIT:1> <EOL> assert opts [ '<STR_LIT>' ] . required <EOL> def test_docopt_parser_with_opts ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert len ( opts ) == <NUM_LIT:4> <EOL> assert opts [ '<STR_LIT:input>' ] is not None <EOL> assert opts [ '<STR_LIT:input>' ] . nargs == "<STR_LIT:*>" <EOL> assert opts [ '<STR_LIT:input>' ] . option_type == TYPE_INPUT <EOL> assert not opts [ '<STR_LIT:input>' ] . required <EOL> assert opts [ '<STR_LIT>' ] is not None <EOL> assert opts [ '<STR_LIT>' ] . nargs == <NUM_LIT:1> <EOL> assert opts [ '<STR_LIT>' ] . option_type == TYPE_OUTPUT <EOL> assert not opts [ '<STR_LIT>' ] . required <EOL> assert opts [ '<STR_LIT:test>' ] is not None <EOL> assert opts [ '<STR_LIT:test>' ] . nargs == <NUM_LIT:0> <EOL> assert not opts [ '<STR_LIT:test>' ] . required <EOL> assert opts [ '<STR_LIT:test>' ] . option_type == TYPE_OPTION <EOL> assert opts [ '<STR_LIT>' ] is not None <EOL> assert opts [ '<STR_LIT>' ] . nargs == <NUM_LIT:1> <EOL> assert opts [ '<STR_LIT>' ] . required <EOL> assert opts [ '<STR_LIT>' ] . option_type == TYPE_OPTION <EOL> def test_docopt_parser_with_defaults ( ) : <EOL> import sys <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert opts [ '<STR_LIT:input>' ] . option_type == TYPE_INPUT <EOL> assert opts [ '<STR_LIT>' ] . option_type == TYPE_OUTPUT <EOL> assert opts [ '<STR_LIT:input>' ] . get ( ) == "<STR_LIT>" <EOL> assert str ( opts [ '<STR_LIT:input>' ] ) == "<STR_LIT>" <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) == sys . stdin <EOL> assert opts [ '<STR_LIT>' ] . raw ( ) == sys . stdout <EOL> assert opts [ '<STR_LIT>' ] . raw ( ) == "<STR_LIT>" <EOL> def test_docopt_type_from_defaults ( ) : <EOL> import sys <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert opts [ '<STR_LIT:input>' ] . option_type == TYPE_INPUT <EOL> assert opts [ '<STR_LIT>' ] . option_type == TYPE_OUTPUT <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) == sys . stdin <EOL> assert opts [ '<STR_LIT>' ] . raw ( ) == sys . stdout <EOL> def test_parsing_args ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> opts . parse ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) == '<STR_LIT>' <EOL> def test_unkonw_argument ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> with pytest . raises ( ParserException ) : <EOL> opts . parse ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_call_to_help ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> with pytest . raises ( ParserException ) : <EOL> opts . parse ( [ '<STR_LIT>' ] ) <EOL> def test_list_arguments_for_fan_out ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> opts . parse ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> with pytest . raises ( ValueError ) : <EOL> opts [ '<STR_LIT:input>' ] . get ( ) <EOL> def test_get_usage_from_docopt ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert opts . usage ( ) == "<STR_LIT>" <EOL> def test_argparse_parser_usage ( ) : <EOL> from argparse import ArgumentParser <EOL> p = ArgumentParser ( prog = "<STR_LIT>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , nargs = "<STR_LIT:*>" , help = "<STR_LIT>" , <EOL> required = True ) <EOL> opts = Options . from_argparse ( p ) <EOL> assert opts . usage ( ) == "<STR_LIT>" "<STR_LIT>" <EOL> def test_streamable_from_default_no_list ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert not opts [ '<STR_LIT>' ] . streamable <EOL> assert opts [ '<STR_LIT:input>' ] . streamable <EOL> def test_options_equality ( ) : <EOL> string_opt = Option ( '<STR_LIT:test>' ) <EOL> assert string_opt == None <EOL> string_opt . value = "<STR_LIT>" <EOL> assert string_opt == "<STR_LIT>" <EOL> string_opt . append ( "<STR_LIT>" ) <EOL> assert string_opt != "<STR_LIT>" <EOL> assert string_opt == [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert string_opt <EOL> string_opt . value = False <EOL> assert not string_opt <EOL> def test_user_specified_option_and_default_recovery ( ) : <EOL> from argparse import ArgumentParser <EOL> p = ArgumentParser ( ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> p . add_argument ( "<STR_LIT>" , type = int , default = <NUM_LIT:1> ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> p . add_argument ( "<STR_LIT>" , "<STR_LIT>" , nargs = "<STR_LIT:*>" , help = "<STR_LIT>" , <EOL> required = True ) <EOL> opts = Options . from_argparse ( p ) <EOL> opts . parse ( [ ] ) <EOL> assert not opts [ '<STR_LIT:test>' ] . raw ( ) <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) is None <EOL> assert opts [ '<STR_LIT>' ] . raw ( ) is None <EOL> assert not opts [ '<STR_LIT>' ] . user_specified <EOL> assert not opts [ '<STR_LIT:input>' ] . user_specified <EOL> assert not opts [ '<STR_LIT:test>' ] . user_specified <EOL> opts . parse ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert opts [ '<STR_LIT:test>' ] . raw ( ) <EOL> assert opts [ '<STR_LIT:input>' ] . raw ( ) == '<STR_LIT>' <EOL> assert opts [ '<STR_LIT>' ] . raw ( ) == [ '<STR_LIT>' ] <EOL> assert opts [ '<STR_LIT>' ] . user_specified <EOL> assert opts [ '<STR_LIT:input>' ] . user_specified <EOL> assert opts [ '<STR_LIT:test>' ] . user_specified <EOL> def test_add_output_list_option ( ) : <EOL> ops = Options ( ) <EOL> o = ops . add_output ( "<STR_LIT:test>" , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert o . hidden <EOL> assert o . raw ( ) == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def test_add_output_single_value_option_none_value ( ) : <EOL> ops = Options ( ) <EOL> o = ops . add_output ( "<STR_LIT:test>" ) <EOL> assert o . hidden <EOL> assert o . raw ( ) is None <EOL> def test_add_output_single_value_option ( ) : <EOL> ops = Options ( ) <EOL> o = ops . add_output ( "<STR_LIT:test>" , "<STR_LIT>" ) <EOL> assert o . hidden <EOL> assert o . raw ( ) == '<STR_LIT>' <EOL> def test_add_output_single_value_option_multiplw_inputs ( ) : <EOL> ops = Options ( ) <EOL> o = ops . add_output ( "<STR_LIT:test>" , value = [ <NUM_LIT:1> , <NUM_LIT:2> ] , nargs = <NUM_LIT:1> ) <EOL> assert o . hidden <EOL> assert o . raw ( ) == [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> with pytest . raises ( ValueError ) : <EOL> o . get ( ) <EOL> def test_option_is_stream_empty_value ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> assert not o . is_stream ( ) <EOL> def test_option_expand_single ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> o . set ( <NUM_LIT:1> ) <EOL> assert o . expand ( ) == [ <NUM_LIT:1> ] <EOL> def test_option_expand_single_default ( ) : <EOL> o = Option ( '<STR_LIT:test>' , default = <NUM_LIT:1> ) <EOL> assert o . expand ( ) == [ <NUM_LIT:1> ] <EOL> def test_option_expand_list ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> o . set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert o . expand ( ) == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def test_option_expand_single_embedded ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> p = Option ( '<STR_LIT>' , default = <NUM_LIT:1> ) <EOL> o . set ( p ) <EOL> assert o . expand ( ) == [ p ] <EOL> def test_option_expand_list_embedded ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> p = Option ( '<STR_LIT>' ) <EOL> p . set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> o . set ( p ) <EOL> assert o . expand ( ) == [ p , p , p ] <EOL> def test_option_expand_mixed_embedded ( ) : <EOL> o = Option ( '<STR_LIT:test>' ) <EOL> p = Option ( '<STR_LIT>' , default = <NUM_LIT:1> ) <EOL> o . set ( [ <NUM_LIT:1> , p , <NUM_LIT:2> ] ) <EOL> assert o . expand ( ) == [ <NUM_LIT:1> , p , <NUM_LIT:2> ] <EOL> def test_list_option_rendering ( ) : <EOL> o = Option ( '<STR_LIT:test>' , nargs = "<STR_LIT:*>" ) <EOL> o . value = [ "<STR_LIT:A>" , "<STR_LIT:B>" ] <EOL> from jip . templates import render_template <EOL> assert render_template ( "<STR_LIT>" , o = o ) == '<STR_LIT>' <EOL> assert render_template ( "<STR_LIT>" , o = o ) == '<STR_LIT>' <EOL> def test_docopt_parser_with_tabs ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert len ( opts ) == <NUM_LIT:3> <EOL> assert opts [ '<STR_LIT:input>' ] is not None <EOL> assert opts [ '<STR_LIT:input>' ] . nargs == "<STR_LIT:*>" <EOL> assert not opts [ '<STR_LIT:input>' ] . required <EOL> assert opts [ '<STR_LIT:t>' ] is not None <EOL> assert opts [ '<STR_LIT:t>' ] . nargs == <NUM_LIT:0> <EOL> assert not opts [ '<STR_LIT:t>' ] . required <EOL> assert opts [ '<STR_LIT>' ] is not None <EOL> assert opts [ '<STR_LIT>' ] . nargs == <NUM_LIT:1> <EOL> assert opts [ '<STR_LIT>' ] . required <EOL> def test_options_setting_as_property ( ) : <EOL> help_string = """<STR_LIT>""" <EOL> opts = Options . from_docopt ( help_string ) <EOL> assert opts is not None <EOL> opts [ '<STR_LIT:name>' ] . set ( "<STR_LIT>" ) <EOL> assert opts [ '<STR_LIT:name>' ] . get ( ) == "<STR_LIT>" <EOL> assert opts . name . get ( ) == "<STR_LIT>" <EOL> assert opts . name == "<STR_LIT>" <EOL> opts [ '<STR_LIT:name>' ] = "<STR_LIT>" <EOL> assert opts [ '<STR_LIT:name>' ] . get ( ) == "<STR_LIT>" <EOL> assert opts . name . get ( ) == "<STR_LIT>" <EOL> assert opts . name == "<STR_LIT>" <EOL> opts . name = '<STR_LIT>' <EOL> assert opts [ '<STR_LIT:name>' ] . get ( ) == "<STR_LIT>" <EOL> assert opts . name . get ( ) == "<STR_LIT>" <EOL> assert opts . name == "<STR_LIT>" </s>
