7 sys.path.append("python")
10 # Memory debug specific
11 libxml2.debugMemory(1)
15 # the testsuite description
17 CONF="test/relaxng/OASIS/spectest.xml"
18 LOG="check-relaxng-test-suite.log"
19 RES="relaxng-test-results.xml"
23 nb_schemas_success = 0
25 nb_instances_tests = 0
26 nb_instances_success = 0
27 nb_instances_failed = 0
29 libxml2.lineNumbersDefault(1)
31 # Error and warnng callbacks
33 def callback(ctx, str):
35 log.write("%s%s" % (ctx, str))
37 libxml2.registerErrorHandler(callback, "")
43 def resolver(URL, ID, ctxt):
46 if resources.has_key(URL):
47 return(StringIO.StringIO(resources[URL]))
48 log.write("Resolver failure: asked %s\n" % (URL))
49 log.write("resources: %s\n" % (resources))
53 # Load the previous results
59 # res = libxml2.parseFile(RES)
61 # log.write("Could not parse %s" % (RES))
64 # handle a valid instance
66 def handle_valid(node, schema):
68 global nb_instances_success
69 global nb_instances_failed
74 if child.type != 'text':
75 instance = instance + child.serialize()
79 doc = libxml2.parseDoc(instance)
84 log.write("\nFailed to parse correct instance:\n-----\n")
86 log.write("\n-----\n")
87 nb_instances_failed = nb_instances_failed + 1
91 ctxt = schema.relaxNGNewValidCtxt()
92 ret = doc.relaxNGValidateDoc(ctxt)
96 log.write("\nFailed to validate correct instance:\n-----\n")
98 log.write("\n-----\n")
99 nb_instances_failed = nb_instances_failed + 1
101 nb_instances_success = nb_instances_success + 1
105 # handle an invalid instance
107 def handle_invalid(node, schema):
109 global nb_instances_success
110 global nb_instances_failed
113 child = node.children
115 if child.type != 'text':
116 instance = instance + child.serialize()
120 doc = libxml2.parseDoc(instance)
125 log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
127 log.write("\n-----\n")
131 ctxt = schema.relaxNGNewValidCtxt()
132 ret = doc.relaxNGValidateDoc(ctxt)
136 log.write("\nFailed to detect validation problem in instance:\n-----\n")
138 log.write("\n-----\n")
139 nb_instances_failed = nb_instances_failed + 1
141 nb_instances_success = nb_instances_success + 1
145 # handle an incorrect test
147 def handle_correct(node):
149 global nb_schemas_success
150 global nb_schemas_failed
153 child = node.children
155 if child.type != 'text':
156 schema = schema + child.serialize()
160 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
161 rngs = rngp.relaxNGParse()
165 log.write("\nFailed to compile correct schema:\n-----\n")
167 log.write("\n-----\n")
168 nb_schemas_failed = nb_schemas_failed + 1
170 nb_schemas_success = nb_schemas_success + 1
173 def handle_incorrect(node):
175 global nb_schemas_success
176 global nb_schemas_failed
179 child = node.children
181 if child.type != 'text':
182 schema = schema + child.serialize()
186 rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
187 rngs = rngp.relaxNGParse()
191 log.write("\nFailed to detect schema error in:\n-----\n")
193 log.write("\n-----\n")
194 nb_schemas_failed = nb_schemas_failed + 1
196 # log.write("\nSuccess detecting schema error in:\n-----\n")
198 # log.write("\n-----\n")
199 nb_schemas_success = nb_schemas_success + 1
203 # resource handling: keep a dictionary of URL->string mappings
205 def handle_resource(node, dir):
209 name = node.prop('name')
213 if name == None or name == '':
214 log.write("resource has no name")
218 # name = libxml2.buildURI(name, dir)
219 name = dir + '/' + name
222 child = node.children
224 if child.type != 'text':
225 res = res + child.serialize()
227 resources[name] = res
230 # dir handling: pseudo directory resources
232 def handle_dir(node, dir):
234 name = node.prop('name')
238 if name == None or name == '':
239 log.write("resource has no name")
243 # name = libxml2.buildURI(name, dir)
244 name = dir + '/' + name
246 dirs = node.xpathEval('dir')
248 handle_dir(dir, name)
249 res = node.xpathEval('resource')
251 handle_resource(r, name)
254 # handle a testCase element
256 def handle_testCase(node):
257 global nb_schemas_tests
258 global nb_instances_tests
261 sections = node.xpathEval('string(section)')
262 log.write("\n ======== test %d line %d section %s ==========\n" % (
264 nb_schemas_tests, node.lineNo(), sections))
267 print "test %d line %d" % (nb_schemas_tests, node.lineNo())
269 dirs = node.xpathEval('dir')
271 handle_dir(dir, None)
272 res = node.xpathEval('resource')
274 handle_resource(r, None)
276 tsts = node.xpathEval('incorrect')
279 print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
280 schema = handle_incorrect(tsts[0])
282 tsts = node.xpathEval('correct')
285 print "warning test line %d has more than one <correct> example"% (node.lineNo())
286 schema = handle_correct(tsts[0])
288 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
290 nb_schemas_tests = nb_schemas_tests + 1;
292 valids = node.xpathEval('valid')
293 invalids = node.xpathEval('invalid')
294 nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
297 handle_valid(valid, schema)
298 for invalid in invalids:
299 handle_invalid(invalid, schema)
303 # handle a testSuite element
305 def handle_testSuite(node, level = 0):
306 global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
307 global nb_instances_tests, nb_instances_success, nb_instances_failed
309 old_schemas_tests = nb_schemas_tests
310 old_schemas_success = nb_schemas_success
311 old_schemas_failed = nb_schemas_failed
312 old_instances_tests = nb_instances_tests
313 old_instances_success = nb_instances_success
314 old_instances_failed = nb_instances_failed
316 docs = node.xpathEval('documentation')
317 authors = node.xpathEval('author')
321 msg = msg + doc.content + " "
323 msg = msg + "written by "
324 for author in authors:
325 msg = msg + author.content + " "
327 sections = node.xpathEval('section')
328 if sections != [] and level <= 0:
330 for section in sections:
331 msg = msg + section.content + " "
332 print "Tests for section %s" % (msg)
333 for test in node.xpathEval('testCase'):
334 handle_testCase(test)
335 for test in node.xpathEval('testSuite'):
336 handle_testSuite(test, level + 1)
339 if level >= 1 and sections != []:
341 for section in sections:
342 msg = msg + section.content + " "
343 print "Result of tests for section %s" % (msg)
344 if nb_schemas_tests != old_schemas_tests:
345 print "found %d test schemas: %d success %d failures" % (
346 nb_schemas_tests - old_schemas_tests,
347 nb_schemas_success - old_schemas_success,
348 nb_schemas_failed - old_schemas_failed)
349 if nb_instances_tests != old_instances_tests:
350 print "found %d test instances: %d success %d failures" % (
351 nb_instances_tests - old_instances_tests,
352 nb_instances_success - old_instances_success,
353 nb_instances_failed - old_instances_failed)
355 # Parse the conf file
357 libxml2.substituteEntitiesDefault(1);
358 testsuite = libxml2.parseFile(CONF)
359 libxml2.setEntityLoader(resolver)
360 root = testsuite.getRootElement()
361 if root.name != 'testSuite':
362 print "%s doesn't start with a testSuite element, aborting" % (CONF)
364 print "Running Relax NG testsuite"
365 handle_testSuite(root)
367 print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
368 nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
369 print "found %d test instances: %d success %d failures" % (
370 nb_instances_tests, nb_instances_success, nb_instances_failed)
374 # Memory debug specific
375 libxml2.relaxNGCleanupTypes()
376 libxml2.cleanupParser()
377 if libxml2.debugMemory(1) == 0:
380 print "Memory leak %d bytes" % (libxml2.debugMemory(1))