Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_text.py
1
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5
6 from twisted.trial import unittest
7 from twisted.python import text
8 from cStringIO import StringIO
9
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
19            --  Auguste Comte, Philosophie Positive, Paris, 1838
20 """
21
22 lineWidth = 72
23
24 def set_lineWidth(n):
25     global lineWidth
26     lineWidth = n
27
28 class WrapTest(unittest.TestCase):
29     def setUp(self):
30         self.sampleSplitText = sampleText.split()
31
32         self.output = text.wordWrap(sampleText, lineWidth)
33
34     def test_wordCount(self):
35         """Compare the number of words."""
36         words = []
37         for line in self.output:
38             words.extend(line.split())
39         wordCount = len(words)
40         sampleTextWordCount = len(self.sampleSplitText)
41
42         self.assertEqual(wordCount, sampleTextWordCount)
43
44     def test_wordMatch(self):
45         """Compare the lists of words."""
46
47         words = []
48         for line in self.output:
49             words.extend(line.split())
50
51         # Using assertEqual here prints out some
52         # rather too long lists.
53         self.failUnless(self.sampleSplitText == words)
54
55     def test_lineLength(self):
56         """Check the length of the lines."""
57         failures = []
58         for line in self.output:
59             if not len(line) <= lineWidth:
60                 failures.append(len(line))
61
62         if failures:
63             self.fail("%d of %d lines were too long.\n"
64                       "%d < %s" % (len(failures), len(self.output),
65                                    lineWidth, failures))
66
67
68 class SplitTest(unittest.TestCase):
69     """Tests for text.splitQuoted()"""
70
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)
76
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)
81
82     # Some of the many tests that would fail:
83
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)
89
90     #def test_escapedSpace(self):
91     #    s = r"One\ Phrase"
92     #    r = text.splitQuoted(s)
93     #    self.assertEqual(["One Phrase"], r)
94
95 class StrFileTest(unittest.TestCase):
96     def setUp(self):
97         self.io = StringIO("this is a test string")
98
99     def tearDown(self):
100         pass
101
102     def test_1_f(self):
103         self.assertEqual(False, text.strFile("x", self.io))
104
105     def test_1_1(self):
106         self.assertEqual(True, text.strFile("t", self.io))
107
108     def test_1_2(self):
109         self.assertEqual(True, text.strFile("h", self.io))
110
111     def test_1_3(self):
112         self.assertEqual(True, text.strFile("i", self.io))
113
114     def test_1_4(self):
115         self.assertEqual(True, text.strFile("s", self.io))
116
117     def test_1_5(self):
118         self.assertEqual(True, text.strFile("n", self.io))
119
120     def test_1_6(self):
121         self.assertEqual(True, text.strFile("g", self.io))
122
123     def test_3_1(self):
124         self.assertEqual(True, text.strFile("thi", self.io))
125
126     def test_3_2(self):
127         self.assertEqual(True, text.strFile("his", self.io))
128
129     def test_3_3(self):
130         self.assertEqual(True, text.strFile("is ", self.io))
131
132     def test_3_4(self):
133         self.assertEqual(True, text.strFile("ing", self.io))
134
135     def test_3_f(self):
136         self.assertEqual(False, text.strFile("bla", self.io))
137
138     def test_large_1(self):
139         self.assertEqual(True, text.strFile("this is a test", self.io))
140
141     def test_large_2(self):
142         self.assertEqual(True, text.strFile("is a test string", self.io))
143
144     def test_large_f(self):
145         self.assertEqual(False, text.strFile("ds jhfsa k fdas", self.io))
146
147     def test_overlarge_f(self):
148         self.assertEqual(False, text.strFile("djhsakj dhsa fkhsa s,mdbnfsauiw bndasdf hreew", self.io))
149
150     def test_self(self):
151         self.assertEqual(True, text.strFile("this is a test string", self.io))
152
153     def test_insensitive(self):
154         self.assertEqual(True, text.strFile("ThIs is A test STRING", self.io, False))
155
156
157
158 testCases = [WrapTest, SplitTest, StrFileTest]