Skip to content

Instantly share code, notes, and snippets.

@oliyoung
Created August 14, 2008 23:50
Show Gist options
  • Save oliyoung/5516 to your computer and use it in GitHub Desktop.
Save oliyoung/5516 to your computer and use it in GitHub Desktop.
Rake tasks for creating and running rspec stories
namespace :stories do
desc "Create Story Scaffolds"
task :scaffold do
Dir.glob("./stories/*_story.txt").each do |file|
filename = File.basename(file, "txt").split("_")[0].downcase.gsub(/ /, '_')
File.open("./stories/#{filename}_story.rb", 'w') do |f|
f.puts "require File.dirname(__FILE__) + '/helper'\n"
f.puts "with_steps_for(:#{filename}) do"
f.puts "\ttell '#{filename}_story', :type => RailsStory"
f.puts "end"
end
File.open("./stories/#{filename}_steps.rb", 'w') do |f|
contexts = {:given=>[],:when=>[], :then=>[]}
context = "given"
f.puts "steps_for(:#{filename}) do"
File.open("./stories/#{filename}_story.txt").each_line do |line|
%w{ given when then and}.each do |trigger|
if line.downcase.include?(trigger)
words = line.downcase.split(" ")
context = trigger.to_sym unless words.first == "and" or trigger == "and"
contexts[context.to_sym] << (words.reverse[0..-2].reverse).join(" ")
end
end
end
contexts.each do |key, values|
values.uniq.each do |value|
arg = ""
if value.scan(/\'.+\'/).length > 0
words = value.split(" ")
argument = value.scan(/\'.+\'/).first
variable = words[words.index(argument) - 1]
value.gsub!(/'.+\'/, "'$#{variable}'")
arg = "|#{variable}|"
end
f.puts "\t#{key.to_s.titlecase} \"#{value}\" do #{arg}"
f.puts "\t\tpending 'Not Yet Done'"
f.puts "\tend\n\n"
end
end
f.puts "end"
end
end
end
desc "Tell Stories"
task :tell do
Dir.glob("./stories/*_story.rb").each do |filename|
story = File.open("./stories/#{File.basename(filename, "rb")}txt", "r").read.split("\n")[0].split(":").last.strip
puts "Telling the '#{story}' story"
system("ruby", filename)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment