I have written the following code for a Point class, and its methods. I want to know how to apply assert statement to test if the Point x, y coordinates moved correctly. I assume that the assert statement tests the validity of the resulting coordinates after a move method is applied to the Point instance. Can you please suggest how i should write an assert statement that validates all given instances of a Point class are moved correctly?
class Point(object): # Modify here
#Creates a point on the x,y coordinate system
def __init__(self,x,y):# The __init__ constructor takes the arguments self( the current instance of the class), and positions x and y
#Defines x and y variables
self.x = x # Define self's x attribute.
self.y = y
#Moves the given point by dx and dy
def move(self,dx,dy):#
#Move the point to different location
self.x=self.x+dx# the self's x + change in x
self.y=self.y+dy
#returns string representation of class Point
def __str__(self,x,y):
return "Point location:",(self.x,self.y)
if __name__ == '__main__':
# Tests for point
# ==================================
def testPoint(x, y):
p = Point(0, -1) # create a point instance
p.move(2, 2) # Use method move, move the point object by x=2 and y=2
assert p.x == 2
assert p.y ==1
print 'Success! Point tests passed!'
testPoint(0,-1)
Is this what you need -- a method parameterized for the given input?
def testPoint(x, y, move_x, move_y):
p = Point(x, y) # create a point instance
p.move(move_x, move_y) # Use method move, move the point object by x=2 and y=2
assert p.x == x + move_x and \
p.y == y + move_y
You can then execute this for a variety of starting points and movement vectors.