7 sys.path.insert(0, "python")
10 # Memory debug specific
11 libxml2.debugMemory(1)
17 # the testsuite description
19 CONF=os.path.join(os.path.dirname(__file__), "test/relaxng/OASIS/spectest.xml")
20 LOG="check-relaxng-test-suite.log"
21 RES="relaxng-test-results.xml"
25 nb_schemas_success = 0
27 nb_instances_tests = 0
28 nb_instances_success = 0
29 nb_instances_failed = 0
31 libxml2.lineNumbersDefault(1)
33 # Error and warnng callbacks
35 def callback(ctx, str):
37 log.write("%s%s" % (ctx, str))
39 libxml2.registerErrorHandler(callback, "")
45 def resolver(URL, ID, ctxt):
48 if string.find(URL, '#') != -1:
49 URL = URL[0:string.find(URL, '#')]
50 if resources.has_key(URL):
51 return(StringIO.StringIO(resources[URL]))
52 log.write("Resolver failure: asked %s\n" % (URL))
53 log.write("resources: %s\n" % (resources))
57 # Load the previous results
63 # res = libxml2.parseFile(RES)
65 # log.write("Could not parse %s" % (RES))
68 # handle a valid instance
70 def handle_valid(node, schema):
72 global nb_instances_success
73 global nb_instances_failed
78 if child.type != 'text':
79 instance = instance + child.serialize()
83 doc = libxml2.parseDoc(instance)
88 log.write("\nFailed to parse correct instance:\n-----\n")
90 log.write("\n-----\n")
91 nb_instances_failed = nb_instances_failed + 1
95 ctxt = schema.relaxNGNewValidCtxt()
96 ret = doc.relaxNGValidateDoc(ctxt)
100 log.write("\nFailed to validate correct instance:\n-----\n")
102 log.write("\n-----\n")
103 nb_instances_failed = nb_instances_failed + 1
105 nb_instances_success = nb_instances_success + 1
109 # handle an invalid instance
111 def handle_invalid(node, schema):
113 global nb_instances_success
114 global nb_instances_failed
117 child = node.children
119 if child.type != 'text':
120 instance = instance + child.serialize()
124 doc = libxml2.parseDoc(instance)
129 log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
131 log.write("\n-----\n")
135 ctxt = schema.relaxNGNewValidCtxt()
136 ret = doc.relaxNGValidateDoc(ctxt)
140 log.write("\nFailed to detect validation problem in instance:\n-----\n")
142 log.write("\n-----\n")
143 nb_instances_failed = nb_instances_failed + 1
145 nb_instances_success = nb_instances_success + 1
149 # handle an incorrect test
151 def handle_correct(node):
153 global nb_schemas_success
154 global nb_schemas_failed
157 child = node.children
159 if child.type != 'text':
160 schema = schema + child.serialize()
164 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
165 rngs = rngp.relaxNGParse()
169 log.write("\nFailed to compile correct schema:\n-----\n")
171 log.write("\n-----\n")
172 nb_schemas_failed = nb_schemas_failed + 1
174 nb_schemas_success = nb_schemas_success + 1
177 def handle_incorrect(node):
179 global nb_schemas_success
180 global nb_schemas_failed
183 child = node.children
185 if child.type != 'text':
186 schema = schema + child.serialize()
190 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
191 rngs = rngp.relaxNGParse()
195 log.write("\nFailed to detect schema error in:\n-----\n")
197 log.write("\n-----\n")
198 nb_schemas_failed = nb_schemas_failed + 1
200 # log.write("\nSuccess detecting schema error in:\n-----\n")
202 # log.write("\n-----\n")
203 nb_schemas_success = nb_schemas_success + 1
207 # resource handling: keep a dictionary of URL->string mappings
209 def handle_resource(node, dir):
213 name = node.prop('name')
217 if name == None or name == '':
218 log.write("resource has no name")
222 # name = libxml2.buildURI(name, dir)
223 name = dir + '/' + name
226 child = node.children
228 if child.type != 'text':
229 res = res + child.serialize()
231 resources[name] = res
234 # dir handling: pseudo directory resources
236 def handle_dir(node, dir):
238 name = node.prop('name')
242 if name == None or name == '':
243 log.write("resource has no name")
247 # name = libxml2.buildURI(name, dir)
248 name = dir + '/' + name
250 dirs = node.xpathEval('dir')
252 handle_dir(dir, name)
253 res = node.xpathEval('resource')
255 handle_resource(r, name)
258 # handle a testCase element
260 def handle_testCase(node):
261 global nb_schemas_tests
262 global nb_instances_tests
265 sections = node.xpathEval('string(section)')
266 log.write("\n ======== test %d line %d section %s ==========\n" % (
268 nb_schemas_tests, node.lineNo(), sections))
271 print "test %d line %d" % (nb_schemas_tests, node.lineNo())
273 dirs = node.xpathEval('dir')
275 handle_dir(dir, None)
276 res = node.xpathEval('resource')
278 handle_resource(r, None)
280 tsts = node.xpathEval('incorrect')
283 print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
284 schema = handle_incorrect(tsts[0])
286 tsts = node.xpathEval('correct')
289 print "warning test line %d has more than one <correct> example"% (node.lineNo())
290 schema = handle_correct(tsts[0])
292 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
294 nb_schemas_tests = nb_schemas_tests + 1;
296 valids = node.xpathEval('valid')
297 invalids = node.xpathEval('invalid')
298 nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
301 handle_valid(valid, schema)
302 for invalid in invalids:
303 handle_invalid(invalid, schema)
307 # handle a testSuite element
309 def handle_testSuite(node, level = 0):
310 global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
311 global nb_instances_tests, nb_instances_success, nb_instances_failed
314 old_schemas_tests = nb_schemas_tests
315 old_schemas_success = nb_schemas_success
316 old_schemas_failed = nb_schemas_failed
317 old_instances_tests = nb_instances_tests
318 old_instances_success = nb_instances_success
319 old_instances_failed = nb_instances_failed
321 docs = node.xpathEval('documentation')
322 authors = node.xpathEval('author')
326 msg = msg + doc.content + " "
328 msg = msg + "written by "
329 for author in authors:
330 msg = msg + author.content + " "
333 sections = node.xpathEval('section')
334 if sections != [] and level <= 0:
336 for section in sections:
337 msg = msg + section.content + " "
339 print "Tests for section %s" % (msg)
340 for test in node.xpathEval('testCase'):
341 handle_testCase(test)
342 for test in node.xpathEval('testSuite'):
343 handle_testSuite(test, level + 1)
346 if verbose and level >= 1 and sections != []:
348 for section in sections:
349 msg = msg + section.content + " "
350 print "Result of tests for section %s" % (msg)
351 if nb_schemas_tests != old_schemas_tests:
352 print "found %d test schemas: %d success %d failures" % (
353 nb_schemas_tests - old_schemas_tests,
354 nb_schemas_success - old_schemas_success,
355 nb_schemas_failed - old_schemas_failed)
356 if nb_instances_tests != old_instances_tests:
357 print "found %d test instances: %d success %d failures" % (
358 nb_instances_tests - old_instances_tests,
359 nb_instances_success - old_instances_success,
360 nb_instances_failed - old_instances_failed)
362 # Parse the conf file
364 libxml2.substituteEntitiesDefault(1);
365 testsuite = libxml2.parseFile(CONF)
366 libxml2.setEntityLoader(resolver)
367 root = testsuite.getRootElement()
368 if root.name != 'testSuite':
369 print "%s doesn't start with a testSuite element, aborting" % (CONF)
372 print "Running Relax NG testsuite"
373 handle_testSuite(root)
377 if quiet == 0 or nb_schemas_failed != 0:
378 print "found %d test schemas: %d success %d failures" % (
379 nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
380 if quiet == 0 or nb_instances_failed != 0:
381 print "found %d test instances: %d success %d failures" % (
382 nb_instances_tests, nb_instances_success, nb_instances_failed)
386 # Memory debug specific
387 libxml2.relaxNGCleanupTypes()
388 libxml2.cleanupParser()
389 if libxml2.debugMemory(1) == 0:
393 print "Memory leak %d bytes" % (libxml2.debugMemory(1))