1 00:00:00,160 --> 00:00:00,200 "Xtreme TDD" 2 00:00:01,560 --> 00:00:01,600 Quentin Ducasse. 3 00:00:04,320 --> 00:00:06,200 -Hi. In this redo of week 2, 4 00:00:06,520 --> 00:00:08,640 what we are going to do is to basically implement 5 00:00:08,960 --> 00:00:12,880 the same Counter as we did in week 1, but exclusively from the debugger 6 00:00:13,200 --> 00:00:15,240 and from a test-driven perspective. 7 00:00:15,560 --> 00:00:17,400 So, let us see what this means. 8 00:00:17,720 --> 00:00:19,400 We will go and open System Browser. 9 00:00:19,720 --> 00:00:23,360 What we will do is define the "Test" class first. 10 00:00:23,680 --> 00:00:26,560 This is the idea of a test-driven development. 11 00:00:26,880 --> 00:00:28,400 So, save that. 12 00:00:28,720 --> 00:00:34,720 Now, we will write our first test, so "testCounterIsSetAndRead". 13 00:00:35,040 --> 00:00:37,200 What will this test do? It will create a Counter, 14 00:00:37,520 --> 00:00:40,400 so temporary variable "c", 15 00:00:40,720 --> 00:00:42,400 to which we will assign "Counter new". 16 00:00:42,720 --> 00:00:47,800 Then, we will assign 7 to the value of the "count" instance variable. 17 00:00:48,120 --> 00:00:54,440 And then, we will check that "c count" is indeed equal to 7. 18 00:00:54,760 --> 00:00:56,960 We can see that "Counter" is emphasized. 19 00:00:57,280 --> 00:01:00,640 We have not defined "count:", we have not defined "count". 20 00:01:00,960 --> 00:01:04,200 Nothing should work, basically, but we will see. 21 00:01:05,280 --> 00:01:08,320 "Unknown variable: Counter, please correct." 22 00:01:08,640 --> 00:01:11,320 "Counter" would like to define a new class, 23 00:01:11,640 --> 00:01:15,840 would like to define it with the "count" instance variable. 24 00:01:16,160 --> 00:01:17,240 And press OK. 25 00:01:17,560 --> 00:01:20,760 You can see that we have got our "Counter" class that is created there 26 00:01:21,080 --> 00:01:22,840 with the instance variable "count". 27 00:01:23,160 --> 00:01:24,520 And now, if we run our test, 28 00:01:24,840 --> 00:01:29,360 we can see that the method "count:" has not been understood. 29 00:01:29,680 --> 00:01:30,800 So, this is the debugger 30 00:01:31,120 --> 00:01:33,080 and basically, this is the execution stack, 31 00:01:33,400 --> 00:01:36,000 this is the body of the method that is being executed, 32 00:01:36,320 --> 00:01:38,720 and those are the different variables. 33 00:01:39,040 --> 00:01:41,800 So, if I go to "a Counter", you can see that... 34 00:01:42,120 --> 00:01:43,360 This is "a Counter"... "Raw"... 35 00:01:43,680 --> 00:01:45,600 You can see that "count" is equal to "nil", 36 00:01:45,920 --> 00:01:47,080 because we have not set it up. 37 00:01:47,400 --> 00:01:50,320 So, we can click on the Create button 38 00:01:50,640 --> 00:01:52,320 to create the method "count:". 39 00:01:52,640 --> 00:01:53,640 Go to Counter, 40 00:01:54,800 --> 00:01:58,800 and what we will do is go to "accessing". 41 00:01:59,120 --> 00:01:59,880 OK. 42 00:02:00,960 --> 00:02:03,920 So, this is the set I would like, so this is fine. 43 00:02:04,240 --> 00:02:05,760 I can press Proceed. 44 00:02:06,080 --> 00:02:09,600 Then, it does not understand the next "count", the getter. 45 00:02:09,920 --> 00:02:12,160 So, we can create it. Counter. 46 00:02:12,480 --> 00:02:15,880 Once again, in the "accessing" protocol. 47 00:02:16,200 --> 00:02:17,360 OK. 48 00:02:17,680 --> 00:02:19,960 "Count", well written "count". That is great. 49 00:02:20,280 --> 00:02:21,560 So, Proceed. 50 00:02:21,880 --> 00:02:24,640 The test is red, but if we click on it, it is green. 51 00:02:24,960 --> 00:02:25,880 So, perfect. 52 00:02:26,200 --> 00:02:29,240 And if we go and have a look at the "Counter" class, 53 00:02:29,560 --> 00:02:33,600 we now have our Setter and Getter for the "count" instance variable, 54 00:02:33,920 --> 00:02:34,960 so that is perfect. 55 00:02:35,280 --> 00:02:37,920 Now, if you remember correctly, we had a "class" method 56 00:02:38,240 --> 00:02:41,040 that had this "choose the starting value", 57 00:02:41,360 --> 00:02:45,320 so we used it as "startingAt: 5". 58 00:02:45,640 --> 00:02:47,280 We will not need this line, 59 00:02:47,600 --> 00:02:52,160 but we would like to assert that the count is indeed equal to 5. 60 00:02:52,480 --> 00:02:53,600 So, let us save this. 61 00:02:53,920 --> 00:02:56,160 It does not understand the "startingAt" message, 62 00:02:56,480 --> 00:02:58,280 but we will define it right now. 63 00:02:58,600 --> 00:03:02,000 Click on it. "Instance of 'Counter' class did not understand 'startingAt'". 64 00:03:02,320 --> 00:03:05,920 So, let us create it in the "Counter" class, 65 00:03:06,240 --> 00:03:11,960 under the "instance creation" protocol. 66 00:03:12,280 --> 00:03:13,040 Press OK. 67 00:03:13,360 --> 00:03:15,720 And we will have to implement it, so basically, 68 00:03:16,040 --> 00:03:18,120 what will "startingAt" do? 69 00:03:18,440 --> 00:03:21,320 It will create a counter: 70 00:03:21,640 --> 00:03:25,240 "c := Counter new." 71 00:03:25,560 --> 00:03:28,920 Then, it will assign the value "anInteger" 72 00:03:29,240 --> 00:03:31,480 to the instance variable "count", 73 00:03:31,800 --> 00:03:33,840 and it will be written in this Counter. 74 00:03:34,160 --> 00:03:35,720 So, let us save this. 75 00:03:36,040 --> 00:03:37,520 Press Proceed. 76 00:03:37,840 --> 00:03:39,640 And you can see that all of this is green. 77 00:03:39,960 --> 00:03:43,360 We still have some code there that is not updated, 78 00:03:43,680 --> 00:03:45,720 but if I go back... There it is. 79 00:03:46,040 --> 00:03:48,640 Everything is understood and everything is green. 80 00:03:48,960 --> 00:03:51,640 Now, another thing that we would like to implement 81 00:03:51,960 --> 00:03:54,080 is the increment and decrement. 82 00:03:54,400 --> 00:03:57,400 So, "testCounterIncrement". 83 00:03:57,720 --> 00:04:03,280 We will go there and do "increment; increment". 84 00:04:03,600 --> 00:04:06,360 And we would like "count" to be equal to 7. 85 00:04:06,680 --> 00:04:08,120 So, let us save this. 86 00:04:08,440 --> 00:04:09,440 We will run it. 87 00:04:10,440 --> 00:04:12,080 "Increment" was not understood. 88 00:04:12,400 --> 00:04:14,400 This is normal, since we have not defined it. 89 00:04:14,720 --> 00:04:16,600 So, go over to the Create button. 90 00:04:16,920 --> 00:04:17,680 We would like to create 91 00:04:18,000 --> 00:04:20,000 the method "increment" in the "Counter" class, 92 00:04:20,320 --> 00:04:22,720 under the "operation" protocol. 93 00:04:23,040 --> 00:04:24,440 OK. 94 00:04:24,760 --> 00:04:25,680 "ShouldBeImplemented". 95 00:04:26,000 --> 00:04:28,000 So, what should our "increment" method do? 96 00:04:28,320 --> 00:04:32,080 "count := count + 1". 97 00:04:32,400 --> 00:04:33,360 Save this. 98 00:04:33,680 --> 00:04:34,840 Press Proceed. 99 00:04:35,160 --> 00:04:37,080 Our method is green. That is cool. 100 00:04:37,400 --> 00:04:39,960 Now, we can do the same thing with decrement. 101 00:04:41,440 --> 00:04:45,760 "Decrement", and there, "testCounterDecrement". 102 00:04:46,080 --> 00:04:47,040 We will save it. 103 00:04:47,360 --> 00:04:48,600 Run this method. 104 00:04:49,800 --> 00:04:53,560 We have to create the method "decrement" in the "Counter" class. 105 00:04:53,880 --> 00:04:55,480 Go to "operation". 106 00:04:57,040 --> 00:04:58,280 Press OK. 107 00:04:58,600 --> 00:04:59,440 Decrement. 108 00:04:59,760 --> 00:05:05,760 In the same way we created increment, we will just do "count := - 1". 109 00:05:07,160 --> 00:05:08,160 We will save this. 110 00:05:08,480 --> 00:05:09,440 Press Proceed. 111 00:05:09,760 --> 00:05:13,000 Our test is not right because I have not changed 112 00:05:13,320 --> 00:05:17,440 the actual value I would like it to be equal to. 113 00:05:18,680 --> 00:05:21,240 I would like it to be equal to 3. 114 00:05:21,560 --> 00:05:23,440 So, I can change it right there from the debugger. 115 00:05:23,760 --> 00:05:26,480 Press Cmd+S and press Proceed. 116 00:05:26,800 --> 00:05:28,840 And all our tests are green. 117 00:05:29,160 --> 00:05:33,840 And what you can see is we have only basically developed a test, 118 00:05:34,160 --> 00:05:38,160 and we still have all the back-end functionalities from the "Counter" class. 119 00:05:38,480 --> 00:05:41,000 So, "count", "count:", "decrement", "increment", 120 00:05:41,320 --> 00:05:44,440 and everything is correctly organized. 121 00:05:44,760 --> 00:05:47,320 And all of this was made from within the debugger. 122 00:05:47,640 --> 00:05:49,680 So, this is one of the strengths of Pharo. 123 00:05:50,000 --> 00:05:51,320 The debugger is a work environment 124 00:05:51,640 --> 00:05:55,640 and you can really boost your productivity by doing this.