mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-03 16:39:25 +01:00
Added ToggleDictionary class and modified the tests to better model the
specification
This commit is contained in:
parent
e03c65f753
commit
ad022a88ed
@ -46,7 +46,7 @@ import utils
|
|||||||
import world
|
import world
|
||||||
import ircutils
|
import ircutils
|
||||||
|
|
||||||
__all__ = ['ChannelDBHandler', 'PeriodicFileDownloader']
|
__all__ = ['ChannelDBHandler', 'PeriodicFileDownloader', 'ToggleDictionary']
|
||||||
|
|
||||||
class ChannelDBHandler(object):
|
class ChannelDBHandler(object):
|
||||||
"""A class to handle database stuff for individual channels transparently.
|
"""A class to handle database stuff for individual channels transparently.
|
||||||
@ -188,5 +188,42 @@ class PeriodicFileDownloader(object):
|
|||||||
t.start()
|
t.start()
|
||||||
world.threadsSpawned += 1
|
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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
@ -44,10 +44,11 @@ class ToggleDictionaryTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(t.get('foo', '#baz'), True)
|
self.assertEqual(t.get('foo', '#baz'), True)
|
||||||
t.toggle('foo', channel='#baz')
|
t.toggle('foo', channel='#baz')
|
||||||
self.assertEqual(t.get('foo', '#baz'), False)
|
self.assertEqual(t.get('foo', '#baz'), False)
|
||||||
|
self.assertRaises(ValueError, t.toggle, 'foo', value='lak')
|
||||||
|
|
||||||
def test__init__(self):
|
def test__init__(self):
|
||||||
self.assertRaises(TypeError, plugins.ToggleDictionary.__init__)
|
self.assertRaises(TypeError, plugins.ToggleDictionary.__init__)
|
||||||
self.assertRaises(ValueError, plugins.ToggleDictionary.__init__, {})
|
self.assertRaises(ValueError, plugins.ToggleDictionary, {})
|
||||||
|
|
||||||
def testToggle(self):
|
def testToggle(self):
|
||||||
t = plugins.ToggleDictionary({'foo': True})
|
t = plugins.ToggleDictionary({'foo': True})
|
||||||
@ -56,15 +57,14 @@ class ToggleDictionaryTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(t.get('bar'), False)
|
self.assertEqual(t.get('bar'), False)
|
||||||
|
|
||||||
def testToString(self):
|
def testToString(self):
|
||||||
t = plugins.ToggleDictionary({'foo': True})
|
t = plugins.ToggleDictionary({'foo': True, 'bar': False})
|
||||||
self.assertEqual(t.toString(), '(foo: On)')
|
self.assertEqual(t.toString(), '(bar: Off; foo: On)')
|
||||||
t.toggle('foo')
|
t.toggle('foo', channel='#foo')
|
||||||
self.assertEqual(t.toString(), '(foo: Off)')
|
self.assertEqual(t.toString(), '(bar: Off; foo: On)')
|
||||||
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')
|
|
||||||
self.assertEqual(t.toString(channel='#foo'),
|
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)')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user