Skip to content

Commit

Permalink
Added comprehensive tests of manager objects, removing 2 more uncovered
Browse files Browse the repository at this point in the history
lines.
  • Loading branch information
strichter committed Mar 25, 2009
1 parent 0582340 commit 2e52dd0
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/z3c/form/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def extractFileName(form, id, cleanup=True, allowEmptyPostfix=False):

class UniqueOrderedKeys(object):
"""Ensures that we only ue unique keys in a list.
This is usefull since we use the keys and values list only as ordered
keys and values addition for our data dict.
This is useful since we use the keys and values list only as ordered keys
and values addition for our data dict.
Note, this list is only used for Manager keys and not for values since we
can't really compare values if we will get new instances of widgets or
actions.
Expand Down Expand Up @@ -150,7 +150,7 @@ def __init__(self, *args, **kw):

@apply
def _data_keys():
"""Use a special ordered list which will check for duplicated keys."""
"""Use a special ordered list which will check for duplicated keys."""
def get(self):
return self.__data_keys
def set(self, values):
Expand Down Expand Up @@ -211,8 +211,8 @@ def select(self, *names):
def omit(self, *names):
"""See interfaces.ISelectionManager"""
return self.__class__(
*[item for item in self.values()
if item.__name__ not in names])
*[item for name, item in self.items()
if name not in names])

def copy(self):
"""See interfaces.ISelectionManager"""
Expand Down
135 changes: 135 additions & 0 deletions src/z3c/form/util.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,138 @@ file upload. We can use the stub form from the previous test.
>>> uploadForm.setFakeFileName('foo.unknown')
>>> util.extractContentType(uploadForm, 'form.widgets.data')
'text/x-unknown-content-type'


`UniqueOrderedKeys` object
--------------------------

This object manages the keys of a dictionary. It ensures that no values are
added multiple times and retains the original order of the keys.

>>> keys = util.UniqueOrderedKeys([1, 2])

Let's now add another key:

>>> keys.append(3)
>>> keys.data
[1, 2, 3]

Trying to add `3` again causes a `ValueError` exception:

>>> keys.append(3)
Traceback (most recent call last):
...
ValueError: 3


`Manager` object
----------------

The manager object is a base class of a mapping object that keeps track of the
key order as they are added.

>>> manager = util.Manager()

Initially the manager is empty:

>>> len(manager)
0

Since this base class mainly defines a read-interface, we have to add the
values manually:

>>> manager._data_values.append(2)
>>> manager._data_keys.append('b')
>>> manager._data['b'] = 2

>>> manager._data_values.append(1)
>>> manager._data_keys.append('a')
>>> manager._data['a'] = 1

Let's iterate through the manager:

>>> tuple(iter(manager))
('b', 'a')
>>> manager.keys()
['b', 'a']
>>> manager.values()
[2, 1]
>>> manager.items()
[('b', 2), ('a', 1)]

Let's ow look at item access:

>>> 'b' in manager
True
>>> manager.get('b')
2
>>> manager.get('c', 'None')
'None'

It also supports deletion:

>>> del manager['b']
>>> manager.items()
[('a', 1)]

When the `_data_keys` is reset it will always produce a `UniqueOrderedKeys`:

>>> manager._data_keys = []
>>> manager._data_keys
<z3c.form.util.UniqueOrderedKeys ...>

>>> manager._data_keys = util.UniqueOrderedKeys()
>>> manager._data_keys
<z3c.form.util.UniqueOrderedKeys ...>


`SelectionManager` object
-------------------------

The selection manager is an extension to the manager and provides a few more
API functions. Unfortunately, this base class is totally useless without a
sensible constructor:

>>> import zope.interface

>>> class MySelectionManager(util.SelectionManager):
... managerInterface = zope.interface.Interface
...
... def __init__(self, *args):
... super(MySelectionManager, self).__init__()
... args = list(args)
... for arg in args:
... if isinstance(arg, MySelectionManager):
... args += arg.values()
... continue
... self._data_values.append(arg)
... self._data_keys.append(str(arg))
... self._data[str(arg)] = arg

Let's now create two managers:

>>> manager1 = MySelectionManager(1, 2)
>>> manager2 = MySelectionManager(3, 4)

You can add two managers:

>>> manager = manager1 + manager2
>>> manager.values()
[1, 2, 3, 4]

Next, you can select only certain names:

>>> manager.select('1', '2', '3').values()
[1, 2, 3]

Or simply omit a value.

>>> manager.omit('2').values()
[1, 3, 4]

You can also easily copy a manager:

>>> manager.copy() is not manager
True

That's all.

0 comments on commit 2e52dd0

Please sign in to comment.