Back to index

python-cliapp  1.20120630
hookmgr_tests.py
Go to the documentation of this file.
00001 # Copyright (C) 2009-2012  Lars Wirzenius
00002 # 
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License along
00014 # with this program; if not, write to the Free Software Foundation, Inc.,
00015 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 
00017 
00018 import unittest
00019 
00020 from cliapp import HookManager, FilterHook
00021 
00022 
00023 class HookManagerTests(unittest.TestCase):
00024 
00025     def setUp(self):
00026         self.hooks = HookManager()
00027         self.hooks.new('foo', FilterHook())
00028         
00029     def callback(self, *args, **kwargs):
00030         self.args = args
00031         self.kwargs = kwargs
00032 
00033     def test_has_no_tests_initially(self):
00034         hooks = HookManager()
00035         self.assertEqual(hooks.hooks, {})
00036         
00037     def test_adds_new_hook(self):
00038         self.assert_(self.hooks.hooks.has_key('foo'))
00039         
00040     def test_adds_callback(self):
00041         self.hooks.add_callback('foo', self.callback)
00042         self.assertEqual(self.hooks.hooks['foo'].callbacks, [self.callback])
00043 
00044     def test_removes_callback(self):
00045         cb_id = self.hooks.add_callback('foo', self.callback)
00046         self.hooks.remove_callback('foo', cb_id)
00047         self.assertEqual(self.hooks.hooks['foo'].callbacks, [])
00048 
00049     def test_calls_callbacks(self):
00050         self.hooks.add_callback('foo', self.callback)
00051         self.hooks.call('foo', 'bar', kwarg='foobar')
00052         self.assertEqual(self.args, ('bar',))
00053         self.assertEqual(self.kwargs, { 'kwarg': 'foobar' })
00054 
00055     def test_call_returns_value_of_callbacks(self):
00056         self.hooks.new('bar', FilterHook())
00057         self.hooks.add_callback('bar', lambda data: data + 1)
00058         self.assertEqual(self.hooks.call('bar', 1), 2)
00059