Skip to content

Commit

Permalink
Don't crash if a shape is compared to another type.
Browse files Browse the repository at this point in the history
Just return False.
  • Loading branch information
kalekundert committed Dec 30, 2016
1 parent a182bba commit 8d28c89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 2 additions & 9 deletions tests/20_test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,8 @@ class VectorLike: pass
assert r != u
assert s != r
assert t != r
assert u != r, "u=<{0.x:.2f}, {0.y:.2f}> r={1}".format(u, r)

try: r == 2
except VectorCastError: pass
else: raise AssertionError

try: r != 2
except VectorCastError: pass
else: raise AssertionError
assert u != r
assert r != None

# Addition operator

Expand Down
1 change: 1 addition & 0 deletions tests/30_test_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_rectangle_accessor_methods():

assert r == r
assert r == r.copy()
assert r != None

assert r.left == 2
assert r.center_x == 5
Expand Down
25 changes: 17 additions & 8 deletions vecrec/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,19 @@ def __getitem__(self, i):
""" Return the specified coordinate. """
return self.tuple[i]

@accept_anything_as_vector
def __eq__(self, other):
return self.x == other.x and self.y == other.y
"""Return true if the given object has the same coordinates as this
vector."""
try:
other = cast_anything_to_vector(other)
return self.x == other.x and self.y == other.y
except VectorCastError:
return False

@accept_anything_as_vector
def __ne__(self, other):
return self.x != other.x or self.y != other.y
"""Return true if the given object has different coordinates than this
vector."""
return not self.__eq__(other)

def __neg__(self):
""" Return a copy of this vector with the signs flipped. """
Expand Down Expand Up @@ -526,10 +532,13 @@ def __str__(self):
self.bottom, self.left, self.width, self.height)

def __eq__(self, other):
return ( self.__bottom == other.__bottom and
self.__left == other.__left and
self.__width == other.__width and
self.__height == other.__height )
try:
return (self.bottom == other.bottom and
self.left == other.left and
self.width == other.width and
self.height == other.height)
except AttributeError:
return False

@accept_anything_as_vector
def __add__(self, vector):
Expand Down

0 comments on commit 8d28c89

Please sign in to comment.