Ticket #5830: test_text.patch
File test_text.patch, 5.0 KB (added by , 6 years ago) |
---|
-
twisted/python/text.py
diff --git twisted/python/text.py twisted/python/text.py index def3c11..11b6314 100644
8 8 Miscellany of text-munging functions. 9 9 """ 10 10 11 import string12 import types13 14 11 15 12 def stringyString(object, indentation=''): 16 13 """ … … def stringyString(object, indentation=''): 26 23 braces = '' 27 24 sl = [] 28 25 29 if type(object) is types.DictType:26 if isinstance(object, dict): 30 27 braces = '{}' 31 28 for key, value in object.items(): 32 29 value = stringyString(value, indentation + ' ') … … def stringyString(object, indentation=''): 39 36 sl.append("%s %s: %s" % (indentation, key, 40 37 value[len(indentation) + 3:])) 41 38 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): 44 41 braces = '()' 45 42 else: 46 43 braces = '[]' 47 44 48 45 for element in object: 49 46 element = stringyString(element, indentation + ' ') 50 sl.append( string.rstrip(element) + ',')47 sl.append(element.rstrip() + ',') 51 48 else: 52 49 sl[:] = map(lambda s, i=indentation: i+s, 53 str ing.split(str(object),'\n'))50 str(object).split('\n')) 54 51 55 52 if not sl: 56 53 sl.append(indentation) … … def stringyString(object, indentation=''): 59 56 sl[0] = indentation + braces[0] + sl[0][len(indentation) + 1:] 60 57 sl[-1] = sl[-1] + braces[-1] 61 58 62 s = string.join(sl, "\n")59 s = "\n".join(sl) 63 60 64 61 if isMultiline(s) and not endsInNewline(s): 65 62 s = s + '\n' … … def stringyString(object, indentation=''): 68 65 69 66 def isMultiline(s): 70 67 """Returns True if this string has a newline in it.""" 71 return (s tring.find(s,'\n') != -1)68 return (s.find('\n') != -1) 72 69 73 70 def endsInNewline(s): 74 71 """Returns True if this string ends in a newline.""" … … def greedyWrap(inString, width=80): 88 85 89 86 #eww, evil hacks to allow paragraphs delimited by two \ns :( 90 87 if inString.find('\n\n') >= 0: 91 paragraphs = string.split(inString,'\n\n')88 paragraphs = inString.split('\n\n') 92 89 for para in paragraphs: 93 90 outLines.extend(greedyWrap(para, width) + ['']) 94 91 return outLines 95 inWords = string.split(inString)92 inWords = inString.split() 96 93 97 94 column = 0 98 95 ptr_line = 0 … … def greedyWrap(inString, width=80): 108 105 # We've gone too far, stop the line one word back. 109 106 ptr_line = ptr_line - 1 110 107 (l, inWords) = (inWords[0:ptr_line], inWords[ptr_line:]) 111 outLines.append( string.join(l,' '))108 outLines.append(' '.join(l)) 112 109 113 110 ptr_line = 0 114 111 column = 0 115 112 elif not (len(inWords) > ptr_line): 116 113 # Clean up the last bit. 117 outLines.append( string.join(inWords, ' '))114 outLines.append(' '.join(inWords)) 118 115 del inWords[:] 119 116 else: 120 117 # 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): 155 155 156 156 157 157 158 testCases = [WrapTest, SplitTest, StrFileTest] 158 class 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 184 class 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 217 testCases = [WrapTest, SplitTest, StrFileTest, NewLineTest, SequenceTypeStringTests]