Update policies on branches and versioning.
[docutils.git] / docutils / test / test_viewlist.py
blob416915da5f26b791793fd7dfce64cf44f15f99bf
1 #! /usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Test module for the ViewList class from statemachine.py.
9 """
11 import unittest
12 import sys
13 import re
14 from DocutilsTestSupport import statemachine
17 class ViewListTests(unittest.TestCase):
19 a_list = list('abcdefg')
20 b_list = list('AEIOU')
21 c_list = list('XYZ')
23 def setUp(self):
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')])
35 def test_lists(self):
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)
44 # get and set values
45 self.assertEqual(self.a[2], self.a_list[2])
46 a = self.a[:]
47 self.assertEqual(a, self.a)
48 a[1] = 3
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:
59 del(self.c[1])
60 self.assertEqual(list(self.c.xitems()),
61 [('c', 0, 'X'), ('c', 2, 'Z')])
62 # operators with extended behaviour
63 ab = self.a + self.b
64 self.assertEqual(ab, self.a_list + self.b_list)
65 self.assertEqual(ab.items, self.a.items + self.b.items)
66 aa = self.a * 2
67 self.assertEqual(aa, self.a_list * 2)
68 self.assertEqual(aa.items, self.a.items * 2)
69 self.a += self.b
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):
74 a = self.a[1:-1]
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)
79 # a.pprint()
81 def test_set_slice(self):
82 a = statemachine.ViewList(self.a[:])
83 s = a[2:-2]
84 s[2:2] = self.b
85 s_list = self.a_list[2:-2]
86 s_list[2:2] = self.b_list
87 # s.pprint()
88 # s[1:4].pprint()
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[:])
95 s = a[2:]
96 s_list = self.a_list[2:]
97 del s[3:5]
98 del s_list[3:5]
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
107 a = self.a[:]
108 self.assertTrue(isinstance(a, statemachine.ViewList))
109 a.insert(2, 'Q', 'runtime')
110 a.insert(4, self.b)
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[:]
117 a_list.append('Q')
118 a_list.extend(self.b_list)
119 a = statemachine.ViewList(self.a)
120 a.append('Q', 'runtime')
121 a.append(self.b)
122 # a.pprint()
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)
131 a.extend(self.b)
132 self.assertEqual(a, a_list)
133 self.assertEqual(a.info(len(self.a) + 1), ('b', 1))
134 # a.pprint()
136 def test_view(self):
137 a = statemachine.ViewList(self.a[:])
138 a.insert(4, self.b)
139 s = a[2:-2]
140 s.insert(5, self.c)
141 self.assertEqual(s, a[2:-2])
142 self.assertEqual(s.items, a[2:-2].items)
143 s.pop()
144 self.assertEqual(s, a[2:-2])
145 self.assertEqual(s.items, a[2:-2].items)
146 s.remove('X')
147 self.assertEqual(s, a[2:-2])
148 self.assertEqual(s.items, a[2:-2].items)
150 def test_trim(self):
151 a = statemachine.ViewList(self.a[:])
152 s = a[1:-1]
153 s.trim_start(1)
154 self.assertEqual(a, self.a)
155 self.assertEqual(s, a[2:-1])
156 s.trim_end(1)
157 self.assertEqual(a, self.a)
158 self.assertEqual(s, a[2:-2])
160 def test_info(self):
161 ab = self.a + self.b
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):
171 c = self.c[:]
172 c.reverse()
173 self.assertEqual(list(c.xitems()),
174 [('c', 2, 'Z'), ('c', 1, 'Y'), ('c', 0, 'X')])
176 def test_sort(self):
177 c = self.c[:]
178 c.reverse()
179 # c.pprint()
180 c.sort()
181 self.assertEqual(self.c, c)
183 # print
184 # print a
185 # print s
186 # print a.items
187 # print s.items
190 class StringList(unittest.TestCase):
192 text = """\
193 This is some
194 example text.
196 Here is some
197 indented text.
199 Unindented text.
202 indented_string = """\
204 literal
205 block"""
208 def setUp(self):
209 self.a_list = self.text.splitlines(1)
210 self.a = statemachine.StringList(self.a_list, 'a')
212 def test_trim_left(self):
213 s = self.a[3:5]
214 s.trim_left(4)
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(),
219 ([], 0, 0))
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__':
227 unittest.main()