Added slicing capability and unit tests.

This commit is contained in:
Alice Bevan-McGregor 2010-03-13 13:50:21 -05:00
parent 35c5d5752c
commit 5f30af085e
2 changed files with 23 additions and 2 deletions

View File

@ -187,9 +187,10 @@ def method_matches_args(method, params, remainder, lax_params=False):
class Path(collections.deque):
def __init__(self, value='/', separator='/'):
self.separator = separator
self._assign(value)
super(Path, self).__init__()
self._assign(value)
def _assign(self, value):
separator = self.separator
@ -212,3 +213,10 @@ class Path(collections.deque):
def __repr__(self):
return "<Path %r>" % super(Path, self).__repr__()
def __getitem__(self, i):
try:
return super(Path, self).__getitem__(i)
except TypeError:
return Path([self[i] for i in xrange(*i.indices(len(self)))])

View File

@ -237,3 +237,16 @@ def test_path_unicode():
instance.path = case
yield assert_path, instance, expected, unicode
def test_path_slicing():
class MockOb(object):
path = Path()
instance = MockOb()
instance.path = '/foo/bar/baz'
assert str(instance.path[1:]) == 'foo/bar/baz'
assert str(instance.path[2:]) == 'bar/baz'
assert str(instance.path[0:2]) == '/foo'
assert str(instance.path[::2]) == '/bar'