From eaa15860d8c4a32d31bcac9926d3be2a17b4404b Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 28 Oct 2003 05:10:03 +0000 Subject: [PATCH] Made sure commaAndify raises TypeError when it's given lists of non-strings. --- src/utils.py | 5 +++-- test/test_utils.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/utils.py b/src/utils.py index 2f2c3adaf..a34c9a009 100755 --- a/src/utils.py +++ b/src/utils.py @@ -256,9 +256,10 @@ def commaAndify(seq, And='and'): if len(L) == 0: return '' elif len(L) == 1: - return L[0] + return ''.join(L) # We need this because it raises TypeError. elif len(L) == 2: - return '%s %s %s' % (L[0], And, L[1]) + L.insert(1, And) + return ' '.join(L) else: L[-1] = '%s %s' % (And, L[-1]) return ', '.join(L) diff --git a/test/test_utils.py b/test/test_utils.py index d65c94ef8..cbd28ab2f 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -180,6 +180,12 @@ class UtilsTest(unittest.TestCase): self.assertEqual(L, original) 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): self.assertEqual(utils.unCommaThe('foo bar'), 'foo bar') self.assertEqual(utils.unCommaThe('foo bar, the'), 'the foo bar')