1 00:00:00,330 --> 00:00:04,290 And this solution, we're going to write some unit tests, remember, the first step and test driven 2 00:00:04,290 --> 00:00:09,660 development is to identify meaningful test cases and for the sake of expediency, already did that part 3 00:00:09,660 --> 00:00:10,180 for you. 4 00:00:10,920 --> 00:00:15,090 The first thing we're going to test is if the simulation can find the placeholder in each play. 5 00:00:15,510 --> 00:00:20,130 So inside game test Java, I'm going to write a unit test name to get placeholder test. 6 00:00:22,920 --> 00:00:24,930 Public void get placeholder test. 7 00:00:34,830 --> 00:00:39,510 And before we can do anything, we have to set up a before method that sets up our game object. 8 00:00:46,450 --> 00:00:49,570 Inside the method set up the game object, as you did in main. 9 00:00:59,240 --> 00:01:03,700 Let me do some cleaning up, remove the two variables and put the objects in directly. 10 00:01:34,810 --> 00:01:38,470 OK, now inside the unit test, we're going to assert equals. 11 00:01:43,800 --> 00:01:48,420 This test expects the value chaser when we call game get placeholder. 12 00:01:52,810 --> 00:01:58,150 In person to it, a play that equals the placeholder chaser chaser gets the next pass. 13 00:02:03,500 --> 00:02:09,770 And now inside game Java write code to make the test fail, I'll get a function that returns a string 14 00:02:09,770 --> 00:02:11,090 called Get Placeholder. 15 00:02:13,560 --> 00:02:18,480 And the unit test implies we need to pass in a string into our function again, it's really nice that 16 00:02:18,480 --> 00:02:20,580 our test determines the code that we write. 17 00:02:22,960 --> 00:02:25,540 And for now, we'll make the test failed by returning hello. 18 00:02:36,330 --> 00:02:37,810 The test fails now. 19 00:02:37,860 --> 00:02:39,430 We write code to make it pass. 20 00:02:39,840 --> 00:02:45,660 I told you to research how to get a string between two characters because we need to get the string 21 00:02:45,660 --> 00:02:47,500 between each pointy brackets. 22 00:02:48,020 --> 00:02:52,710 I'm not going to pretend like I knew how to do this off the top of my head because I remember a good 23 00:02:52,710 --> 00:02:54,510 developer doesn't memorize code. 24 00:02:54,760 --> 00:02:59,760 A good developer knows how to use the Internet to read documentation and find resources. 25 00:03:00,690 --> 00:03:02,820 And I'm going to show you how I did my research. 26 00:03:03,060 --> 00:03:06,540 I just looked up how to get a string between two characters. 27 00:03:08,050 --> 00:03:11,800 OK, the first link looks good, I'll have a look around for the best answer. 28 00:03:14,820 --> 00:03:20,730 I don't like this one just because I don't want to have to import an Apache Commons library to perform 29 00:03:20,730 --> 00:03:21,700 a simple task. 30 00:03:22,080 --> 00:03:24,480 There must be something that we can just use out of the box. 31 00:03:25,200 --> 00:03:26,830 OK, this answer looks really good. 32 00:03:26,850 --> 00:03:31,740 It seems that we can get a substring that starts at a certain inducts and ends at another. 33 00:03:32,520 --> 00:03:37,020 Normally at this point, I would look up the documentation just to have a little bit more information 34 00:03:37,020 --> 00:03:37,840 about this method. 35 00:03:38,280 --> 00:03:42,180 This method is from the string class because they're calling it from a string object. 36 00:03:42,570 --> 00:03:45,420 So I can just look up string documentation Java. 37 00:03:48,990 --> 00:03:50,100 JDK 11. 38 00:03:53,340 --> 00:03:56,790 I'll look up Substring and keep going until I find my method. 39 00:04:01,130 --> 00:04:03,930 Let me add a bracket to filter out some results and perfect. 40 00:04:03,950 --> 00:04:04,520 Here it is. 41 00:04:05,990 --> 00:04:11,300 So the substring starts at the index, you specify, and the end index is whatever you passan minus 42 00:04:11,300 --> 00:04:11,630 one. 43 00:04:12,530 --> 00:04:13,860 I know exactly how this works. 44 00:04:13,880 --> 00:04:15,800 Now I'm going to do it incorrectly. 45 00:04:15,800 --> 00:04:20,779 The first time for the sake of making a test that fails, I'll go back to the method and from the play 46 00:04:20,779 --> 00:04:23,000 string, I'm going to get the substring. 47 00:04:24,870 --> 00:04:26,880 That starts at the index of. 48 00:04:29,070 --> 00:04:34,560 The pointy bracket character and ends at the index of the other pointy bracket character. 49 00:04:47,840 --> 00:04:53,570 Run the test now, and it fails an expected chasers, but we got back chasers with a pointy bracket 50 00:04:53,570 --> 00:04:54,170 in the beginning. 51 00:04:56,680 --> 00:05:02,470 Look back at the documentation, it says the substring is starts at the index, we specify, and we 52 00:05:02,470 --> 00:05:05,830 want to start at the index that comes after the bracket. 53 00:05:08,150 --> 00:05:09,380 So we'll say plus one. 54 00:05:17,000 --> 00:05:17,750 Run the test. 55 00:05:19,780 --> 00:05:20,830 And now it works. 56 00:05:24,830 --> 00:05:29,690 Now, you might be wondering by that logic, shouldn't the substring also include the second pointy 57 00:05:29,690 --> 00:05:30,110 bracket? 58 00:05:30,680 --> 00:05:32,660 But look back at the documentation. 59 00:05:33,260 --> 00:05:36,790 Where the substring ends is whatever you pass in minus one. 60 00:05:37,070 --> 00:05:41,100 So our substring stops one index before the one we specified. 61 00:05:41,540 --> 00:05:46,310 This is why it's really important to read the documentation if you're not sure how a method works. 62 00:05:46,340 --> 00:05:50,060 Just take a few seconds to check out the documentation anyways. 63 00:05:50,060 --> 00:05:52,730 We spend enough time on this onto the second test case. 64 00:05:53,090 --> 00:05:57,350 We need to check if the simulation can replace the placeholder that it finds with a value. 65 00:06:00,890 --> 00:06:05,510 So inside game test Java, I'm going to write a unit test named to replace Placeholder Test. 66 00:06:16,340 --> 00:06:17,480 We'll make an assertion. 67 00:06:20,800 --> 00:06:24,520 And this test expects the sentence Katie gets the next pass. 68 00:06:27,270 --> 00:06:30,780 From a function we're going to call game dot, replace Placeholder. 69 00:06:32,770 --> 00:06:35,770 That receives the play Chaser gets the next pass. 70 00:06:45,070 --> 00:06:48,340 We're going to tell replace Placeholder what the placeholder is. 71 00:06:54,240 --> 00:06:57,450 And the value we want to replace the placeholder with Katie. 72 00:07:03,160 --> 00:07:06,380 And now inside game Java write code to make the test fail. 73 00:07:06,400 --> 00:07:09,880 I'm going to create a function that returns a string called Replace Placeholder. 74 00:07:12,950 --> 00:07:16,820 And the unit test implies we need to pass three parameters into our function. 75 00:07:21,840 --> 00:07:22,650 String play. 76 00:07:23,670 --> 00:07:26,730 String, placeholder and string value. 77 00:07:27,760 --> 00:07:32,110 Again, pretty cool that our test determines the code that we should write, and for now we'll make 78 00:07:32,110 --> 00:07:33,370 the test fail by returning. 79 00:07:33,370 --> 00:07:33,730 Hello? 80 00:07:43,970 --> 00:07:47,540 OK, the test fails now we need to write code to make it pass. 81 00:07:47,870 --> 00:07:51,970 I told you to research how to replace a string in a placeholder with another value. 82 00:07:52,490 --> 00:07:53,600 So I'll do just that. 83 00:07:53,600 --> 00:07:55,670 How to replace part of a string in Java. 84 00:08:09,500 --> 00:08:10,640 Take a look around. 85 00:08:13,490 --> 00:08:14,780 Wow, I like this answer. 86 00:08:14,960 --> 00:08:19,690 It's short and simple, let's just make sure we look up what replaced does exactly before we use it. 87 00:08:22,850 --> 00:08:24,290 I'll go to the documentation. 88 00:08:29,500 --> 00:08:36,850 Command control for Windows and look up, replace and cool takes two arguments and replaces all the 89 00:08:36,850 --> 00:08:39,820 occurrences of the first argument with the second one. 90 00:08:42,250 --> 00:08:47,050 Fair enough, back to the code from the play String Alchol Replace. 91 00:08:52,550 --> 00:08:56,630 And we will replace all occurrences of the placeholder with the value. 92 00:09:09,180 --> 00:09:13,710 And the test fails because we also have to replace these two pointy brackets. 93 00:09:19,700 --> 00:09:22,040 So I'm going to add those to the first parameter. 94 00:09:34,270 --> 00:09:35,220 Let's test it out. 95 00:09:40,200 --> 00:09:41,760 And our test passes. 96 00:09:44,760 --> 00:09:46,200 Can this code be any simpler? 97 00:09:46,470 --> 00:09:47,140 Not really. 98 00:09:47,160 --> 00:09:50,850 I think we're done in the next video, we'll pick up the remaining tasks.