2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 from twisted.trial import unittest
7 from twisted.python import text
8 from cStringIO import StringIO
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
19 -- Auguste Comte, Philosophie Positive, Paris, 1838
28 class WrapTest(unittest.TestCase):
30 self.sampleSplitText = sampleText.split()
32 self.output = text.wordWrap(sampleText, lineWidth)
34 def test_wordCount(self):
35 """Compare the number of words."""
37 for line in self.output:
38 words.extend(line.split())
39 wordCount = len(words)
40 sampleTextWordCount = len(self.sampleSplitText)
42 self.assertEqual(wordCount, sampleTextWordCount)
44 def test_wordMatch(self):
45 """Compare the lists of words."""
48 for line in self.output:
49 words.extend(line.split())
51 # Using assertEqual here prints out some
52 # rather too long lists.
53 self.failUnless(self.sampleSplitText == words)
55 def test_lineLength(self):
56 """Check the length of the lines."""
58 for line in self.output:
59 if not len(line) <= lineWidth:
60 failures.append(len(line))
63 self.fail("%d of %d lines were too long.\n"
64 "%d < %s" % (len(failures), len(self.output),
68 class SplitTest(unittest.TestCase):
69 """Tests for text.splitQuoted()"""
71 def test_oneWord(self):
72 """Splitting strings with one-word phrases."""
73 s = 'This code "works."'
74 r = text.splitQuoted(s)
75 self.assertEqual(['This', 'code', 'works.'], r)
77 def test_multiWord(self):
78 s = 'The "hairy monkey" likes pie.'
79 r = text.splitQuoted(s)
80 self.assertEqual(['The', 'hairy monkey', 'likes', 'pie.'], r)
82 # Some of the many tests that would fail:
84 #def test_preserveWhitespace(self):
85 # phrase = '"MANY SPACES"'
86 # s = 'With %s between.' % (phrase,)
87 # r = text.splitQuoted(s)
88 # self.assertEqual(['With', phrase, 'between.'], r)
90 #def test_escapedSpace(self):
92 # r = text.splitQuoted(s)
93 # self.assertEqual(["One Phrase"], r)
95 class StrFileTest(unittest.TestCase):
97 self.io = StringIO("this is a test string")
103 self.assertEqual(False, text.strFile("x", self.io))
106 self.assertEqual(True, text.strFile("t", self.io))
109 self.assertEqual(True, text.strFile("h", self.io))
112 self.assertEqual(True, text.strFile("i", self.io))
115 self.assertEqual(True, text.strFile("s", self.io))
118 self.assertEqual(True, text.strFile("n", self.io))
121 self.assertEqual(True, text.strFile("g", self.io))
124 self.assertEqual(True, text.strFile("thi", self.io))
127 self.assertEqual(True, text.strFile("his", self.io))
130 self.assertEqual(True, text.strFile("is ", self.io))
133 self.assertEqual(True, text.strFile("ing", self.io))
136 self.assertEqual(False, text.strFile("bla", self.io))
138 def test_large_1(self):
139 self.assertEqual(True, text.strFile("this is a test", self.io))
141 def test_large_2(self):
142 self.assertEqual(True, text.strFile("is a test string", self.io))
144 def test_large_f(self):
145 self.assertEqual(False, text.strFile("ds jhfsa k fdas", self.io))
147 def test_overlarge_f(self):
148 self.assertEqual(False, text.strFile("djhsakj dhsa fkhsa s,mdbnfsauiw bndasdf hreew", self.io))
151 self.assertEqual(True, text.strFile("this is a test string", self.io))
153 def test_insensitive(self):
154 self.assertEqual(True, text.strFile("ThIs is A test STRING", self.io, False))
158 testCases = [WrapTest, SplitTest, StrFileTest]