<s> from argparse2 import ArgumentParser <EOL> import os <EOL> from openrcv . scripts . argparse import HelpRequested , UsageException <EOL> from openrcv . scripts . rcv import create_argparser , RcvArgumentParser <EOL> from openrcv . utiltest . helpers import UnitCase <EOL> VALID_COMMAND = [ '<STR_LIT:count>' , '<STR_LIT>' ] <EOL> class SafeGetLogLevelTest ( UnitCase ) : <EOL> """<STR_LIT>""" <EOL> def _get_valid_args ( self , level ) : <EOL> args = [ '<STR_LIT>' , level ] <EOL> args . extend ( VALID_COMMAND ) <EOL> return args <EOL> def call_safe_get_log_level ( self , level ) : <EOL> parser = create_argparser ( ) <EOL> args = self . _get_valid_args ( level ) <EOL> return parser . safe_get_log_level ( args , error_level = <NUM_LIT> ) <EOL> def call_safe_get_log_level_default_error_level ( self , level ) : <EOL> parser = create_argparser ( ) <EOL> args = self . _get_valid_args ( level ) <EOL> return parser . safe_get_log_level ( args ) <EOL> def test_safe_get_log_level__valid_string ( self ) : <EOL> actual = self . call_safe_get_log_level ( '<STR_LIT>' ) <EOL> self . assertEqual ( actual , <NUM_LIT:10> ) <EOL> def test_safe_get_log_level__int ( self ) : <EOL> actual = self . call_safe_get_log_level ( '<STR_LIT>' ) <EOL> self . assertEqual ( actual , <NUM_LIT> ) <EOL> def test_safe_get_log_level__invalid_value ( self ) : <EOL> """<STR_LIT>""" <EOL> actual = self . call_safe_get_log_level ( '<STR_LIT>' ) <EOL> self . assertEqual ( actual , <NUM_LIT> ) <EOL> def test_safe_get_log_level__default_error_level ( self ) : <EOL> """<STR_LIT>""" <EOL> actual = self . call_safe_get_log_level_default_error_level ( '<STR_LIT>' ) <EOL> self . assertEqual ( actual , <NUM_LIT:20> ) <EOL> def test_safe_get_log_level__no_level ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = create_argparser ( ) <EOL> actual = parser . safe_get_log_level ( VALID_COMMAND , error_level = <NUM_LIT> ) <EOL> self . assertEqual ( actual , <NUM_LIT:20> ) <EOL> def test_safe_get_log_level__invalid_command ( self ) : <EOL> """<STR_LIT>""" <EOL> bad_args = [ '<STR_LIT>' ] <EOL> parser = create_argparser ( ) <EOL> with self . assertRaises ( UsageException ) : <EOL> parser . parse_args ( bad_args ) <EOL> actual = parser . safe_get_log_level ( bad_args , error_level = <NUM_LIT> ) <EOL> self . assertEqual ( actual , <NUM_LIT> ) <EOL> def test_safe_get_log_level__unsupported_parser ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = ArgumentParser ( ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> actual = parser . safe_get_log_level ( [ ] , error_level = <NUM_LIT> ) <EOL> class ArgumentParserTest ( UnitCase ) : <EOL> """<STR_LIT>""" <EOL> def parser ( self ) : <EOL> return create_argparser ( ) <EOL> def test_bad_command ( self ) : <EOL> parser = self . parser ( ) <EOL> with self . assertRaises ( UsageException ) as cm : <EOL> parser . parse_args ( [ '<STR_LIT>' ] ) <EOL> err = cm . exception <EOL> self . assertEqual ( len ( err . args ) , <NUM_LIT:1> ) <EOL> self . assertStartsWith ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> parser = err . parser <EOL> self . assertEqual ( type ( parser ) , RcvArgumentParser ) <EOL> self . assertEqual ( parser . prog , "<STR_LIT>" ) <EOL> def test_help ( self ) : <EOL> parser = self . parser ( ) <EOL> with self . assertRaises ( HelpRequested ) : <EOL> parser . parse_args ( [ "<STR_LIT>" ] ) <EOL> def test_no_args ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = self . parser ( ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> parser . run_command <EOL> def test_bare_command ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = self . parser ( ) <EOL> ns = parser . parse_args ( [ "<STR_LIT>" ] ) <EOL> with open ( os . devnull , "<STR_LIT:w>" ) as f : <EOL> ns . run_command ( ns , stdout = f ) <EOL> def parse_args ( self , pre_args ) : <EOL> parser = create_argparser ( ) <EOL> args = pre_args + VALID_COMMAND <EOL> return parser . parse_args ( args ) <EOL> def parse_log_level ( self , args ) : <EOL> ns = self . parse_args ( args ) <EOL> return ns . log_level <EOL> def test_log_level ( self ) : <EOL> self . assertEqual ( self . parse_log_level ( [ ] ) , <NUM_LIT:20> ) <EOL> self . assertEqual ( self . parse_log_level ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , <NUM_LIT:15> ) <EOL> self . assertEqual ( self . parse_log_level ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , <NUM_LIT:10> ) <EOL> with self . assertRaises ( UsageException ) : <EOL> self . parse_log_level ( [ '<STR_LIT>' ] ) <EOL> with self . assertRaises ( UsageException ) : <EOL> self . parse_log_level ( [ '<STR_LIT>' , '<STR_LIT:foo>' ] ) </s>
