Automated regression testing of Chromecast using Cucumber, JRuby and Rukuli

As you may know, i work as a Developer In Test for Media Playout at the BBC. We enable audio and video playout on the web (desktop, tablet and mobile) using our Standard Media Player.

We recently added support for Chromecast, and i want to write a bit about how i automated testing for it.

Chromecast HDMI dongle

Chromecast HDMI dongle

Chromecast is a HDMI dongle that plugs into a normal television and adds connected TV functionality. The neat thing is you can control it from your laptop, tablet or mobile using the media apps that you already know, such as YouTube, Netflix and now BBC iPlayer. Google also provides an API so that new Chromecast apps can be written all the time.

So how do you test a Chromecast? Well, personally i like to test things from an user’s point of view, actually clicking things and observing what happens as a result. But there’s only so far you can take this. I don’t want to rely on a television having a Chromecast being plugged in all the time and on the same network so that i can talk to it.

Though i’ve got to say .. it would be quite awesome to use a television 2 metres wide for my automated regression testing! :p

About to start casting

About to start casting

I didn’t need to test the Chromecast receiver app; that had already been written and tested by the guys in Salford for use by the iOS iPlayer app. I realised, what we actually care about is the communication between our player and the Chromecast. And there is a well documented Chromecast API. So with the help of colleagues Tim Hewitt and Wyell Hanna, i set about creating a fake Chromecast. Pretty much a bit of javascript that would respond in the same way as a real Chromecast, so that we could test how our player would respond to it.

And now i present to you .. the fake Chromecast!

https://gist.github.com/sermoa/10988494

A lot of it is empty methods that are neccessary to the API. But there are some neat tricks here too. I’ll talk you through a few of the more interesting bits.

Firstly we need to load the fake Chromecast into the page. We can achieve this with a @chromecast tag and a Before hook.

Before('@chromecast') do
  $chromecast = Chromecast.new
end

My Chromecast class has an initialize method that inserts the javascript into the page.

class Chromecast

  def initialize
    $capybara.execute_script <<-EOF
      var el=document.createElement("script");
      el.type="text/javascript";
      el.src = "https://gist.githubusercontent.com/sermoa/10988494/raw/82e08c5a29b5689b5e9f3d03c191b8c981102d85/fakeChromecast.js";
      document.getElementsByTagName("head")[0].appendChild(el);
    EOF
    @media_alive = true
  end

end

With this in place, so long as i set up our player with the relevant business logic that says casting is available, when i hover over the player, i get a cast button!

  @chromecast
  Scenario: Chromecast button appears
    Given Chromecast is available
    When I play media
    Then I should see the Chromecast button
A cast button appears!

A cast button appears!

So how does that work? I assure you, i don’t have a Chromecast on the network right now. I’m not using the Google Chrome extension. I’m actually running it in Firefox! What is this voodoo?!!

Have a look at the fakeChromecast.js file:

window.chrome = {};
window.chrome.cast = {};
window.chrome.cast.isAvailable = true;

See that? We’ve set up a Chromecast and said it’s available! Sneaky, hey? It’s that easy, folks! :)

The Standard Media Player will now attempt to set up a session request with its application ID. That’s fine: we’ll happily enable it to do its thing.

window.chrome.cast.SessionRequest = function(a) { }

Obviously a real Chromecast does something rather significant here. But we don’t have to care. This is a fake Chromecast. We only provide the function so that it doesn’t break.

Next, the media player makes a new ApiConfig. It’ll pass the session request it just obtained (in our case it’s null, but that doesn’t matter), and two callbacks, the second being a receiver listener. That’s the important one. We want to convince it that a Chromecast is available, so we trigger this callback with the special string “available”.

window.chrome.cast.ApiConfig = function(a, b, c) {
  c("available");
}

So Chromecast is available. Now suppose the user clicks the button to begin casting. This should request a session.

  @chromecast
  Scenario: Click Chromecast button and connect to a session
    Given I am playing media
    When I click the Chromecast button
    Then I should see Chromecast is connecting
Connecting to Chromecast

Connecting to Chromecast

How did we do this? Easy! The media player requests a session, sending a success callback. Fake Chromecast store a references to that callback – on the window so that we can trigger it any time we like! The callback function is expected to provide a session. We send it a reference to a fake session, which is entirely within our control. Oh it’s all so much fun!

window.chrome.cast.requestSession = function(a, b) {
  window.triggerConnectingToCC = a;
  window.triggerConnectingToCC(window.fakeSession);
}

As the documentation says, “The Session object also has a Receiver which you can use to display the friendly name of the receiver.” We want to do exactly that. We decided to call our fake Chromecast Dave. Because Dave is a friendly name! :)

window.fakeSession = {};
window.fakeSession.receiver = {friendlyName:"dave", volume:{level:0.7}};

I think i found our app expected the session receiver to supply a volume too, so i added that.

The media player does some shenanigans with media that we don’t need to care about, but when it sends a request to load media it passes its callback to trigger when media is discovered by Chromecast. That’s another important one for us to keep, so we store that one. We wait 1 second for a semi-realistic connection time, and then trigger it, passing a reference to .. fake media, woo!

window.fakeSession.loadMedia = function(a, b, c) {
  window.pretendMediaDiscovered = b;
  setTimeout(function() {
    window.pretendMediaDiscovered(window.fakeMedia);
  }, 1000);
}

And now we are almost there. The last clever trick is the communication of status updates. The media player sends us a callback it wants triggered when there is a media status update. So we store that.

window.fakeMedia.addUpdateListener = function(a) {
  window.updateCCmediaStatus = a;
}

The magic of this is, we stored the references to fakeMedia and fakeSession on the browser’s window object, as well as the callbacks. This means the test script has access to them. Therefore the test script can control the fake Chromecast.

So you want Chromecast to report playing? Make it so!

  @chromecast
  Scenario: Begin casting
    Given I am playing media
    When I click the Chromecast button
    And Chromecast state is "PLAYING"
    Then I should see Chromecast is casting
We are casting!

We are casting!

What does that mean, Chromecast state is “PLAYING”? It’s pretty straightforward now, using the objects and callback functions that the fake Chromecast has set up:

When(/^Chromecast state is "(.*?)"$/) do |state|
  $chromecast.state = state
  $chromecast.update!
end

Those two methods get added to the Chromecast Ruby class:

class Chromecast

  def initialize
    $capybara.execute_script <<-EOF
      var el=document.createElement("script");
      el.type="text/javascript";
      el.src = "https://gist.githubusercontent.com/sermoa/10988494/raw/82e08c5a29b5689b5e9f3d03c191b8c981102d85/fakeChromecast.js";
      document.getElementsByTagName("head")[0].appendChild(el);
    EOF
    @media_alive = true
  end

  def state=(state)
    @media_alive = false if state == 'IDLE'
    $capybara.execute_script "window.fakeMedia.playerState = '#{state}';"
  end

  def update!
    $capybara.execute_script "window.updateCCmediaStatus(#{@media_alive});"
  end

end

Note that i had to do something special for the “IDLE” case because the media status update expects a true/false to indicate whether the media is alive.

So, using these techniques, sending and capturing events and observing reactions, i was able to verify all the two-way communication between the Standard Media Player and Chromecast. I verified playing and pausing, seeking, changing volume, turning subtitles on and off, ending casting, and loading new media.

This fake Chromecast is a lot faster than a real Chromecast, which has to make connections to real media. It’s also possible to put the fake Chromecast into situations that are very difficult to achieve with a real Chromecast. The “IDLE” state, for example, is really hard to achieve. But we need to know what would happen if it occurrs, and by testing it with a fake Chromecast i was able to identify a bug that would likely have gone unnoticed otherwise.

For the curious, here’s a demo of my Chromecast automated regression test in action!

This is all a lot of fun, but there is nothing quite like testing for real with an actual Chromecast plugged into a massive television!

Testing Chromecast for real

Testing Chromecast for real

So that’s how i test Chromecast. If you want to test Chromecast too, please feel free to copy and tweak my fakeChromecast.js script, and ask me questions if you need any help.

Getting chromedriver to work with Ruby selenium-webdriver under OSX Mavericks

I interrupt my soprano-saxophone-and-bass-guitar-playing evening to write very quickly about something that annoyed me today. I can’t find the fix anywhere else on the internet, so i guess it’s up to me to provide the solution.

Yesterday i upgraded to OS X Mavericks. Which is nice. Free upgrade, woo! Most things work well, though i had a few Java version issues.

Today i found that selenium-webdriver was not able to launch Chrome with chromedriver. It would open and immediately quit.

A quick google reveals that the workaround is to start chromedriver with the --verbose flag and then it magically works again. Which is true in my experience. But all the examples are for selenium wedriver called by Java or Python and i couldn’t find one for Ruby.

The first thing i did was upgrade selenium-webdriver gem to 2.37.0 and chromedriver to 2.4. I checked my version of Chrome; it is 30.0.1599.101 which is apparently up to date. When none of those things made any difference, I investigated the verbose option.

I thought i would just have to pass --verbose to the Capybara::Selenium::Driver via the args or switches option. But no. That will send the flag to Chrome when it launches, but i need the flag to be set on chromedriver.

Ruby open source to the rescue!

I poked around in the source code for service args and found this:

# selenium/webdriver/chrome/bridge.rb

module Selenium
  module WebDriver
    module Chrome

      class Bridge < Remote::Bridge

        # ...

        def extract_service_args(opts)
          args = []

          if opts.has_key?(:service_log_path)
            args << "--log-path=#{opts.delete(:service_log_path)}"
          end

          args
        end

        # ...

      end
    end
  end
end

Hmm, so the only service arg you can set is the --log-path. That’s no good.

Ruby monkey patching to the rescue!

In my cucumber features folder i made a support file to monkey patch that method, and add a possibility of sending any service args i like.

# features/support/selenium_webdriver_monkey_patch.rb

module Selenium::WebDriver::Chrome
  class Bridge
    def extract_service_args(opts)
      args = []
      args += opts.delete(:service_args) if opts.has_key?(:service_args)
      args << "--log-path=#{opts.delete(:service_log_path)}" if opts.has_key?(:service_log_path)
      args
    end
  end
end

Now in my env.rb file i just have to require the monkey patch file and i’m able to send the verbose flag.

# features/support/env.rb

# ...

require 'selenium-webdriver'
require 'selenium_webdriver_monkey_patch'

# ...

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    service_args: ["--verbose"])
end

# ...

Hey presto, now i can set the verbose flag on chromedriver, and sure enough, it works. I am testing in Chrome again.

It seems a really horrible hack, and i hope a better solution is found soon, either from a Mavericks update, or chromedriver, or maybe selenium-webdriver will add this option for service args in a future version.

I still don’t really know why verbose works when normal mode does not. But there we go. I can test again tomorrow, and that makes me happy!

Now, back to my bass guitar! :)

Ruby Classes and Superclasses

An interesting question came up in this week’s Seven Languages in Seven Weeks skype call. We initially thought it might be a difference between versions of Ruby, but having looked more closely, i don’t think it is. Both 1.8.7 and 1.9.2 behave in this slightly odd way.

For reference, we are talking about Figure 2.1: Ruby Metamodel on page 26.

As we all now know, 4 is an object.

> 4.class
 => Fixnum

Fixnum inherits from Integer which inherits from Numeric.

> Fixnum.superclass
 => Integer
> Integer.superclass
 => Numeric

Each of Fixnum, Integer and Numeric are of class Class.

> [Fixnum, Integer, Numeric].map(&:class)
 => [Class, Class, Class]

The superclass of Numeric is Object.

> Numeric.superclass
 => Object

But the superclass of Class goes to Module and then to Object.

> Class.superclass
 => Module 
> Module.superclass
 => Object

And now, through the power of my very limited Open Office Drawing skills, i shall try to demonstrate what i think the diagram really looks like.

So where did Module come from? Why is a Class also a Module? And if Numeric is of class Class, why is its superclass not also Module?

I don’t think it’s particularly important to know these things, but they were interesting questions and i don’t know the answers.

Does anyone know?

Week1 Day 3 – Ruby metaprogramming

Seven Languages in Seven Weeks really does more than scratch the surface of a language. It shows off what each language does best. Ruby is great at expressive metaprogramming, so we wasted no time into diving deep into not one, but three types of metaprogramming:

  1. Opening an existing class to add extra methods
  2. Using method_missing to define dynamic methods
  3. Creating mixin modules to extend functionality

For me, this was highly insightful. Metaprogramming is always something that Other People do. I have written a couple of gems before, but i didn’t really understand what i was doing – i just played around copying examples until it did what i wanted. Or more often, i would be quick to use inheritance, not realising that a mixin module might serve me better.

I was inspired by Seven Languages in Seven Weeks where it showed how to start with a simple module and then adapt it a little to make it even more flexible. In future i will refer to this book to guide me as it explains so well what’s going on.

module ActsAsCsv

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_csv
      include InstanceMethods
    end
  end

  module InstanceMethods
    attr_accessor :headers, :csv_contents

    def initialize
      read
    end

    def read
      # ...
    end
  end

end

class RubyCsv
  include ActsAsCsv
  acts_as_csv
end

We write a module which, once included, the first thing it does is adds another module to the class which included it. That module provides a macro method which, when called, adds instance methods and attributes.

I now realise that there is nothing special about the module names ClassMethods and InstanceMethods. They are just named that way for clarity. There is not really any voodoo going on here.

When trying to add an each method i started off like this:

m = RubyCsv.new
m.each{|row| puts row.inspect }

I implemented an each method like this:

def each(&block)
  @csv_contents.each {|r| block.call(r) }
end

Sure enough, i was able to inspect the row. But then we wanted to do this, where one of the headings in the CSV file was ‘one’:

m.each{|row| puts row.one }

“No problem!” i thought. “Time for a method_missing” … but then i realised something i definitely would not have anticipated without doing the exercise.

undefined method `one' for ["lions", "tigers"]:Array (NoMethodError)

There you see the problem?! I’m working with an array at this point! Looking back at the book, it gave me a hint, which i had missed:

Modify the CSV application to support an each method to return a CsvRow object.

Ah, so that’s the answer! I modified my each method to do this:

def each(&block)
  @csv_contents.each do |r|
    row = CsvRow.new(@headings, r)
    block.call(row)
  end
end

I had to decide where to put the CsvRow class, and decided to put it as part of the module so there’s nothing else i had to require when mixing in the module.

I needed to pass the headings found, so that the row could implement its method_missing and find the right position in the array. The CsvRow class ended up like this:

class CsvRow
  attr :contents, :headers

   def initialize(headers, contents)
    @headers = headers
    @contents = contents
  end

  def method_missing(method)
    index = @headers.index(method.to_s)
    raise "No heading '#{method}' in the CSV file." if index.nil?
    @contents[index]
  end

end

The full solution can be found here: week1-ruby/acts_as_csv_module.rb

I am really glad of the opportunity to explore and understand Ruby metaprogramming a lot better. I am also extremely excited for what i’m going to learn in the other six languages in the next six weeks. This has set me up to expect some really awesome things!

Week 1 Day 2 – Diving deeper into Ruby

I had a really great day. Gave myself a day off from working, and went to London to meet my apprentice Rohit, see a few friends and go to eXtreme Tuesday Club in the evening.

At 6pm i met with Dan in Covent Garden and we worked through a bit of #7languages7weeks Day 2 together. We came up with three ways of splitting up an array into groups of four, ending with the beautifully elegant each_slice method. You can see the result of our efforts here: week1-ruby/day2.rb

I worked through the remaining exercises on the train home. One thing really caught me out, which is how Ruby is so eager to convert hashes to arrays when you iterate. For example:

> hash = {1 => 'one', 2 => 'two', 3 => 'three'}
=> {1=>"one", 2=>"two", 3=>"three"}
> hash.each {|part| puts part.inspect }
[1, "one"]
[2, "two"]
[3, "three"]

See that?! Surprise Array!

I had originally said that you can’t convert an array to a hash, but in a case like this you can, and to solve the tree initialization exercise i found i had to. Something like this:

> hash = {1 => 'one', 2 => 'two', 3 => 'three'}
=> {1=>"one", 2=>"two", 3=>"three"}
> hash.each {|part| puts ({part.first => part.last}).inspect }
{1=>"one"}
{2=>"two"}
{3=>"three"}

My solution to the family tree exercise can be found here: week1-ruby/tree.rb

Finally, the find-in-file exercise. I really like this one. The book said the solution was simple, and my word, it truly is.

File.open('tree.rb').each_with_index do |line, line_number|
  puts "#{line_number}: #{line}" if line =~ /tree/
end

That’s it! So neat. I have always liked the application of the each method on a File. Here’s the output of the program:

$ ruby read_file.rb 
13:   def initialize_with_hash(tree)
14:     tree.each_pair do |name, children|
35: ruby_tree = Tree.new('Ruby', [Tree.new('Reia'), Tree.new('MacRuby')])
38: ruby_tree.visit {|node| puts node.node_name }
41: puts 'Visiting entire tree'
42: ruby_tree.visit_all{|node| puts node.node_name }
44: family_tree = Tree.new({'grandpa' => {
50: puts 'Visiting family tree'
51: family_tree.visit_all{|node| puts node.node_name }

Very neat! Although i’ve been programming in Ruby for a long time, i am appreciating its beauty and simplicity all the more tonight!

Update

Since looking at Alberto’s excellent solutions and checking the Ruby Hash#each documentation, i now realise that the each method is supposed to take two elements in the block. For some reason i always thought you had to use each_pair for that. So there we go, talk about failing publicly! :)

Week 1 Day 1 – studying the basics of Ruby

Today i began the Seven Languages in Seven Weeks challenge. The first week is Ruby.

I know Ruby well so it wasn’t difficult for me. But to my great joy, my partner decided to study along with me, so i had the privilege of witnessing a newcomer’s approach to Ruby.

Together we learnt a bit about the syntax of Ruby: variables, conditions, loops, and a few of the things that make Ruby special, such as: Everything is an object, including numbers and even true and false. We then learned a bit about converting objects from one type to another, and duck typing: if it walks like a duck and quacks like a duck then it’s a duck.

I took a back-seat on the exercises and allowed my partner (who isn’t a programmer) to try out some ideas. I had to smile at the attempt to use a goto:

I said, “I can’t fault your logic – it’s perfect! But we don’t program like that anymore!”

You can see our final soution here: week1-ruby/day1.rb

As Stuart pointed out, we could also have used recursion … “But that’s going a bit too far, don’t you think?” (bonus points for a Mary Poppins reference?!)

How did everyone else get on today? In Ruby i feel qualified to answer questions about the language, so feel free to ask me. I’m certainly going to be needing to ask questions in later weeks!

Exposing my ignorance

I have been encouraging my apprentice Rohit to blog about ignorance. It’s great to talk about what you know, and what you can do well, but for people to really trust you it’s important also to be honest about what you don’t know yet, and what you’re looking to learn.

As software crafters learning is part of what we do. There is always more to learn; it never stops. Personally i really enjoy learning. I love the feeling of new information going in and being absorbed.

Since i asked Ro to blog about exposing ignorance, i thought i should do the same. It’s good to start with a review of where you are now.

Where i am now

It’s fair to say my strongest language is Ruby. I’ve been programming in Ruby for about 5 years, i’ve seen my style grow and change a lot. I feel very confident in domain modelling and object orientation using Ruby.

I’m pretty confident with behaviour driven development for Ruby, using Cucumber and Rspec. I know a lot about Rails and a fair amount about Sinatra. I am comfortable using MySQL, PostgreSQL and MongoDB.

Exposing my ignorance

My biggest weakness at the moment is in having only one strong language. And here is where i expose my ignorance. I know a bit about a few other languages, but i’m not where i want to be. I feel embarrassed when people talk about Java, Haskell, Scala, Io, and i am unable to contribute due to my lack of knowledge.

Confronting my ignorance

Since thinking about this blog post, i have already begun confronting my ignorance: my first step was to contact Joel Gascoigne who develops Buffer – a simple tool for scheduling tweets. I wanted an easier way to use Buffer on my phone, so i offered to make an Android app. I saw this as a pet project to get me learning a bit more about Android development in Java.

The app is open source; you can check it out on github.com/bufferapp/Android. A simple working version is now available in the Android Market: Buffer for Android.

We have a few ideas for moving the app forward, but for now i’m happy that we’ve got something out there that works, and it has given me the confidence to develop more Android apps.

My next tactic will be to study the book Seven Languages in Seven Weeks: A pragmatic guide to learning programming languages, by Bruce A. Tate. This is an intensive study of Ruby, Io, Prolog, Scala, Erlang, Clojure and Haskell. By learning how to solve a non-trivial problem in each of these languages, we learn the unique strengths and weaknesses of each language.

I’m looking for other people to study along with me, so if you’re interested, let me know!

Once i’ve studied those seven languages, i want to learn some Python. I’ve been hearing a lot about Python lately, and it seems like a language i really want to know. I already have the book Learning Python by Mark Lutz and i look forward to getting to that.

So that’s me: i have listed my strengths, exposed my ignorance and outlined how i am confronting it to improve myself and learn more. What about you?

Pretty autospec growl messages

It’s interesting how you can sometimes notice something through someone else’s eyes that you’d previously overlooked. This week i have been mostly working with my apprentice Despo on a Rails project. Despo noticed that running specs in Rails takes a long time to initialize, which something i am well aware of but i guess i’ve sort of got used to it.

So we decided to do something about it. The first thing we did was set up Spork. If you want to know how to do that i recommend this post by Chris: How to get Spork working NOW on Rails 3, Rspec 2 and Cucumber.

As Chris mentioned at the end of that post, autotest and autotest-growl play well with Spork, so that is what we did next. The results can be quite nice!

Autotest notices which files have changed and starts running the relevant specs:

Autotest notices modifications

Growl can be configured to show failure messages in red:

Growl can be configured to show failure messages in red

Success messages are shown in green:

Success messages are shown in green

Growl can also show when specs are pending:

Growl can also show when specs are pending

How did we achieve this?

Firstly you will need to install the gems autotest and autotest-growl. Either include them in a bundle, or install them system-wide if you want to use them across all projects.

If you use RVM for different versions of ruby you might find this command useful:

for version in $(rvm list strings); do rvm use $version@global && gem install autotest && gem install autotest-growl; done

Secondly you will need a .autotest file. You can either put it in your project’s root directory, or in your home directory ~/.autotest

Here is mine so you can see what it looks like:

#!/usr/bin/env ruby
require 'rubygems'
require 'autotest/growl'

Autotest::Growl::image_dir = File.join(ENV['HOME'], '.autotest_images')
Autotest::Growl::show_modified_files = true

Autotest.add_hook :initialize do |autotest|
  %w(.git .DS_Store db log tmp).each do |exception|
    autotest.add_exception(exception)
  end
  autotest.order = :random
end

Thirdly you will need to install Growl and configure it to your liking. I have mine set to Music Video because it’s so impossible to miss!

The only trouble with the Music Video style is it can only display one message at a time, so i make it fade in and last for just 1 second. Happily, with growl colouring, you don’t really need to read the message – the colour tells you immediately what happened.

The colours you need to set are:

  • Very Low for passed
  • Moderate for pending
  • Normal for info
  • Emergency for failed (or syntax error)

Here is my Growl configuration:

Here is my Growl configuration

Finally, if you want cute pictures to come up with your growl messages, you simply need to put them in ~/.autotest_images – named passed.png, pending.png, info.png, failed.png and error.png.

These are my autotest images

Running autotest

Now, of course, you need to know how to run it!

With Rails 3 you need

AUTOFEATURE=true autotest

With Rails 2 it’s

AUTOFEATURE=true autospec

If you only want to run specs and not features, just take away the AUTOFEATURE=true bit.

But what about FSEvent?

The normal behaviour for autotest is to constantly poll your filesystem looking for changes. This is wasteful as it will use a lot of CPU and drain your battery, but there is an alternative. From Mac OSX 10.5 onwards an FSEvent service reports modified files.

To make use of FSEvent, you should be able to just install the autotest-fsevent gem and require 'autotest/fsevent' in your .autotest file.

Unfortunately, when i tried it, the mere presence of the autotest-fsevent gem seems to cause the specs not to run. It notices changes but doesn’t do anything about them. So if anyone can help me to understand what’s up with that, i would be grateful!

In the mean time, i’m just trying to remember to stop autotest whenever i’ve finished using it.

The Minisculus Challenge – Winner Announced!

On Friday 7th October, at Software Craftsmanship 2010, eden announced Minisculus – a series of encoding, decoding and code breaking challenges, loosely based on the historic challenges faced during the second world war.

Minisculus SpyMinisculus was set by Steve and Todd. I took the challenge in the week before SC2010 and really enjoyed it. There is enormous satisfaction in figuring out the answer, posting it to the server, and if you got it right, receiving the secret URL to the next question. The progression of the challenge provides new insights and plenty of opportunities for refactoring to keep your code clean and simple.

We ran Minisculus as a competition for a £100 Amazon voucher. We had 12 entries; most written in Ruby, but also including solutions in Java, Clojure, PHP and Python. It is fascinating to see the different ways that people implemented their solutions.

Luke RedpathOur winner is Luke Redpath, who is a freelance Ruby and iPhone developer in London. Luke wrote the solution in Ruby, nicely specified using RSpec. The code is particularly clean, easy to read, well refactored to be very simple. Luke has good use of object orientation, using a ShiftCypher that can accept any number of wheels, which have their own configurations for encoding and decoding, a great example of the Strategy Pattern. Luke also included a code breaker using keyword matching, and automated the communication with the Minisculus server to send answers and receive the next question.

Luke’s was also one of the first entries we received, and a nice little touch helped seal it for Luke: the inclusion of a MarkIII machine that raises an exception when called: “Not Implemented due to budget overruns!” – a joke that shows the attention paid to the story told by the challenge. Have a look at Luke’s code at github.com/lukeredpath/minisculus.

You can see a Hall of Fame on the Minisculus website, showing everybody who completed the challenge, including source code for many of the entries.

Although the competition is now over, the challenge remains for anyone who wants to try it! Thanks to everyone for participating so far!

minisculus.edendevelopment.co.uk

Code Retreat in Winchester

All day today there was a Code Retreat at Eden Development. About 20 people came, from all over the country, and we spent an excellent Saturday pair programming together, sharing ideas, having stimulating conversations and getting to know each other much better.

At a code retreat everybody works on a single problem: Conway’s Game of Life. You pair with different people in each 45 minute session. You have to TDD the development, but you can use whichever programming language you choose as a pair. At the end of each session all the code must be deleted. You do a retrospective as a group, have a little break, then go and pair with somebody else.

We got through 6 sessions, which means i pair programmed with 6 people i didn’t know very well before today. It was great! The first two sessions i paired with Paul and then Quentin. To start with, we were pretty much feeling our way through the problem, implementing the rules of the game, and then getting stuck with determining the position of each cell.

In the third session i paired with Ben and we decided to try a completely different approach: dealing purely with coordinates to represent the positions of live cells. It actually worked quite nicely, although implementing the rule logic became quite difficult.

After lunch we were invited to try “TDD as if you mean it” which has very strict rules about where and when you should create your code, and when you can move it. It’s quite restricting the rules seemed to hold us back from getting very far. I was with Louis that time, who was very good at helping me to program in Java, which i’m not so familiar with.

At the 5th session we were allowed to just do whatever we want, so long as it was still test driven. I paired with Paweł and we just went a bit wild with linking cells to each other and getting them to link back respectively. We were going with North, South, East, West, but we eventually realised that you could really link any cell to any other, and the direction doesn’t matter. At that point it becomes more of a network problem. You could say that as a software coder i live or die depending on the community and people i know. That was an interesting insight.

In the final session I was with Clive and it turned out to be our best session of all. We were extremely controlled in our TDD, making sure we understood the code at each stage, and only taking small steps, and predicting at each test run whether it would pass or fail and what the failure message would be. We had cells that knew their positions, and could receive other cells and decide whether or not to attach to them as neighbours. Meaning we could pass every cell to every other cell and get them to connect based on their positions, then we could count how many neighbours they each have.

I mostly programmed in Ruby today, but there were people coding in Java, C++, C#, Python, and i think Scala, Clojure and PHP. We learnt a lot about how to program the game of life, and more importantly, how not to! A code retreat is a great opportunity to have fun, get to know other people, and code in an environment where it’s safe to try things out and make mistakes.

To find a code retreat near you, look on coderetreat.com or consider running your own!