Driving out a correct implementation of ISO week numbers using TDD #3

  1. Abstract
  2. Refactoring
Our task at hand is to get as much coverage as possible when it comes to errors in the implementation. The ideal coverage is one where all dates are tested. The problem is that the number of dates is infinite. This is of course always a an issue when testing software but it may be more obvious here. If we test all dates there will we a great deal of dates that we know are correct. It should suffice to test only the dates that are incorrect. However, this number is also infinite. We will have to limit our coverage but still get as much breadth as possible. Basically we need to get back to the definition.

According to the ISO standard, in the period 4 January - 28 December the week number is always the same as the Gregorian week and the same also apply for all Thursdays. Thus, the mentioned dates can be safely excluded in our test coverage. Though we still have an infinite number of dates to cover, we are getting there. Finally, we can limit our test coverage to a set of years that can be reasonable argued to appear in the application which we are implementing.

To simplify it the resulting dates are non Thursdays 1-3 January and 29-31 December every year from 2005 - 2015.

Using the calendar in Outlook I compile a matrix of the week numbers in the year interval. The dates that are grayed are Thursdays which should not be part of the test.

  01-jan 02-jan 03-jan 29-dec 30-dec 31-dec
2005 53 53 1 52 52 52
2006 52 1 1 52 52 52
2007 1 1 1 52 52 1
2008 1 1 1 1 1 1
2009 1 1 1 53 53 53
2010 53 53 53 52 52 52
2011 52 52 1 52 52 52
2012 52 1 1 52 52 53
2013 1 1 1 52 1 1
2014 1 1 1 1 1 1
2015 1 1 1 53 53 53

The matrix can be thought of as a set of { date, week number } pairs which can easily be implemented in code.

I create a test which iterates over the set and makes assertions that the correct week was generated by the calendar. It turns out however, that only eleven of the dates generate a wrong week number. I refactor the code to only include the weeks that are wrong. I get a red bar and I see another opportunity for refactoring, the previous two tests can be integrated with this test by just adding the date - week pair to the set. The following is the resulting test:

   1: [Test]
   2: public void GetWeekOfYear_ShouldPass()
   3: {
   4:     Dictionary<DateTime, int> weeks = 
   5:         new Dictionary<DateTime, int>();
   6:     weeks.Add(new DateTime(2001, 12, 31), 1);
   7:     weeks.Add(new DateTime(2003, 12, 31), 1);
   8:     //etc...
   9:     
  10:     StringBuilder error = new StringBuilder();
  11:     foreach (KeyValuePair<DateTime, int> week in weeks)
  12:     {
  13:         if (week.Value != GetIsoWeek(week.Key))
  14:         {
  15:             error.AppendFormat(
  16:                 "\n{0}: Expected: {1}, Found: {2}",
  17:                 week.Key, week.Value, GetIsoWeek(week.Key));
  18:         }
  19:     }
  20:     
  21:     Assert.IsEmpty(error.ToString());
  22: }

Now that we have a test that fails we can come up with a solution to get the test to pass.

Loading
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.