Fixed some mircColors indiscretions and added a test for the new (proper) behavior.

This commit is contained in:
Jeremy Fincher 2003-07-25 04:00:38 +00:00
parent fb730d72a1
commit 7a281d5e4b
2 changed files with 34 additions and 29 deletions

View File

@ -199,38 +199,39 @@ def bold(s):
mircColors = {
None: '',
'white': '0',
'black': '1',
'blue': '2',
'green': '3',
'red': '4',
'brown': '5',
'purple': '6',
'orange': '7',
'yellow': '8',
'light green': '9',
'teal': '10',
'light blue': '11',
'dark blue': '12',
'pink': '13',
'dark grey': '14',
'light grey': '15',
'white': 0,
'black': 1,
'blue': 2,
'green': 3,
'red': 4,
'brown': 5,
'purple': 6,
'orange': 7,
'yellow': 8,
'light green': 9,
'teal': 10,
'light blue': 11,
'dark blue': 12,
'pink': 13,
'dark grey': 14,
'light grey': 15,
}
for (k, v) in mircColors.items():
if v: # Ignore empty string for None.
i = int(v)
mircColors[v] = i
mircColors[i] = i
if k is not None: # Ignore empty string for None.
mircColors[v] = k
def mircColor(s, fg=None, bg=None):
"""Returns s, with the appropriate mIRC color codes applied."""
if fg is None and bg is None:
return s
elif bg is None:
return '\x03%s%s\x03' % (mircColors[fg], s)
fg = mircColors[fg]
if bg is None:
return '\x03%s%s\x03' % (fg, s)
else:
return '\x03%s,%s%s\x03' % (mircColors[fg], mircColors[bg], s)
if isinstance(bg, str):
bg = mircColors[bg]
return '\x03%s,%s%s\x03' % (fg, bg, s)
def isValidArgument(s):

View File

@ -76,18 +76,22 @@ class FunctionsTestCase(unittest.TestCase):
s = 'foo'
self.assertEqual(s, ircutils.mircColor(s))
# Test positional args
self.assertEqual('\x030foo\x03',
ircutils.mircColor(s, 'white'))
self.assertEqual('\x031,2foo\x03',
ircutils.mircColor(s, 'black', 'blue'))
self.assertEqual('\x03,3foo\x03',
ircutils.mircColor(s, None, 'green'))
self.assertEqual('\x030foo\x03', ircutils.mircColor(s, 'white'))
self.assertEqual('\x031,2foo\x03',ircutils.mircColor(s,'black','blue'))
self.assertEqual('\x03,3foo\x03', ircutils.mircColor(s, None, 'green'))
# Test keyword args
self.assertEqual('\x034foo\x03', ircutils.mircColor(s, fg='red'))
self.assertEqual('\x03,5foo\x03', ircutils.mircColor(s, bg='brown'))
self.assertEqual('\x036,7foo\x03',
ircutils.mircColor(s, bg='orange', fg='purple'))
def testMircColors(self):
# Make sure all (k, v) pairs are also (v, k) pairs.
for (k, v) in ircutils.mircColors.items():
if k:
self.assertEqual(ircutils.mircColors[v], k)
def testSafeArgument(self):
s = 'I have been running for 9 seconds'
bolds = ircutils.bold(s)