<s> from . compat import unittest <EOL> from mpdlcd import mpdhooks <EOL> class HookRegistryTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> mpdhooks . HookRegistry . _REGISTRY = { } <EOL> def test_register ( self ) : <EOL> class SomeHook ( object ) : <EOL> pass <EOL> mpdhooks . HookRegistry . register_hook ( '<STR_LIT>' , SomeHook ) <EOL> reg = mpdhooks . HookRegistry ( ) <EOL> hook = reg . create ( '<STR_LIT>' ) <EOL> self . assertEqual ( SomeHook , hook . __class__ ) <EOL> def test_create_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> class SomeHook ( object ) : <EOL> def __init__ ( self , foo = <NUM_LIT:2> ) : <EOL> self . foo = foo <EOL> mpdhooks . HookRegistry . register_hook ( '<STR_LIT>' , SomeHook ) <EOL> reg = mpdhooks . HookRegistry ( ) <EOL> hook = reg . create ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , hook . foo ) <EOL> self . assertEqual ( SomeHook , hook . __class__ ) <EOL> hook2 = reg . create ( '<STR_LIT>' , foo = <NUM_LIT:4> ) <EOL> self . assertEqual ( <NUM_LIT:4> , hook2 . foo ) <EOL> self . assertEqual ( SomeHook , hook2 . __class__ ) <EOL> def test_register_hook_decorator ( self ) : <EOL> """<STR_LIT>""" <EOL> @ mpdhooks . register_hook <EOL> class SomeHook ( object ) : <EOL> name = '<STR_LIT>' <EOL> reg = mpdhooks . HookRegistry ( ) <EOL> hook = reg . create ( '<STR_LIT>' ) <EOL> self . assertEqual ( SomeHook , hook . __class__ ) <EOL> def test_register_hook_decorator_noname ( self ) : <EOL> """<STR_LIT>""" <EOL> def failed_registration ( ) : <EOL> @ mpdhooks . register_hook <EOL> class SomeHook ( object ) : <EOL> pass <EOL> self . assertRaises ( AttributeError , <EOL> failed_registration ) <EOL> def test_register_noname ( self ) : <EOL> """<STR_LIT>""" <EOL> class SomeHook ( object ) : <EOL> base_name = '<STR_LIT>' <EOL> self . assertRaises ( mpdhooks . HookRegistryError , <EOL> mpdhooks . HookRegistry . register_hook , '<STR_LIT>' , SomeHook ) <EOL> def test_register_conflict ( self ) : <EOL> """<STR_LIT>""" <EOL> class SomeHook ( object ) : <EOL> pass <EOL> mpdhooks . HookRegistry . register_hook ( '<STR_LIT>' , SomeHook ) <EOL> class AltHook ( object ) : <EOL> pass <EOL> self . assertRaises ( mpdhooks . HookRegistryError , <EOL> mpdhooks . HookRegistry . register_hook , '<STR_LIT>' , AltHook ) <EOL> def test_register_twice_no_conflict ( self ) : <EOL> """<STR_LIT>""" <EOL> class SomeHook ( object ) : <EOL> base_name = '<STR_LIT>' <EOL> mpdhooks . HookRegistry . register_hook ( '<STR_LIT>' , SomeHook ) <EOL> mpdhooks . HookRegistry . register_hook ( '<STR_LIT>' , SomeHook ) <EOL> reg = mpdhooks . HookRegistry ( ) <EOL> hook = reg . create ( '<STR_LIT>' ) <EOL> self . assertEqual ( SomeHook , hook . __class__ ) <EOL> def test_invalid_name ( self ) : <EOL> """<STR_LIT>""" <EOL> reg = mpdhooks . HookRegistry ( ) <EOL> self . assertRaises ( mpdhooks . HookRegistryError , reg . create , '<STR_LIT:foo>' ) <EOL> class MPDHookTest ( unittest . TestCase ) : <EOL> class FakeClient ( object ) : <EOL> def __init__ ( self , data ) : <EOL> self . data = data <EOL> class FakeHook ( mpdhooks . MPDHook ) : <EOL> name = '<STR_LIT>' <EOL> def fetch ( self , client ) : <EOL> return client . data <EOL> class FakeSubHook ( FakeHook ) : <EOL> def extract_key ( self , data , key ) : <EOL> return data [ key ] <EOL> def test_first_change ( self ) : <EOL> client = self . FakeClient ( <NUM_LIT> ) <EOL> hook = self . FakeHook ( ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( <NUM_LIT> , new ) <EOL> def test_repeated_change ( self ) : <EOL> client = self . FakeClient ( <NUM_LIT> ) <EOL> hook = self . FakeHook ( ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( <NUM_LIT> , new ) <EOL> client2 = self . FakeClient ( <NUM_LIT> ) <EOL> changed2 , new2 = hook . handle ( client2 ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( <NUM_LIT> , new2 ) <EOL> def test_unchanged ( self ) : <EOL> client = self . FakeClient ( <NUM_LIT> ) <EOL> hook = self . FakeHook ( ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( <NUM_LIT> , new ) <EOL> changed2 , new2 = hook . handle ( client ) <EOL> self . assertFalse ( changed2 ) <EOL> self . assertIsNone ( new2 ) <EOL> def test_default_subhook ( self ) : <EOL> client = self . FakeClient ( { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> hook = self . FakeSubHook ( ) <EOL> hook . handle ( client ) <EOL> client2 = self . FakeClient ( { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> changed , new = hook . handle ( client2 ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } , new ) <EOL> def test_subhook_first_change ( self ) : <EOL> client = self . FakeClient ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> hook = self . FakeSubHook ( ) <EOL> changed , new = hook . handle ( client , ( '<STR_LIT:x>' , ) ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } , new ) <EOL> def test_subhook_repeated_change ( self ) : <EOL> client = self . FakeClient ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> hook = self . FakeSubHook ( ) <EOL> changed , new = hook . handle ( client , ( '<STR_LIT:x>' , ) ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } , new ) <EOL> client2 = self . FakeClient ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> changed2 , new2 = hook . handle ( client2 , ( '<STR_LIT:x>' , ) ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } , new2 ) <EOL> def test_subhook_unchanged ( self ) : <EOL> client = self . FakeClient ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> hook = self . FakeSubHook ( ) <EOL> changed , new = hook . handle ( client , ( '<STR_LIT:x>' , ) ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } , new ) <EOL> client2 = self . FakeClient ( { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ) <EOL> changed2 , new2 = hook . handle ( client2 , ( '<STR_LIT:x>' , ) ) <EOL> self . assertFalse ( changed2 ) <EOL> self . assertIsNone ( new2 ) <EOL> class SpecificHooksTest ( unittest . TestCase ) : <EOL> class FakeClient ( object ) : <EOL> def __init__ ( self , status = None , state = None , elapsed_and_total = None , current_song = None ) : <EOL> self . status = status <EOL> self . state = state <EOL> self . elapsed_and_total = elapsed_and_total <EOL> self . current_song = current_song <EOL> def test_status_hook ( self ) : <EOL> hook = mpdhooks . StatusHook ( ) <EOL> client = self . FakeClient ( status = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , new ) <EOL> client2 = self . FakeClient ( status = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> changed2 , new2 = hook . handle ( client2 ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , new2 ) <EOL> client3 = self . FakeClient ( state = '<STR_LIT>' , status = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> changed3 , new3 = hook . handle ( client3 ) <EOL> self . assertFalse ( changed3 ) <EOL> self . assertIsNone ( new3 ) <EOL> def test_state_hook ( self ) : <EOL> hook = mpdhooks . StateHook ( ) <EOL> client = self . FakeClient ( state = '<STR_LIT>' ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( '<STR_LIT>' , new ) <EOL> client2 = self . FakeClient ( state = '<STR_LIT>' ) <EOL> changed2 , new2 = hook . handle ( client2 ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( '<STR_LIT>' , new2 ) <EOL> client3 = self . FakeClient ( state = '<STR_LIT>' , status = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> changed3 , new3 = hook . handle ( client3 ) <EOL> self . assertFalse ( changed3 ) <EOL> self . assertIsNone ( new3 ) <EOL> def test_elapsed_and_total_hook ( self ) : <EOL> hook = mpdhooks . ElapsedAndTotalHook ( ) <EOL> client = self . FakeClient ( elapsed_and_total = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertTrue ( changed ) <EOL> self . assertEqual ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , new ) <EOL> client2 = self . FakeClient ( elapsed_and_total = ( <NUM_LIT:0.1> , <NUM_LIT:10> ) ) <EOL> changed2 , new2 = hook . handle ( client2 ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( ( <NUM_LIT:0.1> , <NUM_LIT:10> ) , new2 ) <EOL> client3 = self . FakeClient ( elapsed_and_total = ( <NUM_LIT:0.1> , <NUM_LIT:10> ) , state = '<STR_LIT>' ) <EOL> changed3 , new3 = hook . handle ( client3 ) <EOL> self . assertFalse ( changed3 ) <EOL> self . assertIsNone ( new3 ) <EOL> def test_song_hook ( self ) : <EOL> hook = mpdhooks . SongHook ( ) <EOL> class FakeSong ( object ) : <EOL> def __init__ ( self , id = <NUM_LIT> , ** extra ) : <EOL> self . id = id <EOL> for k , v in extra . items ( ) : <EOL> setattr ( self , k , v ) <EOL> client = self . FakeClient ( current_song = { } ) <EOL> changed , new = hook . handle ( client ) <EOL> self . assertFalse ( changed ) <EOL> self . assertIsNone ( new ) <EOL> new_song = FakeSong ( ) <EOL> client2 = self . FakeClient ( current_song = new_song ) <EOL> changed2 , new2 = hook . handle ( client2 ) <EOL> self . assertTrue ( changed2 ) <EOL> self . assertEqual ( new_song , new2 ) <EOL> other_song = FakeSong ( id = <NUM_LIT> ) <EOL> client3 = self . FakeClient ( current_song = other_song ) <EOL> changed3 , new3 = hook . handle ( client3 ) <EOL> self . assertTrue ( changed3 ) <EOL> self . assertEqual ( other_song , new3 ) <EOL> client4 = self . FakeClient ( current_song = other_song , state = '<STR_LIT>' ) <EOL> changed4 , new4 = hook . handle ( client4 ) <EOL> self . assertFalse ( changed4 ) <EOL> self . assertIsNone ( new4 ) <EOL> other_song_full = FakeSong ( id = <NUM_LIT> , title = "<STR_LIT>" , artist = "<STR_LIT>" ) <EOL> client5 = self . FakeClient ( current_song = other_song_full ) <EOL> changed5 , new5 = hook . handle ( client5 , ( '<STR_LIT:title>' , '<STR_LIT>' ) ) <EOL> self . assertTrue ( changed5 ) <EOL> self . assertEqual ( other_song_full , new5 ) <EOL> other_song_full2 = FakeSong ( id = <NUM_LIT> , title = "<STR_LIT>" , artist = "<STR_LIT>" ) <EOL> client6 = self . FakeClient ( current_song = other_song_full2 ) <EOL> changed6 , new6 = hook . handle ( client6 , ( '<STR_LIT:title>' , '<STR_LIT>' ) ) <EOL> self . assertFalse ( changed6 ) <EOL> self . assertIsNone ( new6 ) <EOL> third_song_full = FakeSong ( id = <NUM_LIT> , title = "<STR_LIT>" , artist = "<STR_LIT>" ) <EOL> client7 = self . FakeClient ( current_song = third_song_full ) <EOL> changed7 , new7 = hook . handle ( client7 , ( '<STR_LIT:title>' , '<STR_LIT>' ) ) <EOL> self . assertTrue ( changed7 ) <EOL> self . assertEqual ( third_song_full , new7 ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
