Welcome back. Testing a class definition involves creating instances,
and invoking methods on those instances.
Here's a little class that lets us define points on an XY plane.
Instead of just treating him as two numbers or making a tuple,
we're going to actually make an instance of a class.
Each instance will represent one point with
an attribute for its X position and another for its Y position.
I'm going to draw a little grid again just so we have a place to plot our points.
So, we can have a point like three,
comma four, which would be plotted there.
To test our definition of this class,
we're really going to have to test the three methods that we have.
We have a constructor, the underscore init.
We've got the distance from origin,
and we've got the mood method.
Let's start by doing the constructor,
because that's really the heart of any class.
How do we test if we've implemented this to underscore init the constructor method?
How do we test if we implemented it correctly?
To figure that out, you got to remember the mechanics of classes,
or basically we create a new point by invoking the class.
When you call point on line 19,
you will create a new instance of the point class and then behind the scenes,
the constructor, the init method will be called with three getting passed as init X,
four as init Y,
and the new instance itself will be bound to the self parameter.
Now, the purpose of the init method is to change the contents of self.
Initially self has no attributes,
but after we've finished executing the init,
then self should have two attributes.
It should have an X attribute and a Y attribute,
and they should be bound to
the corresponding values three and four that have been passed in.
So, to test the class constructor,
we create a point and then we check whether it has its attributes correctly set.
Is it Y attribute set to four,
is it X attribute set to three.
Now usually, when we're doing a side-effect test,
we would have to create an object like a list or a dictionary,
and then invoke a function,
and then see if the list or the dictionary has appropriately changed.
In this case, we didn't have to do that,
because the invocation of the point class,
both creates the instance and invokes the method.
So, those two are combined into one step.
But then afterwards, we run our tests to see whether it came out right.
By the way, if this is a little confusing for you
because you're just coming from learning classes and the mechanics are a little hazy,
running this in code lens on your own would be really
helpful to make sure you understand what's going on.
Now, the next method that I'm going to show you
test for is the distance from origin method.
You can see that that distance from origin method,
what it's supposed to do is tell you how far away the point is from the origin.
In this case, it's a distance of five.
If we pick some other point like two two,
you would have a different distance.
But the three four point should have a distance away of two.
So, in order to do a test,
this is a method that just returns a value.
It doesn't change the contents of the point.
So, we should be doing a return value test.
I'm just going to get rid of a few of these markings.
So, testing the distance from origin method.
This is a return value test.
I still need to create a point in the first place.
I could have actually just use the P that I defined on line 19.
But when I'm defining a test,
I'd like to have everything that's involved in the test right there.
So, I'm recreating a new point P,
that's at position three, four.
Then I'm checking is at a distance five away from the origin.
I should have some other tests as well.
I should make some different points that are at different distances.
I could check whether a point at two,
zero is a distance two away from the origin.
Finally, I've got this move method.
Now this move method,
what it's doing is taking a point,
like this three, four point,
and it's going to translate it.
It's going to make it move some distance.
So, we'll pass in to the move method,
a distance to move in the X direction,
and a distance to move in the Y direction.
So in order to test that,
that is a method whose purpose is to have a side effect.
It's supposed to change the point.
Therefore, I need to have a side effect test.
So, I'm going to create tests here that again I
create my initial point and then I move it.
So, I'm moving it minus two in the X plane and buy up three in the Y.
So, I'm going to go minus two from three over to one,
and plus three in the Y.
So, I should end up at one, seven.
So, in my tests I'm checking.
So first, I create the point that I move it,
and then I see if it's correctly been moved.
If my move method is correctly implemented,
the X value should be one and the Y value should be seven.
So in this case, we'll run
our tests and we'll find out whether it's all correctly implemented.
It is correctly implemented.
If I had made some change or some of these methods weren't defined yet,
then I would fail some of the tests.
The purpose again of having these tests,
is to check whether the implementation of the methods is correct.
If the methods have been implemented correctly,
you should pass all the tests.
If they haven't been implemented correctly,
then one or more of the test will fail.
So, that's the skeleton of how to test a class definition.
Basically, you need to assess for each method,
whether it's purpose is to return a value or have a side effect.
Testing the class constructor is special,
because each test will invoke the class with some parameters,
and then check whether the returned instance as its instance variables set correctly.
For the other methods,
you will also have to create an instance,
but then you invoke them and either check whether
the return value is correct or check whether
the instance has had its variable to change appropriately. I'll see you next time.