7 sys.path.append("python")
10 # Memory debug specific
11 libxml2.debugMemory(1)
16 # the testsuite description
18 CONF="test/relaxng/OASIS/spectest.xml"
19 LOG="check-relaxng-test-suite.log"
20 RES="relaxng-test-results.xml"
24 nb_schemas_success = 0
26 nb_instances_tests = 0
27 nb_instances_success = 0
28 nb_instances_failed = 0
30 libxml2.lineNumbersDefault(1)
32 # Error and warnng callbacks
34 def callback(ctx, str):
36 log.write("%s%s" % (ctx, str))
38 libxml2.registerErrorHandler(callback, "")
44 def resolver(URL, ID, ctxt):
47 if resources.has_key(URL):
48 return(StringIO.StringIO(resources[URL]))
49 log.write("Resolver failure: asked %s\n" % (URL))
50 log.write("resources: %s\n" % (resources))
54 # Load the previous results
60 # res = libxml2.parseFile(RES)
62 # log.write("Could not parse %s" % (RES))
65 # handle a valid instance
67 def handle_valid(node, schema):
69 global nb_instances_success
70 global nb_instances_failed
75 if child.type != 'text':
76 instance = instance + child.serialize()
80 doc = libxml2.parseDoc(instance)
85 log.write("\nFailed to parse correct instance:\n-----\n")
87 log.write("\n-----\n")
88 nb_instances_failed = nb_instances_failed + 1
92 ctxt = schema.relaxNGNewValidCtxt()
93 ret = doc.relaxNGValidateDoc(ctxt)
97 log.write("\nFailed to validate correct instance:\n-----\n")
99 log.write("\n-----\n")
100 nb_instances_failed = nb_instances_failed + 1
102 nb_instances_success = nb_instances_success + 1
106 # handle an invalid instance
108 def handle_invalid(node, schema):
110 global nb_instances_success
111 global nb_instances_failed
114 child = node.children
116 if child.type != 'text':
117 instance = instance + child.serialize()
121 doc = libxml2.parseDoc(instance)
126 log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
128 log.write("\n-----\n")
132 ctxt = schema.relaxNGNewValidCtxt()
133 ret = doc.relaxNGValidateDoc(ctxt)
137 log.write("\nFailed to detect validation problem in instance:\n-----\n")
139 log.write("\n-----\n")
140 nb_instances_failed = nb_instances_failed + 1
142 nb_instances_success = nb_instances_success + 1
146 # handle an incorrect test
148 def handle_correct(node):
150 global nb_schemas_success
151 global nb_schemas_failed
154 child = node.children
156 if child.type != 'text':
157 schema = schema + child.serialize()
161 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
162 rngs = rngp.relaxNGParse()
166 log.write("\nFailed to compile correct schema:\n-----\n")
168 log.write("\n-----\n")
169 nb_schemas_failed = nb_schemas_failed + 1
171 nb_schemas_success = nb_schemas_success + 1
174 def handle_incorrect(node):
176 global nb_schemas_success
177 global nb_schemas_failed
180 child = node.children
182 if child.type != 'text':
183 schema = schema + child.serialize()
187 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
188 rngs = rngp.relaxNGParse()
192 log.write("\nFailed to detect schema error in:\n-----\n")
194 log.write("\n-----\n")
195 nb_schemas_failed = nb_schemas_failed + 1
197 # log.write("\nSuccess detecting schema error in:\n-----\n")
199 # log.write("\n-----\n")
200 nb_schemas_success = nb_schemas_success + 1
204 # resource handling: keep a dictionary of URL->string mappings
206 def handle_resource(node, dir):
210 name = node.prop('name')
214 if name == None or name == '':
215 log.write("resource has no name")
219 # name = libxml2.buildURI(name, dir)
220 name = dir + '/' + name
223 child = node.children
225 if child.type != 'text':
226 res = res + child.serialize()
228 resources[name] = res
231 # dir handling: pseudo directory resources
233 def handle_dir(node, dir):
235 name = node.prop('name')
239 if name == None or name == '':
240 log.write("resource has no name")
244 # name = libxml2.buildURI(name, dir)
245 name = dir + '/' + name
247 dirs = node.xpathEval('dir')
249 handle_dir(dir, name)
250 res = node.xpathEval('resource')
252 handle_resource(r, name)
255 # handle a testCase element
257 def handle_testCase(node):
258 global nb_schemas_tests
259 global nb_instances_tests
262 sections = node.xpathEval('string(section)')
263 log.write("\n ======== test %d line %d section %s ==========\n" % (
265 nb_schemas_tests, node.lineNo(), sections))
268 print "test %d line %d" % (nb_schemas_tests, node.lineNo())
270 dirs = node.xpathEval('dir')
272 handle_dir(dir, None)
273 res = node.xpathEval('resource')
275 handle_resource(r, None)
277 tsts = node.xpathEval('incorrect')
280 print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
281 schema = handle_incorrect(tsts[0])
283 tsts = node.xpathEval('correct')
286 print "warning test line %d has more than one <correct> example"% (node.lineNo())
287 schema = handle_correct(tsts[0])
289 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
291 nb_schemas_tests = nb_schemas_tests + 1;
293 valids = node.xpathEval('valid')
294 invalids = node.xpathEval('invalid')
295 nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
298 handle_valid(valid, schema)
299 for invalid in invalids:
300 handle_invalid(invalid, schema)
304 # handle a testSuite element
306 def handle_testSuite(node, level = 0):
307 global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
308 global nb_instances_tests, nb_instances_success, nb_instances_failed
310 old_schemas_tests = nb_schemas_tests
311 old_schemas_success = nb_schemas_success
312 old_schemas_failed = nb_schemas_failed
313 old_instances_tests = nb_instances_tests
314 old_instances_success = nb_instances_success
315 old_instances_failed = nb_instances_failed
317 docs = node.xpathEval('documentation')
318 authors = node.xpathEval('author')
322 msg = msg + doc.content + " "
324 msg = msg + "written by "
325 for author in authors:
326 msg = msg + author.content + " "
328 sections = node.xpathEval('section')
329 if sections != [] and level <= 0:
331 for section in sections:
332 msg = msg + section.content + " "
333 print "Tests for section %s" % (msg)
334 for test in node.xpathEval('testCase'):
335 handle_testCase(test)
336 for test in node.xpathEval('testSuite'):
337 handle_testSuite(test, level + 1)
340 if verbose and level >= 1 and sections != []:
342 for section in sections:
343 msg = msg + section.content + " "
344 print "Result of tests for section %s" % (msg)
345 if nb_schemas_tests != old_schemas_tests:
346 print "found %d test schemas: %d success %d failures" % (
347 nb_schemas_tests - old_schemas_tests,
348 nb_schemas_success - old_schemas_success,
349 nb_schemas_failed - old_schemas_failed)
350 if nb_instances_tests != old_instances_tests:
351 print "found %d test instances: %d success %d failures" % (
352 nb_instances_tests - old_instances_tests,
353 nb_instances_success - old_instances_success,
354 nb_instances_failed - old_instances_failed)
356 # Parse the conf file
358 libxml2.substituteEntitiesDefault(1);
359 testsuite = libxml2.parseFile(CONF)
360 libxml2.setEntityLoader(resolver)
361 root = testsuite.getRootElement()
362 if root.name != 'testSuite':
363 print "%s doesn't start with a testSuite element, aborting" % (CONF)
365 print "Running Relax NG testsuite"
366 handle_testSuite(root)
368 print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
369 nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
370 print "found %d test instances: %d success %d failures" % (
371 nb_instances_tests, nb_instances_success, nb_instances_failed)
375 # Memory debug specific
376 libxml2.relaxNGCleanupTypes()
377 libxml2.cleanupParser()
378 if libxml2.debugMemory(1) == 0:
381 print "Memory leak %d bytes" % (libxml2.debugMemory(1))