2
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3
# See LICENSE for details.
6
from twisted.trial import unittest
7
from twisted.python import text
9
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
18
-- Auguste Comte, Philosophie Positive, Paris, 1838
27
class WrapTest(unittest.TestCase):
29
self.sampleSplitText = string.split(sampleText)
31
self.output = text.wordWrap(sampleText, lineWidth)
33
def test_wordCount(self):
34
"""Compare the number of words."""
36
for line in self.output:
37
words.extend(string.split(line))
38
wordCount = len(words)
39
sampleTextWordCount = len(self.sampleSplitText)
41
self.failUnlessEqual(wordCount, sampleTextWordCount)
43
def test_wordMatch(self):
44
"""Compare the lists of words."""
47
for line in self.output:
48
words.extend(string.split(line))
50
# Using failUnlessEqual here prints out some
51
# rather too long lists.
52
self.failUnless(self.sampleSplitText == words)
54
def test_lineLength(self):
55
"""Check the length of the lines."""
57
for line in self.output:
58
if not len(line) <= lineWidth:
59
failures.append(len(line))
62
self.fail("%d of %d lines were too long.\n"
63
"%d < %s" % (len(failures), len(self.output),
67
class SplitTest(unittest.TestCase):
68
"""Tests for text.splitQuoted()"""
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)
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)
81
# Some of the many tests that would fail:
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)
89
#def test_escapedSpace(self):
91
# r = text.splitQuoted(s)
92
# self.failUnlessEqual(["One Phrase"], r)
94
class StrFileTest(unittest.TestCase):
96
self.io = StringIO("this is a test string")
102
self.assertEquals(False, text.strFile("x", self.io))
105
self.assertEquals(True, text.strFile("t", self.io))
108
self.assertEquals(True, text.strFile("h", self.io))
111
self.assertEquals(True, text.strFile("i", self.io))
114
self.assertEquals(True, text.strFile("s", self.io))
117
self.assertEquals(True, text.strFile("n", self.io))
120
self.assertEquals(True, text.strFile("g", self.io))
123
self.assertEquals(True, text.strFile("thi", self.io))
126
self.assertEquals(True, text.strFile("his", self.io))
129
self.assertEquals(True, text.strFile("is ", self.io))
132
self.assertEquals(True, text.strFile("ing", self.io))
135
self.assertEquals(False, text.strFile("bla", self.io))
137
def test_large_1(self):
138
self.assertEquals(True, text.strFile("this is a test", self.io))
140
def test_large_2(self):
141
self.assertEquals(True, text.strFile("is a test string", self.io))
143
def test_large_f(self):
144
self.assertEquals(False, text.strFile("ds jhfsa k fdas", self.io))
146
def test_overlarge_f(self):
147
self.assertEquals(False, text.strFile("djhsakj dhsa fkhsa s,mdbnfsauiw bndasdf hreew", self.io))
150
self.assertEquals(True, text.strFile("this is a test string", self.io))
152
def test_insensitive(self):
153
self.assertEquals(True, text.strFile("ThIs is A test STRING", self.io, False))
155
testCases = [WrapTest, SplitTest, StrFileTest]