Sign in
  • Blog
  • Archive
  • About
  • Contact

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

Rtur.net / ASP.NET Forums / Karstad .NET User Group / JetBrains ReSharper / Scott Hanselman / dnrTV! / MSDN Radio / MIX'08 | Sessions / BlogEngine.Net / YUI Theater

Categories

  • RSS feed for .NET.NET
  • RSS feed for ASP.NET 2.0ASP.NET 2.0
  • RSS feed for BlogEngine.NETBlogEngine.NET
  • RSS feed for C# 2.0C# 2.0
  • RSS feed for C# 3.0C# 3.0
  • RSS feed for CSSCSS
  • RSS feed for Design by ContractDesign by Contract
  • RSS feed for Design PatternsDesign Patterns
  • RSS feed for JavaScriptJavaScript
  • RSS feed for TDDTDD
  • RSS feed for User tipUser tip

Five most recent posts

  • ReSharper User tip #2: Refactor rename namespace
  • Building a Photo Album widget for BlogEngine.NET
  • Applying stylesheets dynamically with jQuery
  • ReSharper User tip: Refactor magical strings to variable
  • Blogging with MS Word 2007

Tag cloud

  • ajax
  • blog
  • blogengine.net
  • c#
  • css
  • dbc
  • design by contract
  • dom
  • douglas crockford
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • humble dialog box
  • iso 8601
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • metaweblog api
  • model-view-presenter
  • mvp
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • syntax
  • syntax highlighter
  • tdd
  • test coverage
  • types
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

Recent comments

  • Syntax Highlighter Release 0.2 Beta (9)
    Rickard wrote: @Tim: Thanks for the positive feedback. To ans… [More]
  • Syntax Highlighter Release 0.2 Beta (9)
    Tim wrote: Do you have this in a working for that does not re… [More]
  • Syntax highlighting in BlogEngine.NET (9)
    darren wrote: thanks for this - got it up and running no problem… [More]
  • Syntax Highlighter Release 0.2 Beta (9)
    Rickard wrote: Line numbers are not supported in the current rele… [More]
  • Syntax Highlighter Release 0.2 Beta (9)
    Jack wrote: How can i show line numbers? [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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   tdd, test coverage, julian bucknall
Categories:   .NET | C# 2.0 | TDD
Actions:   E-mail | Permalink | Kick it! | DZone it! | del.icio.us | Comments (0) | Comment RSSRSS comment feed

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...
Saving the comment

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



 
Copyright © 2008 rickardnilsson.net