Created
May 30, 2010 18:56
-
-
Save apeckham/419229 to your computer and use it in GitHub Desktop.
rake task to convert jsunit tests to jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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