[Zope3-checkins] CVS: Zope3/src/zope/app/services/tests -
test_globalmodule.py:1.1.2.1 test_module.py:1.12.4.1
test_modulelookup.py:1.5.4.1
Fred L. Drake, Jr.
fred at zope.com
Thu Jan 15 15:50:53 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/services/tests
In directory cvs.zope.org:/tmp/cvs-serv1563/src/zope/app/services/tests
Modified Files:
Tag: zope3-fdrake-globalized-modules-branch
test_module.py test_modulelookup.py
Added Files:
Tag: zope3-fdrake-globalized-modules-branch
test_globalmodule.py
Log Message:
Checkpointing the current state of the module globalization work on a branch
so it does not get lost. See http://dev.zope.org/Zope3/ModulesAreGlobal.
=== Added File Zope3/src/zope/app/services/tests/test_globalmodule.py ===
##############################################################################
#
# Copyright (c) 2003 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 stub for zope.app.services.globalmodule.
$Id: test_globalmodule.py,v 1.1.2.1 2004/01/15 20:50:22 fdrake Exp $
"""
import unittest
from zope.interface import implements
from zope.testing.doctestunit import DocTestSuite
from zope.app import zapi
from zope.app.services import globalmodule
from zope.app.services import module
from zope.app.services.servicenames import Modules
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.interfaces.services.module import IModuleRegistration
from zope.app.interfaces.services.registration import \
INamedComponentRegistration, ActiveStatus, RegisteredStatus
class NamedRegistration:
implements(INamedComponentRegistration)
def __init__(self, name):
self._component = object()
self.name = name
self.active = None
def getComponent(self):
return self._component
def activated(self):
self.active = True
def deactivated(self):
# should never get called if activated() hasn't been called
# first
assert self.active is True
self.active = False
class ModuleRegistration(NamedRegistration):
implements(IModuleRegistration)
def __init__(self, name, source=None):
NamedRegistration.__init__(self, name)
if source is None:
source = id(self)
self._component = DummyModule(name, source)
self.componentPath = "/someplace/" + name
class DummyModule:
def __init__(self, name, source=None):
self.source = source
self.__name__ = name
class DummyService:
def __init__(self):
self.actions = []
def activateRegistration(self, stack, registration):
self.actions.append(("activate", registration))
def deactivateRegistration(self, stack, registration):
self.actions.append(("deactivate", registration))
class BaseTest(unittest.TestCase):
NAME = "foo.bar"
def setUp(self):
self.container = DummyService()
self.stack = globalmodule.GlobalRegistrationStack(self.container,
self.NAME)
self.assert_(self.container is self.stack.__parent__)
self.assert_(not self.stack)
self.r1 = NamedRegistration(self.NAME)
self.r2 = NamedRegistration(self.NAME)
def verifyActions(self, expected):
self.assertEqual(self.container.actions, expected)
class GlobalRegistrationStackTestCase(BaseTest):
def test_activate_with_active(self):
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
self.assert_(r1.active is None)
self.assert_(r2.active is None)
stack.activate(r1)
# test
stack.activate(r2)
self.verifyActions([("activate", r1),
("deactivate", r1),
("activate", r2)])
self.assert_(not r1.active)
self.assert_(r2.active)
self.assert_(stack)
self.assert_(stack.active() is r2)
self.assert_(stack.registered(r1))
self.assert_(stack.registered(r2))
def test_activate_with_inactive(self):
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
self.assert_(r1.active is None)
self.assert_(r2.active is None)
# test
stack.activate(r2)
self.verifyActions([("activate", r2)])
self.assert_(r1.active is None)
self.assert_(r2.active)
self.assert_(stack)
self.assert_(stack.active() is r2)
self.assert_(stack.registered(r1))
self.assert_(stack.registered(r2))
def test_deactivate_active(self):
# setup
r, stack = self.r1, self.stack
stack.register(r)
stack.activate(r)
# test
stack.deactivate(r)
self.verifyActions([("activate", r),
("deactivate", r)])
self.assert_(not r.active)
self.assert_(stack.active() is None)
self.assert_(stack)
self.assert_(stack.registered(r))
def test_deactivate_active_with_fallback(self):
# setup; r1 will be the fallback
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
stack.activate(r1)
stack.activate(r2)
# test
stack.deactivate(r2)
self.verifyActions([("activate", r1),
("deactivate", r1),
("activate", r2),
("deactivate", r2),
("activate", r1)])
self.assert_(not r2.active)
self.assert_(r1.active)
self.assert_(stack.active() is r1)
self.assert_(stack)
self.assert_(stack.registered(r1))
self.assert_(stack.registered(r2))
def test_deactivate_inactive(self):
# setup
r, stack = self.r1, self.stack
stack.register(r)
# test
stack.deactivate(r)
self.verifyActions([])
self.assert_(not r.active)
self.assert_(stack.active() is None)
self.assert_(stack)
self.assert_(stack.registered(r))
def test_deactivate_inactive_with_active(self):
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
stack.activate(r2)
# test
stack.deactivate(r1)
self.verifyActions([("activate", r2)])
self.assert_(not r1.active)
self.assert_(stack.active() is r2)
self.assert_(stack)
self.assert_(stack.registered(r1))
def test_register(self):
r1, r2, stack = self.r1, self.r2, self.stack
self.assert_(not stack.registered(r1))
stack.register(r1)
self.assert_(stack)
self.assert_(stack.registered(r1))
self.assert_(not stack.registered(r2))
self.assert_(r1.active is None)
self.assert_(r2.active is None)
stack.register(r2)
self.assert_(stack)
self.assert_(stack.registered(r1))
self.assert_(stack.registered(r2))
self.assert_(stack.active() is None)
self.assert_(r1.active is None)
self.assert_(r2.active is None)
self.verifyActions([])
def test_unregister_active_inactive(self):
# test unregistering without a fallback registration
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.activate(r1)
stack.register(r2)
# test
stack.unregister(r1)
self.verifyActions([("activate", r1),
("deactivate", r1)])
self.assert_(not r1.active)
self.assert_(r2.active is None)
self.assert_(stack)
self.assert_(not stack.registered(r1))
self.assert_(stack.registered(r2))
self.assert_(stack.active() is None)
def test_unregister_active_active(self):
# test that a fallback registration is properly selected
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
stack.activate(r2)
stack.activate(r1)
# test
stack.unregister(r1)
self.verifyActions([("activate", r2),
("deactivate", r2),
("activate", r1),
("deactivate", r1),
("activate", r2)])
self.assert_(not r1.active)
self.assert_(r2.active)
self.assert_(stack)
self.assert_(not stack.registered(r1))
self.assert_(stack.registered(r2))
self.assert_(stack.active() is r2)
# unregister the last registration
stack.unregister(r2)
self.verifyActions([("activate", r2),
("deactivate", r2),
("activate", r1),
("deactivate", r1),
("activate", r2),
("deactivate", r2)])
self.assert_(not r2.active)
self.assert_(not stack)
self.assert_(not stack.registered(r2))
self.assert_(stack.active() is None)
def test_unregister_inactive_active(self):
# setup
r1, r2, stack = self.r1, self.r2, self.stack
stack.register(r1)
stack.register(r2)
stack.activate(r1)
# test
stack.unregister(r1)
self.verifyActions([("activate", r1),
("deactivate", r1)])
self.assert_(not r1.active)
self.assert_(stack.active() is None)
self.assert_(not stack.registered(r1))
self.assert_(stack.registered(r2))
self.assert_(stack)
class GlobalRegistrationStackModuleTestCase(BaseTest):
def setUp(self):
BaseTest.setUp(self)
self.mr1 = ModuleRegistration(self.NAME, "x = 1")
self.mr2 = ModuleRegistration(self.NAME, "x = 2")
self.m1 = self.mr1.getComponent()
self.m2 = self.mr2.getComponent()
def verifyEmptyModule(self, module):
d = module.__dict__.copy()
if "__builtins__" in d:
del d["__builtins__"]
items = d.items()
items.sort()
self.assertEqual(items, [("__doc__", None),
("__name__", self.stack.__name__)])
def test_module_update_on_activate(self):
self.stack.register(self.mr1)
self.stack.activate(self.mr1)
self.assertEqual(self.stack.module.x, 1)
self.verifyActions([("activate", self.mr1)])
def test_module_non_update_on_activate(self):
self.stack.register(self.mr1)
self.stack.register(self.r1)
self.stack.activate(self.mr1)
self.verifyActions([("activate", self.mr1)])
self.assertEqual(self.stack.module.x, 1)
self.assertEqual(self.stack.module.__file__, "/someplace/" + self.NAME)
self.stack.activate(self.r1)
self.verifyActions([("activate", self.mr1),
("deactivate", self.mr1),
("activate", self.r1)])
self.verifyEmptyModule(self.stack.module)
def test_module_non_update_on_deactivate(self):
self.stack.register(self.mr1)
self.stack.register(self.r1)
self.stack.activate(self.r1)
self.stack.activate(self.mr1)
self.assertEqual(self.stack.module.x, 1)
self.stack.deactivate(self.mr1)
self.verifyEmptyModule(self.stack.module)
def test_module_update_on_deactivate(self):
self.stack.register(self.mr1)
self.stack.register(self.mr2)
self.stack.register(self.r1)
self.stack.activate(self.mr1)
self.stack.activate(self.mr2)
self.assertEqual(self.stack.module.x, 2)
self.stack.activate(self.r1)
self.verifyActions([("activate", self.mr1),
("deactivate", self.mr1),
("activate", self.mr2),
("deactivate", self.mr2),
("activate", self.r1),
])
self.verifyEmptyModule(self.stack.module)
self.stack.unregister(self.mr2)
self.stack.deactivate(self.r1)
self.assertEqual(self.stack.module.x, 1)
class GlobalModuleServiceTestCase(unittest.TestCase):
def setUp(self):
self.service = globalmodule.ModuleService()
def test_queryRegistrations_empty(self):
self.assert_(self.service.queryRegistrations("foo.bar") is None)
def test_createRegistrations(self):
stack = self.service.createRegistrations("foo.bar.baz")
foo = self.service.queryRegistrations("foo")
self.assertEqual(foo.__name__, "foo")
self.assert_(not foo)
bar = self.service.queryRegistrations("foo.bar")
self.assertEqual(bar.__name__, "foo.bar")
self.assert_(not bar)
baz = self.service.queryRegistrations("foo.bar.baz")
self.assert_(baz is stack)
self.assertEqual(baz.__name__, "foo.bar.baz")
self.assert_(not baz)
def test_findModule_empty(self):
# modules should not be there when there aren't any modules ;-)
self.assert_(self.service.findModule("notthere") is None)
self.assert_(self.service.findModule("not.there") is None)
def test_findModule_non_empty(self):
stack = self.service.createRegistrations("foo.bar")
mr1 = ModuleRegistration("foo.bar", "x = 42")
stack.register(mr1)
# make sure we can't get a module when it's inactive:
self.assert_(self.service.findModule("foo.bar") is None)
self.assert_(self.service.findModule("foo") is None)
# make sure we *can* get it when active:
stack.activate(mr1)
foo = self.service.findModule("foo")
bar = self.service.findModule("foo.bar")
self.assert_(foo.bar is bar)
self.assertEqual(bar.__name__, "foo.bar")
self.assertEqual(foo.__name__, "foo")
self.failIf(hasattr(foo, "__file__"))
self.assertEqual(bar.__file__, "/someplace/foo.bar")
# it has to disappear again when deactivated:
stack.deactivate(mr1)
self.assert_(self.service.findModule("foo.bar") is None)
self.assert_(self.service.findModule("foo") is None)
def test_non_module_components(self):
# make sure we can put both modules and non-modules in a
# virtual package
s1 = self.service.createRegistrations("foo.bar")
s2 = self.service.createRegistrations("foo.baz")
r1 = NamedRegistration("foo.bar")
mr1 = ModuleRegistration("foo.baz", "x = 42")
s1.register(r1)
s1.activate(r1)
s2.register(mr1)
s2.activate(mr1)
foo = self.service.findModule("foo")
baz = self.service.findModule("foo.baz")
self.assert_(foo.baz is baz)
self.assertEqual(foo.baz.x, 42)
c1 = r1.getComponent()
self.assert_(foo.bar is c1)
# now deactivate the module and make sure bar is still there:
s2.deactivate(mr1)
self.failIf(hasattr(foo, "baz"))
self.assert_(self.service.findModule("foo") is foo)
self.assert_(foo.bar is c1)
self.assert_(self.service.resolve("foo.bar") is c1)
def test_deep_nesting_simple(self):
names, stacks = self.create_deep_nesting()
# check the innermost layers
z = self.service.findModule(names[-1])
self.assertEqual(z.z, 42)
y = self.service.findModule(names[-2])
self.assert_(y.z is z)
x = self.service.findModule(names[-3])
self.assert_(x.y is y)
# check the outermost layers
a = self.service.findModule("a")
b = self.service.findModule("a.b")
self.assert_(a.b is b)
# make sure the deactivation cascades all the way to the top,
# since there's nothing else active
stacks[-1].activate(None)
self.failIf(hasattr(a, "b"))
self.assert_(self.service.findModule("a") is None)
def test_deep_nesting_with_middle_1(self):
names, stacks = self.create_deep_nesting()
ms = self.service.createRegistrations("a.b.c.d.e.frob")
r = NamedRegistration("a.b.c.d.e.frob")
ms.register(r)
ms.activate(r)
e = self.service.findModule("a.b.c.d.e")
f = self.service.findModule("a.b.c.d.e.f")
self.assert_(e.f is f)
self.assert_(e.frob is r.getComponent())
# deactivate the thing in the middle and make sure we don't
# lose the rest
ms.activate(None)
self.assert_(e.f is f)
self.failIf(hasattr(e, "frob"))
y = self.service.findModule(names[-2])
z = self.service.findModule(names[-1])
self.assert_(y.z is z)
def test_deep_nesting_with_middle_2(self):
names, stacks = self.create_deep_nesting()
ms = self.service.createRegistrations("a.b.c.d.e.frob")
r = NamedRegistration("a.b.c.d.e.frob")
ms.register(r)
ms.activate(r)
e = self.service.findModule("a.b.c.d.e")
f = self.service.findModule("a.b.c.d.e.f")
self.assert_(e.f is f)
self.assert_(e.frob is r.getComponent())
# deactivate the thing on the end and make sure we don't
# lose the middle, but that the end gets deactivated
stacks[-1].activate(None)
self.failIf(hasattr(e, "f"))
self.assert_(e.frob is r.getComponent())
def create_deep_nesting(self):
parts = "abcdefghijklmnopqrstuvwxyz"
names = [".".join(parts[:i+1]) for i in range(len(parts))]
stacks = [self.service.createRegistrations(n) for n in names]
rz = ModuleRegistration(names[-1], "z = 42")
stacks[-1].register(rz)
stacks[-1].activate(rz)
return names, stacks
def test_queryActiveComponent_non_module(self):
marker = object()
service = self.service
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
r = NamedRegistration("foobar")
o = r.getComponent()
stack = service.createRegistrations("foobar")
stack.register(r)
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
stack.activate(r)
self.assert_(service.queryActiveComponent("foobar") is o)
self.assert_(service.queryActiveComponent("foobar", marker) is o)
stack.activate(None)
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
def test_queryActiveComponent_module(self):
marker = object()
service = self.service
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
r = ModuleRegistration("foobar", "x = 42")
stack = service.createRegistrations("foobar")
stack.register(r)
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
stack.activate(r)
self.assertEqual(service.queryActiveComponent("foobar").x, 42)
self.assertEqual(service.queryActiveComponent("foobar", marker).x, 42)
stack.activate(None)
self.assert_(service.queryActiveComponent("foobar") is None)
self.assert_(service.queryActiveComponent("foobar", marker) is marker)
class GlobalFolderInteractionTestCase(PlacefulSetup, unittest.TestCase):
"""Tests of the interaction between folder modules and global modules."""
def setUp(self):
PlacefulSetup.setUp(self, site=True)
self.createStandardServices()
self.default = zapi.traverse(self.rootFolder, "++etc++site/default")
self.default["foo.py"] = module.Manager("x = 42\n")
self.folderModule = zapi.traverseName(self.default, "foo.py")
reg = globalmodule.GlobalModuleRegistration(
"a.b.foo", zapi.getPath(self.folderModule))
rm = zapi.traverseName(self.default, "RegistrationManager")
self.registration = reg
rm["spam"] = reg
self.modules = zapi.getService(self.default, Modules)
def test_update_from_source(self):
self.registration.status = ActiveStatus
gm = self.modules.findModule("a.b.foo")
self.assertEqual(gm.x, 42)
self.assert_(gm is not self.folderModule.getModule())
self.folderModule.source = "x = 12\n"
self.folderModule.execute()
self.assertEqual(self.folderModule.getModule().x, 12)
self.assertEqual(gm.x, 12)
def test_non_update_from_source(self):
# to test, we need to get the global module first:
self.registration.status = ActiveStatus
gm = self.modules.findModule("a.b.foo")
self.assertEqual(gm.x, 42)
# now deativate the registration and update the source:
self.registration.status = RegisteredStatus
self.folderModule.source = "x = 12\n"
self.folderModule.execute()
self.assertEqual(self.folderModule.getModule().x, 12)
# make sure the new source wasn't applied to the global module:
self.assertRaises(AttributeError, lambda: gm.x)
def test_multiple_registrations(self):
reg2 = globalmodule.GlobalModuleRegistration(
"c.d.bar", zapi.getPath(self.folderModule))
rm = zapi.traverseName(self.default, "RegistrationManager")
rm["bar"] = reg2
reg2.status = ActiveStatus
self.registration.status = ActiveStatus
# make sure we're starting with the same values for all modules:
gm1 = self.modules.findModule("a.b.foo")
gm2 = self.modules.findModule("c.d.bar")
fm = self.folderModule.getModule()
self.assertEqual(gm1.x, fm.x)
self.assertEqual(gm2.x, fm.x)
# make sure *both* global modules track changes to the folder module:
self.folderModule.source = "x = 42\n"
self.folderModule.execute()
self.assertEqual(fm.x, 42)
self.assertEqual(gm1.x, fm.x)
self.assertEqual(gm2.x, fm.x)
# inactivate one registration and make sure source changes are
# only reflected in the remaining active global module:
reg2.status = RegisteredStatus
self.folderModule.source = "x = 3\n"
self.folderModule.execute()
self.assertEqual(fm.x, 3)
self.assertEqual(gm1.x, fm.x)
self.assertRaises(AttributeError, lambda: gm2.x)
def test_suite():
suite = unittest.makeSuite(GlobalRegistrationStackTestCase)
suite.addTest(unittest.makeSuite(GlobalRegistrationStackModuleTestCase))
suite.addTest(unittest.makeSuite(GlobalModuleServiceTestCase))
suite.addTest(unittest.makeSuite(GlobalFolderInteractionTestCase))
return suite
=== Zope3/src/zope/app/services/tests/test_module.py 1.12 => 1.12.4.1 ===
--- Zope3/src/zope/app/services/tests/test_module.py:1.12 Mon Jan 12 18:25:44 2004
+++ Zope3/src/zope/app/services/tests/test_module.py Thu Jan 15 15:50:22 2004
@@ -19,24 +19,20 @@
import unittest
-from zope.interface import Interface, implements
-from zope.app.services.tests.placefulsetup import PlacefulSetup
-from zope.app.traversing import traverse
-from zope.app.services.module import Manager
-from zodb.storage.mapping import MappingStorage
+from zodb.code.module import PersistentModuleImporter
from zodb.db import DB
-from transaction import get_transaction
-
+from zodb.storage.mapping import MappingStorage
-class ITestService(Interface):
- pass
+from zope.interface import Interface, implements
-class TestService:
+from zope.app import zapi
+from zope.app.services.tests.placefulsetup import PlacefulSetup
+from zope.app.services.module import Manager, isident
- implements(ITestService)
+from transaction import get_transaction
-NAME = 'zope.app.services.tests.sample1'
+NAME = 'mymod'
called = 0
@@ -51,20 +47,62 @@
test_module.called += 1
"""
-class LocalModuleTests(PlacefulSetup, unittest.TestCase):
+
+class FolderModuleTests(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self, site=True)
- self.sm = traverse(self.rootFolder, "++etc++site")
- default = traverse(self.sm, "default")
+ self.createStandardServices()
+ self.default = zapi.traverse(self.rootFolder, "++etc++site/default")
+ self.importer = PersistentModuleImporter()
+ self.importer.install()
+
+ def tearDown(self):
+ self.importer.uninstall()
+
+ def add_module(self, name, source):
+ self.default[name] = mod = Manager(source)
+ return zapi.traverseName(self.default, name)
+
+ def test__name__(self):
+ m = self.add_module("a", "x = 1\n")
+ self.assertEqual(m.__name__, "a")
+ self.assertEqual(m.name, "a")
+ self.assertEqual(m.getModule().__name__, "a")
+ m = self.add_module("b.py", "x = 1\n")
+ self.assertEqual(m.__name__, "b.py")
+ self.assertEqual(m.name, "b")
+ self.assertEqual(m.getModule().__name__, "b")
+ m.__name__ = "ba"
+ self.assertEqual(m.__name__, "ba")
+ self.assertEqual(m.name, "ba")
+ self.assertEqual(m.getModule().__name__, "ba")
+ self.assertRaises(ValueError,
+ setattr, m, "__name__", "a.b")
+ self.assertEqual(m.__name__, "ba")
+ self.assertRaises(ValueError,
+ setattr, m, "__name__", "a.b.py")
+ self.assertEqual(m.__name__, "ba")
+
+ def test_compile(self):
old_called = called
- default[NAME] = Manager(NAME, SOURCE)
- self.manager = traverse(default, NAME)
+ self.manager = self.add_module(NAME, SOURCE)
self.assertEqual(called, old_called)
self.manager.execute()
self.assertEqual(called, old_called + 1)
+ def test_recompile(self):
+ self.test_compile()
+ old_called = called
+ self.manager.source += "\n"
+ self.assertEqual(called, old_called)
+ m = self.manager.getModule()
+ self.assertEqual(called, old_called+1)
+ m = self.manager.getModule()
+ self.assertEqual(called, old_called+1)
+
def test_module_persistence(self):
+ self.test_compile()
db = DB(MappingStorage())
conn = db.open()
root = conn.root()
@@ -72,8 +110,7 @@
get_transaction().commit()
# The findModule() here is only for the
# RegistrationManagerContainer, not the SiteManager.
- default = traverse(self.rootFolder, "++etc++site/default")
- m = default.findModule(NAME)
+ m = self.default.findModule("__folder__." + NAME)
c = m.C(42)
self.assertEqual(c.ini, 42)
@@ -84,26 +121,62 @@
# regression check.
conn2 = db.open()
rootFolder2 = conn2.root()['Application']
- default = traverse(rootFolder2, "++etc++site/default")
- m = default.findModule(NAME)
+ default = zapi.traverse(rootFolder2, "++etc++site/default")
+ m = default.findModule("__folder__." + NAME)
c = m.C(42)
self.assertEqual(c.ini, 42)
self.assertEqual(m.x, 1)
- def test_recompile(self):
- old_called = called
- self.manager.source += "\n"
- self.assertEqual(called, old_called)
- m = self.manager.getModule()
- self.assertEqual(called, old_called+1)
- m = self.manager.getModule()
- self.assertEqual(called, old_called+1)
-
+ def test_folder_import_1(self):
+ self.add_module("foo.py", "x = 42\n")
+ self.add_module("bar.py",
+ "from __folder__.foo import x; y = x * 2\n")
+ m = self.default.findModule("__folder__.bar")
+ self.assertEqual(m.__name__, "bar")
+ self.assertEqual(m.y, 84)
+
+ def test_folder_import_2(self):
+ self.add_module("foo.py", "x = 42\n")
+ self.add_module("bar.py",
+ "import __folder__.foo; y = __folder__.foo.x * 2\n")
+ m = self.default.findModule("__folder__.bar")
+ self.assertEqual(m.__name__, "bar")
+ self.assertEqual(m.y, 84)
+
+ def test_folder_import_3(self):
+ self.add_module("foo.py", "x = 42\n")
+ self.add_module("bar.py",
+ "from __folder__ import foo; y = foo.x * 2\n")
+ m = self.default.findModule("__folder__.bar")
+ self.assertEqual(m.__name__, "bar")
+ self.assertEqual(m.y, 84)
+
+
+class HelperTests(unittest.TestCase):
+
+ def test_isident(self):
+ self.assert_(isident('foo'))
+ self.assert_(isident('CLASS'))
+ self.assert_(isident('mixedCase'))
+
+ self.assert_(not isident(''))
+ self.assert_(not isident('is'))
+ self.assert_(not isident('class'))
+
+ self.assert_(not isident('woohoo!'))
+ self.assert_(not isident('something with spaces'))
+
+
+def _splitname(s):
+ pos = s.rfind(".")
+ return s[:pos], s[pos+1:]
def test_suite():
- return unittest.makeSuite(LocalModuleTests)
+ suite = unittest.makeSuite(FolderModuleTests)
+ suite.addTest(unittest.makeSuite(HelperTests))
+ return suite
if __name__=='__main__':
unittest.main(defaultTest="test_suite")
=== Zope3/src/zope/app/services/tests/test_modulelookup.py 1.5 => 1.5.4.1 ===
--- Zope3/src/zope/app/services/tests/test_modulelookup.py:1.5 Tue Jan 13 09:27:56 2004
+++ Zope3/src/zope/app/services/tests/test_modulelookup.py Thu Jan 15 15:50:22 2004
@@ -19,17 +19,18 @@
$Id$
"""
-from zope.testing.doctestunit import DocTestSuite
+import unittest
from zope.app.services.registration import RegistrationManagerContainer
from zope.app.interfaces.services.module import IModuleManager
from zope.interface import implements
from zope.app.container.contained import Contained, setitem
-from zope.app.tests.placelesssetup import setUp, tearDown
+from zope.app.services.tests.placefulsetup import PlacefulSetup
+
class MyModuleManager(object):
implements(IModuleManager)
-
+
def __init__(self, module):
self.module = module
@@ -38,53 +39,51 @@
class MyFolder(RegistrationManagerContainer, dict, Contained):
def __setitem__(self, name, object):
+ object.__name__ = name
+ object.__parent__ = self
setitem(self, super(MyFolder, self).__setitem__, name, object)
-def test_findMoule():
- """
- Tests for RegistrationManagerContainer.findModule().
-
- >>> folder = MyFolder()
- >>> folder['m1.py'] = MyModuleManager(1)
- >>> folder['m1'] = MyModuleManager(0)
- >>> folder['m2'] = MyModuleManager(2)
- >>> next = MyFolder()
- >>> next['m3'] = MyModuleManager(3)
- >>> next['z.y.m4'] = MyModuleManager(4)
- >>> folder.__parent__ = next
-
- >>> folder.findModule('m1')
- 1
- >>> folder.findModule('m2')
- 2
- >>> folder.findModule('m3')
- 3
- >>> folder.findModule('z.y.m4')
- 4
- >>> folder.findModule('m5')
- Traceback (most recent call last):
- ...
- ImportError: m5
-
- >>> import zope.app.services.tests.test_modulelookup
- >>> m = folder.findModule('zope.app.services.tests.test_modulelookup')
- >>> int(m is zope.app.services.tests.test_modulelookup)
- 1
-
- """
-
-def test_resolve():
- """
- >>> folder = MyFolder()
- >>> import zope.app.services.tests.test_modulelookup
- >>> f = folder.resolve(
- ... 'zope.app.services.tests.test_modulelookup.test_resolve')
- >>> int(f is zope.app.services.tests.test_modulelookup.test_resolve)
- 1
- """
+class ModuleLookupTestCase(PlacefulSetup, unittest.TestCase):
-def test_suite():
- return DocTestSuite(setUp=setUp, tearDown=tearDown)
+ def setUp(self):
+ PlacefulSetup.setUp(self, site=True)
+ self.createStandardServices()
+
+ def test_findMoule_simple(self):
+ folder = MyFolder()
+ folder['m1.py'] = MyModuleManager(1)
+ folder['m1'] = MyModuleManager(0)
+ folder['m2'] = MyModuleManager(2)
+ next = MyFolder()
+ next['m3'] = MyModuleManager(3)
+ next['z.y.m4'] = MyModuleManager(4)
+ folder.__parent__ = next
+ next.__parent__ = self.rootFolder
+
+ self.assertEqual(folder.findModule('__folder__.m1'), 1)
+ self.assertEqual(folder.findModule('__folder__.m2'), 2)
+ self.assert_(folder.findModule('m3') is None)
+ self.assert_(folder.findModule('z.y.m4') is None)
+ self.assert_(folder.findModule('m5') is None)
+ self.assert_(folder.findModule('__folder__.m3') is None)
+ self.assert_(folder.findModule('__folder__.z.y.m4') is None)
+ self.assert_(folder.findModule('__folder__.m5') is None)
+
+ import zope.app.services.tests.test_modulelookup
+ m = folder.findModule('zope.app.services.tests.test_modulelookup')
+ self.assert_(zope.app.services.tests.test_modulelookup is m)
+
+ def test_resolve(self):
+ # check that this eventually falls back to sys.modules
+ folder = MyFolder()
+ folder.__parent__ = self.rootFolder
+
+ import zope.app.services.tests.test_modulelookup
+ f = folder.resolve(
+ 'zope.app.services.tests.test_modulelookup.MyFolder')
+ self.assert_(zope.app.services.tests.test_modulelookup.MyFolder is f)
-if __name__ == '__main__': unittest.main()
+
+def test_suite():
+ return unittest.makeSuite(ModuleLookupTestCase)
More information about the Zope3-Checkins
mailing list