"Xtreme TDD"
Quentin Ducasse.
-Hi. In this redo of week 2,
what we are going to do is to basically implement
the same Counter as we did in week 1, but exclusively from the debugger
and from a test-driven perspective.
So, let us see what this means.
We will go and open System Browser.
What we will do is define the "Test" class first.
This is the idea of a test-driven development.
So, save that.
Now, we will write our first test, so "testCounterIsSetAndRead".
What will this test do? It will create a Counter,
so temporary variable "c",
to which we will assign "Counter new".
Then, we will assign 7 to the value of the "count" instance variable.
And then, we will check that "c count" is indeed equal to 7.
We can see that "Counter" is emphasized.
We have not defined "count:", we have not defined "count".
Nothing should work, basically, but we will see.
"Unknown variable: Counter, please correct."
"Counter" would like to define a new class,
would like to define it with the "count" instance variable.
And press OK.
You can see that we have got our "Counter" class that is created there
with the instance variable "count".
And now, if we run our test,
we can see that the method "count:" has not been understood.
So, this is the debugger
and basically, this is the execution stack,
this is the body of the method that is being executed,
and those are the different variables.
So, if I go to "a Counter", you can see that...
This is "a Counter"... "Raw"...
You can see that "count" is equal to "nil",
because we have not set it up.
So, we can click on the Create button
to create the method "count:".
Go to Counter,
and what we will do is go to "accessing".
OK.
So, this is the set I would like, so this is fine.
I can press Proceed.
Then, it does not understand the next "count", the getter.
So, we can create it. Counter.
Once again, in the "accessing" protocol.
OK.
"Count", well written "count". That is great.
So, Proceed.
The test is red, but if we click on it, it is green.
So, perfect.
And if we go and have a look at the "Counter" class,
we now have our Setter and Getter for the "count" instance variable,
so that is perfect.
Now, if you remember correctly, we had a "class" method
that had this "choose the starting value",
so we used it as "startingAt: 5".
We will not need this line,
but we would like to assert that the count is indeed equal to 5.
So, let us save this.
It does not understand the "startingAt" message,
but we will define it right now.
Click on it. "Instance of 'Counter' class did not understand 'startingAt'".
So, let us create it in the "Counter" class,
under the "instance creation" protocol.
Press OK.
And we will have to implement it, so basically,
what will "startingAt" do?
It will create a counter:
"c := Counter new."
Then, it will assign the value "anInteger"
to the instance variable "count",
and it will be written in this Counter.
So, let us save this.
Press Proceed.
And you can see that all of this is green.
We still have some code there that is not updated,
but if I go back... There it is.
Everything is understood and everything is green.
Now, another thing that we would like to implement
is the increment and decrement.
So, "testCounterIncrement".
We will go there and do "increment; increment".
And we would like "count" to be equal to 7.
So, let us save this.
We will run it.
"Increment" was not understood.
This is normal, since we have not defined it.
So, go over to the Create button.
We would like to create
the method "increment" in the "Counter" class,
under the "operation" protocol.
OK.
"ShouldBeImplemented".
So, what should our "increment" method do?
"count := count + 1".
Save this.
Press Proceed.
Our method is green. That is cool.
Now, we can do the same thing with decrement.
"Decrement", and there, "testCounterDecrement".
We will save it.
Run this method.
We have to create the method "decrement" in the "Counter" class.
Go to "operation".
Press OK.
Decrement.
In the same way we created increment, we will just do "count := - 1".
We will save this.
Press Proceed.
Our test is not right because I have not changed
the actual value I would like it to be equal to.
I would like it to be equal to 3.
So, I can change it right there from the debugger.
Press Cmd+S and press Proceed.
And all our tests are green.
And what you can see is we have only basically developed a test,
and we still have all the back-end functionalities from the "Counter" class.
So, "count", "count:", "decrement", "increment",
and everything is correctly organized.
And all of this was made from within the debugger.
So, this is one of the strengths of Pharo.
The debugger is a work environment
and you can really boost your productivity by doing this.