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

Source Code for Module pytilities.test.event.decorators

  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  # TODO this works! awesome, start doing this more often in test packages. Also 
 24  # do a from . in all other places where applicable. In all other cases, use 
 25  # absolute paths 
 26  from pytilities.event import dispatcher, dispatcher_switch, Dispatcher 
 27  from pytilities import AttributeCollectionBase 
 28  from .helpers import Listener 
29 30 @dispatcher("a", "b") 31 -class ADispatcher(AttributeCollectionBase):
32 - def __init__(self, arg):
33 AttributeCollectionBase.__init__(self) 34 self.inited = True 35 self.arg = arg 36 37 # just checking wheter this exists, I don't care about the event 38 self.__dispatcher.has_event("a")
39
40 - def dispatch_a(self):
41 self.__dispatch("a")
42
43 - def dispatch_b(self):
44 self.__dispatch("b", 5)
45
46 -class BDispatcher(ADispatcher):
47 - def __init__(self, arg):
48 ADispatcher.__init__(self, arg)
49
50 -class DispatcherTestCase(unittest.TestCase):
51 - def setUp(self):
52 self.l = Listener() 53 self.d = ADispatcher(2)
54
55 - def test_success(self):
56 self.assert_(self.d.inited) 57 self.assertEquals(self.d.arg, 2) 58 self.assert_(self.d.has_event("a")) 59 self.assert_(self.d.has_event("b")) 60 61 self.d.add_handler("a", self.l.handle_noarg) 62 self.d.add_handler("b", self.l.handle_1arg) 63 self.d.dispatch_a() 64 65 self.assertEquals(self.l.read(), "noarg") 66 67 self.d.dispatch_b() 68 self.assertEquals(self.l.read(), "1arg: 5")
69
70 71 -class BDispatcherTestCase(DispatcherTestCase):
72 - def setUp(self):
73 self.l = Listener() 74 self.d = BDispatcher(2)
75
76 77 @dispatcher_switch 78 -class Switch(AttributeCollectionBase):
79 - def __init__(self, arg):
80 AttributeCollectionBase.__init__(self) 81 82 self.inited = True 83 self.arg = arg 84 85 # just checking wheter this exists, I don't care about the event 86 self.__dispatcher_switch.has_event("a") 87 88 d = Dispatcher() 89 d.register_events("a", "b") 90 self.__dispatcher_switch.append_dispatchers(d) 91 92 self.__dispatcher = d
93
94 - def dispatch_a(self):
95 self.__dispatcher.dispatch("a")
96
97 - def dispatch_b(self):
98 self.__dispatcher.dispatch("b", 5)
99
100 101 -class BSwitch(Switch):
102 - def __init__(self, arg):
103 Switch.__init__(self, arg)
104
105 106 -class SwitchTestCase(unittest.TestCase):
107 - def setUp(self):
108 self.l = Listener() 109 self.d = Switch(2)
110
111 - def test_success(self):
112 self.assert_(self.d.inited) 113 self.assertEquals(self.d.arg, 2) 114 115 self.d.add_handler("a", self.l.handle_noarg) 116 self.d.add_handler("b", self.l.handle_1arg) 117 self.d.dispatch_a() 118 119 self.assertEquals(self.l.read(), "noarg") 120 121 self.d.dispatch_b() 122 self.assertEquals(self.l.read(), "1arg: 5")
123
124 125 -class BSwitchTestCase(unittest.TestCase):
126 - def setUp(self):
127 self.l = Listener() 128 self.d = BSwitch(2)
129