This file is indexed.

/usr/lib/python3/dist-packages/genshi/tests/path.py is in python3-genshi 0.7-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://genshi.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://genshi.edgewall.org/log/.

import doctest
import unittest

from genshi.input import XML
from genshi.path import Path, PathParser, PathSyntaxError, GenericStrategy, \
                        SingleStepStrategy, SimplePathStrategy


class FakePath(Path):
    def __init__(self, strategy):
        self.strategy = strategy
    def test(self, ignore_context = False):
        return self.strategy.test(ignore_context)


class PathTestCase(unittest.TestCase):

    strategies = [GenericStrategy, SingleStepStrategy, SimplePathStrategy]

    def test_error_no_absolute_path(self):
        self.assertRaises(PathSyntaxError, Path, '/root')

    def test_error_unsupported_axis(self):
        self.assertRaises(PathSyntaxError, Path, '..')
        self.assertRaises(PathSyntaxError, Path, 'parent::ma')

    def test_1step(self):
        xml = XML('<root><elem/></root>')
        self._test_eval(
            path = 'elem',
            equiv = '<Path "child::elem">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = 'elem',
            equiv = '<Path "child::elem">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = 'child::elem',
            equiv = '<Path "child::elem">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = '//elem',
            equiv = '<Path "descendant-or-self::elem">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = 'descendant::elem',
            equiv = '<Path "descendant::elem">',
            input = xml,
            output = '<elem/>'
        )

    def test_1step_self(self):
        xml = XML('<root><elem/></root>')
        self._test_eval(
            path = '.',
            equiv = '<Path "self::node()">',
            input = xml,
            output = '<root><elem/></root>'
        )
        self._test_eval(
            path = 'self::node()',
            equiv = '<Path "self::node()">',
            input = xml,
            output = '<root><elem/></root>'
        )

    def test_1step_wildcard(self):
        xml = XML('<root><elem/></root>')
        self._test_eval(
            path = '*',
            equiv = '<Path "child::*">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = 'child::*',
            equiv = '<Path "child::*">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = 'child::node()',
            equiv = '<Path "child::node()">',
            input = xml,
            output = '<elem/>'
        )
        self._test_eval(
            path = '//*',
            equiv = '<Path "descendant-or-self::*">',
            input = xml,
            output = '<root><elem/></root>'
        )

    def test_1step_attribute(self):
        self._test_eval(
            path = '@foo',
            equiv = '<Path "attribute::foo">',
            input = XML('<root/>'),
            output = ''
        )
        xml = XML('<root foo="bar"/>')
        self._test_eval(
            path = '@foo',
            equiv = '<Path "attribute::foo">',
            input = xml,
            output = 'bar'
        )
        self._test_eval(
            path = './@foo',
            equiv = '<Path "self::node()/attribute::foo">',
            input = xml,
            output = 'bar'
        )

    def test_1step_text(self):
        xml = XML('<root>Hey</root>')
        self._test_eval(
            path = 'text()',
            equiv = '<Path "child::text()">',
            input = xml,
            output = 'Hey'
        )
        self._test_eval(
            path = './text()',
            equiv = '<Path "self::node()/child::text()">',
            input = xml,
            output = 'Hey'
        )
        self._test_eval(
            path = '//text()',
            equiv = '<Path "descendant-or-self::text()">',
            input = xml,
            output = 'Hey'
        )
        self._test_eval(
            path = './/text()',
            equiv = '<Path "self::node()/descendant-or-self::node()/child::text()">',
            input = xml,
            output = 'Hey'
        )

    def test_2step(self):
        xml = XML('<root><foo/><bar/></root>')
        self._test_eval('*', input=xml, output='<foo/><bar/>')
        self._test_eval('bar', input=xml, output='<bar/>')
        self._test_eval('baz', input=xml, output='')

    def test_2step_attribute(self):
        xml = XML('<elem class="x"><span id="joe">Hey Joe</span></elem>')
        self._test_eval('@*', input=xml, output='x')
        self._test_eval('./@*', input=xml, output='x')
        self._test_eval('.//@*', input=xml, output='xjoe')
        self._test_eval('*/@*', input=xml, output='joe')

        xml = XML('<elem><foo id="1"/><foo id="2"/></elem>')
        self._test_eval('@*', input=xml, output='')
        self._test_eval('foo/@*', input=xml, output='12')

    def test_2step_complex(self):
        xml = XML('<root><foo><bar/></foo></root>')
        self._test_eval(
            path = 'foo/bar',
            equiv = '<Path "child::foo/child::bar">',
            input = xml,
            output = '<bar/>'
        )
        self._test_eval(
            path = './bar',
            equiv = '<Path "self::node()/child::bar">',
            input = xml,
            output = ''
        )
        self._test_eval(
            path = 'foo/*',
            equiv = '<Path "child::foo/child::*">',
            input = xml,
            output = '<bar/>'
        )
        xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
        self._test_eval(
            path = './bar',
            equiv = '<Path "self::node()/child::bar">',
            input = xml,
            output = '<bar id="2"/>'
        )
        xml = XML('''<table>
            <tr><td>1</td><td>One</td></tr>
            <tr><td>2</td><td>Two</td></tr>
        </table>''')
        self._test_eval(
            path = 'tr/td[1]',
            input = xml,
            output = '<td>1</td><td>2</td>'
        )
        xml = XML('''<ul>
            <li>item1
                <ul><li>subitem11</li></ul>
            </li>
            <li>item2
                <ul><li>subitem21</li></ul>
            </li>
        </ul>''')
        self._test_eval(
            path = 'li[2]/ul',
            input = xml,
            output = '<ul><li>subitem21</li></ul>'
        )

    def test_2step_text(self):
        xml = XML('<root><item>Foo</item></root>')
        self._test_eval(
            path = 'item/text()',
            equiv = '<Path "child::item/child::text()">',
            input = xml,
            output = 'Foo'
        )
        self._test_eval(
            path = '*/text()',
            equiv = '<Path "child::*/child::text()">',
            input = xml,
            output = 'Foo'
        )
        self._test_eval(
            path = '//text()',
            equiv = '<Path "descendant-or-self::text()">',
            input = xml,
            output = 'Foo'
        )
        self._test_eval(
            path = './text()',
            equiv = '<Path "self::node()/child::text()">',
            input = xml,
            output = ''
        )
        xml = XML('<root><item>Foo</item><item>Bar</item></root>')
        self._test_eval(
            path = 'item/text()',
            equiv = '<Path "child::item/child::text()">',
            input = xml,
            output = 'FooBar'
        )
        xml = XML('<root><item><name>Foo</name><sub><name>Bar</name></sub></item></root>') 
        self._test_eval(
            path = 'item/name/text()',
            equiv = '<Path "child::item/child::name/child::text()">',
            input = xml,
            output = 'Foo'
        )

    def test_3step(self):
        xml = XML('<root><foo><bar/></foo></root>')
        self._test_eval(
            path = 'foo/*',
            equiv = '<Path "child::foo/child::*">',
            input = xml,
            output = '<bar/>'
        )

    def test_3step_complex(self):
        self._test_eval(
            path = '*/bar',
            equiv = '<Path "child::*/child::bar">',
            input = XML('<root><foo><bar/></foo></root>'),
            output = '<bar/>'
        )
        self._test_eval(
            path = '//bar',
            equiv = '<Path "descendant-or-self::bar">',
            input = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>'),
            output = '<bar id="1"/><bar id="2"/>'
        )

    def test_3step_complex_text(self):
        xml = XML('<root><item><bar>Some text </bar><baz><bar>in here.</bar></baz></item></root>')
        self._test_eval(
            path = 'item/bar/text()',
            equiv = '<Path "child::item/child::bar/child::text()">',
            input = xml,
            output = 'Some text '
        )
        self._test_eval(
            path = 'item//bar/text()',
            equiv = '<Path "child::item/descendant-or-self::node()/child::bar/child::text()">',
            input = xml,
            output = 'Some text in here.'
        )

    def test_node_type_comment(self):
        xml = XML('<root><!-- commented --></root>')
        self._test_eval(
            path = 'comment()',
            equiv = '<Path "child::comment()">',
            input = xml,
            output = '<!-- commented -->'
        )

    def test_node_type_text(self):
        xml = XML('<root>Some text <br/>in here.</root>')
        self._test_eval(
            path = 'text()',
            equiv = '<Path "child::text()">',
            input = xml,
            output = 'Some text in here.'
        )

    def test_node_type_node(self):
        xml = XML('<root>Some text <br/>in here.</root>')
        self._test_eval(
            path = 'node()',
            equiv = '<Path "child::node()">',
            input = xml,
            output = 'Some text <br/>in here.'
        )

    def test_node_type_processing_instruction(self):
        xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>')
        self._test_eval(
            path = '//processing-instruction()',
            equiv = '<Path "descendant-or-self::processing-instruction()">',
            input = xml,
            output = '<?python x = 2 * 3 ?><?php echo("x") ?>'
        )
        self._test_eval(
            path = 'processing-instruction()',
            equiv = '<Path "child::processing-instruction()">',
            input = xml,
            output = '<?php echo("x") ?>'
        )
        self._test_eval(
            path = 'processing-instruction("php")',
            equiv = '<Path "child::processing-instruction(\"php\")">',
            input = xml,
            output = '<?php echo("x") ?>'
        )

    def test_simple_union(self):
        xml = XML("""<body>1<br />2<br />3<br /></body>""")
        self._test_eval(
            path = '*|text()',
            equiv = '<Path "child::*|child::text()">',
            input = xml,
            output = '1<br/>2<br/>3<br/>'
        )

    def test_predicate_name(self):
        xml = XML('<root><foo/><bar/></root>')
        self._test_eval('*[name()="foo"]', input=xml, output='<foo/>')

    def test_predicate_localname(self):
        xml = XML('<root><foo xmlns="NS"/><bar/></root>')
        self._test_eval('*[local-name()="foo"]', input=xml,
                              output='<foo xmlns="NS"/>')

    def test_predicate_namespace(self):
        xml = XML('<root><foo xmlns="NS"/><bar/></root>')
        self._test_eval('*[namespace-uri()="NS"]', input=xml,
                                output='<foo xmlns="NS"/>')

    def test_predicate_not_name(self):
        xml = XML('<root><foo/><bar/></root>')
        self._test_eval('*[not(name()="foo")]', input=xml,
                              output='<bar/>')

    def test_predicate_attr(self):
        xml = XML('<root><item/><item important="very"/></root>')
        self._test_eval('item[@important]', input=xml,
                              output='<item important="very"/>')
        self._test_eval('item[@important="very"]', input=xml,
                              output='<item important="very"/>')

    def test_predicate_attr_equality(self):
        xml = XML('<root><item/><item important="notso"/></root>')
        self._test_eval('item[@important="very"]', input=xml, output='')
        self._test_eval('item[@important!="very"]', input=xml,
                              output='<item/><item important="notso"/>')

    def test_predicate_attr_greater_than(self):
        xml = XML('<root><item priority="3"/></root>')
        self._test_eval('item[@priority>3]', input=xml, output='')
        self._test_eval('item[@priority>2]', input=xml,
                              output='<item priority="3"/>')

    def test_predicate_attr_less_than(self):
        xml = XML('<root><item priority="3"/></root>')
        self._test_eval('item[@priority<3]', input=xml, output='')
        self._test_eval('item[@priority<4]', input=xml,
                              output='<item priority="3"/>')

    def test_predicate_attr_and(self):
        xml = XML('<root><item/><item important="very"/></root>')
        self._test_eval('item[@important and @important="very"]',
                                input=xml, output='<item important="very"/>')
        self._test_eval('item[@important and @important="notso"]',
                                input=xml, output='')

    def test_predicate_attr_or(self):
        xml = XML('<root><item/><item important="very"/></root>')
        self._test_eval('item[@urgent or @important]', input=xml,
                              output='<item important="very"/>')
        self._test_eval('item[@urgent or @notso]', input=xml, output='')

    def test_predicate_boolean_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[boolean("")]', input=xml, output='')
        self._test_eval('*[boolean("yo")]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[boolean(0)]', input=xml, output='')
        self._test_eval('*[boolean(42)]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[boolean(false())]', input=xml, output='')
        self._test_eval('*[boolean(true())]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_ceil_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[ceiling("4.5")=5]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_concat_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[name()=concat("f", "oo")]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_contains_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[contains(name(), "oo")]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_matches_function(self):
        xml = XML('<root><foo>bar</foo><bar>foo</bar></root>')
        self._test_eval('*[matches(name(), "foo|bar")]', input=xml,
                              output='<foo>bar</foo><bar>foo</bar>')

    def test_predicate_false_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[false()]', input=xml, output='')

    def test_predicate_floor_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[floor("4.5")=4]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_normalize_space_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[normalize-space(" foo   bar  ")="foo bar"]',
                                input=xml, output='<foo>bar</foo>')

    def test_predicate_number_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[number("3.0")=3]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[number("3.0")=3.0]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[number("0.1")=.1]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_round_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[round("4.4")=4]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[round("4.6")=5]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_starts_with_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[starts-with(name(), "f")]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[starts-with(name(), "b")]', input=xml,
                              output='')

    def test_predicate_string_length_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[string-length(name())=3]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_substring_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[substring(name(), 1)="oo"]', input=xml,
                              output='<foo>bar</foo>')
        self._test_eval('*[substring(name(), 1, 1)="o"]', input=xml,
                              output='<foo>bar</foo>')

    def test_predicate_substring_after_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[substring-after(name(), "f")="oo"]', input=xml,
                                output='<foo>bar</foo>')

    def test_predicate_substring_before_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[substring-before(name(), "oo")="f"]',
                                input=xml, output='<foo>bar</foo>')

    def test_predicate_translate_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[translate(name(), "fo", "ba")="baa"]',
                                input=xml, output='<foo>bar</foo>')

    def test_predicate_true_function(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval('*[true()]', input=xml, output='<foo>bar</foo>')

    def test_predicate_variable(self):
        xml = XML('<root><foo>bar</foo></root>')
        self._test_eval(
            path = '*[name()=$bar]',
            input = xml,
            output = '<foo>bar</foo>',
            variables = {'bar': 'foo'}
        )

    def test_predicate_position(self):
        xml = XML('<root><foo id="a1"/><foo id="a2"/><foo id="a3"/></root>')
        self._test_eval('*[2]', input=xml, output='<foo id="a2"/>')

    def test_predicate_attr_and_position(self):
        xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>')
        self._test_eval('*[@id][2]', input=xml, output='<foo id="a2"/>')

    def test_predicate_position_and_attr(self):
        xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>')
        self._test_eval('*[1][@id]', input=xml, output='')
        self._test_eval('*[2][@id]', input=xml, output='<foo id="a1"/>')

    def test_predicate_advanced_position(self):
        xml = XML('<root><a><b><c><d><e/></d></c></b></a></root>')
        self._test_eval(   'descendant-or-self::*/'
                                'descendant-or-self::*/'
                                'descendant-or-self::*[2]/'
                                'self::*/descendant::*[3]', input=xml,
                                output='<d><e/></d>')

    def test_predicate_child_position(self):
        xml = XML('\
<root><a><b>1</b><b>2</b><b>3</b></a><a><b>4</b><b>5</b></a></root>')
        self._test_eval('//a/b[2]', input=xml, output='<b>2</b><b>5</b>')
        self._test_eval('//a/b[3]', input=xml, output='<b>3</b>')

    def test_name_with_namespace(self):
        xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>')
        self._test_eval(
            path = 'f:foo',
            equiv = '<Path "child::f:foo">',
            input = xml,
            output = '<foo xmlns="FOO">bar</foo>',
            namespaces = {'f': 'FOO'}
        )

    def test_wildcard_with_namespace(self):
        xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>')
        self._test_eval(
            path = 'f:*',
            equiv = '<Path "child::f:*">',
            input = xml,
            output = '<foo xmlns="FOO">bar</foo>',
            namespaces = {'f': 'FOO'}
        )

    def test_predicate_termination(self):
        """
        Verify that a patch matching the self axis with a predicate doesn't
        cause an infinite loop. See <http://genshi.edgewall.org/ticket/82>.
        """
        xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
        self._test_eval('.[@flag="1"]/*', input=xml,
                              output='<li>a</li><li>b</li>')

        xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
        self._test_eval('.[@flag="0"]/*', input=xml, output='')

    def test_attrname_with_namespace(self):
        xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>')
        self._test_eval('foo[@f:bar]', input=xml,
                              output='<foo xmlns:ns1="FOO" ns1:bar="baz"/>',
                              namespaces={'f': 'FOO'})

    def test_attrwildcard_with_namespace(self):
        xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>')
        self._test_eval('foo[@f:*]', input=xml,
                              output='<foo xmlns:ns1="FOO" ns1:bar="baz"/>',
                              namespaces={'f': 'FOO'})

    def test_self_and_descendant(self):
        xml = XML('<root><foo/></root>')
        self._test_eval('self::root', input=xml, output='<root><foo/></root>')
        self._test_eval('self::foo', input=xml, output='')
        self._test_eval('descendant::root', input=xml, output='')
        self._test_eval('descendant::foo', input=xml, output='<foo/>')
        self._test_eval('descendant-or-self::root', input=xml, 
                                output='<root><foo/></root>')
        self._test_eval('descendant-or-self::foo', input=xml, output='<foo/>')

    def test_long_simple_paths(self):
        xml = XML('<root><a><b><a><d><a><b><a><b><a><b><a><c>!'
                    '</c></a></b></a></b></a></b></a></d></a></b></a></root>')
        self._test_eval('//a/b/a/b/a/c', input=xml, output='<c>!</c>')
        self._test_eval('//a/b/a/c', input=xml, output='<c>!</c>')
        self._test_eval('//a/c', input=xml, output='<c>!</c>')
        self._test_eval('//c', input=xml, output='<c>!</c>')
        # Please note that a//b is NOT the same as a/descendant::b 
        # it is a/descendant-or-self::node()/b, which SimplePathStrategy
        # does NOT support
        self._test_eval('a/b/descendant::a/c', input=xml, output='<c>!</c>')
        self._test_eval('a/b/descendant::a/d/descendant::a/c',
                              input=xml, output='<c>!</c>')
        self._test_eval('a/b/descendant::a/d/a/c', input=xml, output='')
        self._test_eval('//d/descendant::b/descendant::b/descendant::b'
                              '/descendant::c', input=xml, output='<c>!</c>')
        self._test_eval('//d/descendant::b/descendant::b/descendant::b'
                              '/descendant::b/descendant::c', input=xml,
                              output='')

    def _test_support(self, strategy_class, text):
        path = PathParser(text, None, -1).parse()[0]
        return strategy_class.supports(path)

    def test_simple_strategy_support(self):
        self.assertTrue(self._test_support(SimplePathStrategy, 'a/b'))
        self.assertTrue(self._test_support(SimplePathStrategy, 'self::a/b'))
        self.assertTrue(self._test_support(SimplePathStrategy, 'descendant::a/b'))
        self.assertTrue(self._test_support(SimplePathStrategy,
                         'descendant-or-self::a/b'))
        self.assertTrue(self._test_support(SimplePathStrategy, '//a/b'))
        self.assertTrue(self._test_support(SimplePathStrategy, 'a/@b'))
        self.assertTrue(self._test_support(SimplePathStrategy, 'a/text()'))

        # a//b is a/descendant-or-self::node()/b
        self.assertTrue(not self._test_support(SimplePathStrategy, 'a//b'))
        self.assertTrue(not self._test_support(SimplePathStrategy, 'node()/@a'))
        self.assertTrue(not self._test_support(SimplePathStrategy, '@a'))
        self.assertTrue(not self._test_support(SimplePathStrategy, 'foo:bar'))
        self.assertTrue(not self._test_support(SimplePathStrategy, 'a/@foo:bar'))

    def _test_strategies(self, input, path, output,
                         namespaces=None, variables=None):
        for strategy in self.strategies:
            if not strategy.supports(path):
                continue
            s = strategy(path)
            rendered = FakePath(s).select(input, namespaces=namespaces,
                                          variables=variables) \
                                  .render(encoding=None)
            msg = 'Bad render using %s strategy' % str(strategy)
            msg += '\nExpected:\t%r' % output
            msg += '\nRendered:\t%r' % rendered
            self.assertEqual(output, rendered, msg)

    def _test_eval(self, path, equiv=None, input=None, output='',
                         namespaces=None, variables=None):
        path = Path(path)
        if equiv is not None:
            self.assertEqual(equiv, repr(path))

        if input is None:
            return

        rendered = path.select(input, namespaces=namespaces,
                               variables=variables).render(encoding=None)
        msg = 'Bad output using whole path'
        msg += '\nExpected:\t%r' % output
        msg += '\nRendered:\t%r' % rendered
        self.assertEqual(output, rendered, msg)

        if len(path.paths) == 1:
            self._test_strategies(input, path.paths[0], output,
                                  namespaces=namespaces, variables=variables)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(Path.__module__))
    suite.addTest(unittest.makeSuite(PathTestCase, 'test'))
    return suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')