[Zope3-checkins] CVS: Zope3/src/zope/configuration/tests - __init__.py:1.1.2.1 basetestdirectivesxml.py:1.1.2.1 directives.py:1.1.2.1 hooktestdummymodule.py:1.1.2.1 test_directivesxml.py:1.1.2.1 test_hookregistry.py:1.1.2.1 test_meta.py:1.1.2.1 test_metameta.py:1.1.2.1 test_metametafordocgen.py:1.1.2.1 test_multiplexml.py:1.1.2.1 test_names.py:1.1.2.1 test_xml.py:1.1.2.1
Jim Fulton
jim@zope.com
Mon, 23 Dec 2002 14:32:45 -0500
Update of /cvs-repository/Zope3/src/zope/configuration/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/configuration/tests
Added Files:
Tag: NameGeddon-branch
__init__.py basetestdirectivesxml.py directives.py
hooktestdummymodule.py test_directivesxml.py
test_hookregistry.py test_meta.py test_metameta.py
test_metametafordocgen.py test_multiplexml.py test_names.py
test_xml.py
Log Message:
Initial renaming before debugging
=== Added File Zope3/src/zope/configuration/tests/__init__.py ===
#
# This file is necessary to make this directory a package.
=== Added File Zope3/src/zope/configuration/tests/basetestdirectivesxml.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Tests of the XML parsing machinery
This mixin passes a series of sets of configuration directives
to xmlconfig to make sure the right things happen. The
directives first define one or or more new directives, implemented
by code in the Directives.py module, and then use those
directives. The tests check to make sure the expected actions
were taken, or the expected errors raised.
These tests test only the capabilities of the meta-directives
implemented by the bootstrap versions.
$Id: basetestdirectivesxml.py,v 1.1.2.1 2002/12/23 19:32:43 jim Exp $
"""
from cStringIO import StringIO
from zope.configuration.meta import InvalidDirective
from zope.configuration.xmlconfig import xmlconfig, testxmlconfig
from zope.configuration.tests.directives import protections, done
ns = 'http://www.zope.org/NS/Zope3/test'
def makeconfig(metadirectives,directives):
return StringIO(
'''<zopeConfigure
xmlns="http://namespaces.zope.org/zope"
xmlns:test="%(ns)s">
<directives namespace="%(ns)s">
%(metadirectives)s
</directives>
%(directives)s
</zopeConfigure>''' % {
'metadirectives': metadirectives,
'directives': directives,
'ns': ns})
class directiveTests:
def testDirective(self):
xmlconfig(makeconfig(
'''<directive
name="doit"
handler="Zope.Configuration.tests.Directives.doit" />''',
'''<test:doit name="splat" />'''
))
self.assertEqual(done, ['splat'])
def testSimpleComplexDirective(self):
xmlconfig(makeconfig(
'''<directive
name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>''',
'''<test:protectClass
name=".Contact" permission="splat" names="update" />'''
))
self.assertEquals(protections, [(".Contact", "splat", 'update')])
def testComplexDirective(self):
xmlconfig(makeconfig(
'''<directive
name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" />
</directive>''',
'''<test:protectClass name=".Contact">
<test:protect permission="edit" names='update' />
<test:protect permission="view" names='name email' />
</test:protectClass>'''
))
self.assertEquals(protections, [
(".Contact", "edit", 'update'),
(".Contact", "view", 'name email'),
])
def testSubSubdirective(self):
xmlconfig(makeconfig(
'''<directive name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="subsub">
<subdirective name="subsub">
<subdirective name="subsub">
<subdirective name="subsub">
<subdirective name="protect"/>
</subdirective>
</subdirective>
</subdirective>
</subdirective>
</directive>''',
'''<test:protectClass
name=".Contact" permission="splat" names="update"
>
<test:subsub>
<test:subsub>
<test:subsub>
<test:subsub>
<test:protect permission="beep" names="update" />
</test:subsub>
</test:subsub>
</test:subsub>
</test:subsub>
</test:protectClass>'''
))
self.assertEquals(protections, [(".Contact", "beep", 'update')])
def testHandlerMethod(self):
xmlconfig(makeconfig(
'''<directive name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective
name="fish"
handler_method="protect" />
</directive>''',
'''<test:protectClass name=".Contact">
<test:fish permission="edit" names='update' />
<test:fish permission="view" names='name email' />
</test:protectClass>'''
))
self.assertEquals(protections, [
(".Contact", "edit", 'update'),
(".Contact", "view", 'name email'),
])
def testBadNoPrefixComplexDirective(self):
self.assertRaises(
InvalidDirective,
testxmlconfig,
makeconfig(
'''<directive
name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>''',
'''<test:protectClass name=".Contact">
<test:protect permission="edit" names='update' />
<protect permission="view" names='name email' />
</test:protectClass>
'''))
def testBadPrefixComplexDirective(self):
try: testxmlconfig(makeconfig(
'''<directive
name="protectClass"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>''',
'''<test:protectClass name=".Contact">
<test2:protect permission="edit" names='update' />
</test:protectClass>''')),
except InvalidDirective, v:
self.assertEqual(str(v), "(None, u'test2:protect')")
else:
self.fail('Should have raised InvalidDirective')
=== Added File Zope3/src/zope/configuration/tests/directives.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""
Test class for use by test modules
$Id: directives.py,v 1.1.2.1 2002/12/23 19:32:43 jim Exp $
"""
from zope.interfaces.configuration import INonEmptyDirective
from zope.interfaces.configuration import ISubdirectiveHandler
protections=[]
count = 0
def _increment():
global count
count += 1
def increment(_context):
return [(None, _increment, (), )]
class protectClass:
__class_implements__ = INonEmptyDirective
__implements__ = ISubdirectiveHandler
def __init__(self, _context, name, permission=None, names=None):
self._name=name
self._permission=permission
self._names=names
self._children=[]
self.__context = _context
def __call__(self):
if not self._children:
p = self._name, self._permission, self._names
d = self._name, self._names
return [(d, protections.append, (p,))]
else:
return ()
def protect(self, _context, permission=None, names=None):
if permission is None: permission=self._permission
if permission is None: raise 'no perm'
p=self._name, permission, names
d=self._name, names
self._children.append(p)
return [(d, protections.append, (p,))]
def subsub(self, _context):
#Dummy subdirective-with-subdirectives. Define this and you
#can define 'protect' subdirectives within it. This lets
#us excercise the subdirectives-of-subdirectives code.
#If you put a protect inside a subsub, that'll set children,
#so when the parser calls us, __call__ will return ().
return self
subsub.__implements__ = INonEmptyDirective
done = []
def doit(_context, name):
return [('d', done.append, (name,))]
def clearDirectives():
del protections[:]
del done[:]
# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
from zope.testing.cleanup import addCleanUp
addCleanUp(clearDirectives)
del addCleanUp
=== Added File Zope3/src/zope/configuration/tests/hooktestdummymodule.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Hook test dummy module
$Id: hooktestdummymodule.py,v 1.1.2.1 2002/12/23 19:32:43 jim Exp $
"""
def dummyHookable():
return dummyHookable_hook()
def dummyHookable_hook():
return "original implementation"
def associatedDummy():
return dummyHookable()
=== Added File Zope3/src/zope/configuration/tests/test_directivesxml.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import sys, unittest
from zope.configuration.tests.basetestdirectivesxml import directiveTests
from zope.testing.cleanup import CleanUp
class Test(CleanUp, unittest.TestCase, directiveTests): pass
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]]()
=== Added File Zope3/src/zope/configuration/tests/test_hookregistry.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""
$Id: test_hookregistry.py,v 1.1.2.1 2002/12/23 19:32:43 jim Exp $
"""
import unittest, sys
def dummyHook():
return "hooked implementation"
class HookRegistryTest(unittest.TestCase):
# def setUp(self):
#
# from Zope.Configuration.tests import Products_
# self.old=sys.modules.get('ZopeProducts', None)
# sys.modules['ZopeProducts']=Products_
#
# def tearDown(self):
# old=self.old
# if old is None: del sys.modules['ZopeProducts']
# else: sys.modules['ZopeProducts']=self.old
def testAddHookable(self):
from zope.configuration.hookregistry import HookRegistry
hookableAddr='Zope.Configuration.tests.hookTestDummyModule.dummyHookable'
hookRegistry=HookRegistry()
hookRegistry.addHookable(hookableAddr)
hookables=hookRegistry.getHookables()
self.assertEquals(len(hookables), 1)
self.assertEquals(hookables[0][0], hookableAddr)
self.assertEquals(hookables[0][1],0)
def testAddDuplicateHookable(self):
from zope.configuration.hookregistry import HookRegistry
from zope.exceptions import DuplicationError
hookableAddr='Zope.Configuration.tests.hookTestDummyModule.dummyHookable'
hookRegistry=HookRegistry()
hookRegistry.addHookable(hookableAddr)
self.assertRaises(DuplicationError, hookRegistry.addHookable,
hookableAddr)
def testAddInvalidHookable(self):
from zope.configuration.hookregistry import HookRegistry, \
BadHookableError
hookRegistry=HookRegistry()
self.assertRaises(BadHookableError, hookRegistry.addHookable,
'foo.bar.this.should.not.resolve.anywhere')
self.assertRaises(BadHookableError, hookRegistry.addHookable,
'Zope')
self.assertRaises(BadHookableError, hookRegistry.addHookable,
'Zope.Configuration.HookRegistry.HookRegistry.addHookable')
def testAddHook(self):
from zope.configuration.hookregistry import \
HookRegistry, BadHookError, DuplicateHookError, \
MissingHookableError
from zope.configuration.tests.hooktestdummymodule \
import associatedDummy
dummyHook = 'Zope.Configuration.tests.testHookRegistry.dummyHook'
suite = 'Zope.Configuration.tests.testHookRegistry.test_suite'
hookableParent = 'Zope.Configuration.tests.hookTestDummyModule'
hookableLast = 'dummyHookable'
hookableAddr = '%s.%s' % (hookableParent, hookableLast)
old = __import__(hookableParent, {}, {}, ('__dict__',)) # for cleanup
old = getattr(old, hookableLast)
self.assertEquals(old(), "original implementation")
hookRegistry = HookRegistry()
hookRegistry.addHookable(hookableAddr)
self.assertRaises(BadHookError, hookRegistry.addHook, hookableAddr,
'foo.bar.this.should.not.resolve.anywhere')
hookRegistry.addHook(hookableAddr, dummyHook)
new = __import__(hookableParent, {}, {}, ('__dict__',))
new = getattr(new, hookableLast)
self.assertEquals(new(), "hooked implementation")
self.assertEquals(associatedDummy(), "hooked implementation")
from zope.configuration.tests.hooktestdummymodule \
import associatedDummy as associatedDummyAgain
self.assertEquals(associatedDummyAgain(), "hooked implementation")
self.assertRaises(DuplicateHookError, hookRegistry.addHook,
hookableAddr, suite)
self.assertRaises(MissingHookableError, hookRegistry.addHook,
suite, dummyHook)
setattr(__import__(hookableParent, {}, {}, ('__dict__',)),
hookableLast, old) # cleanup
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(HookRegistryTest)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/configuration/tests/test_meta.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import unittest
from zope.configuration.tests.directives \
import protectClass, protections, doit, done
from zope.testing.cleanup import CleanUp # Base class w registry cleanup
from zope.configuration import name
ns='http://www.zope.org/NS/Zope3/test'
class MetaTest(CleanUp, unittest.TestCase):
def testImport(self):
import Zope.Configuration.meta
def testDirective(self):
from zope.configuration.meta \
import register, registersub, begin, end, InvalidDirective
self.assertRaises(InvalidDirective, begin, None, name, (ns, 'doit'))
register((ns, 'doit'), doit)
subs = begin(None, (ns, 'doit'), name, name='splat')
(des, callable, args, kw), = end(subs)
self.assertEqual(des, 'd')
callable(*args)
self.failUnless(done==['splat'])
def testSimpleComplexDirective(self):
from zope.configuration.meta \
import register, registersub, begin, end, InvalidDirective
subs = register((ns, 'protectClass'), protectClass)
registersub(subs, (ns, 'protect'))
subs=begin(None, (ns, 'protectClass'), name,
name=".Contact", permission="splat", names='update')
(des, callable, args, kw), = end(subs)
self.assertEqual(des, ('.Contact', 'update'))
callable(*args)
self.assertEquals(protections, [(".Contact", "splat", 'update')])
def testComplexDirective(self):
from zope.configuration.meta \
import register, registersub, begin, sub, end, InvalidDirective
subs = register((ns, 'protectClass'), protectClass)
registersub(subs, (ns, 'protect'))
subs = begin(None, (ns, 'protectClass'), name, name=".Contact")
actions = end(sub(subs, (ns, 'protect'), name,
permission='edit', names='update'))
(des, callable, args, kw), = actions
self.assertEqual(des, ('.Contact', 'update'))
callable(*args)
actions = end(sub(subs, (ns, 'protect'), name,
permission='view', names='name email'))
(des, callable, args, kw), = actions
self.assertEqual(des, ('.Contact', 'name email'))
callable(*args)
self.assertEqual(tuple(end(subs)), ())
self.assertEquals(protections, [
(".Contact", "edit", 'update'),
(".Contact", "view", 'name email'),
])
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(MetaTest)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/configuration/tests/test_metameta.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import sys, unittest
from zope.testing.cleanup import CleanUp
from zope.configuration.xmlconfig import xmlconfig, testxmlconfig, XMLConfig
from zope.configuration.tests.directives import done
from zope.configuration.tests.basetestdirectivesxml import directiveTests
from zope.configuration.tests.basetestdirectivesxml import makeconfig
import zope.configuration
class Test(CleanUp, unittest.TestCase, directiveTests):
def setUp(self):
XMLConfig('metameta.zcml', Zope.Configuration)()
def testDescription(self):
xmlconfig(makeconfig(
'''<directive
name="doit"
description="Something to Do"
handler="Zope.Configuration.tests.Directives.doit" />''',
'''<test:doit name="splat" />'''
))
self.assertEqual(done, ['splat'])
def testAttribute(self):
xmlconfig(makeconfig(
'''<directive
name="doit"
handler="Zope.Configuration.tests.Directives.doit"
description="Something to Do"
>
<attribute
name="name"
required="yes"
description="Just Do It" />
</directive>''',
'''<test:doit name="splat" />'''
))
self.assertEqual(done, ['splat'])
def testBadRequired(self):
self.assertRaises(
ValueError,
testxmlconfig,
makeconfig(
'''<directive
name="doit"
handler="Zope.Configuration.tests.Directives.doit"
>
<attribute
name="name"
required="badvalue"
description="Just Do It" />
</directive>''',
'''<test:doit name="splat" />'''
))
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]]()
=== Added File Zope3/src/zope/configuration/tests/test_metametafordocgen.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import sys, unittest
from zope.testing.cleanup import CleanUp
from zope.configuration.xmlconfig import xmlconfig, testxmlconfig, XMLConfig
from zope.configuration.meta import _directives
from zope.configuration.metametaconfigurefordocgen import _metadataKey
from zope.configuration.tests.directives import done
from zope.configuration.tests.basetestdirectivesxml import directiveTests
from zope.configuration.tests.basetestdirectivesxml import makeconfig, ns
import zope.configuration
class Test(CleanUp, unittest.TestCase, directiveTests):
def setUp(self):
XMLConfig('metameta.zcml', Zope.Configuration)()
XMLConfig('metametaForDocgen.zcml', Zope.Configuration)()
def testDescription(self):
xmlconfig(makeconfig(
'''<directive
name="doit"
description="Just Do It"
handler="Zope.Configuration.tests.Directives.doit" />''',
'''<test:doit name="splat" />'''
))
self.assertEqual(done, ['splat'])
md = _directives[(ns, 'doit')][1][_metadataKey]
self.assertEqual(md['description'], "Just Do It")
def testAttribute(self):
xmlconfig(makeconfig(
'''<directive
name="doit"
handler="Zope.Configuration.tests.Directives.doit"
>
<attribute
name="name"
required="Yes"
description="Just Do It" />
<attribute
name="opt1"
required="no"
description="ho hum" />
<attribute
name="opt2"
description="ho hummer" />
</directive>''',
'''<test:doit name="splat" />'''
))
self.assertEqual(done, ['splat'])
md = _directives[(ns, 'doit')][1][_metadataKey]['attributes']['name']
self.assertEqual(md['description'],'Just Do It')
self.assertEqual(md['required'],'yes')
md = _directives[(ns, 'doit')][1][_metadataKey]['attributes']['opt1']
self.assertEqual(md['description'],'ho hum')
self.assertEqual(md['required'],'no')
md = _directives[(ns, 'doit')][1][_metadataKey]['attributes']['opt2']
self.assertEqual(md['description'],'ho hummer')
self.assertEqual(md['required'],'')
def testBadRequired(self):
self.assertRaises(
ValueError,
testxmlconfig,
makeconfig(
'''<directive
name="doit"
handler="Zope.Configuration.tests.Directives.doit"
>
<attribute
name="name"
required="badvalue"
description="Just Do It" />
</directive>''',
'''<test:doit name="splat" />'''
))
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]]()
=== Added File Zope3/src/zope/configuration/tests/test_multiplexml.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import unittest, sys, os
from tempfile import mktemp
import zope.configuration.tests.directives
from zope.configuration.tests.directives import protections, done
from zope.configuration.xmlconfig import XMLConfig
from zope.testing.cleanup import CleanUp # Base class w registry cleanup
template = """<zopeConfigure
xmlns='http://namespaces.zope.org/zope'
xmlns:test='http://www.zope.org/NS/Zope3/test'>
<directives namespace="http://www.zope.org/NS/Zope3/test">
%s
</directives>
%s
</zopeConfigure>"""
ns='http://www.zope.org/NS/Zope3/test'
class Test(CleanUp, unittest.TestCase):
def testNormal(self):
f2=tfile(template % ('',
'''
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
<test:protectClass
name=".Contact" permission="splat" names="update2"
/>
'''), 'f2')
f1=tfile(template % (
'''<directive name="protectClass" namespace="%s"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>
<directive name="increment" namespace="%s"
handler="Zope.Configuration.tests.Directives.increment">
</directive>
''' % (ns, ns, ns),
'''
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
<test:increment />
<test:increment />
<test:increment />
<include file="%s"/>
''' % f2), 'f1')
XMLConfig(str(f1))()
self.assertEquals(protections, [
(".Contact", "splat", 'update'),
(".Contact", "splat", 'update2'),
])
self.assertEquals(Zope.Configuration.tests.Directives.count, 3)
def testConflicting(self):
f2=tfile(template % ('',
'''
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
<test:protectClass
name=".Contact" permission="splat" names="update2"
/>
'''), 'f2')
f3=tfile(template % ('',
'''
<test:protectClass
name=".Contact" permission="splat" names="update2"
/>
'''), 'f3')
f1=tfile(template % (
'''<directive name="protectClass" namespace="%s"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>''' % (ns, ns),
'''
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
<include file="%s"/>
<include file="%s"/>
''' % (f2, f3)), 'f1')
x=XMLConfig(str(f1))
from zope.configuration.xmlconfig import ZopeConfigurationConflictError
self.assertRaises(ZopeConfigurationConflictError, x)
self.assertEquals(protections, [])
def testConflicting_in_same_location(self):
f1=tfile(template % (
'''<directive name="protectClass" namespace="%s"
handler="Zope.Configuration.tests.Directives.protectClass">
<subdirective name="protect" namespace="%s" />
</directive>''' % (ns, ns),
'''
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
<test:protectClass
name=".Contact" permission="splat" names="update"
/>
'''), 'f1')
x=XMLConfig(str(f1))
from zope.configuration.xmlconfig import ZopeConfigurationConflictError
self.assertRaises(ZopeConfigurationConflictError, x)
self.assertEquals(protections, [])
class tfile:
def __init__(self, string, suffix):
self.name = mktemp(suffix)
file = open(self.name, 'w')
file.write(string)
file.close()
def __str__(self): return self.name
def __del__(self): os.remove(self.name)
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/configuration/tests/test_names.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""
$Id: test_names.py,v 1.1.2.1 2002/12/23 19:32:43 jim Exp $
"""
import unittest, sys
class NameTest(unittest.TestCase):
def setUp(self):
from Zope.Configuration.tests import Products_
self.old=sys.modules.get('ZopeProducts', None)
sys.modules['ZopeProducts']=Products_
def tearDown(self):
old=self.old
if old is None: del sys.modules['ZopeProducts']
else: sys.modules['ZopeProducts']=self.old
def testProductPath(self):
from zope.configuration.name import resolve
c=resolve('.Contact.')
self.assertEquals(c.n, 2)
c=resolve('ZopeProducts.Contact.Contact.Contact')
self.assertEquals(c.n, 2)
def testPackagePath(self):
from zope.configuration.name import resolve
c=resolve('Zope.Configuration.tests.Contact')
import Zope.Configuration.tests.Contact
self.assertEquals(c, Zope.Configuration.tests.Contact)
c=resolve('Zope.Configuration.tests.Contact.')
self.assertEquals(c.n, 1)
c=resolve('Zope.Configuration.tests.Contact.Contact.Contact')
self.assertEquals(c.n, 1)
def testNoDots(self):
from zope.configuration.name import resolve
import Zope
c=resolve('Zope')
self.assertEquals(id(c), id(Zope))
def testPackage(self):
from zope.configuration.name import resolve
c=resolve('Zope.Configuration.tests')
import Zope.Configuration.tests
self.assertEquals(id(c), id(Zope.Configuration.tests))
nameSet={
('Zope.Configuration.tests','Noplace'):'Zope.Configuration.tests',
('Zope.Configuration.tests.tests','Noplace'):'Zope.Configuration.tests+',
('Zope.Configuration.tests.tests.tests','Noplace'):'Zope.Configuration.tests+',
('Zope.Configuration.tests.tests.tests.','Noplace'):'Zope.Configuration.tests+',
('Zope.Configuration.tests+','Noplace'):'Zope.Configuration.tests+',
('Zope.Configuration.tests.tests.tests+','Noplace'):'Zope.Configuration.tests+',
('Zope.Configuration.tests.','Noplace'):'Zope.Configuration.tests+',
('.tests','Zope.Configuration'):'Zope.Configuration.tests',
('.tests.tests','Zope.Configuration'):'Zope.Configuration.tests+',
('.tests.tests.tests','Zope.Configuration'):'Zope.Configuration.tests+',
('.tests.tests.tests.','Zope.Configuration'):'Zope.Configuration.tests+',
('.tests+','Zope.Configuration'):'Zope.Configuration.tests+',
('.tests.tests.tests+','Zope.Configuration'):'Zope.Configuration.tests+',
('.tests.','Zope.Configuration'):'Zope.Configuration.tests+'
}
def testNormalizedName(self):
from zope.configuration.name import getNormalizedName
for args in self.nameSet:
self.assertEquals(self.nameSet[args], getNormalizedName(*args))
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(NameTest)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/src/zope/configuration/tests/test_xml.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
import sys, unittest
from cStringIO import StringIO
# Caution: tempfile.NamedTemporaryFile cannot be used on Windows, because
# the tests here want to open the file more than once. You can't do that
# with an O_TEMPORARY file on Win2K (or higher).
import tempfile
class TempFile:
from os import remove
def __init__(self):
self.file = open(tempfile.mktemp(), 'w')
self.closed = 0
def write(self,buffer):
self.file.write(buffer)
def _name(self):
return self.file.name
name = property(_name)
def flush(self):
self.file.flush()
def close(self):
if not self.closed:
self.file.close()
self.remove(self.file.name)
self.closed = 1
def __del__(self):
self.close()
from zope.configuration.xmlconfig import xmlconfig
from zope.configuration.xmlconfig import testxmlconfig
from zope.configuration.meta import InvalidDirective, BrokenDirective
from zope.testing.cleanup import CleanUp
class Test(CleanUp, unittest.TestCase):
def testInclude(self):
file = TempFile()
name = file.name
file.write(
"""<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<include
package="Zope.Configuration.tests.Contact"
file="contact.zcml" />
</zopeConfigure>""")
file.flush()
from zope.configuration.xmlconfig import XMLConfig
x = XMLConfig(name)
x()
file.close()
def testIncludeAll(self):
file = TempFile()
name = file.name
file.write(
"""<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<include
package="Zope.Configuration.tests.*"
file="contact.zcml" />
</zopeConfigure>""")
file.flush()
from zope.configuration.xmlconfig import XMLConfig
x = XMLConfig(name)
x()
file.close()
def testIncludeNoPackageAndIncluderNoPackage(self):
from os.path import split
file = TempFile()
full_name = file.name
file1 = TempFile()
full_name1 = file1.name
name1 = split(full_name1)[-1]
file.write(
"""<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<include file="%s" />
</zopeConfigure>""" % name1)
file.flush()
file1.write(
"""<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<include
package="Zope.Configuration.tests.Contact"
file="contact.zcml" />
</zopeConfigure>""")
file1.flush()
from zope.configuration.xmlconfig import XMLConfig
x = XMLConfig(full_name)
x()
file.close()
file1.close()
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]]()