1# Check every path through every method of UserList
2
3from collections import UserList
4from test import list_tests
5import unittest
6
7class UserListTest(list_tests.CommonTest):
8    type2test = UserList
9
10    def test_getslice(self):
11        super().test_getslice()
12        l = [0, 1, 2, 3, 4]
13        u = self.type2test(l)
14        for i in range(-3, 6):
15            self.assertEqual(u[:i], l[:i])
16            self.assertEqual(u[i:], l[i:])
17            for j in range(-3, 6):
18                self.assertEqual(u[i:j], l[i:j])
19
20    def test_slice_type(self):
21        l = [0, 1, 2, 3, 4]
22        u = UserList(l)
23        self.assertIsInstance(u[:], u.__class__)
24        self.assertEqual(u[:],u)
25
26    def test_add_specials(self):
27        u = UserList("spam")
28        u2 = u + "eggs"
29        self.assertEqual(u2, list("spameggs"))
30
31    def test_radd_specials(self):
32        u = UserList("eggs")
33        u2 = "spam" + u
34        self.assertEqual(u2, list("spameggs"))
35        u2 = u.__radd__(UserList("spam"))
36        self.assertEqual(u2, list("spameggs"))
37
38    def test_iadd(self):
39        super().test_iadd()
40        u = [0, 1]
41        u += UserList([0, 1])
42        self.assertEqual(u, [0, 1, 0, 1])
43
44    def test_mixedcmp(self):
45        u = self.type2test([0, 1])
46        self.assertEqual(u, [0, 1])
47        self.assertNotEqual(u, [0])
48        self.assertNotEqual(u, [0, 2])
49
50    def test_mixedadd(self):
51        u = self.type2test([0, 1])
52        self.assertEqual(u + [], u)
53        self.assertEqual(u + [2], [0, 1, 2])
54
55    def test_getitemoverwriteiter(self):
56        # Verify that __getitem__ overrides *are* recognized by __iter__
57        class T(self.type2test):
58            def __getitem__(self, key):
59                return str(key) + '!!!'
60        self.assertEqual(next(iter(T((1,2)))), "0!!!")
61
62    def test_userlist_copy(self):
63        u = self.type2test([6, 8, 1, 9, 1])
64        v = u.copy()
65        self.assertEqual(u, v)
66        self.assertEqual(type(u), type(v))
67
68if __name__ == "__main__":
69    unittest.main()
70