[Zope-Checkins] CVS: Zope3/lib/python/Zope/Configuration/tests - testDirectivesXML.py:1.1.2.1

Jim Fulton jim@zope.com
Thu, 3 Jan 2002 16:47:23 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Configuration/tests
In directory cvs.zope.org:/tmp/cvs-serv18972

Added Files:
      Tag: Zope-3x-branch
	testDirectivesXML.py 
Log Message:
Added <directives> directive, so we need tests for it.


=== Added File Zope3/lib/python/Zope/Configuration/tests/testDirectivesXML.py ===
# This software is subject to the provisions of the Zope Public License,
# Version 1.1 (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.

import sys, unittest
from cStringIO import StringIO
from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
from Zope.Configuration.tests.Directives \
     import protections, clearDirectives, done

template = """<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:test='http://www.zope.org/NS/Zope3/test'>
   %s
   %s
   </zopeConfigure>"""


ns='http://www.zope.org/NS/Zope3/test'

class Test(unittest.TestCase):

    def tearDown(self):
        from Zope.Configuration.meta import _clear
        _clear()
        clearDirectives()
        
    def testDirective(self):
        xmlconfig(StringIO(
            template % (
            '''<directives  namespace="%s">
                   <directive name="doit"
                    handler="Zope.Configuration.tests.Directives.doit" />
               </directives>''' % ns,
            '<test:doit name="splat" />'
            )))

        self.assertEqual(done, ['splat'])
        
    def testSimpleComplexDirective(self):
        xmlconfig(StringIO(
            template % (
            '''<directives  namespace="%s">
                   <directive name="protectClass"
                    handler="Zope.Configuration.tests.Directives.protectClass">
                       <subdirective name="protect"/>
               </directive>
               </directives>
                          ''' % ns,
            '''<test:protectClass
              name=".Contact" permission="splat" method="update"
              />'''
            )))

        self.assertEquals(protections, [(".Contact", "splat", 'update', None)])
        
    def testComplexDirective(self):
        xmlconfig(StringIO(
            template % (
            '''<directives  namespace="%s">
                   <directive name="protectClass"
                    handler="Zope.Configuration.tests.Directives.protectClass">
                      <subdirective name="protect" />
                   </directive>
               </directives>''' % ns,
            '''<test:protectClass name=".Contact">
                <test:protect permission="edit" method='update' />
                <test:protect permission="view" methods='name, email' />
              </test:protectClass>'''
            )))

        self.assertEquals(protections, [
            (".Contact", "edit", 'update', None),
            (".Contact", "view", None, 'name, email'),
            ])
        
    def testBadNoPrefixComplexDirective(self):

        self.assertRaises(
            ZopeXMLConfigurationError,
            xmlconfig,
            StringIO(
            template % (
            '''<directives  namespace="%s">
                   <directive name="protectClass"
                    handler="Zope.Configuration.tests.Directives.protectClass">
                  <subdirective name="protect" />
               </directive>
               </directives>''' % ns,

            '''<test:protectClass name=".Contact">
              <test:protect permission="edit" method='update' />
              <protect permission="view" methods='name, email' />
              </test:protectClass>'''
            )))
        
    def testBadPrefixComplexDirective(self):

        try:
            xmlconfig(
                StringIO(
                template % (
            '''<directives  namespace="%s">
                   <directive name="protectClass"
                    handler="Zope.Configuration.tests.Directives.protectClass">
                  <subdirective name="protect" />
               </directive>
               </directives>''' % ns,

                '''<test:protectClass name=".Contact">
                <test2:protect permission="edit" method='update' />
                </test:protectClass>'''
                )))
        except ZopeXMLConfigurationError, v:
            self.assertEqual(
                str(v),
                "InvalidDirective: (None, u'test2:protect') at line 11 "
                "column 16 of <string>")
        else:
            self.fail('Should have raised ZopeXMLConfigurationError')
        

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

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

def debug():
    test_suite().debug()

def pdb():
    import pdb
    pdb.run('debug()')

if __name__=='__main__':
    if len(sys.argv) < 2:
        run()
    else:
        globals()[sys.argv[1]]()