Package pytilities :: Package test :: Package event :: Module dispatcherswitch
[hide private]
[frames] | no frames]

Source Code for Module pytilities.test.event.dispatcherswitch

 1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
 2  #  
 3  # This file is part of pytilities. 
 4  #  
 5  # pytilities is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  #  
10  # pytilities is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  #  
15  # You should have received a copy of the GNU General Public License 
16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
17  # 
18   
19  __docformat__ = 'reStructuredText' 
20   
21  import unittest 
22   
23  from pytilities.event import ( 
24      DispatcherSwitch, Dispatcher, UnsupportedEventError, RestrictedDispatcher) 
25  from .helpers import Listener 
26   
27 -class InitTestCase(unittest.TestCase):
28 - def test_main(self):
29 self.d = Dispatcher() 30 self.d.register_events("a", "b") 31 self.d1 = RestrictedDispatcher(self.d, disallow=("b",)) 32 33 self.d2 = Dispatcher() 34 self.d2.register_events("a", "c") 35 36 self.d3 = Dispatcher() 37 38 self.s = DispatcherSwitch() 39 self.s.append_dispatchers(self.d1, self.d2, self.d3) 40 41 self.l1 = Listener() 42 self.s.add_handler("a", self.l1.handle_noarg) 43 44 self.l2 = Listener() 45 self.s.add_handler("c", self.l2.handle_noarg) 46 47 self.l3 = Listener() 48 self.d.add_handler("b", self.l3.handle_noarg) 49 50 ### 51 52 self.assert_(self.s.has_event("a")) 53 self.assertFalse(self.s.has_event("b")) 54 self.assert_(self.s.has_event("c")) 55 56 self.assertRaises(UnsupportedEventError, 57 self.s.add_handler,"b", self.l3.handle_noarg) 58 59 self.d1.dispatch("a") 60 self.assert_(self.l1.received_noarg) 61 62 self.d2.dispatch("c") 63 self.assert_(self.l2.received_noarg) 64 65 self.d.dispatch("b") 66 self.assert_(self.l3.received_noarg) 67 68 ### 69 70 self.s.remove_handler("a", self.l1.handle_noarg) 71 72 self.d1.dispatch("a") 73 self.assertFalse(self.l1.received_noarg) 74 75 self.s.remove_handlers() 76 77 self.d2.dispatch("c") 78 self.assertFalse(self.l2.received_noarg) 79 80 self.d.dispatch("b") 81 self.assert_(self.l3.received_noarg)
82