Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interval exception detect when modify the lower or the upper property on an exist interval instance #48

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
28 changes: 21 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
language: python
python:
- 2.7
- 3.3
- 3.4
- 3.5
- pypy
- pypy3
matrix:
include:
- python: 2.7
dist: trusty
sudo: false
- python: 3.3
dist: trusty
sudo: false
- python: 3.4
dist: trusty
sudo: false
- python: 3.5
dist: trusty
sudo: false
- python: pypy
dist: trusty
sudo: false
- python: pypy3
dist: trusty
sudo: false

install:
- pip install -e ".[test]"
script:
Expand Down
26 changes: 20 additions & 6 deletions intervals/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,19 @@ def __init__(
self.parser(bounds, lower_inc, upper_inc)
)

if self.lower > self.upper:
self.range_init_and_setter_exception_detect(self.lower, self.upper, self.lower_inc, self.upper_inc)

@classmethod
def range_init_and_setter_exception_detect(cls, lower, upper, lower_inc, upper_inc):
if lower > upper:
raise RangeBoundsException(
self.lower,
self.upper
lower,
upper
)
if (
self.lower == self.upper and
not self.lower_inc and
not self.upper_inc
lower == upper and
not lower_inc and
not upper_inc
):
raise IllegalArgument(
'The bounds may be equal only if at least one of the bounds '
Expand Down Expand Up @@ -320,8 +324,13 @@ def lower(self):
def lower(self, value):
value = self.coerce_value(value)
if value is None:
if hasattr(self, 'upper_inc'):
self.range_init_and_setter_exception_detect(-inf, self.upper, self.lower_inc, self.upper_inc)
self._lower = -inf
else:
if hasattr(self, 'upper_inc'):
self.range_init_and_setter_exception_detect(self.round_value_by_step(value), self.upper, self.lower_inc,
self.upper_inc)
self._lower = self.round_value_by_step(value)

@property
Expand All @@ -332,8 +341,13 @@ def upper(self):
def upper(self, value):
value = self.coerce_value(value)
if value is None:
if hasattr(self, 'upper_inc'):
self.range_init_and_setter_exception_detect(self.lower, inf, self.lower_inc, self.upper_inc)
self._upper = inf
else:
if hasattr(self, 'upper_inc'):
self.range_init_and_setter_exception_detect(self.lower, self.round_value_by_step(value), self.lower_inc,
self.upper_inc)
self._upper = self.round_value_by_step(value)

def round_value_by_step(self, value):
Expand Down
44 changes: 44 additions & 0 deletions tests/interval/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,47 @@ class TestTypeGuessing(object):
)
def test_guesses_types(self, number_range, type):
assert Interval(number_range).type == type


class TestIntervalChanging(object):
@mark.parametrize(
('constructor', 'number_range', 'bad_lower'),
(
(IntInterval, (1, 2), 3),
(IntInterval, [1, 2], 3),
(IntInterval, (1, 2), float('inf')),
(CharacterInterval, ('a', 'b'), 'c'),
(CharacterInterval, ('a', 'b'), 'd'),
(CharacterInterval, ('a', 'b'), inf),
)
)
def test_raises_exception_for_badly_lower_changing(
self,
constructor,
number_range,
bad_lower
):
with raises(RangeBoundsException):
interval = constructor(number_range)
interval.lower = bad_lower

@mark.parametrize(
('constructor', 'number_range', 'bad_upper'),
(
(IntInterval, (1, 2), 0),
(IntInterval, [1, 2], 0),
(IntInterval, (1, 2), float('-inf')),
(CharacterInterval, ('b', 'c'), 'a'),
(CharacterInterval, ('b', 'd'), 'a'),
(CharacterInterval, ('b', 'c'), -inf),
)
)
def test_raises_exception_for_badly_upper_changing(
self,
constructor,
number_range,
bad_upper
):
with raises(RangeBoundsException):
interval = constructor(number_range)
interval.upper = bad_upper