Driving out a correct implementation of ISO week numbers using TDD: Refactor


So, now we make up another test. Looking at the calendar we see that new years eave 2001 should also belong to the first week of 2002 according to ISO 8601. We write the test much like the previous test and we get another red bar. Now its time to refactor. We need to remove the duplicated code in the two tests. First we start with extract method which results in the following method: 

   1: private static int GetIsoWeek(int year, int month, int day) 
   2: {
   3:     GregorianCalendar calendar = new GregorianCalendar();
   4:     DateTime date = new DateTime(year, month, day, calendar);
   5:     return calendar.GetWeekOfYear(date,
   6:             CalendarWeekRule.FirstFourDayWeek,
   7:             DayOfWeek.Monday);
   8: }

Now, we can refactor the tests using the new method like this:

   1: private const string message =
   2:     "The week was not the expected for ISO 8601!";
   4: [Test]
   5: public void GetWeekOfYear_20011231_Week01()
   6: {
   7:     Assert.AreEqual(1, GetIsoWeek(2001, 12, 31), message);
   8: }
  10: [Test]
  11: public void GetWeekOfYear_20031231_Week01()
  12: {
  13:     Assert.AreEqual(1, GetIsoWeek(2003, 12, 31), message);
  14: }

We still get two red bars thus we can be insured that we haven't changed any functionality. The next step should be to come up with another test to get more coverage of the errors in the implementation.
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.