Home | Trees | Indices | Help |
|
---|
|
1 # -*- coding: utf-8 -*- 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) # needed for Py3 12 13 from common_imports import etree, BytesIO, _bytes, HelperTestCase, fileInTestDir 14 from common_imports import doctest, make_doctest 15170 17118 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.assertFalse(schema.error_log.filter_from_errors()) 32 33 self.assertFalse(schema.validate(tree_invalid)) 34 self.assertTrue(schema.error_log.filter_from_errors()) 35 36 self.assertTrue(schema.validate(tree_valid)) # repeat valid 37 self.assertFalse(schema.error_log.filter_from_errors()) # repeat valid3840 tree_valid = self.parse('<a><b></b></a>') 41 tree_invalid = self.parse('<a><c></c></a>') 42 schema_file = BytesIO('''\ 43 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 44 <zeroOrMore> 45 <element name="b"> 46 <text /> 47 </element> 48 </zeroOrMore> 49 </element> 50 ''') 51 schema = etree.RelaxNG(file=schema_file) 52 self.assertTrue(schema.validate(tree_valid)) 53 self.assertFalse(schema.validate(tree_invalid))54 5759 tree_invalid = self.parse('<a><c></c></a>') 60 schema = self.parse('''\ 61 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 62 <zeroOrMore> 63 <element name="b"> 64 <text /> 65 </element> 66 </zeroOrMore> 67 </element> 68 ''') 69 schema = etree.RelaxNG(schema) 70 self.assertFalse(schema.validate(tree_invalid)) 71 errors = schema.error_log 72 self.assertTrue([log for log in errors 73 if log.level_name == "ERROR"]) 74 self.assertTrue([log for log in errors 75 if "not expect" in log.message])7678 schema = self.parse('''\ 79 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 80 <zeroOrMore> 81 <element name="b" /> 82 </zeroOrMore> 83 </element> 84 ''') 85 self.assertRaises(etree.RelaxNGParseError, 86 etree.RelaxNG, schema)8789 schema = self.parse('''\ 90 <grammar xmlns="http://relaxng.org/ns/structure/1.0" /> 91 ''') 92 self.assertRaises(etree.RelaxNGParseError, 93 etree.RelaxNG, schema)9496 schema = self.parse('''\ 97 <grammar xmlns="http://relaxng.org/ns/structure/1.0"> 98 <define name="test"> 99 <element name="test"/> 100 </define> 101 </grammar> 102 ''') 103 self.assertRaises(etree.RelaxNGParseError, 104 etree.RelaxNG, schema)105107 # segfault 108 schema = self.parse('''\ 109 <element name="a" xmlns="mynamespace" /> 110 ''') 111 self.assertRaises(etree.RelaxNGParseError, 112 etree.RelaxNG, schema)113115 # this will only work if we access the file through path or 116 # file object.. 117 f = open(fileInTestDir('test1.rng'), 'rb') 118 try: 119 schema = etree.RelaxNG(file=f) 120 finally: 121 f.close()122124 tree_valid = self.parse('<a><b></b></a>') 125 tree_invalid = self.parse('<a><c></c></a>') 126 schema = self.parse('''\ 127 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 128 <zeroOrMore> 129 <element name="b"> 130 <text /> 131 </element> 132 </zeroOrMore> 133 </element> 134 ''') 135 self.assertTrue(tree_valid.relaxng(schema)) 136 self.assertFalse(tree_invalid.relaxng(schema))137139 tree = self.parse('<a><b>B</b><c>C</c></a>') 140 schema = etree.RelaxNG( self.parse('''\ 141 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 142 <element name="b"> 143 <text /> 144 </element> 145 <element name="c"> 146 <text /> 147 </element> 148 </element> 149 ''') ) 150 self.assertTrue(schema.validate(tree)) 151 self.assertFalse(schema.error_log.filter_from_errors()) 152 153 self.assertTrue(schema.validate(tree)) # repeat valid 154 self.assertFalse(schema.error_log.filter_from_errors()) # repeat valid 155 156 schema = etree.RelaxNG( self.parse('''\ 157 <element name="b" xmlns="http://relaxng.org/ns/structure/1.0"> 158 <text /> 159 </element> 160 ''') ) 161 c_tree = etree.ElementTree(tree.getroot()[1]) 162 self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>')) 163 self.assertFalse(schema.validate(c_tree)) 164 self.assertTrue(schema.error_log.filter_from_errors()) 165 166 b_tree = etree.ElementTree(tree.getroot()[0]) 167 self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>')) 168 self.assertTrue(schema.validate(b_tree)) 169 self.assertFalse(schema.error_log.filter_from_errors())173 suite = unittest.TestSuite() 174 suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)]) 175 suite.addTests( 176 [make_doctest('../../../doc/validation.txt')]) 177 return suite178 179 if __name__ == '__main__': 180 print('to test use test.py %s' % __file__) 181
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Nov 13 08:53:01 2015 | http://epydoc.sourceforge.net |