Ejemplo n.º 1
0
def process_test(test, tester):
    description = get_description(test)
    tester.startGroup(description)
    incorrect_schema = test.xpath('incorrect/*')
    correct_schema = test.xpath('correct/*')
    valid_xml = test.xpath('valid/*')
    invalid_xml = test.xpath('invalid/*')
    if incorrect_schema:
        tester.startTest('Parse invalid schema')
        schema_doc = doc_from_fragment(incorrect_schema[0])
        try:
            validator = RelaxNgValidator(schema_doc)
        except RngSchemaInvalidException:
            pass
        else:
            errmsg = 'Invalid schema parse fails to raise RngSchemaInvalidException!\n'
            errmsg += 'This is the schema:\n%s' % serialize_doc(schema_doc)
            tester.error(errmsg)
        tester.testDone()
    elif correct_schema:
        schema_doc = doc_from_fragment(correct_schema[0])
        valid_docs = [doc_from_fragment(node) for node in valid_xml]
        invalid_docs = [doc_from_fragment(node) for node in invalid_xml]
        i = 0
        for valid_doc in valid_docs:
            i += 1
            desc = 'valid doc %d of %d' % (i, len(valid_docs))
            tester.startTest(desc)
            try:
                validator = RelaxNgValidator(schema_doc)
            except RngSchemaInvalidException, e:
                tester.warning('Could not complete test; schema was not valid: %s' % e)
                tester.message('This is the schema:\n%s' % serialize_doc(schema_doc))
                tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc))
                tester.testDone()
                break
            result = validator.validateNode(valid_doc)
            if not result.nullable():
                if hasattr(result, 'msg'):
                    tester.error('Valid doc fails to validate: %r' % result.msg)
                else:
                    tester.error('Valid doc fails to validate (no reason given)')
                    tester.message('Result was %r' % result)
                tester.message('This is the schema:\n%s' % serialize_doc(schema_doc))
                tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc))
            tester.testDone()
        for invalid_doc in invalid_docs:
            desc = 'invalid doc %d of %d' % (i, len(valid_docs))
            tester.startTest(desc)
            try:
                validator = RelaxNgValidator(schema_doc)
            except RngSchemaInvalidException, e:
                tester.warning('Could not complete test; schema was not valid: %s' % e)
                tester.message('This is the schema:\n%s' % serialize_doc(schema_doc))
                tester.testDone()
                break
            result = validator.validateNode(invalid_doc)
            if result.nullable():
                tester.error('Invalid doc validates as valid!')
                tester.message('This is the schema:\n%s' % serialize_doc(schema_doc))
                tester.message('This is the invalid doc:\n%s' % serialize_doc(invalid_doc))
            tester.testDone()
Ejemplo n.º 2
0
                             "available in the Ft.Xml.ThirdParty package.")

    from Ft.Xml import Domlette
    if validate_flag:
        reader = Domlette.ValidatingReader
    else:
        reader = Domlette.NonvalidatingReader

    try:
        doc = reader.parse(source_isrc)
        CloseStream(source_isrc, quiet=True)

        if rng_isrc is not None:
            validator = RelaxNgValidator(rng_isrc)
            CloseStream(rng_isrc, quiet=True)
            result = validator.validateNode(doc)
            if not result.nullable():
                raise RngInvalid(result)

        if not noserialize:
            from Ft.Xml.Domlette import Print, PrettyPrint
            if pretty:
                PrettyPrint(doc, out_file, encoding, as_html)
            else:
                Print(doc, out_file, encoding, as_html)

    except Exception, e:
        import traceback
        traceback.print_exc(1000, sys.stderr)
        raise
Ejemplo n.º 3
0
def process_test(test, tester):
    description = get_description(test)
    tester.startGroup(description)
    incorrect_schema = test.xpath('incorrect/*')
    correct_schema = test.xpath('correct/*')
    valid_xml = test.xpath('valid/*')
    invalid_xml = test.xpath('invalid/*')
    if incorrect_schema:
        tester.startTest('Parse invalid schema')
        schema_doc = doc_from_fragment(incorrect_schema[0])
        try:
            validator = RelaxNgValidator(schema_doc)
        except RngSchemaInvalidException:
            pass
        else:
            errmsg = 'Invalid schema parse fails to raise RngSchemaInvalidException!\n'
            errmsg += 'This is the schema:\n%s' % serialize_doc(schema_doc)
            tester.error(errmsg)
        tester.testDone()
    elif correct_schema:
        schema_doc = doc_from_fragment(correct_schema[0])
        valid_docs = [doc_from_fragment(node) for node in valid_xml]
        invalid_docs = [doc_from_fragment(node) for node in invalid_xml]
        i = 0
        for valid_doc in valid_docs:
            i += 1
            desc = 'valid doc %d of %d' % (i, len(valid_docs))
            tester.startTest(desc)
            try:
                validator = RelaxNgValidator(schema_doc)
            except RngSchemaInvalidException, e:
                tester.warning(
                    'Could not complete test; schema was not valid: %s' % e)
                tester.message('This is the schema:\n%s' %
                               serialize_doc(schema_doc))
                tester.message('This is the valid doc:\n%s' %
                               serialize_doc(valid_doc))
                tester.testDone()
                break
            result = validator.validateNode(valid_doc)
            if not result.nullable():
                if hasattr(result, 'msg'):
                    tester.error('Valid doc fails to validate: %r' %
                                 result.msg)
                else:
                    tester.error(
                        'Valid doc fails to validate (no reason given)')
                    tester.message('Result was %r' % result)
                tester.message('This is the schema:\n%s' %
                               serialize_doc(schema_doc))
                tester.message('This is the valid doc:\n%s' %
                               serialize_doc(valid_doc))
            tester.testDone()
        for invalid_doc in invalid_docs:
            desc = 'invalid doc %d of %d' % (i, len(valid_docs))
            tester.startTest(desc)
            try:
                validator = RelaxNgValidator(schema_doc)
            except RngSchemaInvalidException, e:
                tester.warning(
                    'Could not complete test; schema was not valid: %s' % e)
                tester.message('This is the schema:\n%s' %
                               serialize_doc(schema_doc))
                tester.testDone()
                break
            result = validator.validateNode(invalid_doc)
            if result.nullable():
                tester.error('Invalid doc validates as valid!')
                tester.message('This is the schema:\n%s' %
                               serialize_doc(schema_doc))
                tester.message('This is the invalid doc:\n%s' %
                               serialize_doc(invalid_doc))
            tester.testDone()