pytilities.event.test.decorators

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

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