File: slice_test_case.py

package info (click to toggle)
python-envisagecore 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,096 kB
  • ctags: 1,063
  • sloc: python: 4,115; makefile: 7; sh: 5
file content (189 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
""" Tests to help find out how trait list events work.

These tests exist because when we are using the 'ExtensionPoint' trait type
we try to mimic trait list events when extensions are added or removed.

"""


# Standard library imports.
import unittest

# Enthought library imports.
from enthought.traits.api import HasTraits, List


# The starting list for all tests.
TEST_LIST = [7, 9, 2, 3, 4, 1, 6, 5, 8, 0]


def listener(obj, trait_name, old, event):
    """ Recreate a list operation from a trait list event. """

    clone = TEST_LIST[:]

    # If nothing was added then this is a 'del' or 'remove' operation.
    if len(event.added) == 0:
        if isinstance(event.index, slice):
            del clone[event.index]

        else:
            del clone[event.index : event.index + len(event.removed)]

    # If nothing was removed then it is an 'append', 'insert' or 'extend'
    # operation.
    elif len(event.removed) == 0:
        if isinstance(event.index, slice):
            clone[event.index] = event.added[0]

        else:
            clone.insert(event.index, event.added[0])
        
    # Otherwise, it is an assigment ('sort' and 'reverse' fall into this
    # category).
    else:
        if isinstance(event.index, slice):
            clone[event.index] = event.added[0]

        else:
            clone[event.index : event.index + len(event.added)] = event.added 
    
    listener.clone = clone

    return


class SliceTestCase(unittest.TestCase):
    """ Tests to help find out how trait list events work. """

    ###########################################################################
    # 'TestCase' interface.
    ###########################################################################

    def setUp(self):
        """ Prepares the test fixture before each test method is called. """

        class Foo(HasTraits):
            l = List

        self.f = Foo(l=TEST_LIST)
        self.f.on_trait_change(listener, 'l_items')

        return

    def tearDown(self):
        """ Called immediately after each test method has been called. """

        # Make sure we successfully recreated the operation.
        self.assertEqual(self.f.l, listener.clone)
        
        return
    
    ###########################################################################
    # Tests.
    ###########################################################################

    def test_append(self):
        """ append """

        self.f.l.append(99)

        return

    def test_insert(self):
        """ insert """

        self.f.l.insert(3, 99)

        return

    def test_extend(self):
        """ extend """

        self.f.l.append([99, 100])

        return

    def test_remove(self):
        """ remove """

        self.f.l.remove(5)

        return

    def test_reverse(self):
        """ reverse """

        self.f.l.reverse()

        return

    def test_sort(self):
        """ sort """

        self.f.l.sort()

        return

    def test_pop(self):
        """ remove """

        self.f.l.pop()

        return

    def test_del_all(self):
        """ del all """

        del self.f.l[:]

        return
    
    def test_assign_item(self):
        """ assign item """

        self.f.l[3] = 99

        return

    def test_del_item(self):
        """ del item """

        del self.f.l[3]

        return

    def test_assign_slice(self):
        """ assign slice """

        self.f.l[2:4] = [88, 99]

        return

    def test_del_slice(self):
        """ del slice """

        del self.f.l[2:5]

        return

    def test_assign_extended_slice(self):
        """ assign extended slice """

        self.f.l[2:6:2] = [88, 99]

        return

    def test_del_extended_slice(self):
        """ del extended slice """

        del self.f.l[2:6:2]

        return


# Entry point for stand-alone testing.
if __name__ == '__main__':
    unittest.main()

#### EOF ######################################################################