Skip to content

Instantly share code, notes, and snippets.

@tie-rack
Created April 5, 2009 03:40
Show Gist options
  • Save tie-rack/90364 to your computer and use it in GitHub Desktop.
Save tie-rack/90364 to your computer and use it in GitHub Desktop.
class JsUnit < Thor
desc 'create_test_directory', 'Create the javascript test directory'
def create_test_directory
FileUtils.mkdir_p(test_path)
end
desc 'create_test_file FILE', 'Create a JsUnit test page for FILE'
def create_test_file(filename)
create_test_directory
File.open(test_filename_full_path(filename), 'w+') do |f|
f.puts(test_contents(filename))
end
end
private
def jsunit_path
File.expand_path(ENV['JSUNIT'])
end
def test_path
File.join(File.expand_path(Dir.pwd), 'test', 'javascript')
end
def test_filename(javascript_filename)
unless File.extname(javascript_filename) == '.js'
raise(ArgumentError, 'JavaScript file required')
end
'test_' + File.basename(javascript_filename, '.js') + '.html'
end
def test_filename_full_path(javascript_filename)
File.join(test_path, test_filename(javascript_filename))
end
def test_contents(javascript_filename)
<<EOF
<html>
<head>
<title>Test Page for #{File.basename(javascript_filename)}</title>
<script language="javascript" src="#{File.expand_path(ENV['JSUNIT'])}/app/jsUnitCore.js"></script>
<script language="javascript" src="#{File.expand_path(javascript_filename)}"></script>
</head>
<body>
<script language="javascript">
function testTruth() {
assert(true);
}
</script>
</body>
</html>
EOF
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment