📄 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("--no-log", action="store_true", dest="disableLog",
default=False,
help="The filepath of the log file to be created")
opa.add_option("--no-test-out", action="store_true", dest="disableTestStdOut",
default=False,
help="The filepath of the log file to be created")
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("--rieo", "--report-internal-errors-only", action="store_true",
dest="reportInternalErrOnly", default=False,
help="Display erroneous tests of type 'internal' 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("--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.")
(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."
testFolderNIST = "nisttest"
testFolderMS = "msxsdtest"
testFolderSUN = "suntest"
###################
# Helper functions.
#
def handleError(test, msg):
test.addLibLog("'%s' LIB: %s" % (test.name, msg))
if msg.find("Unimplemented") > -1:
test.failUnimplemented()
elif msg.find("Internal") > -1:
test.failInternal()
##################
# Test case class.
#
class MSTestCase:
def __init__(self, name, descr, tFolder, sFolder, sFile, sVal, iExists, iFolder, iFile, iVal):
global testFolderNIST, testFolderSUN, testFolderMS
#
# Init.
#
self.name = name
self.descr = descr
self.test_Folder = tFolder
self.schema_Folder = sFolder
self.schema_File = sFile
self.schema_Val = sVal
self.instance_Exists = iExists
self.instance_Folder = iFolder
self.instance_File = iFile
self.instance_Val = iVal
self.failed = False
self.log = []
self.libLog = []
self.phase = ""
self.initialMemUsed = 0
self.memLeak = 0
self.excepted = False
self.bad = False
self.unimplemented = False
self.internalErr = False
#
# Compute combine name of this test.
#
if self.test_Folder == testFolderMS or self.test_Folder == testFolderSUN:
#
# Use the last given directory for the combine name.
#
dirs = self.schema_Folder.split("/")
self.combineName = dirs[len(dirs) -1]
if self.test_Folder == testFolderMS:
if self.combineName == "group":
self.schema_Folder = "Group"
self.instance_Folder = "Group"
elif self.test_Folder == testFolderNIST:
#
# NIST files are named in the following form:
# "NISTSchema-short-pattern-1.xsd"
#
tokens = self.schema_File.split("-")
self.combineName = tokens[1]
else:
self.combineName = "unkown"
raise Exception("Could not compute the combine name of a test.")
#
# Init the log.
#
self.log.append("'%s' descr: %s\n" % (self.name, self.descr))
self.log.append("'%s' exp schema valid: %d\n" % (self.name, self.schema_Val))
if (self.instance_Exists):
self.log.append("'%s' exp instance valid: %d\n" % (self.name, self.instance_Val))
def addLibLog(self, msg):
"""This one is intended to be used by the error handler
function"""
self.libLog.append(msg)
def fail(self, msg):
self.failed = True
self.log.append("'%s' ( FAILED: %s\n" % (self.name, msg))
def failInternal(self):
self.failed = True
self.internalErr = True
self.log.append("'%s' * INTERNAL\n" % self.name)
def failUnimplemented(self):
self.failed = True
self.unimplemented = True
self.log.append("'%s' ? UNIMPLEMENTED\n" % self.name)
def failCritical(self, msg):
self.failed = True
self.bad = True
self.log.append("'%s' ! BAD: %s\n" % (self.name, msg))
def failExcept(self, e):
self.failed = True
self.excepted = True
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):
"""Adds additional info to the log."""
#
# Add libxml2 messages.
#
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 processSchema(self, filePath):
global msgSchemaNotValidButShould, msgSchemaValidButShouldNot
schema = None
#
# Parse the schema.
#
self.debugMsg("loading schema: %s" % filePath)
schema_ParserCtxt = libxml2.schemaNewParserCtxt(filePath)
try:
try:
schema = schema_ParserCtxt.schemaParse()
except:
pass
finally:
self.debugMsg("after loading schema")
del schema_ParserCtxt
if schema is None:
self.debugMsg("schema is None")
self.debugMsg("checking for IO errors...")
if self.isIOError(file, "schema"):
return None
self.debugMsg("checking schema result")
if (schema is None and self.schema_Val) or (schema is not None and self.schema_Val == 0):
self.debugMsg("schema result is BAD")
if (schema == None):
self.fail(msgSchemaNotValidButShould)
else:
self.fail(msgSchemaValidButShouldNot)
else:
self.debugMsg("schema result is OK")
return schema
def processInstance(self, filePath, schema):
global msgInstanceNotValidButShould, msgInstanceValidButShouldNot
instance = None
self.debugMsg("loading instance: %s" % filePath)
instance_parserCtxt = libxml2.newParserCtxt()
if (instance_parserCtxt is None):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -