Adopt, adapt, improve

credit: Wikipedia
Adopt, adapt and improve, this is famous motto of Round Table club. Round table is is a social networking and charitable organisation for men in their 20s, 30s and early 40s, founded in Norwich, in 1927.

In this post I would like to explain how I applied this motto on my testing problem that I had with calculating number of week in two standards: us and iso-8601 (note that iso-8601 standard is still not applied in US and that world is not going to end because of that fact).

In my previous blog post Iron Ruby magic in action I made a statement that week number according to US standard could not be calculated using plain Ruby. As this week I moved my testing to plain Ruby, I made a research on Internet using Google and discovered that I was wrong.

On epochconverter.com I found instructions how to do that in Ruby language:


week_number = Time.now.strftime("%U")

Replace Time.now with Time.local(year,month,day) for other dates.

Formats:

%U - Week number of the year, starting with the first Sunday as the first day of the first week (00..53)
%V - Week number of year according to ISO-8601 (01..53)
%W - Week number of the year, starting with the first Monday as the first day of the first week (00..53)

For US standard, we need to use %U format, because Sundays are start of week. But, I needed to tweak a little bit output of that method. Here is irb session that explains the reason:





Date 2010-01-01 is good testing example, because week number should be from previous year, 52. But Ruby returns zero week number! Here is gist that presents us week number method in plain ruby:





Note that I typed this code in irb session, I did not use famous programmer technique, copy/paste. In order to adopt, adapt and improve as tester, you should also type your test code and reduce copy/paste as much as possible.

Labels: ,