Debian Codegraph @

envisage/tests/test_slice.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ Tests to help find out how trait list events work.
11
12 These tests exist because when we are using the 'ExtensionPoint' trait type
13 we try to mimic trait list events when extensions are added or removed.
14
15 """
16
17 # Standard library imports.
18 import unittest
19
20 # Enthought library imports.
21 from traits.api import HasTraits, List
22
23 # The starting list for all tests.
24 TEST_LIST = [7, 9, 2, 3, 4, 1, 6, 5, 8, 0]
25
26
27 def listener(obj, trait_name, old, event):
28 """Recreate a list operation from a trait list event."""
29
30 clone = TEST_LIST[:]
31
32 added = event.added
33 # Backwards compatibility for Traits < 6.0, where event.added may
34 # be a list containing a list containing the added elements. This
35 # block can be removed once compatibility with Traits < 6.0 is no
36 # longer needed. Ref: enthought/traits#300.
37 if len(added) == 1 and isinstance(added[0], list):
38 added = added[0]
39
40 # If nothing was added then this is a 'del' or 'remove' operation.
41 if len(added) == 0:
42 if isinstance(event.index, slice):
43 del clone[event.index]
44
45 else:
46 # workaroud for traits bug in Python 3
47 # https://github.com/enthought/traits/issues/334
48 index = event.index if event.index is not None else 0
49 del clone[index : index + len(event.removed)]
50
51 # If nothing was removed then it is an 'append', 'insert' or 'extend'
52 # operation.
53 elif len(event.removed) == 0:
54 if isinstance(event.index, slice):
55 clone[event.index] = added
56
57 else:
58 clone[event.index : event.index] = added
59
60 # Otherwise, it is an assigment ('sort' and 'reverse' fall into this
61 # category).
62 else:
63 if isinstance(event.index, slice):
64 clone[event.index] = added
65
66 else:
67 clone[event.index : event.index + len(added)] = added
68
69 listener.clone = clone
70
71
72 class SliceTestCase(unittest.TestCase):
73 """Tests to help find out how trait list events work."""
74
75 def setUp(self):
76 """Prepares the test fixture before each test method is called."""
77
78 class Foo(HasTraits):
79 elts = List
80
81 self.f = Foo(elts=TEST_LIST)
82 self.f.on_trait_change(listener, "elts_items")
83
84 def test_append(self):
85 """append"""
86
87 self.f.elts.append(99)
88 # Make sure we successfully recreated the operation.
89 self.assertEqual(self.f.elts, listener.clone)
90
91 def test_insert(self):
92 """insert"""
93
94 self.f.elts.insert(3, 99)
95 # Make sure we successfully recreated the operation.
96 self.assertEqual(self.f.elts, listener.clone)
97
98 def test_extend(self):
99 """extend"""
100
101 self.f.elts.extend([99, 100])
102 # Make sure we successfully recreated the operation.
103 self.assertEqual(self.f.elts, listener.clone)
104
105 def test_remove(self):
106 """remove"""
107
108 self.f.elts.remove(5)
109 # Make sure we successfully recreated the operation.
110 self.assertEqual(self.f.elts, listener.clone)
111
112 def test_reverse(self):
113 """reverse"""
114
115 self.f.elts.reverse()
116 # Make sure we successfully recreated the operation.
117 self.assertEqual(self.f.elts, listener.clone)
118
119 def test_sort(self):
120 """sort"""
121
122 self.f.elts.sort()
123 # Make sure we successfully recreated the operation.
124 self.assertEqual(self.f.elts, listener.clone)
125
126 def test_pop(self):
127 """remove"""
128
129 self.f.elts.pop()
130 # Make sure we successfully recreated the operation.
131 self.assertEqual(self.f.elts, listener.clone)
132
133 def test_del_all(self):
134 """del all"""
135
136 del self.f.elts[:]
137 # Make sure we successfully recreated the operation.
138 self.assertEqual(self.f.elts, listener.clone)
139
140 def test_assign_item(self):
141 """assign item"""
142
143 self.f.elts[3] = 99
144 # Make sure we successfully recreated the operation.
145 self.assertEqual(self.f.elts, listener.clone)
146
147 def test_del_item(self):
148 """del item"""
149
150 del self.f.elts[3]
151 # Make sure we successfully recreated the operation.
152 self.assertEqual(self.f.elts, listener.clone)
153
154 def test_assign_slice(self):
155 """assign slice"""
156
157 self.f.elts[2:4] = [88, 99]
158 # Make sure we successfully recreated the operation.
159 self.assertEqual(self.f.elts, listener.clone)
160
161 def test_del_slice(self):
162 """del slice"""
163
164 del self.f.elts[2:5]
165 # Make sure we successfully recreated the operation.
166 self.assertEqual(self.f.elts, listener.clone)
167
168 def test_assign_extended_slice(self):
169 """assign extended slice"""
170
171 self.f.elts[2:6:2] = [88, 99]
172 # Make sure we successfully recreated the operation.
173 self.assertEqual(self.f.elts, listener.clone)
174
175 def test_del_extended_slice(self):
176 """del extended slice"""
177
178 del self.f.elts[2:6:2]
179 # Make sure we successfully recreated the operation.
180 self.assertEqual(self.f.elts, listener.clone)