7: Roughed out API.finalize(); added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-07-19 08:31:46 +00:00
parent 91adc9c2d0
commit 26c9f4c881
2 changed files with 43 additions and 12 deletions

View File

@ -158,7 +158,7 @@ class NameSpace(object):
class API(object):
__commands = None
__cmd = None
__objects = None
__locked = False
@ -171,9 +171,9 @@ class API(object):
return self.__objects
objects = property(__get_objects)
def __get_commands(self):
return self.__commands
commands = property(__get_commands)
def __get_cmd(self):
return self.__cmd
cmd = property(__get_cmd)
def __merge(self, base, cls, override):
assert issubclass(base, Named)
@ -184,17 +184,24 @@ class API(object):
raise exceptions.DuplicateError(cls.__name__, id(cls))
if cls.__name__ in self.__names and not override:
raise exceptions.OverrideError(cls.__name__)
prefix = base.prefix
assert cls.__name__.startswith(prefix)
self.__classes.add(cls)
self.__names.add(cls.__name__)
if base not in self.__stage:
self.__stage[base.prefix] = {}
self.__stage[base.prefix][cls.__name__] = cls
if prefix not in self.__stage:
self.__stage[prefix] = {}
self.__stage[prefix][cls.__name__] = cls
def register_command(self, cls, override=False):
self.__merge(Command, cls, override)
def finalize(self):
pass
#i = cls()
#assert cls.__name__ == (base.prefix + '_' + i.name)
for (prefix, d) in self.__stage.items():
n = {}
for cls in d.values():
i = cls()
assert cls.__name__ == (prefix + '_' + i.name)
n[i.name] = i
if prefix == 'cmd':
self.__cmd = NameSpace(n)

View File

@ -267,8 +267,7 @@ class test_API:
Test expectations of a fresh API instance.
"""
api = self.new()
assert read_only(api, 'objects') is None
assert read_only(api, 'objects') is None
assert read_only(api, 'cmd') is None
def test_register_command(self):
api = self.new()
@ -314,3 +313,28 @@ class test_API:
# Check that override=True works:
api.register_command(cmd_my_command, override=True)
def test_finalize(self):
api = self.new()
assert read_only(api, 'cmd') is None
class cmd_my_command(base.Command):
pass
class cmd_another_command(base.Command):
pass
api.register_command(cmd_my_command)
api.register_command(cmd_another_command)
api.finalize()
cmd = read_only(api, 'cmd')
assert isinstance(cmd, base.NameSpace)
assert api.cmd is cmd
assert len(cmd) == 2
assert list(cmd) == ['another_command', 'my_command']
assert isinstance(cmd.my_command, cmd_my_command)
assert cmd.my_command is cmd['my_command']
assert isinstance(cmd.another_command, cmd_another_command)
assert cmd.another_command is cmd['another_command']