1 | n/a | # Copyright 2006 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Unit tests for pytree.py. |
---|
5 | n/a | |
---|
6 | n/a | NOTE: Please *don't* add doc strings to individual test methods! |
---|
7 | n/a | In verbose mode, printing of the module, class and method name is much |
---|
8 | n/a | more helpful than printing of (the first line of) the docstring, |
---|
9 | n/a | especially when debugging a test. |
---|
10 | n/a | """ |
---|
11 | n/a | |
---|
12 | n/a | # Testing imports |
---|
13 | n/a | from . import support |
---|
14 | n/a | |
---|
15 | n/a | from lib2to3 import pytree |
---|
16 | n/a | |
---|
17 | n/a | try: |
---|
18 | n/a | sorted |
---|
19 | n/a | except NameError: |
---|
20 | n/a | def sorted(lst): |
---|
21 | n/a | l = list(lst) |
---|
22 | n/a | l.sort() |
---|
23 | n/a | return l |
---|
24 | n/a | |
---|
25 | n/a | class TestNodes(support.TestCase): |
---|
26 | n/a | |
---|
27 | n/a | """Unit tests for nodes (Base, Leaf, Node).""" |
---|
28 | n/a | |
---|
29 | n/a | def test_instantiate_base(self): |
---|
30 | n/a | if __debug__: |
---|
31 | n/a | # Test that instantiating Base() raises an AssertionError |
---|
32 | n/a | self.assertRaises(AssertionError, pytree.Base) |
---|
33 | n/a | |
---|
34 | n/a | def test_leaf(self): |
---|
35 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
36 | n/a | self.assertEqual(l1.type, 100) |
---|
37 | n/a | self.assertEqual(l1.value, "foo") |
---|
38 | n/a | |
---|
39 | n/a | def test_leaf_repr(self): |
---|
40 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
41 | n/a | self.assertEqual(repr(l1), "Leaf(100, 'foo')") |
---|
42 | n/a | |
---|
43 | n/a | def test_leaf_str(self): |
---|
44 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
45 | n/a | self.assertEqual(str(l1), "foo") |
---|
46 | n/a | l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1))) |
---|
47 | n/a | self.assertEqual(str(l2), " foo") |
---|
48 | n/a | |
---|
49 | n/a | def test_leaf_str_numeric_value(self): |
---|
50 | n/a | # Make sure that the Leaf's value is stringified. Failing to |
---|
51 | n/a | # do this can cause a TypeError in certain situations. |
---|
52 | n/a | l1 = pytree.Leaf(2, 5) |
---|
53 | n/a | l1.prefix = "foo_" |
---|
54 | n/a | self.assertEqual(str(l1), "foo_5") |
---|
55 | n/a | |
---|
56 | n/a | def test_leaf_equality(self): |
---|
57 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
58 | n/a | l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0))) |
---|
59 | n/a | self.assertEqual(l1, l2) |
---|
60 | n/a | l3 = pytree.Leaf(101, "foo") |
---|
61 | n/a | l4 = pytree.Leaf(100, "bar") |
---|
62 | n/a | self.assertNotEqual(l1, l3) |
---|
63 | n/a | self.assertNotEqual(l1, l4) |
---|
64 | n/a | |
---|
65 | n/a | def test_leaf_prefix(self): |
---|
66 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
67 | n/a | self.assertEqual(l1.prefix, "") |
---|
68 | n/a | self.assertFalse(l1.was_changed) |
---|
69 | n/a | l1.prefix = " ##\n\n" |
---|
70 | n/a | self.assertEqual(l1.prefix, " ##\n\n") |
---|
71 | n/a | self.assertTrue(l1.was_changed) |
---|
72 | n/a | |
---|
73 | n/a | def test_node(self): |
---|
74 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
75 | n/a | l2 = pytree.Leaf(200, "bar") |
---|
76 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
77 | n/a | self.assertEqual(n1.type, 1000) |
---|
78 | n/a | self.assertEqual(n1.children, [l1, l2]) |
---|
79 | n/a | |
---|
80 | n/a | def test_node_repr(self): |
---|
81 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
82 | n/a | l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) |
---|
83 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
84 | n/a | self.assertEqual(repr(n1), |
---|
85 | n/a | "Node(1000, [%s, %s])" % (repr(l1), repr(l2))) |
---|
86 | n/a | |
---|
87 | n/a | def test_node_str(self): |
---|
88 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
89 | n/a | l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) |
---|
90 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
91 | n/a | self.assertEqual(str(n1), "foo bar") |
---|
92 | n/a | |
---|
93 | n/a | def test_node_prefix(self): |
---|
94 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
95 | n/a | self.assertEqual(l1.prefix, "") |
---|
96 | n/a | n1 = pytree.Node(1000, [l1]) |
---|
97 | n/a | self.assertEqual(n1.prefix, "") |
---|
98 | n/a | n1.prefix = " " |
---|
99 | n/a | self.assertEqual(n1.prefix, " ") |
---|
100 | n/a | self.assertEqual(l1.prefix, " ") |
---|
101 | n/a | |
---|
102 | n/a | def test_get_suffix(self): |
---|
103 | n/a | l1 = pytree.Leaf(100, "foo", prefix="a") |
---|
104 | n/a | l2 = pytree.Leaf(100, "bar", prefix="b") |
---|
105 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
106 | n/a | |
---|
107 | n/a | self.assertEqual(l1.get_suffix(), l2.prefix) |
---|
108 | n/a | self.assertEqual(l2.get_suffix(), "") |
---|
109 | n/a | self.assertEqual(n1.get_suffix(), "") |
---|
110 | n/a | |
---|
111 | n/a | l3 = pytree.Leaf(100, "bar", prefix="c") |
---|
112 | n/a | n2 = pytree.Node(1000, [n1, l3]) |
---|
113 | n/a | |
---|
114 | n/a | self.assertEqual(n1.get_suffix(), l3.prefix) |
---|
115 | n/a | self.assertEqual(l3.get_suffix(), "") |
---|
116 | n/a | self.assertEqual(n2.get_suffix(), "") |
---|
117 | n/a | |
---|
118 | n/a | def test_node_equality(self): |
---|
119 | n/a | n1 = pytree.Node(1000, ()) |
---|
120 | n/a | n2 = pytree.Node(1000, [], context=(" ", (1, 0))) |
---|
121 | n/a | self.assertEqual(n1, n2) |
---|
122 | n/a | n3 = pytree.Node(1001, ()) |
---|
123 | n/a | self.assertNotEqual(n1, n3) |
---|
124 | n/a | |
---|
125 | n/a | def test_node_recursive_equality(self): |
---|
126 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
127 | n/a | l2 = pytree.Leaf(100, "foo") |
---|
128 | n/a | n1 = pytree.Node(1000, [l1]) |
---|
129 | n/a | n2 = pytree.Node(1000, [l2]) |
---|
130 | n/a | self.assertEqual(n1, n2) |
---|
131 | n/a | l3 = pytree.Leaf(100, "bar") |
---|
132 | n/a | n3 = pytree.Node(1000, [l3]) |
---|
133 | n/a | self.assertNotEqual(n1, n3) |
---|
134 | n/a | |
---|
135 | n/a | def test_replace(self): |
---|
136 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
137 | n/a | l2 = pytree.Leaf(100, "+") |
---|
138 | n/a | l3 = pytree.Leaf(100, "bar") |
---|
139 | n/a | n1 = pytree.Node(1000, [l1, l2, l3]) |
---|
140 | n/a | self.assertEqual(n1.children, [l1, l2, l3]) |
---|
141 | n/a | self.assertIsInstance(n1.children, list) |
---|
142 | n/a | self.assertFalse(n1.was_changed) |
---|
143 | n/a | l2new = pytree.Leaf(100, "-") |
---|
144 | n/a | l2.replace(l2new) |
---|
145 | n/a | self.assertEqual(n1.children, [l1, l2new, l3]) |
---|
146 | n/a | self.assertIsInstance(n1.children, list) |
---|
147 | n/a | self.assertTrue(n1.was_changed) |
---|
148 | n/a | |
---|
149 | n/a | def test_replace_with_list(self): |
---|
150 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
151 | n/a | l2 = pytree.Leaf(100, "+") |
---|
152 | n/a | l3 = pytree.Leaf(100, "bar") |
---|
153 | n/a | n1 = pytree.Node(1000, [l1, l2, l3]) |
---|
154 | n/a | |
---|
155 | n/a | l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")]) |
---|
156 | n/a | self.assertEqual(str(n1), "foo**bar") |
---|
157 | n/a | self.assertIsInstance(n1.children, list) |
---|
158 | n/a | |
---|
159 | n/a | def test_leaves(self): |
---|
160 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
161 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
162 | n/a | l3 = pytree.Leaf(100, "fooey") |
---|
163 | n/a | n2 = pytree.Node(1000, [l1, l2]) |
---|
164 | n/a | n3 = pytree.Node(1000, [l3]) |
---|
165 | n/a | n1 = pytree.Node(1000, [n2, n3]) |
---|
166 | n/a | |
---|
167 | n/a | self.assertEqual(list(n1.leaves()), [l1, l2, l3]) |
---|
168 | n/a | |
---|
169 | n/a | def test_depth(self): |
---|
170 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
171 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
172 | n/a | n2 = pytree.Node(1000, [l1, l2]) |
---|
173 | n/a | n3 = pytree.Node(1000, []) |
---|
174 | n/a | n1 = pytree.Node(1000, [n2, n3]) |
---|
175 | n/a | |
---|
176 | n/a | self.assertEqual(l1.depth(), 2) |
---|
177 | n/a | self.assertEqual(n3.depth(), 1) |
---|
178 | n/a | self.assertEqual(n1.depth(), 0) |
---|
179 | n/a | |
---|
180 | n/a | def test_post_order(self): |
---|
181 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
182 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
183 | n/a | l3 = pytree.Leaf(100, "fooey") |
---|
184 | n/a | c1 = pytree.Node(1000, [l1, l2]) |
---|
185 | n/a | n1 = pytree.Node(1000, [c1, l3]) |
---|
186 | n/a | self.assertEqual(list(n1.post_order()), [l1, l2, c1, l3, n1]) |
---|
187 | n/a | |
---|
188 | n/a | def test_pre_order(self): |
---|
189 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
190 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
191 | n/a | l3 = pytree.Leaf(100, "fooey") |
---|
192 | n/a | c1 = pytree.Node(1000, [l1, l2]) |
---|
193 | n/a | n1 = pytree.Node(1000, [c1, l3]) |
---|
194 | n/a | self.assertEqual(list(n1.pre_order()), [n1, c1, l1, l2, l3]) |
---|
195 | n/a | |
---|
196 | n/a | def test_changed(self): |
---|
197 | n/a | l1 = pytree.Leaf(100, "f") |
---|
198 | n/a | self.assertFalse(l1.was_changed) |
---|
199 | n/a | l1.changed() |
---|
200 | n/a | self.assertTrue(l1.was_changed) |
---|
201 | n/a | |
---|
202 | n/a | l1 = pytree.Leaf(100, "f") |
---|
203 | n/a | n1 = pytree.Node(1000, [l1]) |
---|
204 | n/a | self.assertFalse(n1.was_changed) |
---|
205 | n/a | n1.changed() |
---|
206 | n/a | self.assertTrue(n1.was_changed) |
---|
207 | n/a | |
---|
208 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
209 | n/a | l2 = pytree.Leaf(100, "+") |
---|
210 | n/a | l3 = pytree.Leaf(100, "bar") |
---|
211 | n/a | n1 = pytree.Node(1000, [l1, l2, l3]) |
---|
212 | n/a | n2 = pytree.Node(1000, [n1]) |
---|
213 | n/a | self.assertFalse(l1.was_changed) |
---|
214 | n/a | self.assertFalse(n1.was_changed) |
---|
215 | n/a | self.assertFalse(n2.was_changed) |
---|
216 | n/a | |
---|
217 | n/a | n1.changed() |
---|
218 | n/a | self.assertTrue(n1.was_changed) |
---|
219 | n/a | self.assertTrue(n2.was_changed) |
---|
220 | n/a | self.assertFalse(l1.was_changed) |
---|
221 | n/a | |
---|
222 | n/a | def test_leaf_constructor_prefix(self): |
---|
223 | n/a | for prefix in ("xyz_", ""): |
---|
224 | n/a | l1 = pytree.Leaf(100, "self", prefix=prefix) |
---|
225 | n/a | self.assertTrue(str(l1), prefix + "self") |
---|
226 | n/a | self.assertEqual(l1.prefix, prefix) |
---|
227 | n/a | |
---|
228 | n/a | def test_node_constructor_prefix(self): |
---|
229 | n/a | for prefix in ("xyz_", ""): |
---|
230 | n/a | l1 = pytree.Leaf(100, "self") |
---|
231 | n/a | l2 = pytree.Leaf(100, "foo", prefix="_") |
---|
232 | n/a | n1 = pytree.Node(1000, [l1, l2], prefix=prefix) |
---|
233 | n/a | self.assertTrue(str(n1), prefix + "self_foo") |
---|
234 | n/a | self.assertEqual(n1.prefix, prefix) |
---|
235 | n/a | self.assertEqual(l1.prefix, prefix) |
---|
236 | n/a | self.assertEqual(l2.prefix, "_") |
---|
237 | n/a | |
---|
238 | n/a | def test_remove(self): |
---|
239 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
240 | n/a | l2 = pytree.Leaf(100, "foo") |
---|
241 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
242 | n/a | n2 = pytree.Node(1000, [n1]) |
---|
243 | n/a | |
---|
244 | n/a | self.assertEqual(n1.remove(), 0) |
---|
245 | n/a | self.assertEqual(n2.children, []) |
---|
246 | n/a | self.assertEqual(l1.parent, n1) |
---|
247 | n/a | self.assertEqual(n1.parent, None) |
---|
248 | n/a | self.assertEqual(n2.parent, None) |
---|
249 | n/a | self.assertFalse(n1.was_changed) |
---|
250 | n/a | self.assertTrue(n2.was_changed) |
---|
251 | n/a | |
---|
252 | n/a | self.assertEqual(l2.remove(), 1) |
---|
253 | n/a | self.assertEqual(l1.remove(), 0) |
---|
254 | n/a | self.assertEqual(n1.children, []) |
---|
255 | n/a | self.assertEqual(l1.parent, None) |
---|
256 | n/a | self.assertEqual(n1.parent, None) |
---|
257 | n/a | self.assertEqual(n2.parent, None) |
---|
258 | n/a | self.assertTrue(n1.was_changed) |
---|
259 | n/a | self.assertTrue(n2.was_changed) |
---|
260 | n/a | |
---|
261 | n/a | def test_remove_parentless(self): |
---|
262 | n/a | n1 = pytree.Node(1000, []) |
---|
263 | n/a | n1.remove() |
---|
264 | n/a | self.assertEqual(n1.parent, None) |
---|
265 | n/a | |
---|
266 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
267 | n/a | l1.remove() |
---|
268 | n/a | self.assertEqual(l1.parent, None) |
---|
269 | n/a | |
---|
270 | n/a | def test_node_set_child(self): |
---|
271 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
272 | n/a | n1 = pytree.Node(1000, [l1]) |
---|
273 | n/a | |
---|
274 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
275 | n/a | n1.set_child(0, l2) |
---|
276 | n/a | self.assertEqual(l1.parent, None) |
---|
277 | n/a | self.assertEqual(l2.parent, n1) |
---|
278 | n/a | self.assertEqual(n1.children, [l2]) |
---|
279 | n/a | |
---|
280 | n/a | n2 = pytree.Node(1000, [l1]) |
---|
281 | n/a | n2.set_child(0, n1) |
---|
282 | n/a | self.assertEqual(l1.parent, None) |
---|
283 | n/a | self.assertEqual(n1.parent, n2) |
---|
284 | n/a | self.assertEqual(n2.parent, None) |
---|
285 | n/a | self.assertEqual(n2.children, [n1]) |
---|
286 | n/a | |
---|
287 | n/a | self.assertRaises(IndexError, n1.set_child, 4, l2) |
---|
288 | n/a | # I don't care what it raises, so long as it's an exception |
---|
289 | n/a | self.assertRaises(Exception, n1.set_child, 0, list) |
---|
290 | n/a | |
---|
291 | n/a | def test_node_insert_child(self): |
---|
292 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
293 | n/a | n1 = pytree.Node(1000, [l1]) |
---|
294 | n/a | |
---|
295 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
296 | n/a | n1.insert_child(0, l2) |
---|
297 | n/a | self.assertEqual(l2.parent, n1) |
---|
298 | n/a | self.assertEqual(n1.children, [l2, l1]) |
---|
299 | n/a | |
---|
300 | n/a | l3 = pytree.Leaf(100, "abc") |
---|
301 | n/a | n1.insert_child(2, l3) |
---|
302 | n/a | self.assertEqual(n1.children, [l2, l1, l3]) |
---|
303 | n/a | |
---|
304 | n/a | # I don't care what it raises, so long as it's an exception |
---|
305 | n/a | self.assertRaises(Exception, n1.insert_child, 0, list) |
---|
306 | n/a | |
---|
307 | n/a | def test_node_append_child(self): |
---|
308 | n/a | n1 = pytree.Node(1000, []) |
---|
309 | n/a | |
---|
310 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
311 | n/a | n1.append_child(l1) |
---|
312 | n/a | self.assertEqual(l1.parent, n1) |
---|
313 | n/a | self.assertEqual(n1.children, [l1]) |
---|
314 | n/a | |
---|
315 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
316 | n/a | n1.append_child(l2) |
---|
317 | n/a | self.assertEqual(l2.parent, n1) |
---|
318 | n/a | self.assertEqual(n1.children, [l1, l2]) |
---|
319 | n/a | |
---|
320 | n/a | # I don't care what it raises, so long as it's an exception |
---|
321 | n/a | self.assertRaises(Exception, n1.append_child, list) |
---|
322 | n/a | |
---|
323 | n/a | def test_node_next_sibling(self): |
---|
324 | n/a | n1 = pytree.Node(1000, []) |
---|
325 | n/a | n2 = pytree.Node(1000, []) |
---|
326 | n/a | p1 = pytree.Node(1000, [n1, n2]) |
---|
327 | n/a | |
---|
328 | n/a | self.assertIs(n1.next_sibling, n2) |
---|
329 | n/a | self.assertEqual(n2.next_sibling, None) |
---|
330 | n/a | self.assertEqual(p1.next_sibling, None) |
---|
331 | n/a | |
---|
332 | n/a | def test_leaf_next_sibling(self): |
---|
333 | n/a | l1 = pytree.Leaf(100, "a") |
---|
334 | n/a | l2 = pytree.Leaf(100, "b") |
---|
335 | n/a | p1 = pytree.Node(1000, [l1, l2]) |
---|
336 | n/a | |
---|
337 | n/a | self.assertIs(l1.next_sibling, l2) |
---|
338 | n/a | self.assertEqual(l2.next_sibling, None) |
---|
339 | n/a | self.assertEqual(p1.next_sibling, None) |
---|
340 | n/a | |
---|
341 | n/a | def test_node_prev_sibling(self): |
---|
342 | n/a | n1 = pytree.Node(1000, []) |
---|
343 | n/a | n2 = pytree.Node(1000, []) |
---|
344 | n/a | p1 = pytree.Node(1000, [n1, n2]) |
---|
345 | n/a | |
---|
346 | n/a | self.assertIs(n2.prev_sibling, n1) |
---|
347 | n/a | self.assertEqual(n1.prev_sibling, None) |
---|
348 | n/a | self.assertEqual(p1.prev_sibling, None) |
---|
349 | n/a | |
---|
350 | n/a | def test_leaf_prev_sibling(self): |
---|
351 | n/a | l1 = pytree.Leaf(100, "a") |
---|
352 | n/a | l2 = pytree.Leaf(100, "b") |
---|
353 | n/a | p1 = pytree.Node(1000, [l1, l2]) |
---|
354 | n/a | |
---|
355 | n/a | self.assertIs(l2.prev_sibling, l1) |
---|
356 | n/a | self.assertEqual(l1.prev_sibling, None) |
---|
357 | n/a | self.assertEqual(p1.prev_sibling, None) |
---|
358 | n/a | |
---|
359 | n/a | |
---|
360 | n/a | class TestPatterns(support.TestCase): |
---|
361 | n/a | |
---|
362 | n/a | """Unit tests for tree matching patterns.""" |
---|
363 | n/a | |
---|
364 | n/a | def test_basic_patterns(self): |
---|
365 | n/a | # Build a tree |
---|
366 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
367 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
368 | n/a | l3 = pytree.Leaf(100, "foo") |
---|
369 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
370 | n/a | n2 = pytree.Node(1000, [l3]) |
---|
371 | n/a | root = pytree.Node(1000, [n1, n2]) |
---|
372 | n/a | # Build a pattern matching a leaf |
---|
373 | n/a | pl = pytree.LeafPattern(100, "foo", name="pl") |
---|
374 | n/a | r = {} |
---|
375 | n/a | self.assertFalse(pl.match(root, results=r)) |
---|
376 | n/a | self.assertEqual(r, {}) |
---|
377 | n/a | self.assertFalse(pl.match(n1, results=r)) |
---|
378 | n/a | self.assertEqual(r, {}) |
---|
379 | n/a | self.assertFalse(pl.match(n2, results=r)) |
---|
380 | n/a | self.assertEqual(r, {}) |
---|
381 | n/a | self.assertTrue(pl.match(l1, results=r)) |
---|
382 | n/a | self.assertEqual(r, {"pl": l1}) |
---|
383 | n/a | r = {} |
---|
384 | n/a | self.assertFalse(pl.match(l2, results=r)) |
---|
385 | n/a | self.assertEqual(r, {}) |
---|
386 | n/a | # Build a pattern matching a node |
---|
387 | n/a | pn = pytree.NodePattern(1000, [pl], name="pn") |
---|
388 | n/a | self.assertFalse(pn.match(root, results=r)) |
---|
389 | n/a | self.assertEqual(r, {}) |
---|
390 | n/a | self.assertFalse(pn.match(n1, results=r)) |
---|
391 | n/a | self.assertEqual(r, {}) |
---|
392 | n/a | self.assertTrue(pn.match(n2, results=r)) |
---|
393 | n/a | self.assertEqual(r, {"pn": n2, "pl": l3}) |
---|
394 | n/a | r = {} |
---|
395 | n/a | self.assertFalse(pn.match(l1, results=r)) |
---|
396 | n/a | self.assertEqual(r, {}) |
---|
397 | n/a | self.assertFalse(pn.match(l2, results=r)) |
---|
398 | n/a | self.assertEqual(r, {}) |
---|
399 | n/a | |
---|
400 | n/a | def test_wildcard(self): |
---|
401 | n/a | # Build a tree for testing |
---|
402 | n/a | l1 = pytree.Leaf(100, "foo") |
---|
403 | n/a | l2 = pytree.Leaf(100, "bar") |
---|
404 | n/a | l3 = pytree.Leaf(100, "foo") |
---|
405 | n/a | n1 = pytree.Node(1000, [l1, l2]) |
---|
406 | n/a | n2 = pytree.Node(1000, [l3]) |
---|
407 | n/a | root = pytree.Node(1000, [n1, n2]) |
---|
408 | n/a | # Build a pattern |
---|
409 | n/a | pl = pytree.LeafPattern(100, "foo", name="pl") |
---|
410 | n/a | pn = pytree.NodePattern(1000, [pl], name="pn") |
---|
411 | n/a | pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw") |
---|
412 | n/a | r = {} |
---|
413 | n/a | self.assertFalse(pw.match_seq([root], r)) |
---|
414 | n/a | self.assertEqual(r, {}) |
---|
415 | n/a | self.assertFalse(pw.match_seq([n1], r)) |
---|
416 | n/a | self.assertEqual(r, {}) |
---|
417 | n/a | self.assertTrue(pw.match_seq([n2], r)) |
---|
418 | n/a | # These are easier to debug |
---|
419 | n/a | self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"]) |
---|
420 | n/a | self.assertEqual(r["pl"], l1) |
---|
421 | n/a | self.assertEqual(r["pn"], n2) |
---|
422 | n/a | self.assertEqual(r["pw"], [n2]) |
---|
423 | n/a | # But this is equivalent |
---|
424 | n/a | self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]}) |
---|
425 | n/a | r = {} |
---|
426 | n/a | self.assertTrue(pw.match_seq([l1, l3], r)) |
---|
427 | n/a | self.assertEqual(r, {"pl": l3, "pw": [l1, l3]}) |
---|
428 | n/a | self.assertIs(r["pl"], l3) |
---|
429 | n/a | r = {} |
---|
430 | n/a | |
---|
431 | n/a | def test_generate_matches(self): |
---|
432 | n/a | la = pytree.Leaf(1, "a") |
---|
433 | n/a | lb = pytree.Leaf(1, "b") |
---|
434 | n/a | lc = pytree.Leaf(1, "c") |
---|
435 | n/a | ld = pytree.Leaf(1, "d") |
---|
436 | n/a | le = pytree.Leaf(1, "e") |
---|
437 | n/a | lf = pytree.Leaf(1, "f") |
---|
438 | n/a | leaves = [la, lb, lc, ld, le, lf] |
---|
439 | n/a | root = pytree.Node(1000, leaves) |
---|
440 | n/a | pa = pytree.LeafPattern(1, "a", "pa") |
---|
441 | n/a | pb = pytree.LeafPattern(1, "b", "pb") |
---|
442 | n/a | pc = pytree.LeafPattern(1, "c", "pc") |
---|
443 | n/a | pd = pytree.LeafPattern(1, "d", "pd") |
---|
444 | n/a | pe = pytree.LeafPattern(1, "e", "pe") |
---|
445 | n/a | pf = pytree.LeafPattern(1, "f", "pf") |
---|
446 | n/a | pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe], |
---|
447 | n/a | [pa, pb], [pc, pd], [pe, pf]], |
---|
448 | n/a | min=1, max=4, name="pw") |
---|
449 | n/a | self.assertEqual([x[0] for x in pw.generate_matches(leaves)], |
---|
450 | n/a | [3, 5, 2, 4, 6]) |
---|
451 | n/a | pr = pytree.NodePattern(type=1000, content=[pw], name="pr") |
---|
452 | n/a | matches = list(pytree.generate_matches([pr], [root])) |
---|
453 | n/a | self.assertEqual(len(matches), 1) |
---|
454 | n/a | c, r = matches[0] |
---|
455 | n/a | self.assertEqual(c, 1) |
---|
456 | n/a | self.assertEqual(str(r["pr"]), "abcdef") |
---|
457 | n/a | self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf]) |
---|
458 | n/a | for c in "abcdef": |
---|
459 | n/a | self.assertEqual(r["p" + c], pytree.Leaf(1, c)) |
---|
460 | n/a | |
---|
461 | n/a | def test_has_key_example(self): |
---|
462 | n/a | pattern = pytree.NodePattern(331, |
---|
463 | n/a | (pytree.LeafPattern(7), |
---|
464 | n/a | pytree.WildcardPattern(name="args"), |
---|
465 | n/a | pytree.LeafPattern(8))) |
---|
466 | n/a | l1 = pytree.Leaf(7, "(") |
---|
467 | n/a | l2 = pytree.Leaf(3, "x") |
---|
468 | n/a | l3 = pytree.Leaf(8, ")") |
---|
469 | n/a | node = pytree.Node(331, [l1, l2, l3]) |
---|
470 | n/a | r = {} |
---|
471 | n/a | self.assertTrue(pattern.match(node, r)) |
---|
472 | n/a | self.assertEqual(r["args"], [l2]) |
---|