source: exist/trunk/python/elementtree-1.3a6-20070220-badc/unittests/test_find.py @ 3578

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070220-badc/unittests/test_find.py@3578
Revision 3578, 4.4 KB checked in by pjkersha, 11 years ago (diff)

Latest releases from Fredrik Lundh. 10 March release has exclusive C14N support with namespace prefixes.

Line 
1# $Id: test_find.py 3341 2007-12-19 18:24:41Z fredrik $
2
3from support import *
4
5def find():
6    """
7    Test find methods (including xpath syntax).
8
9    >>> elem = SAMPLE_XML
10    >>> elem.find("tag").tag
11    'tag'
12    >>> ET.ElementTree(elem).find("tag").tag
13    'tag'
14    >>> elem.find("section/tag").tag
15    'tag'
16    >>> ET.ElementTree(elem).find("section/tag").tag
17    'tag'
18    >>> elem.findtext("tag")
19    'text'
20    >>> elem.findtext("tog")
21    >>> elem.findtext("tog", "default")
22    'default'
23    >>> ET.ElementTree(elem).findtext("tag")
24    'text'
25    >>> elem.findtext("section/tag")
26    'subtext'
27    >>> ET.ElementTree(elem).findtext("section/tag")
28    'subtext'
29    >>> summarize_list(elem.findall("tag"))
30    ['tag#1', 'tag#2']
31    >>> summarize_list(elem.findall("*"))
32    ['tag#1', 'tag#2', 'section']
33    >>> summarize_list(elem.findall(".//*"))
34    ['tag#1', 'tag#2', 'section', 'tag#inner']
35    >>> summarize_list(elem.findall(".//section"))
36    ['section']
37    >>> summarize_list(elem.findall(".//tag"))
38    ['tag#1', 'tag#2', 'tag#inner']
39    >>> summarize_list(elem.findall("section/tag"))
40    ['tag#inner']
41    >>> summarize_list(elem.findall("section//tag"))
42    ['tag#inner']
43    >>> summarize_list(elem.findall("section/*"))
44    ['tag#inner']
45    >>> summarize_list(elem.findall("section//*"))
46    ['tag#inner']
47    >>> summarize_list(elem.findall("section/.//*"))
48    ['tag#inner']
49    >>> summarize_list(elem.findall("*/*"))
50    ['tag#inner']
51    >>> summarize_list(elem.findall("*//*"))
52    ['tag#inner']
53    >>> summarize_list(elem.findall("*/tag"))
54    ['tag#inner']
55    >>> summarize_list(elem.findall("*/./tag"))
56    ['tag#inner']
57    >>> summarize_list(elem.findall("./tag"))
58    ['tag#1', 'tag#2']
59    >>> summarize_list(elem.findall(".//tag"))
60    ['tag#1', 'tag#2', 'tag#inner']
61    >>> summarize_list(elem.findall("././tag"))
62    ['tag#1', 'tag#2']
63    >>> summarize_list(elem.findall(".//tag[@class]"))
64    ['tag#1', 'tag#2', 'tag#inner']
65    >>> summarize_list(elem.findall(".//tag[@class='a']"))
66    ['tag#1']
67    >>> summarize_list(elem.findall(".//tag[@class='b']"))
68    ['tag#2', 'tag#inner']
69    >>> summarize_list(elem.findall(".//tag[@id='inner']"))
70    ['tag#inner']
71    >>> summarize_list(elem.findall(".//tag[@id][@href]"))
72    ['tag#inner']
73    >>> summarize_list(elem.findall(".//section[tag]"))
74    ['section']
75    >>> summarize_list(elem.findall(".//section[element]"))
76    []
77    >>> summarize_list(elem.findall("../tag"))
78    []
79    >>> summarize_list(elem.findall("section/../tag"))
80    ['tag#1', 'tag#2']
81    >>> summarize_list(elem.findall("section/../tag[1]"))
82    ['tag#1']
83    >>> summarize_list(elem.findall("section//"))
84    ['tag#inner']
85    >>> summarize_list(elem.findall(".//tag[1]"))
86    ['tag#1', 'tag#inner']
87    >>> summarize_list(elem.findall(".//tag[2]"))
88    ['tag#2']
89    >>> summarize_list(elem.findall(".//tag[last()]"))
90    ['tag#2', 'tag#inner']
91    >>> summarize_list(elem.findall(".//tag[last()-1]"))
92    ['tag#1']
93    >>> summarize_list(ET.ElementTree(elem).findall("./tag"))
94    ['tag#1', 'tag#2']
95
96    """
97
98
99def find_ns():
100    """
101    Test find methods with namespace notation.
102
103    >>> elem = SAMPLE_XML_NS
104    >>> namespaces=dict(ns='ns')
105    >>> elem.find("{ns}tag").tag
106    '{ns}tag'
107    >>> ET.ElementTree(elem).find("{ns}tag").tag
108    '{ns}tag'
109    >>> elem.find("ns:tag", namespaces=namespaces).tag
110    '{ns}tag'
111    >>> ET.ElementTree(elem).find("{ns}tag").tag
112    '{ns}tag'
113    >>> ET.ElementTree(elem).find("ns:tag", namespaces=namespaces).tag
114    '{ns}tag'
115
116    (to be continued)
117
118    """
119
120def bad_find():
121    """
122    Check bad or unsupported path expressions.
123
124    >>> elem = SAMPLE_XML
125    >>> elem.findall("/tag")
126    Traceback (most recent call last):
127    SyntaxError: cannot use absolute path on element
128    """
129
130# --------------------------------------------------------------------
131
132def bug_xmltoolkit25():
133    """
134    typo in findtext
135
136    >>> tree = ET.ElementTree(SAMPLE_XML)
137    >>> tree.findtext("tag")
138    'text'
139    >>> tree.findtext("section/tag")
140    'subtext'
141    """
142
143def bug_xmltoolkit28():
144    """
145    .//tag causes exceptions
146
147    >>> tree = ET.XML("<doc><table><tbody/></table></doc>")
148    >>> summarize_list(tree.findall(".//thead"))
149    []
150    >>> summarize_list(tree.findall(".//tbody"))
151    ['tbody']
152    """
Note: See TracBrowser for help on using the repository browser.