1
2
3 """
4 Test cases related to RelaxNG parsing and validation
5 """
6
7 from __future__ import absolute_import
8
9 import unittest
10
11 from .common_imports import (
12 etree, BytesIO, _bytes, HelperTestCase, fileInTestDir, make_doctest, skipif
13 )
14
15 try:
16 import rnc2rng
17 except ImportError:
18 rnc2rng = None
19
20
43
45 tree_valid = self.parse('<a><b></b></a>')
46 tree_invalid = self.parse('<a><c></c></a>')
47 schema_file = BytesIO('''\
48 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
49 <zeroOrMore>
50 <element name="b">
51 <text />
52 </element>
53 </zeroOrMore>
54 </element>
55 ''')
56 schema = etree.RelaxNG(file=schema_file)
57 self.assertTrue(schema.validate(tree_valid))
58 self.assertFalse(schema.validate(tree_invalid))
59
62
64 tree_invalid = self.parse('<a><c></c></a>')
65 schema = self.parse('''\
66 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
67 <zeroOrMore>
68 <element name="b">
69 <text />
70 </element>
71 </zeroOrMore>
72 </element>
73 ''')
74 schema = etree.RelaxNG(schema)
75 self.assertFalse(schema.validate(tree_invalid))
76 errors = schema.error_log
77 self.assertTrue([log for log in errors
78 if log.level_name == "ERROR"])
79 self.assertTrue([log for log in errors
80 if "not expect" in log.message])
81
83 tree_invalid = self.parse('''\
84 <test>
85 <reference id="my-ref">This is my unique ref.</reference>
86 <data ref="my-ref">Valid data</data>
87 <data ref="myref">Invalid data</data>
88 </test>
89 ''')
90 schema = self.parse('''\
91 <grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
92 xmlns="http://relaxng.org/ns/structure/1.0">
93 <define name="by-ref">
94 <data type="IDREF"/>
95 </define>
96 <start>
97 <element name="test">
98 <zeroOrMore>
99 <element name="reference">
100 <attribute name="id">
101 <data type="ID"/>
102 </attribute>
103 <text/>
104 </element>
105 </zeroOrMore>
106 <zeroOrMore>
107 <element name="data">
108 <attribute name="ref">
109 <data type="IDREF"/>
110 </attribute>
111 <text/>
112 </element>
113 </zeroOrMore>
114 </element>
115 </start>
116 </grammar>
117 ''')
118
119 schema = etree.RelaxNG(schema)
120 self.assertFalse(schema.validate(tree_invalid))
121 errors = schema.error_log
122 self.assertTrue(errors)
123 self.assertTrue([log for log in errors if "IDREF" in log.message])
124 self.assertTrue([log for log in errors if "myref" in log.message])
125
127 schema = self.parse('''\
128 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
129 <zeroOrMore>
130 <element name="b" />
131 </zeroOrMore>
132 </element>
133 ''')
134 self.assertRaises(etree.RelaxNGParseError,
135 etree.RelaxNG, schema)
136
143
145 schema = self.parse('''\
146 <grammar xmlns="http://relaxng.org/ns/structure/1.0">
147 <define name="test">
148 <element name="test"/>
149 </define>
150 </grammar>
151 ''')
152 self.assertRaises(etree.RelaxNGParseError,
153 etree.RelaxNG, schema)
154
162
171
173 tree_valid = self.parse('<a><b></b></a>')
174 tree_invalid = self.parse('<a><c></c></a>')
175 schema = self.parse('''\
176 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
177 <zeroOrMore>
178 <element name="b">
179 <text />
180 </element>
181 </zeroOrMore>
182 </element>
183 ''')
184 self.assertTrue(tree_valid.relaxng(schema))
185 self.assertFalse(tree_invalid.relaxng(schema))
186
219
220
222
223 pytestmark = skipif('rnc2rng is None')
224
226 tree_valid = self.parse('<a><b>B</b><c>C</c></a>')
227 tree_invalid = self.parse('<a><b></b></a>')
228 schema = etree.RelaxNG(file=fileInTestDir('test.rnc'))
229 self.assertTrue(schema.validate(tree_valid))
230 self.assertFalse(schema.validate(tree_invalid))
231
233 with open(fileInTestDir('test.rnc'), 'r') as f:
234 schema = etree.RelaxNG(file=f)
235
236 tree_valid = self.parse('<a><b>B</b><c>C</c></a>')
237 tree_invalid = self.parse('<a><b></b></a>')
238 self.assertTrue(schema.validate(tree_valid))
239 self.assertFalse(schema.validate(tree_invalid))
240
242 tree_valid = self.parse('<a><b>B</b></a>')
243 tree_invalid = self.parse('<a><b>X</b></a>')
244 rnc_str = 'element a { element b { "B" } }'
245 schema = etree.RelaxNG.from_rnc_string(rnc_str)
246 self.assertTrue(schema.validate(tree_valid))
247 self.assertFalse(schema.validate(tree_invalid))
248
249
258
259 if __name__ == '__main__':
260 print('to test use test.py %s' % __file__)
261