The next test to implement is as follows:

  • Add a Bookmark, Retrieve using the label

All the tests I wrote previously and the resulting implementation focused on whether or not the count was correct as bookmarks were added and removed from the collection. This test demands that the collection return a bookmark using the label. Let's look at the test:

[Test]
public void AddABookmarkRetrieveWithLabel()
{
   collection.Add(exampleDotComLabel, exampleDotComUri);

   
Uri
actualUri = collection[exampleDotComLabel];
   Assert.AreEqual(exampleDotComUri, actualUri);
}

To implement the retrieval I used an Indexer. When I compile this test it fails because I have not defined the Indexer in the BookmarkCollection. Here's the first implementation:

public Uri this[string label]
{
   get
 
   {
      return null
   }
}

This compiles and when I run the test it fails because a value was expected and <(null)> was returned. To tell the truth I am not really surprised the method returns null. The question at hand, now that we have a failing test, is what the implementation choice should be. It seems pretty clear from the existing tests that we have to store more than one bookmark. However, we could pass this test without using a collection by simply storing the last Uri entered. (If you don't believe me you should give it a try.)  This seems at the first glance to be the simplest implementation that could work. That said, given that we have existing tests that add more than one bookmark I am going to go ahead and store the bookmarks in a Dictionary. A number of people who are familiar with TDD are probably saying that the step I am taking is too big and I am anticipating future tests. I believe this is not the case, I have existing tests that add more than 1 bookmark and I need to store the bookmarks for retrieval. At this point we'll see how it plays out as we add more tests. Here is the smallest amount of code I needed to write to get my test to pass:

private Dictionary<string, Uri> dictionary = new Dictionary<string, Uri>();

public void Add(string label, Uri site)
{
   dictionary[label] = site;
}

public Uri this[string label]
{
   get
 
   {
      return dictionary[label]
   }
}

When I compile this and run the test it passes but a number of other tests fail because the Add method is no longer incrementing the count member variable. This is an example of how the existing tests keep the code in check while we add new functionality. Here is the BookmarkCollection modified to use the Dictionary class:  

public class BookmarkCollection
{
   private Dictionary<string, Uri> dictionary = new Dictionary<string, Uri
>();

   public int Count
   {
      get { return
dictionary.Count; }
   }

   public void Add(string label, Uri site)
   {
      dictionary[label] = site; 
   }

   public void Remove(string label)
   {
      dictionary.Remove(label);
   }

   public Uri this[string label]
   {
      get
 
      {
         return
dictionary[label]; 
      }
   }
}

Reflecting on this implementation it looks like I could have just used a Dictionary instead of creating a BookmarkCollection. Let's see how long this plays out. The test for next time:

  • Retrieve a Bookmark that is not in the collection, return null

Until next time...