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.assert_(schema.validate(tree_valid))
31 self.assert_(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.assert_(schema.validate(tree_valid))
47 self.assert_(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.assert_(not schema.validate(tree_invalid))
65 errors = schema.error_log
66 self.assert_([ log for log in errors
67 if log.level_name == "ERROR" ])
68 self.assert_([ 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
113
115 tree_valid = self.parse('<a><b></b></a>')
116 tree_invalid = self.parse('<a><c></c></a>')
117 schema = self.parse('''\
118 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
119 <zeroOrMore>
120 <element name="b">
121 <text />
122 </element>
123 </zeroOrMore>
124 </element>
125 ''')
126 self.assert_(tree_valid.relaxng(schema))
127 self.assert_(not tree_invalid.relaxng(schema))
128
156
157
159 suite = unittest.TestSuite()
160 suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
161 suite.addTests(
162 [make_doctest('../../../doc/validation.txt')])
163 return suite
164
165 if __name__ == '__main__':
166 print('to test use test.py %s' % __file__)
167