pytilities.test.event.restricteddispatcher

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

# Source Code for Module pytilities.test.event.restricteddispatcher

     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, RestrictedDispatcher, UnsupportedEventError
    24  from .helpers import Listener
    25
    26 -class AllowTestCase(unittest.TestCase):
    27      '''Using the allow arg'''
    28 -    def setUp(self):
    29          self.l = Listener()
    30
    31          d = Dispatcher()
    32          d.register_events("a", "b")
    33          self.ed = RestrictedDispatcher(d, allow=("b",))
    34
    35 -    def test_fails(self):
    36          self.assertFalse(self.ed.has_event("a"))
    37          self.assertFalse(self.ed.has_event("c"))
    38          self.assertRaises(UnsupportedEventError, self.ed.add_handler, "a",
    39                            self.l.handle_noarg)
    40          self.assertRaises(UnsupportedEventError, self.ed.remove_handler, "a",
    41                            self.l.handle_noarg)
    42          self.assertRaises(UnsupportedEventError, self.ed.dispatch, "a")
    43
    44 -    def test_success(self):
    45          self.assert_(self.ed.has_event("b"))
    46
    47          self.ed.add_handler("b", self.l.handle_noarg)
    48          self.ed.dispatch("b")
    49
    50          #has handler b
    51          self.assertEquals(self.l.last, "noarg")
    52
    53 -class DisallowTestCase(unittest.TestCase):
    54      '''Using the disallow arg'''
    55 -    def setUp(self):
    56          self.l = Listener()
    57
    58          d = Dispatcher()
    59          d.register_events("a", "b")
    60          self.ed = RestrictedDispatcher(d, disallow=("a",))
    61
    62 -    def test_fails(self):
    63          self.assertFalse(self.ed.has_event("a"))
    64          self.assertFalse(self.ed.has_event("c"))
    65          self.assertRaises(UnsupportedEventError, self.ed.add_handler, "a",
    66                            self.l.handle_noarg)
    67          self.assertRaises(UnsupportedEventError, self.ed.remove_handler, "a",
    68                            self.l.handle_noarg)
    69          self.assertRaises(UnsupportedEventError, self.ed.dispatch, "a")
    70
    71 -    def test_success(self):
    72          self.assert_(self.ed.has_event("b"))
    73
    74          self.ed.add_handler("b", self.l.handle_noarg)
    75          self.ed.dispatch("b")
    76
    77          #has handler b
    78          self.assertEquals(self.l.last, "noarg")
    79