1
2
3 """
4 Test cases related to RelaxNG parsing and validation
5 """
6
7 import unittest, sys, os.path
8
9 this_dir = os.path.dirname(__file__)
10 if this_dir not in sys.path:
11 sys.path.insert(0, this_dir)
12
13 from common_imports import etree, BytesIO, _bytes, HelperTestCase, fileInTestDir
14 from common_imports import doctest, make_doctest
15
18 tree_valid = self.parse('<a><b></b></a>')
19 tree_invalid = self.parse('<a><c></c></a>')
20 schema = self.parse('''\
21 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
22 <zeroOrMore>
23 <element name="b">
24 <text />
25 </element>
26 </zeroOrMore>
27 </element>
28 ''')
29 schema = etree.RelaxNG(schema)
30 self.assertTrue(schema.validate(tree_valid))
31 self.assertTrue(not schema.validate(tree_invalid))
32
34 tree_valid = self.parse('<a><b></b></a>')
35 tree_invalid = self.parse('<a><c></c></a>')
36 schema_file = BytesIO('''\
37 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
38 <zeroOrMore>
39 <element name="b">
40 <text />
41 </element>
42 </zeroOrMore>
43 </element>
44 ''')
45 schema = etree.RelaxNG(file=schema_file)
46 self.assertTrue(schema.validate(tree_valid))
47 self.assertTrue(not schema.validate(tree_invalid))
48
51
53 tree_invalid = self.parse('<a><c></c></a>')
54 schema = self.parse('''\
55 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
56 <zeroOrMore>
57 <element name="b">
58 <text />
59 </element>
60 </zeroOrMore>
61 </element>
62 ''')
63 schema = etree.RelaxNG(schema)
64 self.assertTrue(not schema.validate(tree_invalid))
65 errors = schema.error_log
66 self.assertTrue([ log for log in errors
67 if log.level_name == "ERROR" ])
68 self.assertTrue([ log for log in errors
69 if "not expect" in log.message ])
70
72 schema = self.parse('''\
73 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
74 <zeroOrMore>
75 <element name="b" />
76 </zeroOrMore>
77 </element>
78 ''')
79 self.assertRaises(etree.RelaxNGParseError,
80 etree.RelaxNG, schema)
81
88
90 schema = self.parse('''\
91 <grammar xmlns="http://relaxng.org/ns/structure/1.0">
92 <define name="test">
93 <element name="test"/>
94 </define>
95 </grammar>
96 ''')
97 self.assertRaises(etree.RelaxNGParseError,
98 etree.RelaxNG, schema)
99
107
116
118 tree_valid = self.parse('<a><b></b></a>')
119 tree_invalid = self.parse('<a><c></c></a>')
120 schema = self.parse('''\
121 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
122 <zeroOrMore>
123 <element name="b">
124 <text />
125 </element>
126 </zeroOrMore>
127 </element>
128 ''')
129 self.assertTrue(tree_valid.relaxng(schema))
130 self.assertTrue(not tree_invalid.relaxng(schema))
131
133 tree = self.parse('<a><b>B</b><c>C</c></a>')
134 schema = etree.RelaxNG( self.parse('''\
135 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
136 <element name="b">
137 <text />
138 </element>
139 <element name="c">
140 <text />
141 </element>
142 </element>
143 ''') )
144 self.assertTrue(schema.validate(tree))
145 self.assertTrue(schema.validate(tree))
146
147 schema = etree.RelaxNG( self.parse('''\
148 <element name="b" xmlns="http://relaxng.org/ns/structure/1.0">
149 <text />
150 </element>
151 ''') )
152 c_tree = etree.ElementTree(tree.getroot()[1])
153 self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>'))
154 self.assertTrue(not schema.validate(c_tree))
155
156 b_tree = etree.ElementTree(tree.getroot()[0])
157 self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>'))
158 self.assertTrue(schema.validate(b_tree))
159
160
162 suite = unittest.TestSuite()
163 suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
164 suite.addTests(
165 [make_doctest('../../../doc/validation.txt')])
166 return suite
167
168 if __name__ == '__main__':
169 print('to test use test.py %s' % __file__)
170