[Zope-CVS] CVS: Packages/HTMLStructure/tests - testParser.py:1.1 testValidator.py:1.1

Evan Simpson evan@zope.com
Wed, 23 Jan 2002 10:57:57 -0500


Update of /cvs-repository/Packages/HTMLStructure/tests
In directory cvs.zope.org:/tmp/cvs-serv29501/tests

Added Files:
	testParser.py testValidator.py 
Log Message:
Initial checkin.


=== Added File Packages/HTMLStructure/tests/testParser.py ===
# To run these tests, use:
#   python unittest.py DateTime.tests.suite

import unittest
import Parser

parseTables = Parser.ParseTables()

class ParserTests (unittest.TestCase):

    def assertValid(self, text, table, partial=0):
        result = Parser.tag(text, getattr(parseTables, 'pt_' + table))
        self.failUnless(result[0] and (partial or result[2] == len(text)),
                        msg='Parse failure at %s in %s' % (result[2],
                                                           `text`))
        return result[1]

    def assertInvalid(self, text, table, partial=0):
        result = Parser.tag(text, getattr(parseTables, 'pt_' + table))
        self.failIf(result[0] and (partial or result[2] == len(text)),
                    msg='Invalid %s %s accepted' % (table, `text`))
        return result

    def testValidCharRef(self):
        '''Valid character entity references'''
        check = self.assertValid
        for after in '', ';', 'g':
            for n in 1, 100, 500, 10000:
                check('&#%s%s' % (n, after), 'charref')
                check('&#x%s%s' % (hex(n)[2:], after), 'charref')
            for name in 'amp', 'x', 'ns:a-z':
                check('&%s%s' % (name, after), 'charref')

    def testInvalidCharRef(self):
        '''Invalid character entity references'''
        check = self.assertInvalid
        for bogus in ('a', '#0', '#xFF', '&#xena;', '&34g'):
            check(bogus, 'charref')
        check = self.assertValid
        for bogus in ('&#xena;', '&34g', '&*name;', '&#FF;'):
            result = check(bogus, 'charref', partial=1)
            self.failUnless(result and result[-1] and result[-1][0] == 'error',
                            msg='Bad parse of %s: %s' % (bogus, result))

    def testValidAttributeValue(self):
        '''Valid attribute values'''
        check = self.assertValid
        for value in '', 'foo', ' & ', '"', ''', 'one\ntwo':
            check('"%s"' % value, 'attrvalue1')
            check("'%s'" % value, 'attrvalue2')

    def testValidAttribute(self):
        '''Valid attributes'''
        check = self.assertValid
        for name in 'a', 'ba', 'c:2':
            for eq in '=', '  =', '=  ', ' = ':
                check('%s%s""' % (name, eq), 'attribute1')
        check('checked', 'attribute2')
        check('one=1', 'attribute1')

    def testInvalidAttribute(self):
        '''Invalid attributes'''
        check = self.assertInvalid
        for attr in '2e', 'a=="b"':
            check(attr, 'attribute1')
            check(attr, 'attribute2')

    def testValidOpenTag(self):
        '''Valid opening (and empty) tags'''
        check = self.assertValid
        for name in 'a', 'img', 'tal:block':
            for attrs in ('', ' href="foo"',
                          """ name="fred" id='f'""",
                          "checked id='f'   name = fred",):
                for end in '', ' /':
                    check('%s%s%s>' % (name, attrs, end), 'open_tag')

    def testValidCloseTag(self):
        '''Valid closing tags'''
        check = self.assertValid
        for name in 'a', 'img', 'tal:block':
            check('/%s>' % name, 'close_tag')

    def testValidComment(self):
        '''Valid comments'''
        check = self.assertValid
        for ctxt in '', ' ', 'word', 'hyphen-ated', '- ', ' white\n\tspace ':
            check('!--%s-->' % ctxt, 'comment')

    def testValidDirective(self):
        '''Valid directives'''
        check = self.assertValid
        for name in 'DOCTYPE', 'ELEMENT':
            check('!%s blah>' % name, 'directive')

    def testValidPI(self):
        '''Valid processing instructions'''
        check = self.assertValid
        for name in 'xml', 'a':
            for content in '', ' stuff':
                check('?%s%s?>' % (name, content), 'PI')

    def testValidDocument(self):
        '''A complex document'''
        docfile = open('doc.html')
        self.assertValid(docfile.read(), 'html')
        docfile.close()


def test_suite():
    return unittest.makeSuite(ParserTests)

if __name__=="__main__":
   unittest.TextTestRunner().run(test_suite())


=== Added File Packages/HTMLStructure/tests/testValidator.py ===
# To run these tests, use:
#   python unittest.py DateTime.tests.suite

import unittest
from Parser import ParsedText
from Validator import Validator

class HTMLValidatorTests (unittest.TestCase):

    EntitiesData = (
        ('a & p', 1, (0,)),
        ('a & p', 1, ()),
        ('c &#0387032746873490867983476989483069834098;', 2, (1,)),
        )
    
    def testEntities(self):
        for text, n, erroridxs in self.EntitiesData:
            v = Validator(ParsedText(text))
            all, errors = v.entities()
            self.failUnless(len(all) == n,
                            msg = '%s entities expected, but %s found.' %
                            (n, len(all)))
            self.failUnless(len(errors) == len(erroridxs),
                            msg = '%s mistakes expected, but %s found.' %
                            (n, len(errors)))
            for i in range(len(erroridxs)):
                self.failUnless(all[erroridxs[i]] == errors[i],
                                msg = 'Mismatch %s was not entity %s' %
                                (i, erroridxs[i]))

    NoTagMatchData = (
        '', ' \n  ', '&',
        '<!-- comment -->', '<br>',
        '<?xml?>', '<!DOCTYPE foo>', '<baz/>'
        ) 

    def testNoTagMatch(self):
        for text in self.NoTagMatchData:
            v = Validator(ParsedText(text))
            matched, unclosed, unopened = v.match_tags()
            for thing, name in ((matched, 'match'),
                                (unclosed, 'unclosed'),
                                (unopened, 'unopened'),):
                self.failIf(thing,
                            msg = 'Unexpected %s %s in %s'
                            % (name, `matched`, `text`))

    AllMatchData = (
        ('<b>bold</b>', [(0,1)]),
        ('<i>1 &amp; 2</i>', [(0,2)]),
        ('<p></p><p></p>', [(0,1),(2,3)]),
        ('<p><p></p></p>', [(0,3),(1,2)]),
        )
                            
    def testAllMatch(self):
        for text, matches in self.AllMatchData:
            v = Validator(ParsedText(text))
            matched, unclosed, unopened = v.match_tags()
            matched.sort()
            self.failUnless(matched == matches,
                            msg = 'Improper match %s in %s'
                            % (`matched`, `text`))
            for thing, name in ((unclosed, 'unclosed'),
                                (unopened, 'unopened'),):
                self.failIf(thing,
                            msg = 'Unexpected %s %s in %s'
                            % (name, `matched`, `text`))


    ImplMatch1Data = (
        {'text': 'sdkj <div>k <p> kod <p></div>',
         'matches': ([(0,3)], [1,2],[]),
         'implicit': ([(1,2), (2,3)], []),},
        {'text': 'k</div> <p> kod <p>',
         'matches': ([], [1,2],[0]),
         'implicit': ([(1,2), (2,-1)], []),},        
        {'text': '<table><tr><td>bleh <i>blah!</i><tr><td>foo</table>',
         'matches': ([(0,7), (3,4)], [1,2,5,6], []),
         'implicit': ([(1,5), (2,6), (5,7)], [6]),},        
        )

    def testImplMatch1(self):
        for data in self.ImplMatch1Data:
            text = data['text']
            v = Validator(ParsedText(text))
            matches = v.match_tags()
            matches[0].sort()
            self.failUnless(matches == data['matches'],
                            msg = 'Improper parse %s in %s'
                            % (`matches`, `text`))
            implicit = v.implicit_match_tags(matches[0], matches[1])
            self.failUnless(implicit == data['implicit'],
                            msg = 'Improper parse %s in %s'
                            % (`implicit`, `text`))

def test_suite():
    return unittest.makeSuite(HTMLValidatorTests)

if __name__=="__main__":
   unittest.TextTestRunner().run(test_suite())