<s> import unittest <EOL> from lymph . core . events import EventDispatcher <EOL> class EventDispatcherTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . dispatcher = EventDispatcher ( ) <EOL> self . handler_log = [ ] <EOL> self . handlers = { } <EOL> def make_handler ( self , name ) : <EOL> if name in self . handlers : <EOL> return self . handlers [ name ] <EOL> def handler ( * args ) : <EOL> self . handler_log . append ( ( name , args ) ) <EOL> handler . __name__ = name <EOL> self . handlers [ name ] = handler <EOL> return handler <EOL> def assert_dispatched_patterns_equal ( self , event_type , patterns ) : <EOL> self . assertEqual ( <EOL> set ( pattern for pattern , handler in self . dispatcher . dispatch ( event_type ) ) , <EOL> set ( patterns ) , <EOL> ) <EOL> def assert_dispatched_handlers_equal ( self , event_type , handlers ) : <EOL> self . assertEqual ( <EOL> set ( handler . __name__ for pattern , handler in self . dispatcher . dispatch ( event_type ) ) , <EOL> set ( handlers ) , <EOL> ) <EOL> def test_basic_dispatch ( self ) : <EOL> self . dispatcher . register ( '<STR_LIT:foo>' , self . make_handler ( '<STR_LIT:foo>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT:bar>' , self . make_handler ( '<STR_LIT:bar>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT:foo>' , { '<STR_LIT:foo>' } ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT:bar>' , { '<STR_LIT:bar>' } ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , [ ] ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , [ ] ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , [ ] ) <EOL> def test_wildcard_dispatch ( self ) : <EOL> self . dispatcher . register ( '<STR_LIT:foo>' , self . make_handler ( '<STR_LIT:foo>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT:#>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT:*>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT:foo>' , { '<STR_LIT:foo>' , '<STR_LIT:*>' , '<STR_LIT:#>' } ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , { '<STR_LIT:#>' , '<STR_LIT>' , '<STR_LIT>' } ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , { '<STR_LIT:#>' , '<STR_LIT>' } ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT>' , { '<STR_LIT:#>' } ) <EOL> def test_multi_pattern_registration ( self ) : <EOL> self . dispatcher . register ( '<STR_LIT:foo>' , self . make_handler ( '<STR_LIT:foo>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT:#>' , self . make_handler ( '<STR_LIT:foo>' ) ) <EOL> self . assert_dispatched_patterns_equal ( '<STR_LIT:foo>' , { '<STR_LIT:foo>' , '<STR_LIT:#>' } ) <EOL> self . assert_dispatched_handlers_equal ( '<STR_LIT:foo>' , { '<STR_LIT:foo>' } ) <EOL> def test_update ( self ) : <EOL> self . dispatcher . register ( '<STR_LIT:foo>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> self . dispatcher . register ( '<STR_LIT:#>' , self . make_handler ( '<STR_LIT>' ) ) <EOL> ed = EventDispatcher ( ) <EOL> ed . register ( '<STR_LIT:foo>' , self . make_handler ( '<STR_LIT:foo>' ) ) <EOL> ed . register ( '<STR_LIT:bar>' , self . make_handler ( '<STR_LIT:bar>' ) ) <EOL> self . dispatcher . update ( ed ) <EOL> self . assert_dispatched_handlers_equal ( '<STR_LIT:foo>' , { '<STR_LIT:foo>' , '<STR_LIT>' , '<STR_LIT>' } ) <EOL> self . assert_dispatched_handlers_equal ( '<STR_LIT:bar>' , { '<STR_LIT>' , '<STR_LIT:bar>' } ) </s>
