Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created April 18, 2012 15:27
Show Gist options
  • Save johnbintz/2414336 to your computer and use it in GitHub Desktop.
Save johnbintz/2414336 to your computer and use it in GitHub Desktop.
Write out undefined Cucumber step definitions, one per file
# this writes out undefined step definitions, one per file, in the usual way that i do cucumber stuff.
# it saves a *lot* of copypaste from the terminal. still in progress as i refine the process.
# use it as a formatter: cucumber -f StepWriter --out features/step_definitions
# save it to support/env/step_writer.rb
require 'cucumber/formatter/io'
class StepWriter
include Cucumber::Formatter::Io
class << self
def after_write(&block)
if block
@after_write = block
else
@after_write
end
end
end
def initialize(step_mother, path_or_io, options)
@step_mother, @io = step_mother, ensure_dir(path_or_io, 'step writer')
end
def after_features(features)
undefined = @step_mother.steps(:undefined)
return if undefined.empty?
undefined.each do |step|
step_name = Cucumber::Undefined === step.exception ? step.exception.step_name : step.name
step_multiline_class = step.multiline_arg ? step.multiline_arg.class : nil
path = Pathname(@io).join(step.actual_keyword.downcase.strip, step_name.underscore.gsub(%r{[^\w]+}, '_') + '.rb')
if !path.file?
puts "Writing new step to #{path}"
path.parent.mkpath
path.open('wb') { |fh| fh.print @step_mother.snippet_text(step.actual_keyword, step_name, step_multiline_class) }
end
end
StepWriter.after_write.call(@io) if StepWriter.after_write
end
end
# after creating new steps, open the step definitions folder so you can rearrange the steps
# i typically have subdirectories for each "area" that i'm testing. it's fluid, i don't have a really rigid structure for
# how i organize those. it's still better than monolithic *_steps.rb files (imho).
StepWriter.after_write do |dir|
system %{open #{dir}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment