| Index: chrome/common/extensions/docs/server2/test_object_store.py
|
| diff --git a/chrome/common/extensions/docs/server2/test_object_store.py b/chrome/common/extensions/docs/server2/test_object_store.py
|
| index d78746224267ddb51ef758efe1114388706b8a01..c1e5bc45a92bca49879c30e1fbd666357f3494f4 100644
|
| --- a/chrome/common/extensions/docs/server2/test_object_store.py
|
| +++ b/chrome/common/extensions/docs/server2/test_object_store.py
|
| @@ -6,19 +6,58 @@ from future import Future
|
| from object_store import ObjectStore
|
|
|
| class TestObjectStore(ObjectStore):
|
| - '''An object store which records its namespace and behaves like a dict, for
|
| - testing.
|
| + '''An object store which records its namespace and behaves like a dict.
|
| + Specify |init| with an initial object for the object store.
|
| + Use CheckAndReset to assert how many times Get/Set/Del have been called. Get
|
| + is a special case; it is only incremented once the future has had Get called.
|
| '''
|
| - def __init__(self, namespace):
|
| + def __init__(self, namespace, init=None):
|
| self.namespace = namespace
|
| - self._store = {}
|
| + self._store = init or {}
|
| + self._get_count = 0
|
| + self._set_count = 0
|
| + self._del_count = 0
|
|
|
| - def SetMulti(self, mapping, **optarg):
|
| + #
|
| + # ObjectStore implementation.
|
| + #
|
| +
|
| + def GetMulti(self, keys):
|
| + class FutureImpl(object):
|
| + def Get(self2):
|
| + self._get_count += 1
|
| + return dict((k, self._store.get(k)) for k in keys if k in self._store)
|
| + return Future(delegate=FutureImpl())
|
| +
|
| + def SetMulti(self, mapping):
|
| + self._set_count += 1
|
| self._store.update(mapping)
|
|
|
| - def GetMulti(self, keys, **optargs):
|
| - return Future(value=dict((k, v) for k, v in self._store.items()
|
| - if k in keys))
|
| + def DelMulti(self, keys):
|
| + self._del_count += 1
|
| + for k in keys:
|
| + self._store.pop(k, None)
|
| +
|
| + #
|
| + # Testing methods.
|
| + #
|
| +
|
| + def CheckAndReset(self, get_count=0, set_count=0, del_count=0):
|
| + '''Returns a tuple (success, error). Use in tests like:
|
| + self.assertTrue(*object_store.CheckAndReset(...))
|
| + '''
|
| + errors = []
|
| + for desc, expected, actual in (('get_count', get_count, self._get_count),
|
| + ('set_count', set_count, self._set_count),
|
| + ('del_count', del_count, self._del_count)):
|
| + if actual != expected:
|
| + errors.append('%s: expected %s got %s' % (desc, expected, actual))
|
| + try:
|
| + return (len(errors) == 0, ','.join(errors))
|
| + finally:
|
| + self.Reset()
|
|
|
| - def Delete(self, key):
|
| - del self._store[key]
|
| + def Reset(self):
|
| + self._get_count = 0
|
| + self._set_count = 0
|
| + self._del_count = 0
|
|
|