| OLD | NEW |
| (Empty) |
| 1 | |
| 2 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 3 # See LICENSE for details. | |
| 4 | |
| 5 | |
| 6 from twisted.trial import unittest | |
| 7 from twisted.python import text | |
| 8 import string | |
| 9 from cStringIO import StringIO | |
| 10 | |
| 11 sampleText = \ | |
| 12 """Every attempt to employ mathematical methods in the study of chemical | |
| 13 questions must be considered profoundly irrational and contrary to the | |
| 14 spirit of chemistry ... If mathematical analysis should ever hold a | |
| 15 prominent place in chemistry - an aberration which is happily almost | |
| 16 impossible - it would occasion a rapid and widespread degeneration of that | |
| 17 science. | |
| 18 -- Auguste Comte, Philosophie Positive, Paris, 1838 | |
| 19 """ | |
| 20 | |
| 21 lineWidth = 72 | |
| 22 | |
| 23 def set_lineWidth(n): | |
| 24 global lineWidth | |
| 25 lineWidth = n | |
| 26 | |
| 27 class WrapTest(unittest.TestCase): | |
| 28 def setUp(self): | |
| 29 self.sampleSplitText = string.split(sampleText) | |
| 30 | |
| 31 self.output = text.wordWrap(sampleText, lineWidth) | |
| 32 | |
| 33 def test_wordCount(self): | |
| 34 """Compare the number of words.""" | |
| 35 words = [] | |
| 36 for line in self.output: | |
| 37 words.extend(string.split(line)) | |
| 38 wordCount = len(words) | |
| 39 sampleTextWordCount = len(self.sampleSplitText) | |
| 40 | |
| 41 self.failUnlessEqual(wordCount, sampleTextWordCount) | |
| 42 | |
| 43 def test_wordMatch(self): | |
| 44 """Compare the lists of words.""" | |
| 45 | |
| 46 words = [] | |
| 47 for line in self.output: | |
| 48 words.extend(string.split(line)) | |
| 49 | |
| 50 # Using failUnlessEqual here prints out some | |
| 51 # rather too long lists. | |
| 52 self.failUnless(self.sampleSplitText == words) | |
| 53 | |
| 54 def test_lineLength(self): | |
| 55 """Check the length of the lines.""" | |
| 56 failures = [] | |
| 57 for line in self.output: | |
| 58 if not len(line) <= lineWidth: | |
| 59 failures.append(len(line)) | |
| 60 | |
| 61 if failures: | |
| 62 self.fail("%d of %d lines were too long.\n" | |
| 63 "%d < %s" % (len(failures), len(self.output), | |
| 64 lineWidth, failures)) | |
| 65 | |
| 66 | |
| 67 class SplitTest(unittest.TestCase): | |
| 68 """Tests for text.splitQuoted()""" | |
| 69 | |
| 70 def test_oneWord(self): | |
| 71 """Splitting strings with one-word phrases.""" | |
| 72 s = 'This code "works."' | |
| 73 r = text.splitQuoted(s) | |
| 74 self.failUnlessEqual(['This', 'code', 'works.'], r) | |
| 75 | |
| 76 def test_multiWord(self): | |
| 77 s = 'The "hairy monkey" likes pie.' | |
| 78 r = text.splitQuoted(s) | |
| 79 self.failUnlessEqual(['The', 'hairy monkey', 'likes', 'pie.'], r) | |
| 80 | |
| 81 # Some of the many tests that would fail: | |
| 82 | |
| 83 #def test_preserveWhitespace(self): | |
| 84 # phrase = '"MANY SPACES"' | |
| 85 # s = 'With %s between.' % (phrase,) | |
| 86 # r = text.splitQuoted(s) | |
| 87 # self.failUnlessEqual(['With', phrase, 'between.'], r) | |
| 88 | |
| 89 #def test_escapedSpace(self): | |
| 90 # s = r"One\ Phrase" | |
| 91 # r = text.splitQuoted(s) | |
| 92 # self.failUnlessEqual(["One Phrase"], r) | |
| 93 | |
| 94 class StrFileTest(unittest.TestCase): | |
| 95 def setUp(self): | |
| 96 self.io = StringIO("this is a test string") | |
| 97 | |
| 98 def tearDown(self): | |
| 99 pass | |
| 100 | |
| 101 def test_1_f(self): | |
| 102 self.assertEquals(False, text.strFile("x", self.io)) | |
| 103 | |
| 104 def test_1_1(self): | |
| 105 self.assertEquals(True, text.strFile("t", self.io)) | |
| 106 | |
| 107 def test_1_2(self): | |
| 108 self.assertEquals(True, text.strFile("h", self.io)) | |
| 109 | |
| 110 def test_1_3(self): | |
| 111 self.assertEquals(True, text.strFile("i", self.io)) | |
| 112 | |
| 113 def test_1_4(self): | |
| 114 self.assertEquals(True, text.strFile("s", self.io)) | |
| 115 | |
| 116 def test_1_5(self): | |
| 117 self.assertEquals(True, text.strFile("n", self.io)) | |
| 118 | |
| 119 def test_1_6(self): | |
| 120 self.assertEquals(True, text.strFile("g", self.io)) | |
| 121 | |
| 122 def test_3_1(self): | |
| 123 self.assertEquals(True, text.strFile("thi", self.io)) | |
| 124 | |
| 125 def test_3_2(self): | |
| 126 self.assertEquals(True, text.strFile("his", self.io)) | |
| 127 | |
| 128 def test_3_3(self): | |
| 129 self.assertEquals(True, text.strFile("is ", self.io)) | |
| 130 | |
| 131 def test_3_4(self): | |
| 132 self.assertEquals(True, text.strFile("ing", self.io)) | |
| 133 | |
| 134 def test_3_f(self): | |
| 135 self.assertEquals(False, text.strFile("bla", self.io)) | |
| 136 | |
| 137 def test_large_1(self): | |
| 138 self.assertEquals(True, text.strFile("this is a test", self.io)) | |
| 139 | |
| 140 def test_large_2(self): | |
| 141 self.assertEquals(True, text.strFile("is a test string", self.io)) | |
| 142 | |
| 143 def test_large_f(self): | |
| 144 self.assertEquals(False, text.strFile("ds jhfsa k fdas", self.io)) | |
| 145 | |
| 146 def test_overlarge_f(self): | |
| 147 self.assertEquals(False, text.strFile("djhsakj dhsa fkhsa s,mdbnfsauiw b
ndasdf hreew", self.io)) | |
| 148 | |
| 149 def test_self(self): | |
| 150 self.assertEquals(True, text.strFile("this is a test string", self.io)) | |
| 151 | |
| 152 def test_insensitive(self): | |
| 153 self.assertEquals(True, text.strFile("ThIs is A test STRING", self.io, F
alse)) | |
| 154 | |
| 155 testCases = [WrapTest, SplitTest, StrFileTest] | |
| 156 | |
| OLD | NEW |