📄 xstc.py
字号:
#!/usr/bin/env python
#
# This is the MS subset of the W3C test suite for XML Schemas.
# This file is generated from the MS W3c test suite description file.
#
import sys, os
import exceptions, optparse
import libxml2
opa = optparse.OptionParser()
opa.add_option("-b", "--base", action="store", type="string", dest="baseDir",
default="",
help="""The base directory; i.e. the parent folder of the
"nisttest", "suntest" and "msxsdtest" directories.""")
opa.add_option("-o", "--out", action="store", type="string", dest="logFile",
default="test.log",
help="The filepath of the log file to be created")
opa.add_option("--log", action="store_true", dest="enableLog",
default=False,
help="Create the log file")
opa.add_option("--no-test-out", action="store_true", dest="disableTestStdOut",
default=False,
help="Don't output test results")
opa.add_option("-s", "--silent", action="store_true", dest="silent", default=False,
help="Disables display of all tests")
opa.add_option("-v", "--verbose", action="store_true", dest="verbose",
default=False,
help="Displays all tests (only if --silent is not set)")
opa.add_option("-x", "--max", type="int", dest="maxTestCount",
default="-1",
help="The maximum number of tests to be run")
opa.add_option("-t", "--test", type="string", dest="singleTest",
default=None,
help="Runs the specified test only")
opa.add_option("--tsw", "--test-starts-with", type="string", dest="testStartsWith",
default=None,
help="Runs the specified test(s), starting with the given string")
opa.add_option("--rieo", "--report-internal-errors-only", action="store_true",
dest="reportInternalErrOnly", default=False,
help="Display erroneous tests of type 'internal' only")
opa.add_option("--rueo", "--report-unimplemented-errors-only", action="store_true",
dest="reportUnimplErrOnly", default=False,
help="Display erroneous tests of type 'unimplemented' only")
opa.add_option("--rmleo", "--report-mem-leak-errors-only", action="store_true",
dest="reportMemLeakErrOnly", default=False,
help="Display erroneous tests of type 'memory leak' only")
opa.add_option("-c", "--combines", type="string", dest="combines",
default=None,
help="Combines to be run (all if omitted)")
opa.add_option("--csw", "--csw", type="string", dest="combineStartsWith",
default=None,
help="Combines to be run (all if omitted)")
opa.add_option("--rc", "--report-combines", action="store_true",
dest="reportCombines", default=False,
help="Display combine reports")
opa.add_option("--rec", "--report-err-combines", action="store_true",
dest="reportErrCombines", default=False,
help="Display erroneous combine reports only")
opa.add_option("--debug", action="store_true",
dest="debugEnabled", default=False,
help="Displays debug messages")
opa.add_option("--info", action="store_true",
dest="info", default=False,
help="Displays info on the suite only. Does not run any test.")
opa.add_option("--sax", action="store_true",
dest="validationSAX", default=False,
help="Use SAX2-driven validation.")
opa.add_option("--tn", action="store_true",
dest="displayTestName", default=False,
help="Display the test name in every case.")
(options, args) = opa.parse_args()
if options.combines is not None:
options.combines = options.combines.split()
################################################
# The vars below are not intended to be changed.
#
msgSchemaNotValidButShould = "The schema should be valid."
msgSchemaValidButShouldNot = "The schema should be invalid."
msgInstanceNotValidButShould = "The instance should be valid."
msgInstanceValidButShouldNot = "The instance should be invalid."
vendorNIST = "NIST"
vendorNIST_2 = "NIST-2"
vendorSUN = "SUN"
vendorMS = "MS"
###################
# Helper functions.
#
vendor = None
def handleError(test, msg):
global options
if not options.silent:
test.addLibLog("'%s' LIB: %s" % (test.name, msg))
if msg.find("Unimplemented") > -1:
test.failUnimplemented()
elif msg.find("Internal") > -1:
test.failInternal()
def fixFileNames(fileName):
if (fileName is None) or (fileName == ""):
return ""
dirs = fileName.split("/")
if dirs[1] != "Tests":
fileName = os.path.join(".", "Tests")
for dir in dirs[1:]:
fileName = os.path.join(fileName, dir)
return fileName
class XSTCTestGroup:
def __init__(self, name, schemaFileName, descr):
global vendor, vendorNIST_2
self.name = name
self.descr = descr
self.mainSchema = True
self.schemaFileName = fixFileNames(schemaFileName)
self.schemaParsed = False
self.schemaTried = False
def setSchema(self, schemaFileName, parsed):
if not self.mainSchema:
return
self.mainSchema = False
self.schemaParsed = parsed
self.schemaTried = True
class XSTCTestCase:
# <!-- groupName, Name, Accepted, File, Val, Descr
def __init__(self, isSchema, groupName, name, accepted, file, val, descr):
global options
#
# Constructor.
#
self.testRunner = None
self.isSchema = isSchema
self.groupName = groupName
self.name = name
self.accepted = accepted
self.fileName = fixFileNames(file)
self.val = val
self.descr = descr
self.failed = False
self.combineName = None
self.log = []
self.libLog = []
self.initialMemUsed = 0
self.memLeak = 0
self.excepted = False
self.bad = False
self.unimplemented = False
self.internalErr = False
self.noSchemaErr = False
self.failed = False
#
# Init the log.
#
if not options.silent:
if self.descr is not None:
self.log.append("'%s' descr: %s\n" % (self.name, self.descr))
self.log.append("'%s' exp validity: %d\n" % (self.name, self.val))
def initTest(self, runner):
global vendorNIST, vendorSUN, vendorMS, vendorNIST_2, options, vendor
#
# Get the test-group.
#
self.runner = runner
self.group = runner.getGroup(self.groupName)
if vendor == vendorMS or vendor == vendorSUN:
#
# Use the last given directory for the combine name.
#
dirs = self.fileName.split("/")
self.combineName = dirs[len(dirs) -2]
elif vendor == vendorNIST:
#
# NIST files are named in the following form:
# "NISTSchema-short-pattern-1.xsd"
#
tokens = self.name.split("-")
self.combineName = tokens[1]
elif vendor == vendorNIST_2:
#
# Group-names have the form: "atomic-normalizedString-length-1"
#
tokens = self.groupName.split("-")
self.combineName = "%s-%s" % (tokens[0], tokens[1])
else:
self.combineName = "unkown"
raise Exception("Could not compute the combine name of a test.")
if (not options.silent) and (self.group.descr is not None):
self.log.append("'%s' group-descr: %s\n" % (self.name, self.group.descr))
def addLibLog(self, msg):
"""This one is intended to be used by the error handler
function"""
global options
if not options.silent:
self.libLog.append(msg)
def fail(self, msg):
global options
self.failed = True
if not options.silent:
self.log.append("'%s' ( FAILED: %s\n" % (self.name, msg))
def failNoSchema(self):
global options
self.failed = True
self.noSchemaErr = True
if not options.silent:
self.log.append("'%s' X NO-SCHEMA\n" % (self.name))
def failInternal(self):
global options
self.failed = True
self.internalErr = True
if not options.silent:
self.log.append("'%s' * INTERNAL\n" % self.name)
def failUnimplemented(self):
global options
self.failed = True
self.unimplemented = True
if not options.silent:
self.log.append("'%s' ? UNIMPLEMENTED\n" % self.name)
def failCritical(self, msg):
global options
self.failed = True
self.bad = True
if not options.silent:
self.log.append("'%s' ! BAD: %s\n" % (self.name, msg))
def failExcept(self, e):
global options
self.failed = True
self.excepted = True
if not options.silent:
self.log.append("'%s' # EXCEPTION: %s\n" % (self.name, e.__str__()))
def setUp(self):
#
# Set up Libxml2.
#
self.initialMemUsed = libxml2.debugMemory(1)
libxml2.initParser()
libxml2.lineNumbersDefault(1)
libxml2.registerErrorHandler(handleError, self)
def tearDown(self):
libxml2.schemaCleanupTypes()
libxml2.cleanupParser()
self.memLeak = libxml2.debugMemory(1) - self.initialMemUsed
def isIOError(self, file, docType):
err = None
try:
err = libxml2.lastError()
except:
# Suppress exceptions.
pass
if (err is None):
return False
if err.domain() == libxml2.XML_FROM_IO:
self.failCritical("failed to access the %s resource '%s'\n" % (docType, file))
def debugMsg(self, msg):
global options
if options.debugEnabled:
sys.stdout.write("'%s' DEBUG: %s\n" % (self.name, msg))
def finalize(self):
global options
"""Adds additional info to the log."""
#
# Add libxml2 messages.
#
if not options.silent:
self.log.extend(self.libLog)
#
# Add memory leaks.
#
if self.memLeak != 0:
self.log.append("%s + memory leak: %d bytes\n" % (self.name, self.memLeak))
def run(self):
"""Runs a test."""
global options
##filePath = os.path.join(options.baseDir, self.fileName)
# filePath = "%s/%s/%s/%s" % (options.baseDir, self.test_Folder, self.schema_Folder, self.schema_File)
if options.displayTestName:
sys.stdout.write("'%s'\n" % self.name)
try:
self.validate()
except (Exception, libxml2.parserError, libxml2.treeError), e:
self.failExcept(e)
def parseSchema(fileName):
schema = None
ctxt = libxml2.schemaNewParserCtxt(fileName)
try:
try:
schema = ctxt.schemaParse()
except:
pass
finally:
del ctxt
return schema
class XSTCSchemaTest(XSTCTestCase):
def __init__(self, groupName, name, accepted, file, val, descr):
XSTCTestCase.__init__(self, 1, groupName, name, accepted, file, val, descr)
def validate(self):
global msgSchemaNotValidButShould, msgSchemaValidButShouldNot
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -