Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Testing the Tableau: Removing Cards5:37 with Ben Deitch
In this video we'll write a test to make sure that our 'removeCards' function works correctly!
So far, so good. 0:00 Now, let's keep this streak alive with our removeCards function. 0:01 To test this function, 0:04 we'll start with a TableauPile that already contains a few cards. 0:06 Then we'll remove some of the cards. 0:10 And finally, we'll check to make sure that we've got the right cards remaining. 0:12 So first, Let's copy and paste in the arrange, act, assert comments. 0:16 And then, let's start in the arrange section by declaring our Tableau Pile. 0:20 And let's just copy this one to keep things simple. 0:24 Then let's delete the king of spades. 0:31 And instead, let's pass in the five of clubs, four of diamonds and 0:35 three of spades. 0:40 And remember, for numbered cards their index is one less. 0:42 So this is going to be a card with a value of four, and 0:46 a suit of clubs for the five of clubs. 0:50 Then,card three and diamonds for 0:54 the four of diamonds and card two and spades. 0:57 For the three of spades. 1:03 Then, in the act section. 1:05 Let's remove the cards at index one or higher by typing, 1:08 tableauPile.removeCards and pass a tappedIndex of one. 1:14 This should leave us with only the five of clubs. 1:19 So in the insert section, let's make sure that's true. 1:22 Assert equals, and we're expecting to have a mutable list 1:28 of, The five of clubs. 1:33 And let's not forget to pass in true for the third parameter to this card. 1:40 Because if this is the only card left, it should be face up. 1:45 And this should be equal to our tableauPile.cards. 1:49 And, of course, the last step is to run it. 1:57 So, let's hit the Run button and okay, 1:59 we got an index out of bounds exception, index 2 size 2:05 2 and it's happening on this line. 2:10 So it looks like we're trying to remove a card that's no longer in our list. 2:15 Let's see why that's happening by walking through our test. 2:21 We start off by creating a tabloeauPile. 2:25 And we give it the five of clubs, the four of diamonds and the three of spades. 2:27 Then we want to remove all the cards at index one or higher. 2:32 So we call the remove cards function on our tableauPile and press on one. 2:36 Inside this function, we loop from tappedIndex. 2:40 Two cards last index in this case will be looping from one 2:45 to two, because the last index is two. 2:51 So first, will call card start or move at with a one for the index, which will take 2:53 out the four of diamonds and leave us with the five of clubs and the three of spades. 3:00 Then we'll loop and call cards are removedAt, 3:05 again, but this time with a two for the index, 3:08 which is a huge problem because right now our list is just two cards. 3:12 So there is no index two. 3:18 There is just zero and one. 3:20 Instead, what we should be doing is removing the card at the tappedIndex 3:22 on each loop. 3:27 This way, we won't be trying to remove cards that don't exist. 3:29 So let's replace i with tappedIndex. 3:32 And then let's Run our test again. 3:36 And lame, it still fails. 3:44 But, at least, now it's failing for a different reason. 3:47 This time, it's because we're comparing two different cards. 3:50 Now, don't get me wrong. 3:53 These two cards should have equal values, equal suits and 3:55 equal phase of properties, but they're still different objects. 4:00 It's like comparing the ace of spades from one deck, 4:04 to the ace of spades on another deck. 4:07 They may look the same, but it's clearly two different cards. 4:08 What we really want to check is not that we have two of the exact same card object, 4:12 but instead that we have two cards that look the same. 4:17 We can do that by checking that the properties match between the cards. 4:20 They should have the same value, suit, and a face up properties. 4:25 Let's go over to our Card class, and right before the word class, let's type data. 4:29 In Kotlin, when we have a class that's just for 4:37 holding data, we can declare it as a data class. 4:40 Data classes have a couple cool properties that make them just what we need. 4:43 For starters, we get a little bit more readable to string method, 4:48 which is nice, but also the equals function of a data class 4:51 is based entirely on its constructor arguments. 4:56 So, now when we check for 4:59 equality with our card class, it will only be based on these three properties. 5:01 As long as the cards look the same, they'll be considered equal. 5:07 Now, let's run the test again. 5:10 And perfect, we passed all our tests. 5:15 Cool. 5:18 I'll admit, I am feeling a little bit better about our game now. 5:19 We found an issue and we sort of that out. 5:23 Found another issue and sort of that went out to. 5:25 Nothing but blue skies from here, right? 5:28 Coming up, we'll get a little more familiar with the game of solitaire 5:31 by linking all of this together and our game model. 5:34
You need to sign up for Treehouse in order to download course files.Sign up