Skip to content

Instantly share code, notes, and snippets.

@apeckham
Created May 30, 2010 18:56
Show Gist options
  • Save apeckham/419229 to your computer and use it in GitHub Desktop.
Save apeckham/419229 to your computer and use it in GitHub Desktop.
rake task to convert jsunit tests to jasmine
task :convert do
require 'nokogiri'
Dir.glob("public/javascripts/test-pages/**/*_test.html").each do |file|
doc = Nokogiri::HTML(File.read(file))
name = File.basename(file, "_test.html")
script = doc.css("script").find { |tag| tag[:src].blank? }
if body = doc.at("body")
File.open("spec/javascripts/fixtures/#{name}.html", "w") do |out|
out << body.children
end
end
File.open("spec/javascripts/#{name}_spec.js", "w") do |out|
filtered = script.children.to_s
indent = filtered[/( *)function test/, 1]
filtered.gsub! "function setUp() {", "beforeEach(function() {"
filtered.gsub! "function tearDown() {", "afterEach(function() {"
filtered.gsub! /function test(.+)\(\) \{/, "it(\"\\1\", function() {"
filtered.gsub! /^#{indent}\}$/, "#{indent}});"
out << <<END
describe("#{name}", function() {
beforeEach(function() {
Fixtures.load("#{name}");
});
#{filtered}
});
END
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment