Ticket #6393: 6393.2.patch

File 6393.2.patch, 2.6 KB (added by Francis Sylvain, 8 years ago)
  • twisted/python/text.py

     
    206206        if buf.find(p) != -1:
    207207            return True
    208208
     209def itemizedList(things, finalDelimiter, delimiter=', '):
     210    """
     211    Produce a string containing each thing in C{things},
     212    separated by a C{delimiter}, with the last couple being separated
     213    by C{finalDelimiter}
     214   
     215    @rtype: C{str}
     216    """
     217    if len(things) == 1:
     218        return str(things[0])
     219    else:
     220        return "%s %s %s" % (delimiter.join(things[:-1]), finalDelimiter, things[-1])
  • twisted/test/test_text.py

     
    240240    def test_insensitive(self):
    241241        self.assertEqual(True, text.strFile("ThIs is A test STRING", self.io, False))
    242242
     243class ItemizedListTest(unittest.TestCase):
     244    """Input is transformed into a string representation of the list,
     245    with each item separated by delimiter (defaulting to a comma) and the final
     246    two being separated by a final delimiter """
     247    def test_oneWord(self):
     248        """With a single item, the item is returned"""
     249        sample = ['One']
     250        expected = 'One'
     251        result = text.itemizedList(sample, 'and')
     252        self.assertEqual(expected, result)
     253
     254    def test_twoWords(self):
     255        """Two words are separated by the final delimiter"""
     256        sample = ['One', 'Two']
     257        expected = 'One and Two'
     258        result = text.itemizedList(sample, 'and')
     259        self.assertEqual(expected, result)
     260
     261    def test_threeWords(self):
     262        """With more than two words, the first two are separated by the delimiter"""
     263        sample = ['One', 'Two', 'Three']
     264        expected = 'One, Two and Three'
     265        result = text.itemizedList(sample, 'and')
     266        self.assertEqual(expected, result)
     267
     268    def test_fourWords(self):
     269        """If a delimiter is specified, it is used instead of the default comma"""
     270        sample = ['One', 'Two', 'Three', 'Four']
     271        expected = 'One; Two; Three or Four'
     272        result = text.itemizedList(sample, 'or', delimiter='; ')
     273        self.assertEqual(expected, result)
     274 No newline at end of file
  • twisted/topfiles/6393.feature

     
     1Added an itemizedList method to twisted.python.text