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

Source Code for Module pytilities.test.event.dispatcher

  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 Dispatcher, UnsupportedEventError 
 24  from .helpers import Listener 
 25   
26 -class InitTestCase(unittest.TestCase):
27 - def setUp(self):
28 self.l = Listener() 29 self.ed = Dispatcher()
30
31 - def test_fail_no_events(self):
32 self.assertFalse(self.ed.has_event("a")) 33 self.assertRaises(UnsupportedEventError, self.ed.add_handler, "a", 34 self.l.handle_noarg) 35 self.assertRaises(UnsupportedEventError, self.ed.remove_handler, "a", 36 self.l.handle_noarg) 37 self.assertRaises(UnsupportedEventError, self.ed.dispatch, "a")
38
39 - def test_success(self):
40 self.ed.register_events("a", "b") 41 42 self.assert_(self.ed.has_event("b")) 43 self.assert_(self.ed.has_event("b")) 44 45 self.ed.add_handler("a", self.l.handle_noarg) 46 self.ed.dispatch("a") 47 48 #has handler a 49 self.assertEquals(self.l.last, "noarg") 50 51 self.ed.add_handler("b", self.l.handle_1arg) 52 self.ed.dispatch("b", 5) 53 54 #has handler a and b 55 self.assertEquals(self.l.last, "1arg: 5") 56 57 self.l.last = "" 58 self.ed.remove_handler("a", self.l.handle_noarg) 59 self.ed.dispatch("a") 60 61 #has handler b 62 self.assertNotEquals(self.l.last, "noarg") 63 64 self.ed.add_handler("a", self.l.handle_noarg) 65 self.ed.remove_handlers("a") 66 self.ed.dispatch("a") 67 68 self.assertNotEquals(self.l.last, "noarg") 69 70 self.ed.dispatch("b", 5) 71 72 self.assertEquals(self.l.last, "1arg: 5") 73 74 self.l.last = "" 75 self.ed.add_handler("a", self.l.handle_noarg) 76 self.ed.remove_handlers() 77 self.ed.dispatch("a") 78 79 # has no handlers left 80 self.assertNotEquals(self.l.last, "noarg") 81 82 self.ed.dispatch("b", 5) 83 84 self.assertNotEquals(self.l.last, "1arg: 5")
85
86 - def test_success_owner(self):
87 self.ed.register_events("a", "b") 88 89 self.ed.add_handler("a", self.l.handle_noarg, self.l) 90 self.ed.dispatch("a") 91 92 #has handler a 93 self.assertEquals(self.l.last, "noarg") 94 95 self.ed.add_handler("b", self.l.handle_1arg) 96 self.ed.dispatch("b", 5) 97 98 #has handler a and b 99 self.assertEquals(self.l.last, "1arg: 5") 100 101 self.l.last = "" 102 self.ed.remove_handlers(owner=self.l) 103 self.ed.dispatch("a") 104 105 #has handler b 106 self.assertNotEquals(self.l.last, "noarg") 107 108 self.ed.dispatch("b", 5) 109 110 self.assertEquals(self.l.last, "1arg: 5")
111