Ticket #5830: test_text.patch

File test_text.patch, 5.0 KB (added by vperic, 4 years ago)

First version

  • twisted/python/text.py

    diff --git twisted/python/text.py twisted/python/text.py
    index def3c11..11b6314 100644
     
    88Miscellany of text-munging functions.
    99"""
    1010
    11 import string
    12 import types
    13 
    1411
    1512def stringyString(object, indentation=''):
    1613    """
    def stringyString(object, indentation=''): 
    2623    braces = ''
    2724    sl = []
    2825
    29     if type(object) is types.DictType:
     26    if isinstance(object, dict):
    3027        braces = '{}'
    3128        for key, value in object.items():
    3229            value = stringyString(value, indentation + '   ')
    def stringyString(object, indentation=''): 
    3936                sl.append("%s %s: %s" % (indentation, key,
    4037                                         value[len(indentation) + 3:]))
    4138
    42     elif type(object) in (types.TupleType, types.ListType):
    43         if type(object) is types.TupleType:
     39    elif isinstance(object, (tuple, list)):
     40        if isinstance(object, tuple):
    4441            braces = '()'
    4542        else:
    4643            braces = '[]'
    4744
    4845        for element in object:
    4946            element = stringyString(element, indentation + ' ')
    50             sl.append(string.rstrip(element) + ',')
     47            sl.append(element.rstrip() + ',')
    5148    else:
    5249        sl[:] = map(lambda s, i=indentation: i+s,
    53                     string.split(str(object),'\n'))
     50                    str(object).split('\n'))
    5451
    5552    if not sl:
    5653        sl.append(indentation)
    def stringyString(object, indentation=''): 
    5956        sl[0] = indentation + braces[0] + sl[0][len(indentation) + 1:]
    6057        sl[-1] = sl[-1] + braces[-1]
    6158
    62     s = string.join(sl, "\n")
     59    s = "\n".join(sl)
    6360
    6461    if isMultiline(s) and not endsInNewline(s):
    6562        s = s + '\n'
    def stringyString(object, indentation=''): 
    6865
    6966def isMultiline(s):
    7067    """Returns True if this string has a newline in it."""
    71     return (string.find(s, '\n') != -1)
     68    return (s.find('\n') != -1)
    7269
    7370def endsInNewline(s):
    7471    """Returns True if this string ends in a newline."""
    def greedyWrap(inString, width=80): 
    8885
    8986    #eww, evil hacks to allow paragraphs delimited by two \ns :(
    9087    if inString.find('\n\n') >= 0:
    91         paragraphs = string.split(inString, '\n\n')
     88        paragraphs = inString.split('\n\n')
    9289        for para in paragraphs:
    9390            outLines.extend(greedyWrap(para, width) + [''])
    9491        return outLines
    95     inWords = string.split(inString)
     92    inWords = inString.split()
    9693
    9794    column = 0
    9895    ptr_line = 0
    def greedyWrap(inString, width=80): 
    108105                # We've gone too far, stop the line one word back.
    109106                ptr_line = ptr_line - 1
    110107            (l, inWords) = (inWords[0:ptr_line], inWords[ptr_line:])
    111             outLines.append(string.join(l,' '))
     108            outLines.append(' '.join(l))
    112109
    113110            ptr_line = 0
    114111            column = 0
    115112        elif not (len(inWords) > ptr_line):
    116113            # Clean up the last bit.
    117             outLines.append(string.join(inWords, ' '))
     114            outLines.append(' '.join(inWords))
    118115            del inWords[:]
    119116        else:
    120117            # Space
  • twisted/test/test_text.py

    diff --git twisted/test/test_text.py twisted/test/test_text.py
    index 92fad77..963d988 100644
    class StrFileTest(unittest.TestCase): 
    155155
    156156
    157157
    158 testCases = [WrapTest, SplitTest, StrFileTest]
     158class NewLineTest(unittest.TestCase):
     159    """
     160    Tests for misc methods related to finding newlines in strings.
     161    """
     162
     163    def test_isMultiLine(self):
     164        """
     165        L{isMultiline} only returns true if the string has a newline in it.
     166        """
     167        s1 = "multi\nline string"
     168        s2 = "single line string"
     169        self.assertTrue(text.isMultiline(s1))
     170        self.assertFalse(text.isMultiline(s2))
     171
     172
     173    def test_endsInNewline(self):
     174        """
     175        L{endsInNewline} returns true if the string ends in a newline.
     176        """
     177        s1 = "a string\n"
     178        s2 = "another string"
     179        self.assertTrue(text.endsInNewline(s1))
     180        self.assertFalse(text.endsInNewline(s2))
     181
     182
     183
     184class SequenceTypeStringTests(unittest.TestCase):
     185    """
     186    Tests for expansive printing of sequence types.
     187    """
     188
     189    def test_tuples(self):
     190        """
     191        Tuples are printed with each element on a separate line.
     192        """
     193        t = (1, 2, 3)
     194        self.assertEqual(text.stringyString(t), '(1,\n 2,\n 3,)\n')
     195
     196
     197    def test_lists(self):
     198        """
     199        Lists are printed with each element on a separate line.
     200        """
     201        l = [1, 2, 3]
     202        self.assertEqual(text.stringyString(l), '[1,\n 2,\n 3,]\n')
     203
     204
     205    def test_dicts(self):
     206        """
     207        Dicts are printed with each element on a separate line.
     208
     209        Because ordering inside dicts is implementation-dependant, test just the
     210        trivial case.
     211        """
     212        d1 = {'one': 1}
     213        self.assertEqual(text.stringyString(d1), '{one: 1}')
     214
     215
     216
     217testCases = [WrapTest, SplitTest, StrFileTest, NewLineTest, SequenceTypeStringTests]