Back to index

python-cliapp  1.20120630
pluginmgr_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 PluginManager
00021 
00022 
00023 class PluginManagerInitialStateTests(unittest.TestCase):
00024 
00025     def setUp(self):
00026         self.pm = PluginManager()
00027     
00028     def test_locations_is_empty_list(self):
00029         self.assertEqual(self.pm.locations, [])
00030 
00031     def test_plugins_is_empty_list(self):
00032         self.assertEqual(self.pm.plugins, [])
00033 
00034     def test_application_version_is_zeroes(self):
00035         self.assertEqual(self.pm.application_version, '0.0.0')
00036 
00037     def test_plugin_files_is_empty(self):
00038         self.assertEqual(self.pm.plugin_files, [])
00039 
00040     def test_plugin_arguments_is_empty(self):
00041         self.assertEqual(self.pm.plugin_arguments, [])
00042         
00043     def test_plugin_keyword_arguments_is_empty(self):
00044         self.assertEqual(self.pm.plugin_keyword_arguments, {})
00045 
00046 
00047 class PluginManagerTests(unittest.TestCase):
00048 
00049     def setUp(self):
00050         self.pm = PluginManager()
00051         self.pm.locations = ['test-plugins', 'not-exist']
00052         self.pm.plugin_arguments = ('fooarg',)
00053         self.pm.plugin_keyword_arguments = { 'bar': 'bararg' }
00054         
00055         self.files = sorted(['test-plugins/hello_plugin.py',
00056                              'test-plugins/aaa_hello_plugin.py',
00057                              'test-plugins/oldhello_plugin.py',
00058                              'test-plugins/wrongversion_plugin.py'])
00059 
00060     def test_finds_the_right_plugin_files(self):
00061         self.assertEqual(self.pm.find_plugin_files(), self.files)
00062 
00063     def test_plugin_files_attribute_implicitly_searches(self):
00064         self.assertEqual(self.pm.plugin_files, self.files)
00065 
00066     def test_loads_hello_plugin(self):
00067         plugins = self.pm.load_plugins()
00068         self.assertEqual(len(plugins), 1)
00069         self.assertEqual(plugins[0].name, 'Hello')
00070 
00071     def test_plugins_attribute_implicitly_searches(self):
00072         self.assertEqual(len(self.pm.plugins), 1)
00073         self.assertEqual(self.pm.plugins[0].name, 'Hello')
00074 
00075     def test_initializes_hello_with_correct_args(self):
00076         plugin = self.pm['Hello']
00077         self.assertEqual(plugin.foo, 'fooarg')
00078         self.assertEqual(plugin.bar, 'bararg')
00079 
00080     def test_raises_keyerror_for_unknown_plugin(self):
00081         self.assertRaises(KeyError, self.pm.__getitem__, 'Hithere')
00082 
00083     def test_enable_plugins_enables_all_plugins(self):
00084         enabled = set()
00085         for plugin in self.pm.plugins:
00086             plugin.enable = lambda: enabled.add(plugin)
00087         self.pm.enable_plugins()
00088         self.assertEqual(enabled, set(self.pm.plugins))
00089 
00090     def test_disable_plugins_disables_all_plugins(self):
00091         disabled = set()
00092         for plugin in self.pm.plugins:
00093             plugin.disable = lambda: disabled.add(plugin)
00094         self.pm.disable_plugins()
00095         self.assertEqual(disabled, set(self.pm.plugins))
00096 
00097 
00098 class PluginManagerCompatibleApplicationVersionTests(unittest.TestCase):
00099 
00100     def setUp(self):
00101         self.pm = PluginManager()
00102         self.pm.application_version = '1.2.3'
00103         
00104     def test_rejects_zero(self):
00105         self.assertFalse(self.pm.compatible_version('0'))
00106         
00107     def test_rejects_two(self):
00108         self.assertFalse(self.pm.compatible_version('2'))
00109         
00110     def test_rejects_one_two_four(self):
00111         self.assertFalse(self.pm.compatible_version('1.2.4'))
00112         
00113     def test_accepts_one(self):
00114         self.assert_(self.pm.compatible_version('1'))
00115         
00116     def test_accepts_one_two_three(self):
00117         self.assert_(self.pm.compatible_version('1.2.3'))
00118