Made sure commaAndify raises TypeError when it's given lists of non-strings.

This commit is contained in:
Jeremy Fincher 2003-10-28 05:10:03 +00:00
parent 5750480714
commit eaa15860d8
2 changed files with 9 additions and 2 deletions

View File

@ -256,9 +256,10 @@ def commaAndify(seq, And='and'):
if len(L) == 0: if len(L) == 0:
return '' return ''
elif len(L) == 1: elif len(L) == 1:
return L[0] return ''.join(L) # We need this because it raises TypeError.
elif len(L) == 2: elif len(L) == 2:
return '%s %s %s' % (L[0], And, L[1]) L.insert(1, And)
return ' '.join(L)
else: else:
L[-1] = '%s %s' % (And, L[-1]) L[-1] = '%s %s' % (And, L[-1])
return ', '.join(L) return ', '.join(L)

View File

@ -180,6 +180,12 @@ class UtilsTest(unittest.TestCase):
self.assertEqual(L, original) self.assertEqual(L, original)
self.failUnless(utils.commaAndify(sets.Set(L))) self.failUnless(utils.commaAndify(sets.Set(L)))
def testCommaAndifyRaisesTypeError(self):
L = [(2,)]
self.assertRaises(TypeError, utils.commaAndify, L)
L.append((3,))
self.assertRaises(TypeError, utils.commaAndify, L)
def testUnCommaThe(self): def testUnCommaThe(self):
self.assertEqual(utils.unCommaThe('foo bar'), 'foo bar') self.assertEqual(utils.unCommaThe('foo bar'), 'foo bar')
self.assertEqual(utils.unCommaThe('foo bar, the'), 'the foo bar') self.assertEqual(utils.unCommaThe('foo bar, the'), 'the foo bar')