Added ToggleDictionary class and modified the tests to better model the

specification
This commit is contained in:
James Vega 2003-10-28 00:52:27 +00:00
parent e03c65f753
commit ad022a88ed
2 changed files with 49 additions and 12 deletions

View File

@ -46,7 +46,7 @@ import utils
import world
import ircutils
__all__ = ['ChannelDBHandler', 'PeriodicFileDownloader']
__all__ = ['ChannelDBHandler', 'PeriodicFileDownloader', 'ToggleDictionary']
class ChannelDBHandler(object):
"""A class to handle database stuff for individual channels transparently.
@ -188,5 +188,42 @@ class PeriodicFileDownloader(object):
t.start()
world.threadsSpawned += 1
class ToggleDictionary(object):
"""I am ToggleDictionary! Hear me roar!
"""
def __init__(self, toggles):
if toggles.keys() == []:
raise ValueError
self.toggles = {}
self.toggles['Default'] = toggles
def get(self, key, channel='Default'):
if channel != 'Default':
try:
return self.toggles[channel][key]
except KeyError:
return self.toggles['Default'][key]
else:
return self.toggles[channel][key]
def toggle(self, key, value=None, channel='Default'):
if value is not None and value != True and value != False:
raise ValueError
if not self.toggles.has_key(channel):
self.toggles[channel] = self.toggles['Default'].copy()
if value is None:
self.toggles[channel][key] = not self.toggles[channel][key]
else:
self.toggles[channel][key] = value
def toString(self, channel='Default'):
resp = []
for k,v in self.toggles[channel].iteritems():
if v:
resp.append('%s: On' % k)
else:
resp.append('%s: Off' % k)
resp.sort()
return '(%s)' % '; '.join(resp)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -44,10 +44,11 @@ class ToggleDictionaryTestCase(unittest.TestCase):
self.assertEqual(t.get('foo', '#baz'), True)
t.toggle('foo', channel='#baz')
self.assertEqual(t.get('foo', '#baz'), False)
self.assertRaises(ValueError, t.toggle, 'foo', value='lak')
def test__init__(self):
self.assertRaises(TypeError, plugins.ToggleDictionary.__init__)
self.assertRaises(ValueError, plugins.ToggleDictionary.__init__, {})
self.assertRaises(ValueError, plugins.ToggleDictionary, {})
def testToggle(self):
t = plugins.ToggleDictionary({'foo': True})
@ -56,15 +57,14 @@ class ToggleDictionaryTestCase(unittest.TestCase):
self.assertEqual(t.get('bar'), False)
def testToString(self):
t = plugins.ToggleDictionary({'foo': True})
self.assertEqual(t.toString(), '(foo: On)')
t.toggle('foo')
self.assertEqual(t.toString(), '(foo: Off)')
t.toggle('bar', value=True)
self.assertEqual(t.toString(), '(bar: On, foo: Off)')
t.toggle('baz', value=True)
self.assertEqual(t.toString(), '(bar: On, baz: On, foo: Off)')
t.toggle('baz', channel='#foo')
t = plugins.ToggleDictionary({'foo': True, 'bar': False})
self.assertEqual(t.toString(), '(bar: Off; foo: On)')
t.toggle('foo', channel='#foo')
self.assertEqual(t.toString(), '(bar: Off; foo: On)')
self.assertEqual(t.toString(channel='#foo'),
'(bar: On, baz: Off, foo: Off)')
'(bar: Off; foo: Off)')
t.toggle('bar', value=True)
self.assertEqual(t.toString(), '(bar: On; foo: On)')
self.assertEqual(t.toString(channel='#foo'),
'(bar: Off; foo: Off)')