Ticket #6393: 6393.6.patch

File 6393.6.patch, 5.6 KB (added by James Broadhead, 8 years ago)
  • new file twisted/topfiles/6393.feature

    diff --git twisted/topfiles/6393.feature twisted/topfiles/6393.feature
    new file mode 100644
    index 0000000..f6881c3
    - +  
     1Added a toPhrase method to twisted.trial.util
  • twisted/trial/test/test_util.py

    diff --git twisted/trial/test/test_util.py twisted/trial/test/test_util.py
    index bf8cdea..d8bd324 100644
    class AcquireAttributeTests(SynchronousTestCase): 
    681681        """
    682682        default = object()
    683683        self.assertTrue(default is acquireAttribute([object()], "foo", default))
     684
     685
     686
     687class TestListToPhrase(SynchronousTestCase):
     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._listToPhrase(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._listToPhrase(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._listToPhrase(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._listToPhrase(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._listToPhrase(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._listToPhrase(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._listToPhrase, 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._listToPhrase, 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._listToPhrase, sample, 'and')
     780        self.assertEqual(error.message, "Things must be a list or a tuple")
  • twisted/trial/util.py

    diff --git twisted/trial/util.py twisted/trial/util.py
    index a3103f0..4526774 100644
    class _Janitor(object): 
    157157        reactor = self._getReactor()
    158158        if interfaces.IReactorThreads.providedBy(reactor):
    159159            if reactor.threadpool is not None:
    160                 # Stop the threadpool now so that a new one is created. 
     160                # Stop the threadpool now so that a new one is created.
    161161                # This improves test isolation somewhat (although this is a
    162162                # post class cleanup hook, so it's only isolating classes
    163163                # from each other, not methods from each other).
    def _unusedTestDirectory(base): 
    409409# Remove this, and move lockfile import, after ticket #5960 is resolved:
    410410if _PY3:
    411411    del _unusedTestDirectory
     412
     413
     414
     415def _listToPhrase(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        (typically 'and' or 'or')
     426    @type finalDelimiter: L{str}
     427
     428    @param delimiter: The separator to use between each thing,
     429        not including the last two. Should typically include a trailing space.
     430    @type delimiter: L{str}
     431
     432    @return: The resulting phrase
     433    @rtype: L{str}
     434    """
     435    if not isinstance(things, (list, tuple)):
     436        raise TypeError("Things must be a list or a tuple")
     437    if not things:
     438        return ''
     439    if len(things) == 1:
     440        return str(things[0])
     441    if len(things) == 2:
     442        return "%s %s %s" % (str(things[0]), finalDelimiter, str(things[1]))
     443    else:
     444        strThings = []
     445        for thing in things:
     446            strThings.append(str(thing))
     447        return "%s%s%s %s" % (delimiter.join(strThings[:-1]), delimiter, finalDelimiter, strThings[-1])