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/xsdtest/xsdtestsuite.xml")
20 LOG="check-xsddata-test-suite.log"
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 # handle a valid instance
56 def handle_valid(node, schema):
58 global nb_instances_success
59 global nb_instances_failed
61 instance = node.prop("dtd")
66 if child.type != 'text':
67 instance = instance + child.serialize()
70 mem = libxml2.debugMemory(1);
72 doc = libxml2.parseDoc(instance)
77 log.write("\nFailed to parse correct instance:\n-----\n")
79 log.write("\n-----\n")
80 nb_instances_failed = nb_instances_failed + 1
84 print "instance line %d" % (node.lineNo())
87 ctxt = schema.relaxNGNewValidCtxt()
88 ret = doc.relaxNGValidateDoc(ctxt)
94 if mem != libxml2.debugMemory(1):
95 print "validating instance %d line %d leaks" % (
96 nb_instances_tests, node.lineNo())
99 log.write("\nFailed to validate correct instance:\n-----\n")
101 log.write("\n-----\n")
102 nb_instances_failed = nb_instances_failed + 1
104 nb_instances_success = nb_instances_success + 1
107 # handle an invalid instance
109 def handle_invalid(node, schema):
111 global nb_instances_success
112 global nb_instances_failed
114 instance = node.prop("dtd")
117 child = node.children
119 if child.type != 'text':
120 instance = instance + child.serialize()
123 # mem = libxml2.debugMemory(1);
126 doc = libxml2.parseDoc(instance)
131 log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
133 log.write("\n-----\n")
137 print "instance line %d" % (node.lineNo())
140 ctxt = schema.relaxNGNewValidCtxt()
141 ret = doc.relaxNGValidateDoc(ctxt)
148 # if mem != libxml2.debugMemory(1):
149 # print "validating instance %d line %d leaks" % (
150 # nb_instances_tests, node.lineNo())
153 log.write("\nFailed to detect validation problem in instance:\n-----\n")
155 log.write("\n-----\n")
156 nb_instances_failed = nb_instances_failed + 1
158 nb_instances_success = nb_instances_success + 1
161 # handle an incorrect test
163 def handle_correct(node):
165 global nb_schemas_success
166 global nb_schemas_failed
169 child = node.children
171 if child.type != 'text':
172 schema = schema + child.serialize()
176 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
177 rngs = rngp.relaxNGParse()
181 log.write("\nFailed to compile correct schema:\n-----\n")
183 log.write("\n-----\n")
184 nb_schemas_failed = nb_schemas_failed + 1
186 nb_schemas_success = nb_schemas_success + 1
189 def handle_incorrect(node):
191 global nb_schemas_success
192 global nb_schemas_failed
195 child = node.children
197 if child.type != 'text':
198 schema = schema + child.serialize()
202 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
203 rngs = rngp.relaxNGParse()
207 log.write("\nFailed to detect schema error in:\n-----\n")
209 log.write("\n-----\n")
210 nb_schemas_failed = nb_schemas_failed + 1
212 # log.write("\nSuccess detecting schema error in:\n-----\n")
214 # log.write("\n-----\n")
215 nb_schemas_success = nb_schemas_success + 1
219 # resource handling: keep a dictionary of URL->string mappings
221 def handle_resource(node, dir):
225 name = node.prop('name')
229 if name == None or name == '':
230 log.write("resource has no name")
234 # name = libxml2.buildURI(name, dir)
235 name = dir + '/' + name
238 child = node.children
240 if child.type != 'text':
241 res = res + child.serialize()
243 resources[name] = res
246 # dir handling: pseudo directory resources
248 def handle_dir(node, dir):
250 name = node.prop('name')
254 if name == None or name == '':
255 log.write("resource has no name")
259 # name = libxml2.buildURI(name, dir)
260 name = dir + '/' + name
262 dirs = node.xpathEval('dir')
264 handle_dir(dir, name)
265 res = node.xpathEval('resource')
267 handle_resource(r, name)
270 # handle a testCase element
272 def handle_testCase(node):
273 global nb_schemas_tests
274 global nb_instances_tests
277 sections = node.xpathEval('string(section)')
278 log.write("\n ======== test %d line %d section %s ==========\n" % (
280 nb_schemas_tests, node.lineNo(), sections))
283 print "test %d line %d" % (nb_schemas_tests, node.lineNo())
285 dirs = node.xpathEval('dir')
287 handle_dir(dir, None)
288 res = node.xpathEval('resource')
290 handle_resource(r, None)
292 tsts = node.xpathEval('incorrect')
295 print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
296 schema = handle_incorrect(tsts[0])
298 tsts = node.xpathEval('correct')
301 print "warning test line %d has more than one <correct> example"% (node.lineNo())
302 schema = handle_correct(tsts[0])
304 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
306 nb_schemas_tests = nb_schemas_tests + 1;
308 valids = node.xpathEval('valid')
309 invalids = node.xpathEval('invalid')
310 nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
313 handle_valid(valid, schema)
314 for invalid in invalids:
315 handle_invalid(invalid, schema)
319 # handle a testSuite element
321 def handle_testSuite(node, level = 0):
322 global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
323 global nb_instances_tests, nb_instances_success, nb_instances_failed
324 if verbose and level >= 0:
325 old_schemas_tests = nb_schemas_tests
326 old_schemas_success = nb_schemas_success
327 old_schemas_failed = nb_schemas_failed
328 old_instances_tests = nb_instances_tests
329 old_instances_success = nb_instances_success
330 old_instances_failed = nb_instances_failed
332 docs = node.xpathEval('documentation')
333 authors = node.xpathEval('author')
337 msg = msg + doc.content + " "
339 msg = msg + "written by "
340 for author in authors:
341 msg = msg + author.content + " "
344 sections = node.xpathEval('section')
345 if verbose and sections != [] and level <= 0:
347 for section in sections:
348 msg = msg + section.content + " "
350 print "Tests for section %s" % (msg)
351 for test in node.xpathEval('testCase'):
352 handle_testCase(test)
353 for test in node.xpathEval('testSuite'):
354 handle_testSuite(test, level + 1)
357 if verbose and level >= 0 :
360 for section in sections:
361 msg = msg + section.content + " "
362 print "Result of tests for section %s" % (msg)
366 msg = msg + doc.content + " "
367 print "Result of tests for %s" % (msg)
369 if nb_schemas_tests != old_schemas_tests:
370 print "found %d test schemas: %d success %d failures" % (
371 nb_schemas_tests - old_schemas_tests,
372 nb_schemas_success - old_schemas_success,
373 nb_schemas_failed - old_schemas_failed)
374 if nb_instances_tests != old_instances_tests:
375 print "found %d test instances: %d success %d failures" % (
376 nb_instances_tests - old_instances_tests,
377 nb_instances_success - old_instances_success,
378 nb_instances_failed - old_instances_failed)
380 # Parse the conf file
382 libxml2.substituteEntitiesDefault(1);
383 testsuite = libxml2.parseFile(CONF)
386 # Error and warnng callbacks
388 def callback(ctx, str):
390 log.write("%s%s" % (ctx, str))
392 libxml2.registerErrorHandler(callback, "")
394 libxml2.setEntityLoader(resolver)
395 root = testsuite.getRootElement()
396 if root.name != 'testSuite':
397 print "%s doesn't start with a testSuite element, aborting" % (CONF)
400 print "Running Relax NG testsuite"
401 handle_testSuite(root)
403 if quiet == 0 or nb_schemas_failed != 0:
404 print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
405 nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
406 if quiet == 0 or nb_instances_failed != 0:
407 print "found %d test instances: %d success %d failures" % (
408 nb_instances_tests, nb_instances_success, nb_instances_failed)
412 # Memory debug specific
413 libxml2.relaxNGCleanupTypes()
414 libxml2.cleanupParser()
415 if libxml2.debugMemory(1) == 0:
419 print "Memory leak %d bytes" % (libxml2.debugMemory(1))