Skip to content

Commit

Permalink
Vector2.scale_to: Raise ValueError when given a negative length
Browse files Browse the repository at this point in the history
  • Loading branch information
nbraud committed Dec 8, 2018
1 parent 671e1ae commit 1b003f2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ def scale_to(self: VectorOrSub, length: Realish) -> VectorOrSub:
"""
Scale the vector to the given length
"""
if length < 0:
raise ValueError("Vector2.scale_to takes non-negative lengths.")

return (length / self.length) * self

scale = scale_to
Expand Down
2 changes: 2 additions & 0 deletions tests/test_vector2_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def test_scale_to_length(x: Vector2, l: float):
assert isclose(x.scale_to(l).length, l)
except ZeroDivisionError:
assert x == (0, 0)
except ValueError:
assert l < 0


@given(x=vectors(), l=floats(min_value=1e150, max_value=1e150))
Expand Down

0 comments on commit 1b003f2

Please sign in to comment.