[CMF-checkins] CVS: CMF/CMFCore/tests - test_TextFilters.py:1.1.2.1

Tres Seaver tseaver@zope.com
Wed, 19 Dec 2001 19:18:45 -0500


Update of /cvs-repository/CMF/CMFCore/tests
In directory cvs.zope.org:/tmp/cvs-serv19344/CMFCore/tests

Added Files:
      Tag: tseaver-texthandler-branch
	test_TextFilters.py 
Log Message:


    - Added 'portal_textmanager', a registry for filters which can be
      used both to munge inbound text content (e.g., decapitating
      HTML, extracting RFC822-style STX headers, etc.) and to render
      outbout content (e.g., rendering STX to HTML).  Implemented
      several "stock" handlers.


=== Added File CMF/CMFCore/tests/test_TextFilters.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
""" Unit tests for CMFCore.TextFilters. """

import unittest

SAMPLE_TEXT = """Sample text"""

class TextInfoTests( unittest.TestCase ):

    def _testInitialValues( self, info, text=SAMPLE_TEXT, **kwargs ):

        self.assertEqual( info.getText(), text )
        self.assertEqual( info(), text )
        self.assertEqual( len( info.keys() ), len( kwargs ) )
        for k,v in kwargs.items():
            self.assertEqual( info.get( k ), v )

    def testInterface( self ):

        from Products.CMFCore.interfaces.portal_textmanager import TextInfo
        from Products.CMFCore.TextFilters import TextInfoImpl

        self.failUnless( TextInfo.isImplementedByInstancesOf( TextInfoImpl ) )


    def testInitFromString( self ):

        from Products.CMFCore.TextFilters import _makeTextInfo

        info = _makeTextInfo( SAMPLE_TEXT )
        self._testInitialValues( info )

    def testInitFromDict( self ):

        from Products.CMFCore.TextFilters import _makeTextInfo

        info = _makeTextInfo( { 'text' : SAMPLE_TEXT } )
        self._testInitialValues( info )

        info = _makeTextInfo( { 'text' : SAMPLE_TEXT
                             , 'foo'  : 'bar'
                             } )
        self._testInitialValues( info, foo='bar' )

    def testInitFromInfo( self ):

        from Products.CMFCore.TextFilters import _makeTextInfo

        source = _makeTextInfo( SAMPLE_TEXT )
        info = _makeTextInfo( source )
        self._testInitialValues( info )
        info[ 'foo' ] = 'bar'
        info2 = _makeTextInfo( info )
        self._testInitialValues( info, foo='bar' )

class PassthroughFilterTests( unittest.TestCase ):

    def testInterface( self ):

        from Products.CMFCore.interfaces.portal_textmanager import TextFilter
        from Products.CMFCore.TextFilters import PassthroughFilter

        self.failUnless(
                TextFilter.isImplementedByInstancesOf( PassthroughFilter ) )

    def testSimple( self ):

        from Products.CMFCore.TextFilters import PassthroughFilter
        pt = PassthroughFilter()

        ti = pt()
        self.failIf( ti() )

        ti = pt( '' )
        self.assertEqual( ti(), '' )

        ti = pt( SAMPLE_TEXT )
        self.assertEqual( ti(), SAMPLE_TEXT )

        ti = pt( ti )
        self.assertEqual( ti(), SAMPLE_TEXT )

HTML_TEMPLATE = '''\
<html><head>
 <title>%(title)s</title>
</head>
<body>%(body)s</body>
</html>
'''

HTML_TEMPLATE_WITH_METADATA = '''\
<html><head>
 <title>%(title)s</title>
 <meta name="description" content="%(description)s" />
 <meta name="keywords" content="%(keywords)s" />
</head>
<body>%(body)s</body>
</html>
'''

class HTMLDecapitatorTests( unittest.TestCase ):

    def testInterface( self ):

        from Products.CMFCore.interfaces.portal_textmanager import TextFilter
        from Products.CMFCore.TextFilters import HTMLDecapitator

        self.failUnless(
                TextFilter.isImplementedByInstancesOf( HTMLDecapitator ) )

    def testChopSimple( self ):

        from Products.CMFCore.TextFilters import HTMLDecapitator
        chopper = HTMLDecapitator()

        HTML = HTML_TEMPLATE % { 'title' : 'Sample title'
                               , 'body' : SAMPLE_TEXT
                               }
        
        ti = chopper.filterText( HTML )

        self.assertEqual( ti(), SAMPLE_TEXT )
        self.failUnless( ti[ 'metadata' ] )
        self.assertEqual( ti[ 'metadata' ][ 'Title' ], 'Sample title' )

    def testChopWithMeta( self ):

        from Products.CMFCore.TextFilters import HTMLDecapitator
        chopper = HTMLDecapitator()

        HTML = (
            HTML_TEMPLATE_WITH_METADATA % { 'title' : 'Sample title'
                                          , 'body' : SAMPLE_TEXT
                                          , 'description' : 'Sample description'
                                          , 'keywords' : 'sample, test'
                                          } )
        
        ti = chopper.filterText( HTML )

        self.assertEqual( ti(), SAMPLE_TEXT )
        md = ti[ 'metadata' ]
        self.assertEqual( md.get( 'Title', None ), 'Sample title' )
        self.assertEqual( md.get(  'Description', None ), 'Sample description' )

        subject = md.get( 'Subject', () )
        self.assertEqual( len( subject ), 2 )
        self.failUnless( 'sample' in subject )
        self.failUnless( 'test'   in subject )


STX_TEMPLATE = """\
Title: %(title)s
Description: %(description)s

%(body)s"""

class STXDecapitatorTests( unittest.TestCase ):

    def testInterface( self ):

        from Products.CMFCore.interfaces.portal_textmanager import TextFilter
        from Products.CMFCore.TextFilters import STXDecapitator

        self.failUnless(
                TextFilter.isImplementedByInstancesOf( STXDecapitator ) )

    def testChopNoHeaders( self ):

        from Products.CMFCore.TextFilters import STXDecapitator
        chopper = STXDecapitator()

        ti = chopper.filterText( SAMPLE_TEXT )
        self.assertEqual( ti(), SAMPLE_TEXT )
        self.failIf( ti[ 'metadata' ] )

    def testChopWithHeaders( self ):

        from Products.CMFCore.TextFilters import STXDecapitator
        chopper = STXDecapitator()

        STX = STX_TEMPLATE % { 'title' : 'Sample Title'
                             , 'description' : 'Sample description'
                             , 'body' : SAMPLE_TEXT
                             }

        ti = chopper.filterText( STX )
        self.assertEqual( ti(), SAMPLE_TEXT )
        md = ti[ 'metadata' ]

        self.assertEqual( md[ 'Title' ], 'Sample Title' )
        self.assertEqual( md[ 'Description' ], 'Sample description' )

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest( unittest.makeSuite( TextInfoTests ) )
    suite.addTest( unittest.makeSuite( PassthroughFilterTests ) )
    suite.addTest( unittest.makeSuite( HTMLDecapitatorTests ) )
    suite.addTest( unittest.makeSuite( STXDecapitatorTests ) )
    return suite

def run():
    unittest.TextTestRunner().run(test_suite())

if __name__ == '__main__':
    run()