Merge pull request #14 from jab/test_slicing2

refactor _fwd_slice with tests
This commit is contained in:
jab 2015-04-29 15:44:53 -04:00
commit eba11251d1
2 changed files with 56 additions and 3 deletions

View File

@ -56,10 +56,14 @@ class BidirectionalMapping(Mapping):
Returns True if only its start is not None and False if only its stop
is not None.
"""
if slice.step is not None or \
(not ((slice.start is None) ^ (slice.stop is None))):
start_missing = slice.start is None
start_found = not start_missing
stop_missing = slice.stop is None
step_found = slice.step is not None
if step_found or start_missing == stop_missing:
raise TypeError('Slice must specify only either start or stop')
return slice.start is not None
return start_found
def __getitem__(self, keyorslice):
"""

49
tests/test_slice.py Normal file
View File

@ -0,0 +1,49 @@
from itertools import product
import pytest
from bidict import bidict
@pytest.fixture
def b():
return bidict(H='hydrogen')
single_test_data = {'H', 'hydrogen', -1, 0, 1}
bad_start_values = single_test_data - {'H'}
bad_stop_values = single_test_data - {'hydrogen'}
pair_test_data = product(single_test_data, single_test_data)
def test_good_start(b):
assert b['H'] == 'hydrogen'
assert b['H':] == 'hydrogen'
@pytest.mark.parametrize('start', bad_start_values)
def test_bad_start(b, start):
with pytest.raises(KeyError):
b[start]
with pytest.raises(KeyError):
b[start:]
def test_good_stop(b):
assert b[:'hydrogen'] == 'H'
@pytest.mark.parametrize('stop', bad_stop_values)
def test_stop(b, stop):
with pytest.raises(KeyError):
b[:stop]
@pytest.mark.parametrize('start, stop', pair_test_data)
def test_start_stop(b, start, stop):
with pytest.raises(TypeError):
b[start:stop]
@pytest.mark.parametrize('step', single_test_data)
def test_step(b, step):
with pytest.raises(TypeError):
b[::step]
def test_empty(b):
with pytest.raises(TypeError):
b[::]