Ticket #6393: 6393.4.patch

File 6393.4.patch, 5.3 KB (added by Francis Sylvain, 8 years ago)
  • twisted/topfiles/6393.feature

     
     1Added a toPhrase method to twisted.trial.util
  • twisted/trial/test/test_util.py

     
    1919from twisted.internet.base import DelayedCall
    2020from twisted.python.failure import Failure
    2121
    22 from twisted.trial.unittest import SynchronousTestCase
     22from twisted.trial.unittest import SynchronousTestCase, TestCase
    2323from twisted.trial import util
    2424from twisted.trial.util import (
    2525    DirtyReactorAggregateError, _Janitor, excInfoOrFailureToExcInfo,
     
    681681        """
    682682        default = object()
    683683        self.assertTrue(default is acquireAttribute([object()], "foo", default))
     684
     685
     686
     687class TestToPhrase(TestCase):
     688    """
     689    Input is transformed into a string representation of the list,
     690    with each item separated by delimiter (defaulting to a comma) and the final
     691    two being separated by a final delimiter.
     692    """
     693   
     694    def test_empty(self):
     695        """
     696        If things is empty, an empty string is returned.
     697        """
     698        sample = []
     699        expected = ''
     700        result = util._toPhrase(sample, 'and')
     701        self.assertEqual(expected, result)
     702   
     703   
     704    def test_oneWord(self):
     705        """
     706        With a single item, the item is returned.
     707        """
     708        sample = ['One']
     709        expected = 'One'
     710        result = util._toPhrase(sample, 'and')
     711        self.assertEqual(expected, result)
     712
     713
     714    def test_twoWords(self):
     715        """
     716        Two words are separated by the final delimiter.
     717        """
     718        sample = ['One', 'Two']
     719        expected = 'One and Two'
     720        result = util._toPhrase(sample, 'and')
     721        self.assertEqual(expected, result)
     722
     723
     724    def test_threeWords(self):
     725        """
     726        With more than two words, the first two are separated by the delimiter.
     727        """
     728        sample = ['One', 'Two', 'Three']
     729        expected = 'One, Two, and Three'
     730        result = util._toPhrase(sample, 'and')
     731        self.assertEqual(expected, result)
     732
     733
     734    def test_fourWords(self):
     735        """
     736        If a delimiter is specified, it is used instead of the default comma.
     737        """
     738        sample = ['One', 'Two', 'Three', 'Four']
     739        expected = 'One; Two; Three; or Four'
     740        result = util._toPhrase(sample, 'or', delimiter='; ')
     741        self.assertEqual(expected, result)
     742
     743
     744    def test_notString(self):
     745        """
     746        If something in things is not a string, it is converted into one.
     747        """
     748        sample = [1, 2, 'three']
     749        expected = '1, 2, and three'
     750        result = util._toPhrase(sample, 'and')
     751        self.assertEqual(expected, result)
     752       
     753       
     754    def test_stringTypeError(self):
     755        """
     756        If things is a string, a TypeError is raised.
     757        """
     758        sample = "One, two, three"
     759        error = self.assertRaises(TypeError, util._toPhrase, sample, 'and')
     760        self.assertEqual(error.message, "Things must be a list or a tuple")
     761
     762
     763    def test_iteratorTypeError(self):
     764        """
     765        If things is an iterator, a TypeError is raised.
     766        """
     767        sample = iter([1, 2, 3])
     768        error = self.assertRaises(TypeError, util._toPhrase, sample, 'and')
     769        self.assertEqual(error.message, "Things must be a list or a tuple")
     770
     771
     772    def test_generatorTypeError(self):
     773        """
     774        If things is a generator, a TypeError is raised.
     775        """
     776        def sample():
     777            for i in range(2):
     778                yield i
     779        error = self.assertRaises(TypeError, util._toPhrase, sample, 'and')
     780        self.assertEqual(error.message, "Things must be a list or a tuple")
  • twisted/trial/util.py

     
    409409# Remove this, and move lockfile import, after ticket #5960 is resolved:
    410410if _PY3:
    411411    del _unusedTestDirectory
     412
     413
     414
     415def _toPhrase(things, finalDelimiter, delimiter=', '):
     416    """
     417    Produce a string containing each thing in C{things},
     418    separated by a C{delimiter}, with the last couple being separated
     419    by C{finalDelimiter}
     420
     421    @param things: The elements of the resulting phrase
     422    @type things: L{list} or L{tuple}
     423
     424    @param finalDelimiter: What to put between the last two things
     425    @type finalDelimiter: L{str}
     426
     427    @param delimiter: The separator to use between each thing
     428    @type delimiter: L{str}
     429
     430    @return: The resulting phrase
     431    @rtype: L{str}
     432    """
     433    if not isinstance(things, (list, tuple)):
     434        raise TypeError("Things must be a list or a tuple")
     435    if not things:
     436        return ''
     437    if len(things) == 1:
     438        return str(things[0])
     439    if len(things) == 2:
     440        return "%s %s %s" % (str(things[0]), finalDelimiter, str(things[1]))
     441    else:
     442        strThings = []
     443        for thing in things:
     444            strThings.append(str(thing))
     445        return "%s%s%s %s" % (delimiter.join(strThings[:-1]), delimiter, finalDelimiter, strThings[-1])