• Blog
  • Archive
  • About
  • Contact
Sign in

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Sites I've visited recently

  • Twitter
  • Facebook
  • Philip Wildenstam
  • Ninetech - Affärsnytta med IT
  • JetBrains ReSharper
  • Vimeo
  • dnrTV!
  • YUI Theater
  • BlogEngine.Net

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • TDD
  • Testing
  • Unit testing
  • Unity
  • User tip

Five most recent posts

  • Prime Factors Kata in C#
  • iPhone developer
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Isolate your code from ASP.NET with Moles Isolation Framework
  • Moles Isolation Framework from Microsoft to be compared with TypeMock Isolator

Tag cloud

  • agile
  • ajax
  • asp.net
  • asp.net 3.5
  • asp.net mvc 2
  • bdd
  • blog
  • blogengine.net
  • c#
  • cocoa touch
  • code kata
  • correction
  • css
  • dbc
  • dependency injection
  • design by contract
  • dom
  • douglas crockford
  • fakes
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • httpcontext
  • humble dialog box
  • inversion of control
  • ioc container
  • iphone
  • iphone os
  • iso 8601
  • isolation
  • isolation framework
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • klarsynt
  • live template
  • metaweblog api
  • microsoft research
  • mocks
  • model-view-presenter
  • moles
  • mvp
  • ninetech
  • objective-c
  • patterns & practices
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • roy osherove
  • stubs
  • syntax
  • syntax highlighter
  • tdd
  • tdd masterclass
  • test coverage
  • testing
  • typemock
  • types
  • unit test
  • unity
  • unity 2.0
  • update
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

Recent comments

  • Code Kata Cast (10)
    Rickard wrote: @Marcus Eklund Classical music is royalty free sin… [More]
  • Code Kata Cast (10)
    Marcus Eklund wrote: http://creativecommons.org/ is a good place. Cl… [More]
  • Code Kata Cast (10)
    Rickard wrote: @Johan Lindfors Thanks! @Andrea I use TestDrive… [More]
  • Code Kata Cast (10)
    Marcus Eklund wrote: Nice one Rickard, A bit quiet though :P Used to t… [More]
  • Code Kata Cast (10)
    Andrea wrote: Nice one You can use the R# test runner and assig… [More]
<< Driving out a correct implementation of ISO week numbers using TDD #3 | JavaScript resources >>

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

Tuesday, 18 March 2008 23:59 by Rickard
  1. Abstract 
  2. Refactoring
  3. Test coverage 

Proceeding with a solution there is no way of changing the implementation of the GregorianCalendar class to get the correct behavior so we have to go about this another way. One is to subclass the GregorianCalendar and override the GetWeekOfYear method. Another is to implement the GetWeekOfYear in a class totally separate from the .NET framework. I think that the first one is the way to go since the error that we're trying to correct is in the framework itself.

So, first we alter the help method in the test case to use our sub classed IsoCalendar (which we haven't written yet).

So, we are not in a compile state yet so lets do the simplest thing to get this running. I decided to subclass the GregorianCalendar class since what is wrong is the GetWeekOfYTear method of that class. I basically override that method like below.

We run the test a get a red bar. Fine, but now lets go ahead and implement the algorithm. However, this has been done over and over with varying elegantness. My favorite is the work of programmer and mathematician Julian Bucknall. His original implementation can be found at his blog. So, instead of returning -1 I call the GetIsoWeek method from Bucknall's implementation and take the modulus of 100 (since the result from the algorithm is of the form YYYYWW). We run the test and we get a green bar!

 

Now when we get a green bar there's just one more thing to add. The ISO 8601 week should only be returned when the parameters rule and firstDayOfWeek is set properly according to the documentation. In all other cases the GregorianCalendar implementation works just fine. We fix this with a guard clause at the top and we're done.

Summary

During this series we covered test coverage for the faulty implementation of weeks according to ISO 8601. We used Test-Driven Development to provide a fix using Julian Bucknall's really smart implementation. Next we should take a look at refactoring to design patterns.

Tags:   tdd, test coverage, julian bucknall
Categories:   .NET | C# 2.0 | TDD
Actions:  
Share | |

Related posts

Driving out a correct implementation of ISO week numbers using TDD #3 Abstract Refactoring Our task at hand is to get as much coverage as possible when it comes t...Driving out a correct implementation of ISO week numbers using TDD: Refactor Abstract So, now we make up another test. Looking at the calendar we see that new years eave 20...Abstract: Driving out a correct implementation of week numbers using TDD The implementation of weeks according to ISO 8601 (used e.g. in Sweden) is faulty in .NET Fra...
 
Copyright © 2008-2009 rickardnilsson.net