<s> from __future__ import unicode_literals , print_function , division <EOL> __author__ = "<STR_LIT>" <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> from ezodf . observer import Observer <EOL> class Listener : <EOL> msg = '<STR_LIT>' <EOL> def __init__ ( self , xmlnode = None ) : <EOL> self . xmlnode = xmlnode <EOL> def on_save_handler ( self , msg ) : <EOL> self . msg = '<STR_LIT>' <EOL> def get_xmlroot ( self ) : <EOL> return self . xmlnode <EOL> class ListenerWithoutGetXMLRootMethod : <EOL> msg = '<STR_LIT>' <EOL> def on_save_handler ( self , msg ) : <EOL> self . msg = '<STR_LIT>' <EOL> class TestObserver ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . observer = Observer ( ) <EOL> def test_count_listener ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , self . observer . _count_listeners ( '<STR_LIT:test>' ) ) <EOL> def test_can_subscribe_with_no_get_root ( self ) : <EOL> listerner = ListenerWithoutGetXMLRootMethod ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listerner ) <EOL> self . assertTrue ( self . observer . _has_listener ( '<STR_LIT>' ) ) <EOL> def test_multiple_listeners ( self ) : <EOL> L1 = Listener ( ) <EOL> L2 = Listener ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , L1 ) <EOL> self . observer . subscribe ( '<STR_LIT>' , L2 ) <EOL> self . assertEqual ( self . observer . _count_listeners ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> def test_subscribe_event ( self ) : <EOL> listener = Listener ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> self . assertTrue ( self . observer . _has_listener ( '<STR_LIT>' ) ) <EOL> def test_subscribe_without_event_handler ( self ) : <EOL> listener = Listener ( ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> def test_unsubscribe_existing_event ( self ) : <EOL> listener = Listener ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> self . observer . unsubscribe ( '<STR_LIT>' , listener ) <EOL> self . assertFalse ( self . observer . _has_listener ( '<STR_LIT>' ) ) <EOL> def test_unsubscribe_not_existing_event ( self ) : <EOL> listener = Listener ( ) <EOL> with self . assertRaises ( KeyError ) : <EOL> self . observer . unsubscribe ( '<STR_LIT>' , listener ) <EOL> def test_unsubscribe_not_existing_listener ( self ) : <EOL> listener = Listener ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> with self . assertRaises ( KeyError ) : <EOL> self . observer . unsubscribe ( '<STR_LIT>' , self ) <EOL> def test_broadcast ( self ) : <EOL> listener = Listener ( ) <EOL> self . assertEqual ( listener . msg , '<STR_LIT>' ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> self . observer . broadcast ( event = '<STR_LIT>' , msg = self ) <EOL> self . assertEqual ( listener . msg , '<STR_LIT>' ) <EOL> def test_broadcast_without_listeners_is_ok ( self ) : <EOL> self . observer . broadcast ( event = '<STR_LIT>' , msg = self ) <EOL> self . assertTrue ( True ) <EOL> def test_broadcast_to_destroyed_listeners ( self ) : <EOL> listener = Listener ( ) <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> del listener <EOL> self . assertEqual ( self . observer . _count_listeners ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> self . observer . broadcast ( event = '<STR_LIT>' , msg = self ) <EOL> def test_broadcast_to_different_roots ( self ) : <EOL> root1 = object ( ) <EOL> root2 = object ( ) <EOL> listeners = [ Listener ( root1 ) , Listener ( root2 ) , ListenerWithoutGetXMLRootMethod ( ) ] <EOL> for listener in listeners : <EOL> self . observer . subscribe ( '<STR_LIT>' , listener ) <EOL> self . observer . broadcast ( event = '<STR_LIT>' , msg = self , root = root1 ) <EOL> self . assertEqual ( listeners [ <NUM_LIT:0> ] . msg , '<STR_LIT>' ) <EOL> self . assertEqual ( listeners [ <NUM_LIT:1> ] . msg , '<STR_LIT>' ) <EOL> self . assertEqual ( listeners [ <NUM_LIT:2> ] . msg , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
