<s> from __future__ import absolute_import , unicode_literals <EOL> import unittest <EOL> from mopidy . mpd import exceptions , protocol <EOL> class TestConverts ( unittest . TestCase ) : <EOL> def test_integer ( self ) : <EOL> self . assertEqual ( <NUM_LIT> , protocol . INT ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( - <NUM_LIT> , protocol . INT ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT> , protocol . INT ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( ValueError , protocol . INT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . INT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . INT , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( ValueError , protocol . INT , '<STR_LIT>' ) <EOL> def test_unsigned_integer ( self ) : <EOL> self . assertEqual ( <NUM_LIT> , protocol . UINT ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( ValueError , protocol . UINT , '<STR_LIT>' ) <EOL> def test_boolean ( self ) : <EOL> self . assertEqual ( True , protocol . BOOL ( '<STR_LIT:1>' ) ) <EOL> self . assertEqual ( False , protocol . BOOL ( '<STR_LIT:0>' ) ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT:true>' ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT:false>' ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( ValueError , protocol . BOOL , '<STR_LIT>' ) <EOL> def test_range ( self ) : <EOL> self . assertEqual ( slice ( <NUM_LIT:1> , <NUM_LIT:2> ) , protocol . RANGE ( '<STR_LIT:1>' ) ) <EOL> self . assertEqual ( slice ( <NUM_LIT:0> , <NUM_LIT:1> ) , protocol . RANGE ( '<STR_LIT:0>' ) ) <EOL> self . assertEqual ( slice ( <NUM_LIT:0> , None ) , protocol . RANGE ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( slice ( <NUM_LIT:1> , <NUM_LIT:3> ) , protocol . RANGE ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT:true>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT:false>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT:abc>' ) <EOL> self . assertRaises ( ValueError , protocol . RANGE , '<STR_LIT>' ) <EOL> class TestCommands ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . commands = protocol . Commands ( ) <EOL> def test_add_as_a_decorator ( self ) : <EOL> @ self . commands . add ( '<STR_LIT:test>' ) <EOL> def test ( context ) : <EOL> pass <EOL> def test_register_second_command_to_same_name_fails ( self ) : <EOL> def func ( context ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( func ) <EOL> with self . assertRaises ( Exception ) : <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( func ) <EOL> def test_function_only_takes_context_succeeds ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context : sentinel ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' ] ) ) <EOL> def test_function_has_required_arg_succeeds ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context , required : sentinel ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT>' ] ) ) <EOL> def test_function_has_optional_args_succeeds ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context , optional = None : sentinel ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' ] ) ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT>' ] ) ) <EOL> def test_function_has_required_and_optional_args_succeeds ( self ) : <EOL> sentinel = object ( ) <EOL> def func ( context , required , optional = None ) : <EOL> return sentinel <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( func ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_function_has_varargs_succeeds ( self ) : <EOL> sentinel , args = object ( ) , [ ] <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context , * args : sentinel ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:bar>' ] + args ) ) <EOL> args . append ( '<STR_LIT:test>' ) <EOL> def test_function_has_only_varags_succeeds ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT>' ) ( lambda * args : sentinel ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT>' ] ) ) <EOL> def test_function_has_no_arguments_fails ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> self . commands . add ( '<STR_LIT:test>' ) ( lambda : True ) <EOL> def test_function_has_required_and_varargs_fails ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> def func ( context , required , * args ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:test>' ) ( func ) <EOL> def test_function_has_optional_and_varargs_fails ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> def func ( context , optional = None , * args ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:test>' ) ( func ) <EOL> def test_function_hash_keywordargs_fails ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> self . commands . add ( '<STR_LIT:test>' ) ( lambda context , ** kwargs : True ) <EOL> def test_call_chooses_correct_handler ( self ) : <EOL> sentinel1 , sentinel2 , sentinel3 = object ( ) , object ( ) , object ( ) <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context : sentinel1 ) <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context : sentinel2 ) <EOL> self . commands . add ( '<STR_LIT>' ) ( lambda context : sentinel3 ) <EOL> self . assertEqual ( sentinel1 , self . commands . call ( [ '<STR_LIT:foo>' ] ) ) <EOL> self . assertEqual ( sentinel2 , self . commands . call ( [ '<STR_LIT:bar>' ] ) ) <EOL> self . assertEqual ( sentinel3 , self . commands . call ( [ '<STR_LIT>' ] ) ) <EOL> def test_call_with_nonexistent_handler ( self ) : <EOL> with self . assertRaises ( exceptions . MpdUnknownCommand ) : <EOL> self . commands . call ( [ '<STR_LIT:bar>' ] ) <EOL> def test_call_passes_context ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context : context ) <EOL> self . assertEqual ( <EOL> sentinel , self . commands . call ( [ '<STR_LIT:foo>' ] , context = sentinel ) ) <EOL> def test_call_without_args_fails ( self ) : <EOL> with self . assertRaises ( exceptions . MpdNoCommand ) : <EOL> self . commands . call ( [ ] ) <EOL> def test_call_passes_required_argument ( self ) : <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context , required : required ) <EOL> self . assertEqual ( '<STR_LIT>' , self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT>' ] ) ) <EOL> def test_call_passes_optional_argument ( self ) : <EOL> sentinel = object ( ) <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context , optional = sentinel : optional ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:foo>' ] ) ) <EOL> self . assertEqual ( '<STR_LIT:test>' , self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT:test>' ] ) ) <EOL> def test_call_passes_required_and_optional_argument ( self ) : <EOL> def func ( context , required , optional = None ) : <EOL> return ( required , optional ) <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( func ) <EOL> self . assertEqual ( ( '<STR_LIT>' , None ) , self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_call_passes_varargs ( self ) : <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context , * args : args ) <EOL> def test_call_incorrect_args ( self ) : <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context : context ) <EOL> with self . assertRaises ( exceptions . MpdArgError ) : <EOL> self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . commands . add ( '<STR_LIT:bar>' ) ( lambda context , required : context ) <EOL> with self . assertRaises ( exceptions . MpdArgError ) : <EOL> self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> self . commands . add ( '<STR_LIT>' ) ( lambda context , optional = None : context ) <EOL> with self . assertRaises ( exceptions . MpdArgError ) : <EOL> self . commands . call ( [ '<STR_LIT>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> def test_validator_gets_applied_to_required_arg ( self ) : <EOL> sentinel = object ( ) <EOL> def func ( context , required ) : <EOL> return required <EOL> self . commands . add ( '<STR_LIT:test>' , required = lambda v : sentinel ) ( func ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:test>' , '<STR_LIT:foo>' ] ) ) <EOL> def test_validator_gets_applied_to_optional_arg ( self ) : <EOL> sentinel = object ( ) <EOL> def func ( context , optional = None ) : <EOL> return optional <EOL> self . commands . add ( '<STR_LIT:foo>' , optional = lambda v : sentinel ) ( func ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:foo>' , '<STR_LIT>' ] ) ) <EOL> def test_validator_skips_optional_default ( self ) : <EOL> sentinel = object ( ) <EOL> def func ( context , optional = sentinel ) : <EOL> return optional <EOL> self . commands . add ( '<STR_LIT:foo>' , optional = lambda v : None ) ( func ) <EOL> self . assertEqual ( sentinel , self . commands . call ( [ '<STR_LIT:foo>' ] ) ) <EOL> def test_validator_applied_to_non_existent_arg_fails ( self ) : <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( lambda context , arg : arg ) <EOL> with self . assertRaises ( TypeError ) : <EOL> def func ( context , wrong_arg ) : <EOL> return wrong_arg <EOL> self . commands . add ( '<STR_LIT:bar>' , arg = lambda v : v ) ( func ) <EOL> def test_validator_called_context_fails ( self ) : <EOL> return <EOL> with self . assertRaises ( TypeError ) : <EOL> def func ( context ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:bar>' , context = lambda v : v ) ( func ) <EOL> def test_validator_value_error_is_converted ( self ) : <EOL> def validdate ( value ) : <EOL> raise ValueError <EOL> def func ( context , arg ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:bar>' , arg = validdate ) ( func ) <EOL> with self . assertRaises ( exceptions . MpdArgError ) : <EOL> self . commands . call ( [ '<STR_LIT:bar>' , '<STR_LIT:test>' ] ) <EOL> def test_auth_required_gets_stored ( self ) : <EOL> def func1 ( context ) : <EOL> pass <EOL> def func2 ( context ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( func1 ) <EOL> self . commands . add ( '<STR_LIT:bar>' , auth_required = False ) ( func2 ) <EOL> self . assertTrue ( self . commands . handlers [ '<STR_LIT:foo>' ] . auth_required ) <EOL> self . assertFalse ( self . commands . handlers [ '<STR_LIT:bar>' ] . auth_required ) <EOL> def test_list_command_gets_stored ( self ) : <EOL> def func1 ( context ) : <EOL> pass <EOL> def func2 ( context ) : <EOL> pass <EOL> self . commands . add ( '<STR_LIT:foo>' ) ( func1 ) <EOL> self . commands . add ( '<STR_LIT:bar>' , list_command = False ) ( func2 ) <EOL> self . assertTrue ( self . commands . handlers [ '<STR_LIT:foo>' ] . list_command ) <EOL> self . assertFalse ( self . commands . handlers [ '<STR_LIT:bar>' ] . list_command ) </s>
