323: Added Command.run() method that dispatches to execute() or forward(); added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-24 02:52:19 +00:00
parent 4dbbf5656d
commit 19bbc48eb6
3 changed files with 52 additions and 3 deletions

View File

@ -343,6 +343,11 @@ class Command(plugable.Plugin):
print ' args =', args
print ' kw =', kw
def forward(self, *args, **kw):
print '%s.execute():' % self.name
print ' args =', args
print ' kw =', kw
def __call__(self, *args, **kw):
if len(args) > 0:
arg_kw = self.args_to_kw(*args)
@ -353,7 +358,15 @@ class Command(plugable.Plugin):
kw.update(self.get_default(**kw))
self.validate(**kw)
args = tuple(kw.pop(name) for name in self.args)
self.execute(*args, **kw)
return self.run(*args, **kw)
def run(self, *args, **kw):
if self.api.env.in_server_context:
target = self.execute
else:
target = self.forward
object.__setattr__(self, 'run', target)
return target(*args, **kw)
def args_to_kw(self, *values):
if self.max_args is not None and len(values) > self.max_args:

View File

@ -691,11 +691,15 @@ class API(DictProxy):
Dynamic API object through which `Plugin` instances are accessed.
"""
__finalized = False
server_context = True
def __init__(self, *allowed):
def __init__(self, *allowed, **kw):
self.__d = dict()
self.register = Registrar(*allowed)
default = dict(
in_server_context=True,
)
default.update(kw)
self.env = MagicDict(default)
super(API, self).__init__(self.__d)
def finalize(self):

View File

@ -620,6 +620,38 @@ class test_Command(ClassChecker):
assert o.kw_to_args(whatever='hello', two='Two', one='One') == \
('One', 'Two')
def test_run(self):
"""
Test the `frontend.Command.run` method.
"""
class my_cmd(self.cls):
def execute(self, *args, **kw):
return ('execute', args, kw)
def forward(self, *args, **kw):
return ('forward', args, kw)
args = ('Hello,', 'world,')
kw = dict(how_are='you', on_this='fine day?')
# Test in server context:
api = plugable.API(self.cls, in_server_context=True)
api.finalize()
o = my_cmd()
o.set_api(api)
assert o.run.im_func is self.cls.run.im_func
assert ('execute', args, kw) == o.run(*args, **kw)
assert o.run.im_func is my_cmd.execute.im_func
# Test in non-server context
api = plugable.API(self.cls, in_server_context=False)
api.finalize()
o = my_cmd()
o.set_api(api)
assert o.run.im_func is self.cls.run.im_func
assert ('forward', args, kw) == o.run(*args, **kw)
assert o.run.im_func is my_cmd.forward.im_func
class test_Object(ClassChecker):
"""