7 sys
.path
.insert(0, "python")
10 # Memory debug specific
11 libxml2
.debugMemory(1)
16 # the testsuite description
18 CONF
=os
.path
.join(os
.path
.dirname(__file__
), "test/relaxng/testsuite.xml")
19 LOG
="check-relaxng-test-suite2.log"
23 nb_schemas_success
= 0
25 nb_instances_tests
= 0
26 nb_instances_success
= 0
27 nb_instances_failed
= 0
29 libxml2
.lineNumbersDefault(1)
34 def resolver(URL
, ID
, ctxt
):
37 if resources
.has_key(URL
):
38 return(StringIO
.StringIO(resources
[URL
]))
39 log
.write("Resolver failure: asked %s\n" % (URL
))
40 log
.write("resources: %s\n" % (resources
))
44 # Load the previous results
50 # res = libxml2.parseFile(RES)
52 # log.write("Could not parse %s" % (RES))
55 # handle a valid instance
57 def handle_valid(node
, schema
):
59 global nb_instances_success
60 global nb_instances_failed
62 instance
= node
.prop("dtd")
67 if child
.type != 'text':
68 instance
= instance
+ child
.serialize()
71 # mem = libxml2.debugMemory(1);
73 doc
= libxml2
.parseDoc(instance
)
78 log
.write("\nFailed to parse correct instance:\n-----\n")
80 log
.write("\n-----\n")
81 nb_instances_failed
= nb_instances_failed
+ 1
85 print "instance line %d" % (node
.lineNo())
88 ctxt
= schema
.relaxNGNewValidCtxt()
89 ret
= doc
.relaxNGValidateDoc(ctxt
)
95 # if mem != libxml2.debugMemory(1):
96 # print "validating instance %d line %d leaks" % (
97 # nb_instances_tests, node.lineNo())
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
108 # handle an invalid instance
110 def handle_invalid(node
, schema
):
112 global nb_instances_success
113 global nb_instances_failed
115 instance
= node
.prop("dtd")
118 child
= node
.children
120 if child
.type != 'text':
121 instance
= instance
+ child
.serialize()
124 # mem = libxml2.debugMemory(1);
127 doc
= libxml2
.parseDoc(instance
)
132 log
.write("\nStrange: failed to parse incorrect instance:\n-----\n")
134 log
.write("\n-----\n")
138 print "instance line %d" % (node
.lineNo())
141 ctxt
= schema
.relaxNGNewValidCtxt()
142 ret
= doc
.relaxNGValidateDoc(ctxt
)
149 # mem2 = libxml2.debugMemory(1)
151 # print "validating instance %d line %d leaks %d bytes" % (
152 # nb_instances_tests, node.lineNo(), mem2 - mem)
155 log
.write("\nFailed to detect validation problem in instance:\n-----\n")
157 log
.write("\n-----\n")
158 nb_instances_failed
= nb_instances_failed
+ 1
160 nb_instances_success
= nb_instances_success
+ 1
163 # handle an incorrect test
165 def handle_correct(node
):
167 global nb_schemas_success
168 global nb_schemas_failed
171 child
= node
.children
173 if child
.type != 'text':
174 schema
= schema
+ child
.serialize()
178 rngp
= libxml2
.relaxNGNewMemParserCtxt(schema
, len(schema
))
179 rngs
= rngp
.relaxNGParse()
183 log
.write("\nFailed to compile correct schema:\n-----\n")
185 log
.write("\n-----\n")
186 nb_schemas_failed
= nb_schemas_failed
+ 1
188 nb_schemas_success
= nb_schemas_success
+ 1
191 def handle_incorrect(node
):
193 global nb_schemas_success
194 global nb_schemas_failed
197 child
= node
.children
199 if child
.type != 'text':
200 schema
= schema
+ child
.serialize()
204 rngp
= libxml2
.relaxNGNewMemParserCtxt(schema
, len(schema
))
205 rngs
= rngp
.relaxNGParse()
209 log
.write("\nFailed to detect schema error in:\n-----\n")
211 log
.write("\n-----\n")
212 nb_schemas_failed
= nb_schemas_failed
+ 1
214 # log.write("\nSuccess detecting schema error in:\n-----\n")
216 # log.write("\n-----\n")
217 nb_schemas_success
= nb_schemas_success
+ 1
221 # resource handling: keep a dictionary of URL->string mappings
223 def handle_resource(node
, dir):
227 name
= node
.prop('name')
231 if name
== None or name
== '':
232 log
.write("resource has no name")
236 # name = libxml2.buildURI(name, dir)
237 name
= dir + '/' + name
240 child
= node
.children
242 if child
.type != 'text':
243 res
= res
+ child
.serialize()
245 resources
[name
] = res
248 # dir handling: pseudo directory resources
250 def handle_dir(node
, dir):
252 name
= node
.prop('name')
256 if name
== None or name
== '':
257 log
.write("resource has no name")
261 # name = libxml2.buildURI(name, dir)
262 name
= dir + '/' + name
264 dirs
= node
.xpathEval('dir')
266 handle_dir(dir, name
)
267 res
= node
.xpathEval('resource')
269 handle_resource(r
, name
)
272 # handle a testCase element
274 def handle_testCase(node
):
275 global nb_schemas_tests
276 global nb_instances_tests
279 sections
= node
.xpathEval('string(section)')
280 log
.write("\n ======== test %d line %d section %s ==========\n" % (
282 nb_schemas_tests
, node
.lineNo(), sections
))
285 print "test %d line %d" % (nb_schemas_tests
, node
.lineNo())
287 dirs
= node
.xpathEval('dir')
289 handle_dir(dir, None)
290 res
= node
.xpathEval('resource')
292 handle_resource(r
, None)
294 tsts
= node
.xpathEval('incorrect')
297 print "warning test line %d has more than one <incorrect> example" %(node
.lineNo())
298 schema
= handle_incorrect(tsts
[0])
300 tsts
= node
.xpathEval('correct')
303 print "warning test line %d has more than one <correct> example"% (node
.lineNo())
304 schema
= handle_correct(tsts
[0])
306 print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node
.lineNo())
308 nb_schemas_tests
= nb_schemas_tests
+ 1;
310 valids
= node
.xpathEval('valid')
311 invalids
= node
.xpathEval('invalid')
312 nb_instances_tests
= nb_instances_tests
+ len(valids
) + len(invalids
)
315 handle_valid(valid
, schema
)
316 for invalid
in invalids
:
317 handle_invalid(invalid
, schema
)
321 # handle a testSuite element
323 def handle_testSuite(node
, level
= 0):
324 global nb_schemas_tests
, nb_schemas_success
, nb_schemas_failed
325 global nb_instances_tests
, nb_instances_success
, nb_instances_failed
327 old_schemas_tests
= nb_schemas_tests
328 old_schemas_success
= nb_schemas_success
329 old_schemas_failed
= nb_schemas_failed
330 old_instances_tests
= nb_instances_tests
331 old_instances_success
= nb_instances_success
332 old_instances_failed
= nb_instances_failed
334 docs
= node
.xpathEval('documentation')
335 authors
= node
.xpathEval('author')
339 msg
= msg
+ doc
.content
+ " "
341 msg
= msg
+ "written by "
342 for author
in authors
:
343 msg
= msg
+ author
.content
+ " "
346 sections
= node
.xpathEval('section')
347 if sections
!= [] and level
<= 0:
349 for section
in sections
:
350 msg
= msg
+ section
.content
+ " "
352 print "Tests for section %s" % (msg
)
353 for test
in node
.xpathEval('testCase'):
354 handle_testCase(test
)
355 for test
in node
.xpathEval('testSuite'):
356 handle_testSuite(test
, level
+ 1)
359 if level
>= 1 and sections
!= []:
361 for section
in sections
:
362 msg
= msg
+ section
.content
+ " "
363 print "Result of tests for section %s" % (msg
)
364 if nb_schemas_tests
!= old_schemas_tests
:
365 print "found %d test schemas: %d success %d failures" % (
366 nb_schemas_tests
- old_schemas_tests
,
367 nb_schemas_success
- old_schemas_success
,
368 nb_schemas_failed
- old_schemas_failed
)
369 if nb_instances_tests
!= old_instances_tests
:
370 print "found %d test instances: %d success %d failures" % (
371 nb_instances_tests
- old_instances_tests
,
372 nb_instances_success
- old_instances_success
,
373 nb_instances_failed
- old_instances_failed
)
375 # Parse the conf file
377 libxml2
.substituteEntitiesDefault(1);
378 testsuite
= libxml2
.parseFile(CONF
)
381 # Error and warnng callbacks
383 def callback(ctx
, str):
385 log
.write("%s%s" % (ctx
, str))
387 libxml2
.registerErrorHandler(callback
, "")
389 libxml2
.setEntityLoader(resolver
)
390 root
= testsuite
.getRootElement()
391 if root
.name
!= 'testSuite':
392 print "%s doesn't start with a testSuite element, aborting" % (CONF
)
395 print "Running Relax NG testsuite"
396 handle_testSuite(root
)
400 if quiet
== 0 or nb_schemas_failed
!= 0:
401 print "found %d test schemas: %d success %d failures" % (
402 nb_schemas_tests
, nb_schemas_success
, nb_schemas_failed
)
403 if quiet
== 0 or nb_instances_failed
!= 0:
404 print "found %d test instances: %d success %d failures" % (
405 nb_instances_tests
, nb_instances_success
, nb_instances_failed
)
410 # Memory debug specific
411 libxml2
.relaxNGCleanupTypes()
412 libxml2
.cleanupParser()
413 if libxml2
.debugMemory(1) == 0:
417 print "Memory leak %d bytes" % (libxml2
.debugMemory(1))