Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1 | # Tests that work for both bytes and buffer objects. |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2 | # See PEP 3137. |
| 3 | |
| 4 | import struct |
| 5 | import sys |
| 6 | |
| 7 | class MixinBytesBufferCommonTests(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8 | """Tests that work for both bytes and buffer objects. |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 18 | self.assertTrue(self.marshal(b'a').islower()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 19 | self.assertFalse(self.marshal(b'A').islower()) |
| 20 | self.assertFalse(self.marshal(b'\n').islower()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 21 | self.assertTrue(self.marshal(b'abc').islower()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 22 | self.assertFalse(self.marshal(b'aBc').islower()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 23 | self.assertTrue(self.marshal(b'abc\n').islower()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 29 | self.assertTrue(self.marshal(b'A').isupper()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 30 | self.assertFalse(self.marshal(b'\n').isupper()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 31 | self.assertTrue(self.marshal(b'ABC').isupper()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 32 | self.assertFalse(self.marshal(b'AbC').isupper()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 33 | self.assertTrue(self.marshal(b'ABC\n').isupper()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 39 | self.assertTrue(self.marshal(b'A').istitle()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 40 | self.assertFalse(self.marshal(b'\n').istitle()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 41 | self.assertTrue(self.marshal(b'A Titlecased Line').istitle()) |
| 42 | self.assertTrue(self.marshal(b'A\nTitlecased Line').istitle()) |
| 43 | self.assertTrue(self.marshal(b'A Titlecased, Line').istitle()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 53 | self.assertTrue(self.marshal(b' ').isspace()) |
| 54 | self.assertTrue(self.marshal(b'\t').isspace()) |
| 55 | self.assertTrue(self.marshal(b'\r').isspace()) |
| 56 | self.assertTrue(self.marshal(b'\n').isspace()) |
| 57 | self.assertTrue(self.marshal(b' \t\r\n').isspace()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 63 | self.assertTrue(self.marshal(b'a').isalpha()) |
| 64 | self.assertTrue(self.marshal(b'A').isalpha()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 65 | self.assertFalse(self.marshal(b'\n').isalpha()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 66 | self.assertTrue(self.marshal(b'abc').isalpha()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 73 | self.assertTrue(self.marshal(b'a').isalnum()) |
| 74 | self.assertTrue(self.marshal(b'A').isalnum()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 75 | self.assertFalse(self.marshal(b'\n').isalnum()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 76 | self.assertTrue(self.marshal(b'123abc456').isalnum()) |
| 77 | self.assertTrue(self.marshal(b'a1b3c').isalnum()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 85 | self.assertTrue(self.marshal(b'0').isdigit()) |
| 86 | self.assertTrue(self.marshal(b'0123456789').isdigit()) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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)) |
Benjamin Peterson | 7fe9853 | 2009-04-02 00:33:55 +0000 | [diff] [blame] | 115 | self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, b'*')) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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)) |
Benjamin Peterson | 7fe9853 | 2009-04-02 00:33:55 +0000 | [diff] [blame] | 123 | self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, b'*')) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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)) |
Benjamin Peterson | 7fe9853 | 2009-04-02 00:33:55 +0000 | [diff] [blame] | 131 | self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, b'*')) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 175 | if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4: |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 176 | self.assertRaises(OverflowError, |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 177 | self.marshal(b'\ta\n\tb').expandtabs, sys.maxsize) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 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) |