1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-10 08:58:16 +03:00

Port some test scripts to Python 3

This commit is contained in:
Nick Wellnhofer 2022-03-29 15:55:51 +02:00
parent 92bff86614
commit 649ddb4b2f
6 changed files with 595 additions and 585 deletions

View File

@ -2,8 +2,12 @@
import sys
import time
import os
import string
import StringIO
try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
sys.path.insert(0, "python")
import libxml2
@ -45,10 +49,10 @@ resources = {}
def resolver(URL, ID, ctxt):
global resources
if string.find(URL, '#') != -1:
URL = URL[0:string.find(URL, '#')]
if resources.has_key(URL):
return(StringIO.StringIO(resources[URL]))
if URL.find('#') != -1:
URL = URL[0:URL.find('#')]
if URL in resources:
return(StringIO(resources[URL]))
log.write("Resolver failure: asked %s\n" % (URL))
log.write("resources: %s\n" % (resources))
return None
@ -63,7 +67,7 @@ def resolver(URL, ID, ctxt):
# res = libxml2.parseFile(RES)
#except:
# log.write("Could not parse %s" % (RES))
#
# handle a valid instance
#
@ -76,33 +80,33 @@ def handle_valid(node, schema):
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nFailed to parse correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
return
nb_instances_failed = nb_instances_failed + 1
return
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
ret = doc.relaxNGValidateDoc(ctxt)
except:
ret = -1
if ret != 0:
log.write("\nFailed to validate correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
doc.freeDoc()
#
@ -117,32 +121,32 @@ def handle_invalid(node, schema):
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
return
return
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
ret = doc.relaxNGValidateDoc(ctxt)
except:
ret = -1
if ret == 0:
log.write("\nFailed to detect validation problem in instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
doc.freeDoc()
#
@ -157,23 +161,23 @@ def handle_correct(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs == None:
if rngs is None:
log.write("\nFailed to compile correct schema:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
nb_schemas_success = nb_schemas_success + 1
nb_schemas_success = nb_schemas_success + 1
return rngs
def handle_incorrect(node):
global log
global nb_schemas_success
@ -183,24 +187,24 @@ def handle_incorrect(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs != None:
log.write("\nFailed to detect schema error in:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
return None
#
@ -210,14 +214,14 @@ def handle_resource(node, dir):
global resources
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -226,8 +230,8 @@ def handle_resource(node, dir):
child = node.children
while child != None:
if child.type != 'text':
res = res + child.serialize()
child = child.next
res = res + child.serialize()
child = child.next
resources[name] = res
#
@ -235,14 +239,14 @@ def handle_resource(node, dir):
#
def handle_dir(node, dir):
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -268,7 +272,7 @@ def handle_testCase(node):
nb_schemas_tests, node.lineNo(), sections))
resources = {}
if debug:
print "test %d line %d" % (nb_schemas_tests, node.lineNo())
print("test %d line %d" % (nb_schemas_tests, node.lineNo()))
dirs = node.xpathEval('dir')
for dir in dirs:
@ -280,27 +284,27 @@ def handle_testCase(node):
tsts = node.xpathEval('incorrect')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
schema = handle_incorrect(tsts[0])
print("warning test line %d has more than one <incorrect> example" %(node.lineNo()))
schema = handle_incorrect(tsts[0])
else:
tsts = node.xpathEval('correct')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <correct> example"% (node.lineNo())
schema = handle_correct(tsts[0])
else:
print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
if tsts != []:
if len(tsts) != 1:
print("warning test line %d has more than one <correct> example"% (node.lineNo()))
schema = handle_correct(tsts[0])
else:
print("warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo()))
nb_schemas_tests = nb_schemas_tests + 1;
valids = node.xpathEval('valid')
invalids = node.xpathEval('invalid')
nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
if schema != None:
for valid in valids:
handle_valid(valid, schema)
handle_valid(valid, schema)
for invalid in invalids:
handle_invalid(invalid, schema)
handle_invalid(invalid, schema)
#
@ -311,53 +315,53 @@ def handle_testSuite(node, level = 0):
global nb_instances_tests, nb_instances_success, nb_instances_failed
global quiet
if level >= 1:
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
docs = node.xpathEval('documentation')
authors = node.xpathEval('author')
if docs != []:
msg = ""
for doc in docs:
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print msg
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print(msg)
sections = node.xpathEval('section')
if sections != [] and level <= 0:
msg = ""
for section in sections:
msg = msg + section.content + " "
if quiet == 0:
print "Tests for section %s" % (msg)
msg = msg + section.content + " "
if quiet == 0:
print("Tests for section %s" % (msg))
for test in node.xpathEval('testCase'):
handle_testCase(test)
for test in node.xpathEval('testSuite'):
handle_testSuite(test, level + 1)
if verbose and level >= 1 and sections != []:
msg = ""
for section in sections:
msg = msg + section.content + " "
print "Result of tests for section %s" % (msg)
msg = msg + section.content + " "
print("Result of tests for section %s" % (msg))
if nb_schemas_tests != old_schemas_tests:
print "found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed)
if nb_instances_tests != old_instances_tests:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed)
print("found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed))
if nb_instances_tests != old_instances_tests:
print("found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed))
#
# Parse the conf file
#
@ -366,20 +370,20 @@ testsuite = libxml2.parseFile(CONF)
libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
print "%s doesn't start with a testSuite element, aborting" % (CONF)
print("%s doesn't start with a testSuite element, aborting" % (CONF))
sys.exit(1)
if quiet == 0:
print "Running Relax NG testsuite"
print("Running Relax NG testsuite")
handle_testSuite(root)
if quiet == 0:
print "\nTOTAL:\n"
print("\nTOTAL:\n")
if quiet == 0 or nb_schemas_failed != 0:
print "found %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
print("found %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed))
if quiet == 0 or nb_instances_failed != 0:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed)
print("found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed))
testsuite.freeDoc()
@ -388,7 +392,7 @@ libxml2.relaxNGCleanupTypes()
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
if quiet == 0:
print "OK"
print("OK")
else:
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
libxml2.dumpMemory()

View File

@ -2,8 +2,12 @@
import sys
import time
import os
import string
import StringIO
try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
sys.path.insert(0, "python")
import libxml2
@ -34,8 +38,8 @@ resources = {}
def resolver(URL, ID, ctxt):
global resources
if resources.has_key(URL):
return(StringIO.StringIO(resources[URL]))
if URL in resources:
return(StringIO(resources[URL]))
log.write("Resolver failure: asked %s\n" % (URL))
log.write("resources: %s\n" % (resources))
return None
@ -50,7 +54,7 @@ def resolver(URL, ID, ctxt):
# res = libxml2.parseFile(RES)
#except:
# log.write("Could not parse %s" % (RES))
#
# handle a valid instance
#
@ -60,49 +64,49 @@ def handle_valid(node, schema):
global nb_instances_failed
instance = node.prop("dtd")
if instance == None:
if instance is None:
instance = ""
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
# mem = libxml2.debugMemory(1);
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nFailed to parse correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
return
nb_instances_failed = nb_instances_failed + 1
return
if debug:
print "instance line %d" % (node.lineNo())
print("instance line %d" % (node.lineNo()))
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
except:
ret = -1
doc.freeDoc()
# if mem != libxml2.debugMemory(1):
# print "validating instance %d line %d leaks" % (
# nb_instances_tests, node.lineNo())
# print("validating instance %d line %d leaks" % (
# nb_instances_tests, node.lineNo()))
if ret != 0:
log.write("\nFailed to validate correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
#
# handle an invalid instance
@ -113,34 +117,34 @@ def handle_invalid(node, schema):
global nb_instances_failed
instance = node.prop("dtd")
if instance == None:
if instance is None:
instance = ""
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
# mem = libxml2.debugMemory(1);
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
return
return
if debug:
print "instance line %d" % (node.lineNo())
print("instance line %d" % (node.lineNo()))
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
except:
ret = -1
@ -148,16 +152,16 @@ def handle_invalid(node, schema):
doc.freeDoc()
# mem2 = libxml2.debugMemory(1)
# if mem != mem2:
# print "validating instance %d line %d leaks %d bytes" % (
# nb_instances_tests, node.lineNo(), mem2 - mem)
# print("validating instance %d line %d leaks %d bytes" % (
# nb_instances_tests, node.lineNo(), mem2 - mem))
if ret == 0:
log.write("\nFailed to detect validation problem in instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
#
# handle an incorrect test
@ -171,23 +175,23 @@ def handle_correct(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs == None:
if rngs is None:
log.write("\nFailed to compile correct schema:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
nb_schemas_success = nb_schemas_success + 1
nb_schemas_success = nb_schemas_success + 1
return rngs
def handle_incorrect(node):
global log
global nb_schemas_success
@ -197,24 +201,24 @@ def handle_incorrect(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs != None:
log.write("\nFailed to detect schema error in:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
return None
#
@ -224,14 +228,14 @@ def handle_resource(node, dir):
global resources
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -240,8 +244,8 @@ def handle_resource(node, dir):
child = node.children
while child != None:
if child.type != 'text':
res = res + child.serialize()
child = child.next
res = res + child.serialize()
child = child.next
resources[name] = res
#
@ -249,14 +253,14 @@ def handle_resource(node, dir):
#
def handle_dir(node, dir):
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -282,7 +286,7 @@ def handle_testCase(node):
nb_schemas_tests, node.lineNo(), sections))
resources = {}
if debug:
print "test %d line %d" % (nb_schemas_tests, node.lineNo())
print("test %d line %d" % (nb_schemas_tests, node.lineNo()))
dirs = node.xpathEval('dir')
for dir in dirs:
@ -294,27 +298,27 @@ def handle_testCase(node):
tsts = node.xpathEval('incorrect')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
schema = handle_incorrect(tsts[0])
print("warning test line %d has more than one <incorrect> example" %(node.lineNo()))
schema = handle_incorrect(tsts[0])
else:
tsts = node.xpathEval('correct')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <correct> example"% (node.lineNo())
schema = handle_correct(tsts[0])
else:
print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
if tsts != []:
if len(tsts) != 1:
print("warning test line %d has more than one <correct> example"% (node.lineNo()))
schema = handle_correct(tsts[0])
else:
print("warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo()))
nb_schemas_tests = nb_schemas_tests + 1;
valids = node.xpathEval('valid')
invalids = node.xpathEval('invalid')
nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
if schema != None:
for valid in valids:
handle_valid(valid, schema)
handle_valid(valid, schema)
for invalid in invalids:
handle_invalid(invalid, schema)
handle_invalid(invalid, schema)
#
@ -324,53 +328,53 @@ def handle_testSuite(node, level = 0):
global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
global nb_instances_tests, nb_instances_success, nb_instances_failed
if level >= 1:
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
docs = node.xpathEval('documentation')
authors = node.xpathEval('author')
if docs != []:
msg = ""
for doc in docs:
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print msg
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print(msg)
sections = node.xpathEval('section')
if sections != [] and level <= 0:
msg = ""
for section in sections:
msg = msg + section.content + " "
if quiet == 0:
print "Tests for section %s" % (msg)
msg = msg + section.content + " "
if quiet == 0:
print("Tests for section %s" % (msg))
for test in node.xpathEval('testCase'):
handle_testCase(test)
for test in node.xpathEval('testSuite'):
handle_testSuite(test, level + 1)
if level >= 1 and sections != []:
msg = ""
for section in sections:
msg = msg + section.content + " "
print "Result of tests for section %s" % (msg)
msg = msg + section.content + " "
print("Result of tests for section %s" % (msg))
if nb_schemas_tests != old_schemas_tests:
print "found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed)
if nb_instances_tests != old_instances_tests:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed)
print("found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed))
if nb_instances_tests != old_instances_tests:
print("found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed))
#
# Parse the conf file
#
@ -389,22 +393,22 @@ libxml2.registerErrorHandler(callback, "")
libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
print "%s doesn't start with a testSuite element, aborting" % (CONF)
print("%s doesn't start with a testSuite element, aborting" % (CONF))
sys.exit(1)
if quiet == 0:
print "Running Relax NG testsuite"
print("Running Relax NG testsuite")
handle_testSuite(root)
if quiet == 0:
print "\nTOTAL:\n"
print("\nTOTAL:\n")
if quiet == 0 or nb_schemas_failed != 0:
print "found %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
print("found %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed))
if quiet == 0 or nb_instances_failed != 0:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed)
print("found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed))
log.close()
testsuite.freeDoc()
# Memory debug specific
@ -412,7 +416,7 @@ libxml2.relaxNGCleanupTypes()
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
if quiet == 0:
print "OK"
print("OK")
else:
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
libxml2.dumpMemory()

View File

@ -2,7 +2,6 @@
import sys
import time
import os
import string
sys.path.insert(0, "python")
import libxml2
@ -31,13 +30,13 @@ def errorHandler(ctx, str):
global error_nr
global error_msg
if string.find(str, "error:") >= 0:
error_nr = error_nr + 1
if str.find("error:") >= 0:
error_nr = error_nr + 1
if len(error_msg) < 300:
if len(error_msg) == 0 or error_msg[-1] == '\n':
error_msg = error_msg + " >>" + str
else:
error_msg = error_msg + str
error_msg = error_msg + " >>" + str
else:
error_msg = error_msg + str
libxml2.registerErrorHandler(errorHandler, None)
@ -49,7 +48,7 @@ def testXInclude(filename, id):
error_nr = 0
error_msg = ''
print "testXInclude(%s, %s)" % (filename, id)
print("testXInclude(%s, %s)" % (filename, id))
return 1
def runTest(test, basedir):
@ -64,158 +63,158 @@ def runTest(test, basedir):
uri = test.prop('href')
id = test.prop('id')
type = test.prop('type')
if uri == None:
print "Test without ID:", uri
return -1
if id == None:
print "Test without URI:", id
return -1
if type == None:
print "Test without URI:", id
return -1
if uri is None:
print("Test without ID:", uri)
return -1
if id is None:
print("Test without URI:", id)
return -1
if type is None:
print("Test without URI:", id)
return -1
if basedir != None:
URI = basedir + "/" + uri
URI = basedir + "/" + uri
else:
URI = uri
if os.access(URI, os.R_OK) == 0:
print "Test %s missing: base %s uri %s" % (URI, basedir, uri)
return -1
print("Test %s missing: base %s uri %s" % (URI, basedir, uri))
return -1
expected = None
outputfile = None
diff = None
if type != 'error':
output = test.xpathEval('string(output)')
if output == 'No output file.':
output = None
if output == '':
output = None
if output != None:
if basedir != None:
output = basedir + "/" + output
if os.access(output, os.R_OK) == 0:
print "Result for %s missing: %s" % (id, output)
output = None
else:
try:
f = open(output)
expected = f.read()
outputfile = output
except:
print "Result for %s unreadable: %s" % (id, output)
output = test.xpathEval('string(output)')
if output == 'No output file.':
output = None
if output == '':
output = None
if output != None:
if basedir != None:
output = basedir + "/" + output
if os.access(output, os.R_OK) == 0:
print("Result for %s missing: %s" % (id, output))
output = None
else:
try:
f = open(output)
expected = f.read()
outputfile = output
except:
print("Result for %s unreadable: %s" % (id, output))
try:
# print "testing %s" % (URI)
doc = libxml2.parseFile(URI)
# print("testing %s" % (URI))
doc = libxml2.parseFile(URI)
except:
doc = None
if doc != None:
res = doc.xincludeProcess()
if res >= 0 and expected != None:
result = doc.serialize()
if result != expected:
print "Result for %s differs" % (id)
open("xinclude.res", "w").write(result)
diff = os.popen("diff %s xinclude.res" % outputfile).read()
if res >= 0 and expected != None:
result = doc.serialize()
if result != expected:
print("Result for %s differs" % (id))
open("xinclude.res", "w").write(result)
diff = os.popen("diff %s xinclude.res" % outputfile).read()
doc.freeDoc()
doc.freeDoc()
else:
print "Failed to parse %s" % (URI)
res = -1
print("Failed to parse %s" % (URI))
res = -1
test_nr = test_nr + 1
if type == 'success':
if res > 0:
test_succeed = test_succeed + 1
elif res == 0:
test_failed = test_failed + 1
print "Test %s: no substitution done ???" % (id)
elif res < 0:
test_error = test_error + 1
print "Test %s: failed valid XInclude processing" % (id)
if res > 0:
test_succeed = test_succeed + 1
elif res == 0:
test_failed = test_failed + 1
print("Test %s: no substitution done ???" % (id))
elif res < 0:
test_error = test_error + 1
print("Test %s: failed valid XInclude processing" % (id))
elif type == 'error':
if res > 0:
test_error = test_error + 1
print "Test %s: failed to detect invalid XInclude processing" % (id)
elif res == 0:
test_failed = test_failed + 1
print "Test %s: Invalid but no substitution done" % (id)
elif res < 0:
test_succeed = test_succeed + 1
if res > 0:
test_error = test_error + 1
print("Test %s: failed to detect invalid XInclude processing" % (id))
elif res == 0:
test_failed = test_failed + 1
print("Test %s: Invalid but no substitution done" % (id))
elif res < 0:
test_succeed = test_succeed + 1
elif type == 'optional':
if res > 0:
test_succeed = test_succeed + 1
else:
print "Test %s: failed optional test" % (id)
if res > 0:
test_succeed = test_succeed + 1
else:
print("Test %s: failed optional test" % (id))
# Log the ontext
if res != 1:
log.write("Test ID %s\n" % (id))
log.write(" File: %s\n" % (URI))
content = string.strip(test.content)
while content[-1] == '\n':
content = content[0:-1]
log.write(" %s:%s\n\n" % (type, content))
if error_msg != '':
log.write(" ----\n%s ----\n" % (error_msg))
error_msg = ''
log.write("\n")
log.write("Test ID %s\n" % (id))
log.write(" File: %s\n" % (URI))
content = test.content.strip()
while content[-1] == '\n':
content = content[0:-1]
log.write(" %s:%s\n\n" % (type, content))
if error_msg != '':
log.write(" ----\n%s ----\n" % (error_msg))
error_msg = ''
log.write("\n")
if diff != None:
log.write("diff from test %s:\n" %(id))
log.write(" -----------\n%s\n -----------\n" % (diff));
log.write(" -----------\n%s\n -----------\n" % (diff));
return 0
def runTestCases(case):
creator = case.prop('creator')
if creator != None:
print "=>", creator
print("=>", creator)
base = case.getBase(None)
basedir = case.prop('basedir')
if basedir != None:
base = libxml2.buildURI(basedir, base)
base = libxml2.buildURI(basedir, base)
test = case.children
while test != None:
if test.name == 'testcase':
runTest(test, base)
if test.name == 'testcases':
runTestCases(test)
runTest(test, base)
if test.name == 'testcases':
runTestCases(test)
test = test.next
conf = libxml2.parseFile(CONF)
if conf == None:
print "Unable to load %s" % CONF
if conf is None:
print("Unable to load %s" % CONF)
sys.exit(1)
testsuite = conf.getRootElement()
if testsuite.name != 'testsuite':
print "Expecting TESTSUITE root element: aborting"
print("Expecting TESTSUITE root element: aborting")
sys.exit(1)
profile = testsuite.prop('PROFILE')
if profile != None:
print profile
print(profile)
start = time.time()
case = testsuite.children
while case != None:
if case.name == 'testcases':
old_test_nr = test_nr
old_test_succeed = test_succeed
old_test_failed = test_failed
old_test_error = test_error
old_test_nr = test_nr
old_test_succeed = test_succeed
old_test_failed = test_failed
old_test_error = test_error
runTestCases(case)
print " Ran %d tests: %d succeeded, %d failed and %d generated an error" % (
test_nr - old_test_nr, test_succeed - old_test_succeed,
test_failed - old_test_failed, test_error - old_test_error)
print(" Ran %d tests: %d succeeded, %d failed and %d generated an error" % (
test_nr - old_test_nr, test_succeed - old_test_succeed,
test_failed - old_test_failed, test_error - old_test_error))
case = case.next
conf.freeDoc()
log.close()
print "Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s." % (
test_nr, test_succeed, test_failed, test_error, time.time() - start)
print("Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s." % (
test_nr, test_succeed, test_failed, test_error, time.time() - start))

View File

@ -2,7 +2,6 @@
import sys
import time
import os
import string
sys.path.insert(0, "python")
import libxml2
@ -31,9 +30,9 @@ def errorHandler(ctx, str):
error_nr = error_nr + 1
if len(error_msg) < 300:
if len(error_msg) == 0 or error_msg[-1] == '\n':
error_msg = error_msg + " >>" + str
else:
error_msg = error_msg + str
error_msg = error_msg + " >>" + str
else:
error_msg = error_msg + str
libxml2.registerErrorHandler(errorHandler, None)
@ -53,17 +52,17 @@ libxml2.registerErrorHandler(errorHandler, None)
#
def loadNoentDoc(filename):
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return None
ctxt.replaceEntities(1)
ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if ctxt.wellFormed() != 1:
doc.freeDoc()
return None
return None
return doc
#
@ -79,20 +78,20 @@ def testNotWf(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if doc != None:
doc.freeDoc()
doc.freeDoc()
if ret == 0 or ctxt.wellFormed() != 0:
print "%s: error: Well Formedness error not detected" % (id)
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
print("%s: error: Well Formedness error not detected" % (id))
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
return 1
def testNotWfEnt(filename, id):
@ -104,21 +103,21 @@ def testNotWfEnt(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.replaceEntities(1)
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if doc != None:
doc.freeDoc()
doc.freeDoc()
if ret == 0 or ctxt.wellFormed() != 0:
print "%s: error: Well Formedness error not detected" % (id)
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
print("%s: error: Well Formedness error not detected" % (id))
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
return 1
def testNotWfEntDtd(filename, id):
@ -130,22 +129,22 @@ def testNotWfEntDtd(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.replaceEntities(1)
ctxt.loadSubset(1)
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if doc != None:
doc.freeDoc()
doc.freeDoc()
if ret == 0 or ctxt.wellFormed() != 0:
print "%s: error: Well Formedness error not detected" % (id)
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
print("%s: error: Well Formedness error not detected" % (id))
log.write("%s: error: Well Formedness error not detected\n" % (id))
return 0
return 1
def testWfEntDtd(filename, id):
@ -157,27 +156,27 @@ def testWfEntDtd(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.replaceEntities(1)
ctxt.loadSubset(1)
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if doc == None or ret != 0 or ctxt.wellFormed() == 0:
print "%s: error: wrongly failed to parse the document" % (id)
log.write("%s: error: wrongly failed to parse the document\n" % (id))
if doc != None:
doc.freeDoc()
return 0
if doc is None or ret != 0 or ctxt.wellFormed() == 0:
print("%s: error: wrongly failed to parse the document" % (id))
log.write("%s: error: wrongly failed to parse the document\n" % (id))
if doc != None:
doc.freeDoc()
return 0
if error_nr != 0:
print "%s: warning: WF document generated an error msg" % (id)
log.write("%s: error: WF document generated an error msg\n" % (id))
doc.freeDoc()
return 2
print("%s: warning: WF document generated an error msg" % (id))
log.write("%s: error: WF document generated an error msg\n" % (id))
doc.freeDoc()
return 2
doc.freeDoc()
return 1
@ -190,26 +189,26 @@ def testError(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.replaceEntities(1)
ctxt.loadSubset(1)
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
if doc != None:
doc.freeDoc()
doc.freeDoc()
if ctxt.wellFormed() == 0:
print "%s: warning: failed to parse the document but accepted" % (id)
log.write("%s: warning: failed to parse the document but accepte\n" % (id))
return 2
print("%s: warning: failed to parse the document but accepted" % (id))
log.write("%s: warning: failed to parse the document but accepte\n" % (id))
return 2
if error_nr != 0:
print "%s: warning: WF document generated an error msg" % (id)
log.write("%s: error: WF document generated an error msg\n" % (id))
return 2
print("%s: warning: WF document generated an error msg" % (id))
log.write("%s: error: WF document generated an error msg\n" % (id))
return 2
return 1
def testInvalid(filename, id):
@ -221,31 +220,31 @@ def testInvalid(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.validate(1)
ret = ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
valid = ctxt.isValid()
if doc == None:
print "%s: error: wrongly failed to parse the document" % (id)
log.write("%s: error: wrongly failed to parse the document\n" % (id))
return 0
if doc is None:
print("%s: error: wrongly failed to parse the document" % (id))
log.write("%s: error: wrongly failed to parse the document\n" % (id))
return 0
if valid == 1:
print "%s: error: Validity error not detected" % (id)
log.write("%s: error: Validity error not detected\n" % (id))
doc.freeDoc()
return 0
print("%s: error: Validity error not detected" % (id))
log.write("%s: error: Validity error not detected\n" % (id))
doc.freeDoc()
return 0
if error_nr == 0:
print "%s: warning: Validity error not reported" % (id)
log.write("%s: warning: Validity error not reported\n" % (id))
doc.freeDoc()
return 2
print("%s: warning: Validity error not reported" % (id))
log.write("%s: warning: Validity error not reported\n" % (id))
doc.freeDoc()
return 2
doc.freeDoc()
return 1
@ -257,30 +256,30 @@ def testValid(filename, id):
error_msg = ''
ctxt = libxml2.createFileParserCtxt(filename)
if ctxt == None:
if ctxt is None:
return -1
ctxt.validate(1)
ctxt.parseDocument()
try:
doc = ctxt.doc()
doc = ctxt.doc()
except:
doc = None
valid = ctxt.isValid()
if doc == None:
print "%s: error: wrongly failed to parse the document" % (id)
log.write("%s: error: wrongly failed to parse the document\n" % (id))
return 0
if doc is None:
print("%s: error: wrongly failed to parse the document" % (id))
log.write("%s: error: wrongly failed to parse the document\n" % (id))
return 0
if valid != 1:
print "%s: error: Validity check failed" % (id)
log.write("%s: error: Validity check failed\n" % (id))
doc.freeDoc()
return 0
print("%s: error: Validity check failed" % (id))
log.write("%s: error: Validity check failed\n" % (id))
doc.freeDoc()
return 0
if error_nr != 0 or valid != 1:
print "%s: warning: valid document reported an error" % (id)
log.write("%s: warning: valid document reported an error\n" % (id))
doc.freeDoc()
return 2
print("%s: warning: valid document reported an error" % (id))
log.write("%s: warning: valid document reported an error\n" % (id))
doc.freeDoc()
return 2
doc.freeDoc()
return 1
@ -293,21 +292,21 @@ def runTest(test):
uri = test.prop('URI')
id = test.prop('ID')
if uri == None:
print "Test without ID:", uri
return -1
if id == None:
print "Test without URI:", id
return -1
if uri is None:
print("Test without ID:", uri)
return -1
if id is None:
print("Test without URI:", id)
return -1
base = test.getBase(None)
URI = libxml2.buildURI(uri, base)
if os.access(URI, os.R_OK) == 0:
print "Test %s missing: base %s uri %s" % (URI, base, uri)
return -1
print("Test %s missing: base %s uri %s" % (URI, base, uri))
return -1
type = test.prop('TYPE')
if type == None:
print "Test %s missing TYPE" % (id)
return -1
if type is None:
print("Test %s missing TYPE" % (id))
return -1
extra = None
if type == "invalid":
@ -316,94 +315,94 @@ def runTest(test):
res = testValid(URI, id)
elif type == "not-wf":
extra = test.prop('ENTITIES')
# print URI
#if extra == None:
# res = testNotWfEntDtd(URI, id)
#elif extra == 'none':
# res = testNotWf(URI, id)
#elif extra == 'general':
# res = testNotWfEnt(URI, id)
#elif extra == 'both' or extra == 'parameter':
res = testNotWfEntDtd(URI, id)
#else:
# print "Unknown value %s for an ENTITIES test value" % (extra)
# return -1
# print(URI)
#if extra is None:
# res = testNotWfEntDtd(URI, id)
#elif extra == 'none':
# res = testNotWf(URI, id)
#elif extra == 'general':
# res = testNotWfEnt(URI, id)
#elif extra == 'both' or extra == 'parameter':
res = testNotWfEntDtd(URI, id)
#else:
# print("Unknown value %s for an ENTITIES test value" % (extra))
# return -1
elif type == "error":
res = testError(URI, id)
res = testError(URI, id)
else:
# TODO skipped for now
return -1
return -1
test_nr = test_nr + 1
if res > 0:
test_succeed = test_succeed + 1
test_succeed = test_succeed + 1
elif res == 0:
test_failed = test_failed + 1
test_failed = test_failed + 1
elif res < 0:
test_error = test_error + 1
test_error = test_error + 1
# Log the ontext
if res != 1:
log.write(" File: %s\n" % (URI))
content = string.strip(test.content)
while content[-1] == '\n':
content = content[0:-1]
if extra != None:
log.write(" %s:%s:%s\n" % (type, extra, content))
else:
log.write(" %s:%s\n\n" % (type, content))
if error_msg != '':
log.write(" ----\n%s ----\n" % (error_msg))
error_msg = ''
log.write("\n")
log.write(" File: %s\n" % (URI))
content = test.content.strip()
while content[-1] == '\n':
content = content[0:-1]
if extra != None:
log.write(" %s:%s:%s\n" % (type, extra, content))
else:
log.write(" %s:%s\n\n" % (type, content))
if error_msg != '':
log.write(" ----\n%s ----\n" % (error_msg))
error_msg = ''
log.write("\n")
return 0
def runTestCases(case):
profile = case.prop('PROFILE')
if profile != None and \
string.find(profile, "IBM XML Conformance Test Suite - Production") < 0:
print "=>", profile
profile.find("IBM XML Conformance Test Suite - Production") < 0:
print("=>", profile)
test = case.children
while test != None:
if test.name == 'TEST':
runTest(test)
if test.name == 'TESTCASES':
runTestCases(test)
runTest(test)
if test.name == 'TESTCASES':
runTestCases(test)
test = test.next
conf = loadNoentDoc(CONF)
if conf == None:
print "Unable to load %s" % CONF
if conf is None:
print("Unable to load %s" % CONF)
sys.exit(1)
testsuite = conf.getRootElement()
if testsuite.name != 'TESTSUITE':
print "Expecting TESTSUITE root element: aborting"
print("Expecting TESTSUITE root element: aborting")
sys.exit(1)
profile = testsuite.prop('PROFILE')
if profile != None:
print profile
print(profile)
start = time.time()
case = testsuite.children
while case != None:
if case.name == 'TESTCASES':
old_test_nr = test_nr
old_test_succeed = test_succeed
old_test_failed = test_failed
old_test_error = test_error
old_test_nr = test_nr
old_test_succeed = test_succeed
old_test_failed = test_failed
old_test_error = test_error
runTestCases(case)
print " Ran %d tests: %d succeeded, %d failed and %d generated an error" % (
test_nr - old_test_nr, test_succeed - old_test_succeed,
test_failed - old_test_failed, test_error - old_test_error)
print(" Ran %d tests: %d succeeded, %d failed and %d generated an error" % (
test_nr - old_test_nr, test_succeed - old_test_succeed,
test_failed - old_test_failed, test_error - old_test_error))
case = case.next
conf.freeDoc()
log.close()
print "Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s." % (
test_nr, test_succeed, test_failed, test_error, time.time() - start)
print("Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s." % (
test_nr, test_succeed, test_failed, test_error, time.time() - start))

View File

@ -2,8 +2,12 @@
import sys
import time
import os
import string
import StringIO
try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
sys.path.insert(0, "python")
import libxml2
@ -44,8 +48,8 @@ resources = {}
def resolver(URL, ID, ctxt):
global resources
if resources.has_key(URL):
return(StringIO.StringIO(resources[URL]))
if URL in resources:
return(StringIO(resources[URL]))
log.write("Resolver failure: asked %s\n" % (URL))
log.write("resources: %s\n" % (resources))
return None
@ -59,49 +63,49 @@ def handle_valid(node, schema):
global nb_instances_failed
instance = node.prop("dtd")
if instance == None:
if instance is None:
instance = ""
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
mem = libxml2.debugMemory(1);
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nFailed to parse correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
return
nb_instances_failed = nb_instances_failed + 1
return
if debug:
print "instance line %d" % (node.lineNo())
print("instance line %d" % (node.lineNo()))
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
except:
ret = -1
doc.freeDoc()
if mem != libxml2.debugMemory(1):
print "validating instance %d line %d leaks" % (
nb_instances_tests, node.lineNo())
print("validating instance %d line %d leaks" % (
nb_instances_tests, node.lineNo()))
if ret != 0:
log.write("\nFailed to validate correct instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
#
# handle an invalid instance
@ -112,50 +116,50 @@ def handle_invalid(node, schema):
global nb_instances_failed
instance = node.prop("dtd")
if instance == None:
if instance is None:
instance = ""
child = node.children
while child != None:
if child.type != 'text':
instance = instance + child.serialize()
child = child.next
instance = instance + child.serialize()
child = child.next
# mem = libxml2.debugMemory(1);
try:
doc = libxml2.parseDoc(instance)
doc = libxml2.parseDoc(instance)
except:
doc = None
if doc == None:
if doc is None:
log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
return
return
if debug:
print "instance line %d" % (node.lineNo())
print("instance line %d" % (node.lineNo()))
try:
ctxt = schema.relaxNGNewValidCtxt()
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
ret = doc.relaxNGValidateDoc(ctxt)
del ctxt
except:
ret = -1
doc.freeDoc()
# if mem != libxml2.debugMemory(1):
# print "validating instance %d line %d leaks" % (
# nb_instances_tests, node.lineNo())
# print("validating instance %d line %d leaks" % (
# nb_instances_tests, node.lineNo()))
if ret == 0:
log.write("\nFailed to detect validation problem in instance:\n-----\n")
log.write(instance)
log.write(instance)
log.write("\n-----\n")
nb_instances_failed = nb_instances_failed + 1
nb_instances_failed = nb_instances_failed + 1
else:
nb_instances_success = nb_instances_success + 1
nb_instances_success = nb_instances_success + 1
#
# handle an incorrect test
@ -169,23 +173,23 @@ def handle_correct(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs == None:
if rngs is None:
log.write("\nFailed to compile correct schema:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
nb_schemas_success = nb_schemas_success + 1
nb_schemas_success = nb_schemas_success + 1
return rngs
def handle_incorrect(node):
global log
global nb_schemas_success
@ -195,24 +199,24 @@ def handle_incorrect(node):
child = node.children
while child != None:
if child.type != 'text':
schema = schema + child.serialize()
child = child.next
schema = schema + child.serialize()
child = child.next
try:
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
except:
rngs = None
if rngs != None:
log.write("\nFailed to detect schema error in:\n-----\n")
log.write(schema)
log.write(schema)
log.write("\n-----\n")
nb_schemas_failed = nb_schemas_failed + 1
nb_schemas_failed = nb_schemas_failed + 1
else:
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
# log.write("\nSuccess detecting schema error in:\n-----\n")
# log.write(schema)
# log.write("\n-----\n")
nb_schemas_success = nb_schemas_success + 1
return None
#
@ -222,14 +226,14 @@ def handle_resource(node, dir):
global resources
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -238,8 +242,8 @@ def handle_resource(node, dir):
child = node.children
while child != None:
if child.type != 'text':
res = res + child.serialize()
child = child.next
res = res + child.serialize()
child = child.next
resources[name] = res
#
@ -247,14 +251,14 @@ def handle_resource(node, dir):
#
def handle_dir(node, dir):
try:
name = node.prop('name')
name = node.prop('name')
except:
name = None
if name == None or name == '':
if name is None or name == '':
log.write("resource has no name")
return;
return;
if dir != None:
# name = libxml2.buildURI(name, dir)
name = dir + '/' + name
@ -280,7 +284,7 @@ def handle_testCase(node):
nb_schemas_tests, node.lineNo(), sections))
resources = {}
if debug:
print "test %d line %d" % (nb_schemas_tests, node.lineNo())
print("test %d line %d" % (nb_schemas_tests, node.lineNo()))
dirs = node.xpathEval('dir')
for dir in dirs:
@ -292,27 +296,27 @@ def handle_testCase(node):
tsts = node.xpathEval('incorrect')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <incorrect> example" %(node.lineNo())
schema = handle_incorrect(tsts[0])
print("warning test line %d has more than one <incorrect> example" %(node.lineNo()))
schema = handle_incorrect(tsts[0])
else:
tsts = node.xpathEval('correct')
if tsts != []:
if len(tsts) != 1:
print "warning test line %d has more than one <correct> example"% (node.lineNo())
schema = handle_correct(tsts[0])
else:
print "warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo())
if tsts != []:
if len(tsts) != 1:
print("warning test line %d has more than one <correct> example"% (node.lineNo()))
schema = handle_correct(tsts[0])
else:
print("warning <testCase> line %d has no <correct> nor <incorrect> child" % (node.lineNo()))
nb_schemas_tests = nb_schemas_tests + 1;
valids = node.xpathEval('valid')
invalids = node.xpathEval('invalid')
nb_instances_tests = nb_instances_tests + len(valids) + len(invalids)
if schema != None:
for valid in valids:
handle_valid(valid, schema)
handle_valid(valid, schema)
for invalid in invalids:
handle_invalid(invalid, schema)
handle_invalid(invalid, schema)
#
@ -322,60 +326,60 @@ def handle_testSuite(node, level = 0):
global nb_schemas_tests, nb_schemas_success, nb_schemas_failed
global nb_instances_tests, nb_instances_success, nb_instances_failed
if verbose and level >= 0:
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
old_schemas_tests = nb_schemas_tests
old_schemas_success = nb_schemas_success
old_schemas_failed = nb_schemas_failed
old_instances_tests = nb_instances_tests
old_instances_success = nb_instances_success
old_instances_failed = nb_instances_failed
docs = node.xpathEval('documentation')
authors = node.xpathEval('author')
if docs != []:
msg = ""
for doc in docs:
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print msg
msg = msg + doc.content + " "
if authors != []:
msg = msg + "written by "
for author in authors:
msg = msg + author.content + " "
if quiet == 0:
print(msg)
sections = node.xpathEval('section')
if verbose and sections != [] and level <= 0:
msg = ""
for section in sections:
msg = msg + section.content + " "
if quiet == 0:
print "Tests for section %s" % (msg)
msg = msg + section.content + " "
if quiet == 0:
print("Tests for section %s" % (msg))
for test in node.xpathEval('testCase'):
handle_testCase(test)
for test in node.xpathEval('testSuite'):
handle_testSuite(test, level + 1)
if verbose and level >= 0 :
if sections != []:
msg = ""
for section in sections:
msg = msg + section.content + " "
print "Result of tests for section %s" % (msg)
elif docs != []:
msg = ""
for doc in docs:
msg = msg + doc.content + " "
print "Result of tests for %s" % (msg)
msg = ""
for section in sections:
msg = msg + section.content + " "
print("Result of tests for section %s" % (msg))
elif docs != []:
msg = ""
for doc in docs:
msg = msg + doc.content + " "
print("Result of tests for %s" % (msg))
if nb_schemas_tests != old_schemas_tests:
print "found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed)
if nb_instances_tests != old_instances_tests:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed)
print("found %d test schemas: %d success %d failures" % (
nb_schemas_tests - old_schemas_tests,
nb_schemas_success - old_schemas_success,
nb_schemas_failed - old_schemas_failed))
if nb_instances_tests != old_instances_tests:
print("found %d test instances: %d success %d failures" % (
nb_instances_tests - old_instances_tests,
nb_instances_success - old_instances_success,
nb_instances_failed - old_instances_failed))
#
# Parse the conf file
#
@ -394,18 +398,18 @@ libxml2.registerErrorHandler(callback, "")
libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
print "%s doesn't start with a testSuite element, aborting" % (CONF)
print("%s doesn't start with a testSuite element, aborting" % (CONF))
sys.exit(1)
if quiet == 0:
print "Running Relax NG testsuite"
print("Running Relax NG testsuite")
handle_testSuite(root)
if quiet == 0 or nb_schemas_failed != 0:
print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
print("\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
nb_schemas_tests, nb_schemas_success, nb_schemas_failed))
if quiet == 0 or nb_instances_failed != 0:
print "found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed)
print("found %d test instances: %d success %d failures" % (
nb_instances_tests, nb_instances_success, nb_instances_failed))
testsuite.freeDoc()
@ -414,7 +418,7 @@ libxml2.relaxNGCleanupTypes()
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
if quiet == 0:
print "OK"
print("OK")
else:
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
libxml2.dumpMemory()

View File

@ -6,7 +6,7 @@
#
import sys, os
import exceptions, optparse
import optparse
import libxml2
opa = optparse.OptionParser()
@ -322,7 +322,7 @@ class XSTCTestCase:
sys.stdout.write("'%s'\n" % self.name)
try:
self.validate()
except (Exception, libxml2.parserError, libxml2.treeError), e:
except (Exception, libxml2.parserError, libxml2.treeError) as e:
self.failExcept(e)
def parseSchema(fileName):
@ -359,7 +359,7 @@ class XSTCSchemaTest(XSTCTestCase):
if schema is None:
self.debugMsg("schema is None")
self.debugMsg("checking for IO errors...")
if self.isIOError(file, "schema"):
if self.isIOError(filePath, "schema"):
return
self.debugMsg("checking schema result")
if (schema is None and self.val) or (schema is not None and self.val == 0):
@ -614,7 +614,7 @@ class XSTCTestRunner:
def addToCombines(self, test):
found = False
if self.combinesRan.has_key(test.combineName):
if test.combineName in self.combinesRan:
self.combinesRan[test.combineName].append(test)
else:
self.combinesRan[test.combineName] = [test]