4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Test module for the ViewList class from statemachine.py.
14 from DocutilsTestSupport
import statemachine
17 class ViewListTests(unittest
.TestCase
):
19 a_list
= list('abcdefg')
20 b_list
= list('AEIOU')
24 self
.a
= statemachine
.ViewList(self
.a_list
, 'a')
25 self
.b
= statemachine
.ViewList(self
.b_list
, 'b')
26 self
.c
= statemachine
.ViewList(self
.c_list
, 'c')
28 def test_xitems(self
):
29 self
.assertEqual(list(self
.b
.xitems()),
30 [('b', 0, 'A'), ('b', 1, 'E'),
31 ('b', 2, 'I'), ('b', 3, 'O'), ('b', 4, 'U')])
32 self
.assertEqual(list(self
.c
.xitems()),
33 [('c', 0, 'X'), ('c', 1, 'Y'), ('c', 2, 'Z')])
36 # be compatible to standard lists whenever sensible:
37 self
.assertEqual(self
.a
, self
.a_list
)
38 self
.assertEqual(str(self
.a
), str(self
.a_list
))
39 self
.assertEqual(self
.b
, self
.b_list
)
40 self
.assertEqual(self
.c
, self
.c_list
)
41 self
.assertEqual(len(self
.a
), len(self
.a_list
))
42 self
.assertTrue('d' in self
.a
) # __contains__
43 self
.assertEqual([value
for value
in self
.a
], self
.a_list
)
45 self
.assertEqual(self
.a
[2], self
.a_list
[2])
47 self
.assertEqual(a
, self
.a
)
49 self
.assertEqual(a
[1], 3)
50 # the `items` list contains the metadata (source/offset tuples)
51 self
.assertEqual(self
.a
.items
,
52 [('a', i
) for (i
, v
) in enumerate(self
.a_list
)])
54 def test_special_class_methods(self
):
55 # `repr` returns instantiation expression
56 self
.assertEqual(repr(self
.a
), "ViewList(%s, items=%s)" %
57 (repr(self
.a_list
), repr(self
.a
.items
)))
58 # `del` also deletes meta-data:
60 self
.assertEqual(list(self
.c
.xitems()),
61 [('c', 0, 'X'), ('c', 2, 'Z')])
62 # operators with extended behaviour
64 self
.assertEqual(ab
, self
.a_list
+ self
.b_list
)
65 self
.assertEqual(ab
.items
, self
.a
.items
+ self
.b
.items
)
67 self
.assertEqual(aa
, self
.a_list
* 2)
68 self
.assertEqual(aa
.items
, self
.a
.items
* 2)
70 self
.assertEqual(self
.a
, self
.a_list
+ self
.b_list
)
71 # self.assertEqual(self.a.items, self.a.items + self.b.items)
73 def test_get_slice(self
):
75 a_list
= self
.a_list
[1:-1]
76 self
.assertEqual(a
, a_list
)
77 self
.assertEqual(a
.items
, [('a', i
+1) for (i
, v
) in enumerate(a_list
)])
78 self
.assertEqual(a
.parent
, self
.a
)
81 def test_set_slice(self
):
82 a
= statemachine
.ViewList(self
.a
[:])
85 s_list
= self
.a_list
[2:-2]
86 s_list
[2:2] = self
.b_list
89 self
.assertEqual(s
, s_list
)
90 self
.assertEqual(s
, a
[2:-2])
91 self
.assertEqual(s
.items
, a
[2:-2].items
)
93 def test_del_slice(self
):
94 a
= statemachine
.ViewList(self
.a
[:])
96 s_list
= self
.a_list
[2:]
99 self
.assertEqual(s
, s_list
)
100 self
.assertEqual(s
, a
[2:])
101 self
.assertEqual(s
.items
, a
[2:].items
)
103 def test_insert(self
):
104 a_list
= self
.a_list
[:]
105 a_list
.insert(2, 'Q')
106 a_list
[4:4] = self
.b_list
108 self
.assertTrue(isinstance(a
, statemachine
.ViewList
))
109 a
.insert(2, 'Q', 'runtime')
111 self
.assertEqual(a
, a_list
)
112 self
.assertEqual(a
.info(2), ('runtime', 0))
113 self
.assertEqual(a
.info(5), ('b', 1))
115 def test_append(self
):
116 a_list
= self
.a_list
[:]
118 a_list
.extend(self
.b_list
)
119 a
= statemachine
.ViewList(self
.a
)
120 a
.append('Q', 'runtime')
123 self
.assertEqual(a
, a_list
)
124 self
.assertEqual(a
.info(len(self
.a
)), ('runtime', 0))
125 self
.assertEqual(a
.info(-2), ('b', len(self
.b
) - 2))
127 def test_extend(self
):
128 a_list
= self
.a_list
[:]
129 a_list
.extend(self
.b_list
)
130 a
= statemachine
.ViewList(self
.a
)
132 self
.assertEqual(a
, a_list
)
133 self
.assertEqual(a
.info(len(self
.a
) + 1), ('b', 1))
137 a
= statemachine
.ViewList(self
.a
[:])
141 self
.assertEqual(s
, a
[2:-2])
142 self
.assertEqual(s
.items
, a
[2:-2].items
)
144 self
.assertEqual(s
, a
[2:-2])
145 self
.assertEqual(s
.items
, a
[2:-2].items
)
147 self
.assertEqual(s
, a
[2:-2])
148 self
.assertEqual(s
.items
, a
[2:-2].items
)
151 a
= statemachine
.ViewList(self
.a
[:])
154 self
.assertEqual(a
, self
.a
)
155 self
.assertEqual(s
, a
[2:-1])
157 self
.assertEqual(a
, self
.a
)
158 self
.assertEqual(s
, a
[2:-2])
162 self
.assertEqual(ab
.info(0), ('a', 0))
163 self
.assertEqual(ab
.info(-1), ('b', len(self
.b
)-1))
164 # report source if index is off the list by one
165 self
.assertEqual(ab
.info(len(ab
)), ('b', None))
166 # `source` and `offset` methods are based on info
167 self
.assertEqual(ab
.source(-1), 'b')
168 self
.assertEqual(ab
.offset(-1), len(self
.b
)-1)
170 def test_reverse(self
):
173 self
.assertEqual(list(c
.xitems()),
174 [('c', 2, 'Z'), ('c', 1, 'Y'), ('c', 0, 'X')])
181 self
.assertEqual(self
.c
, c
)
190 class StringList(unittest
.TestCase
):
202 indented_string
= """\
209 self
.a_list
= self
.text
.splitlines(1)
210 self
.a
= statemachine
.StringList(self
.a_list
, 'a')
212 def test_trim_left(self
):
215 self
.assertEqual(s
, [line
.lstrip() for line
in self
.a_list
[3:5]])
217 def test_get_indented(self
):
218 self
.assertEqual(self
.a
.get_indented(),
220 block
= statemachine
.StringList(
221 statemachine
.string2lines(self
.indented_string
))
222 self
.assertEqual(block
.get_indented(),
223 ([s
[6:] for s
in block
], 6, 1))
226 if __name__
== '__main__':