PageRenderTime 43ms CodeModel.GetById 2ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/buffer_tests.py

http://unladen-swallow.googlecode.com/
Python | 206 lines | 196 code | 4 blank | 6 comment | 0 complexity | b47c1289af7c2f18d229f935ad63f129 MD5 | raw file
  1# Tests that work for both bytes and buffer objects.
  2# See PEP 3137.
  3
  4import struct
  5import sys
  6
  7class MixinBytesBufferCommonTests(object):
  8    """Tests that work for both bytes and buffer objects.
  9    See PEP 3137.
 10    """
 11
 12    def marshal(self, x):
 13        """Convert x into the appropriate type for these tests."""
 14        raise RuntimeError('test class must provide a marshal method')
 15
 16    def test_islower(self):
 17        self.assertFalse(self.marshal(b'').islower())
 18        self.assert_(self.marshal(b'a').islower())
 19        self.assertFalse(self.marshal(b'A').islower())
 20        self.assertFalse(self.marshal(b'\n').islower())
 21        self.assert_(self.marshal(b'abc').islower())
 22        self.assertFalse(self.marshal(b'aBc').islower())
 23        self.assert_(self.marshal(b'abc\n').islower())
 24        self.assertRaises(TypeError, self.marshal(b'abc').islower, 42)
 25
 26    def test_isupper(self):
 27        self.assertFalse(self.marshal(b'').isupper())
 28        self.assertFalse(self.marshal(b'a').isupper())
 29        self.assert_(self.marshal(b'A').isupper())
 30        self.assertFalse(self.marshal(b'\n').isupper())
 31        self.assert_(self.marshal(b'ABC').isupper())
 32        self.assertFalse(self.marshal(b'AbC').isupper())
 33        self.assert_(self.marshal(b'ABC\n').isupper())
 34        self.assertRaises(TypeError, self.marshal(b'abc').isupper, 42)
 35
 36    def test_istitle(self):
 37        self.assertFalse(self.marshal(b'').istitle())
 38        self.assertFalse(self.marshal(b'a').istitle())
 39        self.assert_(self.marshal(b'A').istitle())
 40        self.assertFalse(self.marshal(b'\n').istitle())
 41        self.assert_(self.marshal(b'A Titlecased Line').istitle())
 42        self.assert_(self.marshal(b'A\nTitlecased Line').istitle())
 43        self.assert_(self.marshal(b'A Titlecased, Line').istitle())
 44        self.assertFalse(self.marshal(b'Not a capitalized String').istitle())
 45        self.assertFalse(self.marshal(b'Not\ta Titlecase String').istitle())
 46        self.assertFalse(self.marshal(b'Not--a Titlecase String').istitle())
 47        self.assertFalse(self.marshal(b'NOT').istitle())
 48        self.assertRaises(TypeError, self.marshal(b'abc').istitle, 42)
 49
 50    def test_isspace(self):
 51        self.assertFalse(self.marshal(b'').isspace())
 52        self.assertFalse(self.marshal(b'a').isspace())
 53        self.assert_(self.marshal(b' ').isspace())
 54        self.assert_(self.marshal(b'\t').isspace())
 55        self.assert_(self.marshal(b'\r').isspace())
 56        self.assert_(self.marshal(b'\n').isspace())
 57        self.assert_(self.marshal(b' \t\r\n').isspace())
 58        self.assertFalse(self.marshal(b' \t\r\na').isspace())
 59        self.assertRaises(TypeError, self.marshal(b'abc').isspace, 42)
 60
 61    def test_isalpha(self):
 62        self.assertFalse(self.marshal(b'').isalpha())
 63        self.assert_(self.marshal(b'a').isalpha())
 64        self.assert_(self.marshal(b'A').isalpha())
 65        self.assertFalse(self.marshal(b'\n').isalpha())
 66        self.assert_(self.marshal(b'abc').isalpha())
 67        self.assertFalse(self.marshal(b'aBc123').isalpha())
 68        self.assertFalse(self.marshal(b'abc\n').isalpha())
 69        self.assertRaises(TypeError, self.marshal(b'abc').isalpha, 42)
 70
 71    def test_isalnum(self):
 72        self.assertFalse(self.marshal(b'').isalnum())
 73        self.assert_(self.marshal(b'a').isalnum())
 74        self.assert_(self.marshal(b'A').isalnum())
 75        self.assertFalse(self.marshal(b'\n').isalnum())
 76        self.assert_(self.marshal(b'123abc456').isalnum())
 77        self.assert_(self.marshal(b'a1b3c').isalnum())
 78        self.assertFalse(self.marshal(b'aBc000 ').isalnum())
 79        self.assertFalse(self.marshal(b'abc\n').isalnum())
 80        self.assertRaises(TypeError, self.marshal(b'abc').isalnum, 42)
 81
 82    def test_isdigit(self):
 83        self.assertFalse(self.marshal(b'').isdigit())
 84        self.assertFalse(self.marshal(b'a').isdigit())
 85        self.assert_(self.marshal(b'0').isdigit())
 86        self.assert_(self.marshal(b'0123456789').isdigit())
 87        self.assertFalse(self.marshal(b'0123456789a').isdigit())
 88
 89        self.assertRaises(TypeError, self.marshal(b'abc').isdigit, 42)
 90
 91    def test_lower(self):
 92        self.assertEqual(b'hello', self.marshal(b'HeLLo').lower())
 93        self.assertEqual(b'hello', self.marshal(b'hello').lower())
 94        self.assertRaises(TypeError, self.marshal(b'hello').lower, 42)
 95
 96    def test_upper(self):
 97        self.assertEqual(b'HELLO', self.marshal(b'HeLLo').upper())
 98        self.assertEqual(b'HELLO', self.marshal(b'HELLO').upper())
 99        self.assertRaises(TypeError, self.marshal(b'hello').upper, 42)
100
101    def test_capitalize(self):
102        self.assertEqual(b' hello ', self.marshal(b' hello ').capitalize())
103        self.assertEqual(b'Hello ', self.marshal(b'Hello ').capitalize())
104        self.assertEqual(b'Hello ', self.marshal(b'hello ').capitalize())
105        self.assertEqual(b'Aaaa', self.marshal(b'aaaa').capitalize())
106        self.assertEqual(b'Aaaa', self.marshal(b'AaAa').capitalize())
107
108        self.assertRaises(TypeError, self.marshal(b'hello').capitalize, 42)
109
110    def test_ljust(self):
111        self.assertEqual(b'abc       ', self.marshal(b'abc').ljust(10))
112        self.assertEqual(b'abc   ', self.marshal(b'abc').ljust(6))
113        self.assertEqual(b'abc', self.marshal(b'abc').ljust(3))
114        self.assertEqual(b'abc', self.marshal(b'abc').ljust(2))
115        self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, '*'))
116        self.assertRaises(TypeError, self.marshal(b'abc').ljust)
117
118    def test_rjust(self):
119        self.assertEqual(b'       abc', self.marshal(b'abc').rjust(10))
120        self.assertEqual(b'   abc', self.marshal(b'abc').rjust(6))
121        self.assertEqual(b'abc', self.marshal(b'abc').rjust(3))
122        self.assertEqual(b'abc', self.marshal(b'abc').rjust(2))
123        self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, '*'))
124        self.assertRaises(TypeError, self.marshal(b'abc').rjust)
125
126    def test_center(self):
127        self.assertEqual(b'   abc    ', self.marshal(b'abc').center(10))
128        self.assertEqual(b' abc  ', self.marshal(b'abc').center(6))
129        self.assertEqual(b'abc', self.marshal(b'abc').center(3))
130        self.assertEqual(b'abc', self.marshal(b'abc').center(2))
131        self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, '*'))
132        self.assertRaises(TypeError, self.marshal(b'abc').center)
133
134    def test_swapcase(self):
135        self.assertEqual(b'hEllO CoMPuTErS',
136            self.marshal(b'HeLLo cOmpUteRs').swapcase())
137
138        self.assertRaises(TypeError, self.marshal(b'hello').swapcase, 42)
139
140    def test_zfill(self):
141        self.assertEqual(b'123', self.marshal(b'123').zfill(2))
142        self.assertEqual(b'123', self.marshal(b'123').zfill(3))
143        self.assertEqual(b'0123', self.marshal(b'123').zfill(4))
144        self.assertEqual(b'+123', self.marshal(b'+123').zfill(3))
145        self.assertEqual(b'+123', self.marshal(b'+123').zfill(4))
146        self.assertEqual(b'+0123', self.marshal(b'+123').zfill(5))
147        self.assertEqual(b'-123', self.marshal(b'-123').zfill(3))
148        self.assertEqual(b'-123', self.marshal(b'-123').zfill(4))
149        self.assertEqual(b'-0123', self.marshal(b'-123').zfill(5))
150        self.assertEqual(b'000', self.marshal(b'').zfill(3))
151        self.assertEqual(b'34', self.marshal(b'34').zfill(1))
152        self.assertEqual(b'0034', self.marshal(b'34').zfill(4))
153
154        self.assertRaises(TypeError, self.marshal(b'123').zfill)
155
156    def test_expandtabs(self):
157        self.assertEqual(b'abc\rab      def\ng       hi',
158                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
159        self.assertEqual(b'abc\rab      def\ng       hi',
160                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
161        self.assertEqual(b'abc\rab  def\ng   hi',
162                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(4))
163        self.assertEqual(b'abc\r\nab  def\ng   hi',
164                         self.marshal(b'abc\r\nab\tdef\ng\thi').expandtabs(4))
165        self.assertEqual(b'abc\rab      def\ng       hi',
166                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
167        self.assertEqual(b'abc\rab      def\ng       hi',
168                         self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
169        self.assertEqual(b'abc\r\nab\r\ndef\ng\r\nhi',
170            self.marshal(b'abc\r\nab\r\ndef\ng\r\nhi').expandtabs(4))
171        self.assertEqual(b'  a\n b', self.marshal(b' \ta\n\tb').expandtabs(1))
172
173        self.assertRaises(TypeError, self.marshal(b'hello').expandtabs, 42, 42)
174        # This test is only valid when sizeof(int) == sizeof(void*) == 4.
175        if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
176            self.assertRaises(OverflowError,
177                              self.marshal(b'\ta\n\tb').expandtabs, sys.maxint)
178
179    def test_title(self):
180        self.assertEqual(b' Hello ', self.marshal(b' hello ').title())
181        self.assertEqual(b'Hello ', self.marshal(b'hello ').title())
182        self.assertEqual(b'Hello ', self.marshal(b'Hello ').title())
183        self.assertEqual(b'Format This As Title String',
184                         self.marshal(b'fOrMaT thIs aS titLe String').title())
185        self.assertEqual(b'Format,This-As*Title;String',
186                         self.marshal(b'fOrMaT,thIs-aS*titLe;String').title())
187        self.assertEqual(b'Getint', self.marshal(b'getInt').title())
188        self.assertRaises(TypeError, self.marshal(b'hello').title, 42)
189
190    def test_splitlines(self):
191        self.assertEqual([b'abc', b'def', b'', b'ghi'],
192                         self.marshal(b'abc\ndef\n\rghi').splitlines())
193        self.assertEqual([b'abc', b'def', b'', b'ghi'],
194                         self.marshal(b'abc\ndef\n\r\nghi').splitlines())
195        self.assertEqual([b'abc', b'def', b'ghi'],
196                         self.marshal(b'abc\ndef\r\nghi').splitlines())
197        self.assertEqual([b'abc', b'def', b'ghi'],
198                         self.marshal(b'abc\ndef\r\nghi\n').splitlines())
199        self.assertEqual([b'abc', b'def', b'ghi', b''],
200                         self.marshal(b'abc\ndef\r\nghi\n\r').splitlines())
201        self.assertEqual([b'', b'abc', b'def', b'ghi', b''],
202                         self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines())
203        self.assertEqual([b'\n', b'abc\n', b'def\r\n', b'ghi\n', b'\r'],
204                         self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(1))
205
206        self.assertRaises(TypeError, self.marshal(b'abc').splitlines, 42, 42)