Back to index

python-cliapp  1.20120630
hook_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 Hook, FilterHook
00021 
00022 
00023 class HookTests(unittest.TestCase):
00024 
00025     def setUp(self):
00026         self.hook = Hook()
00027         
00028     def callback(self, *args, **kwargs):
00029         self.args = args
00030         self.kwargs = kwargs
00031 
00032     def test_has_no_callbacks_by_default(self):
00033         self.assertEqual(self.hook.callbacks, [])
00034         
00035     def test_adds_callback(self):
00036         self.hook.add_callback(self.callback)
00037         self.assertEqual(self.hook.callbacks, [self.callback])
00038         
00039     def test_adds_callback_only_once(self):
00040         self.hook.add_callback(self.callback)
00041         self.hook.add_callback(self.callback)
00042         self.assertEqual(self.hook.callbacks, [self.callback])
00043 
00044     def test_calls_callback(self):
00045         self.hook.add_callback(self.callback)
00046         self.hook.call_callbacks('bar', kwarg='foobar')
00047         self.assertEqual(self.args, ('bar',))
00048         self.assertEqual(self.kwargs, { 'kwarg': 'foobar' })
00049 
00050     def test_removes_callback(self):
00051         cb_id = self.hook.add_callback(self.callback)
00052         self.hook.remove_callback(cb_id)
00053         self.assertEqual(self.hook.callbacks, [])
00054 
00055 
00056 class FilterHookTests(unittest.TestCase):
00057 
00058     def setUp(self):
00059         self.hook = FilterHook()
00060 
00061     def callback(self, data, *args, **kwargs):
00062         self.args = args
00063         self.kwargs = kwargs
00064         return data + ['callback']
00065 
00066     def test_returns_argument_if_no_callbacks(self):
00067         self.assertEqual(self.hook.call_callbacks(['foo']), ['foo'])
00068         
00069     def test_calls_callback_and_returns_modified_data(self):
00070         self.hook.add_callback(self.callback)
00071         data = self.hook.call_callbacks([])
00072         self.assertEqual(data, ['callback'])
00073 
00074     def test_calls_callback_with_extra_args(self):
00075         self.hook.add_callback(self.callback)
00076         self.hook.call_callbacks(['data'], 'extra', kwextra='kwextra')
00077         self.assertEqual(self.args, ('extra',))
00078         self.assertEqual(self.kwargs, { 'kwextra': 'kwextra' })
00079