Looks like I am blogging again: printing out Cucumber steps

ruby cucumber rake

Sun Jun 07 00:11:14 -0500 2009

UPDATE: This functionality turned into a patch and got into Cucumber as a built-in formatter. Use cucumber -f steps to use it.

For at least a minimally interesting inaugural post, let me explain the name of the blog:

Rice and Beans is a very frequent food combination seen in Brazil, which food experts believe to be a healthy foundation for a lot of typical brazilian dishes due to the mix of carbs and proteins.

Now, to something really useful. This is a little rake task I wrote to printout any Cucumber step definition for your project. It is very hackish, but it works for now. I intend to turn this into a proper formatter and add it to Cucumber.

desc 'List all step definitions'
task :steps do

  # let's collect all steps in this hash, grouped by source file
  $steps = {}

  module Cucumber
    class StepDefinition
      # let's override the step constructor to 
      # capture the necessary information
      alias_method :old_initialize, :initialize
      def initialize(regexp, &proc)
        caller[1] =~/.+\/(.+)\./
        $steps[$1] ||= []
        $steps[$1] << regexp.to_s[8..-3]
        old_initialize(regexp, &proc)
      end
    end
  end

  # require the files containing the step definitions,
  # so they get all instantiated and captured
  require File.dirname(__FILE__) + '/path/to/your_steps.rb'

  # printing out results
  $steps.keys.sort.each do |file|
    puts file + ":"
    $steps[file].sort.each { |s| puts "  " + s }
    puts
  end
end

And you get this kind of output on your console when the task is ran:

checkbox_steps:
  I click the checkbox "([^\"]*)"
  the checkbox "([^\"]*)" should (not )*be selected

combobox_steps:
  I change the combobox "([^\"]*)" to "([^\"]*)"
  I should have the combobox "([^\"]*)" with "([^\"]*)"

dialog_steps:
  I close the dialog "([^\"]*)"
  I should (not )*see the dialog "([^\"]*)"
  the dialog "([^\"]*)" is the container
  the dialog "([^\"]*)" is visible
blog comments powered by Disqus