Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 1 | # Check every path through every method of UserList |
| 2 | |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 3 | from collections import UserList |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support, list_tests |
Tim Peters | f5fa0fc | 2000-09-19 20:31:24 +0000 | [diff] [blame] | 5 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 6 | class UserListTest(list_tests.CommonTest): |
| 7 | type2test = UserList |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 9 | def test_getslice(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10 | super().test_getslice() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 11 | l = [0, 1, 2, 3, 4] |
| 12 | u = self.type2test(l) |
| 13 | for i in range(-3, 6): |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 14 | self.assertEqual(u[:i], l[:i]) |
| 15 | self.assertEqual(u[i:], l[i:]) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 16 | for j in range(-3, 6): |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 17 | self.assertEqual(u[i:j], l[i:j]) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 19 | def test_add_specials(self): |
| 20 | u = UserList("spam") |
| 21 | u2 = u + "eggs" |
| 22 | self.assertEqual(u2, list("spameggs")) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 23 | |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 24 | def test_radd_specials(self): |
| 25 | u = UserList("eggs") |
| 26 | u2 = "spam" + u |
| 27 | self.assertEqual(u2, list("spameggs")) |
| 28 | u2 = u.__radd__(UserList("spam")) |
| 29 | self.assertEqual(u2, list("spameggs")) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 30 | |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 31 | def test_iadd(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | super().test_iadd() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 33 | u = [0, 1] |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 34 | u += UserList([0, 1]) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 35 | self.assertEqual(u, [0, 1, 0, 1]) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 36 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 37 | def test_mixedcmp(self): |
| 38 | u = self.type2test([0, 1]) |
| 39 | self.assertEqual(u, [0, 1]) |
| 40 | self.assertNotEqual(u, [0]) |
| 41 | self.assertNotEqual(u, [0, 2]) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 42 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 43 | def test_mixedadd(self): |
| 44 | u = self.type2test([0, 1]) |
| 45 | self.assertEqual(u + [], u) |
| 46 | self.assertEqual(u + [2], [0, 1, 2]) |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 47 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 48 | def test_getitemoverwriteiter(self): |
| 49 | # Verify that __getitem__ overrides *are* recognized by __iter__ |
| 50 | class T(self.type2test): |
| 51 | def __getitem__(self, key): |
| 52 | return str(key) + '!!!' |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 53 | self.assertEqual(next(iter(T((1,2)))), "0!!!") |
Guido van Rossum | 447b4a0 | 1999-03-26 16:20:45 +0000 | [diff] [blame] | 54 | |
Raymond Hettinger | 1c7b7f7 | 2011-05-05 14:34:35 -0700 | [diff] [blame] | 55 | def test_userlist_copy(self): |
| 56 | u = self.type2test([6, 8, 1, 9, 1]) |
| 57 | v = u.copy() |
| 58 | self.assertEqual(u, v) |
| 59 | self.assertEqual(type(u), type(v)) |
| 60 | |
Walter Dörwald | 7585229 | 2003-02-13 18:07:43 +0000 | [diff] [blame] | 61 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 62 | unittest.main() |