Created
April 5, 2009 16:14
-
-
Save tie-rack/90469 to your computer and use it in GitHub Desktop.
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
# A few assumptions are made here: | |
# | |
# 1. You want your javascript tests in test/javascript | |
# 2. Your jsunittest.js file is called that (you've stripped the version) | |
# 3. You've got a jsunittest.css file in the same place | |
# 4. You've set JSUNITTEST as the path to where the jsunittest files live | |
# | |
# Run it like this: thor jsunittest:test path/to/your/js.js | |
require 'pathname' | |
class Jsunittest < Thor | |
TEST_FILE = 'jsunittest.js' | |
CSS_FILE = 'jsunittest.css' | |
desc 'prepare', 'Create the javascript test directory and move files there' | |
def prepare | |
FileUtils.mkdir_p(test_path) | |
copy_jsunittest_files_to_test | |
end | |
desc 'test FILE', 'Create a JsUnitTest test page for FILE' | |
def test(filename) | |
prepare | |
File.open(test_filename_full_path(filename), 'w+') do |f| | |
f.puts(test_contents(filename)) | |
end | |
end | |
private | |
def copy_jsunittest_files_to_test | |
[TEST_FILE, CSS_FILE].each do |file| | |
unless File.exist?(File.join(test_path, file)) | |
FileUtils.cp(File.join(jsunittest_path, file), test_path) | |
end | |
end | |
end | |
def jsunittest_path | |
File.expand_path(ENV['JSUNITTEST']) | |
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 path_to_javascript_file(filename) | |
test_dir = Pathname.new(test_path) | |
file_dir = Pathname.new(File.expand_path(filename)) | |
file_dir.relative_path_from(test_dir).to_s | |
end | |
def test_contents(javascript_filename) | |
<<EOF | |
<html> | |
<head> | |
<title>Test Page for #{File.basename(javascript_filename)}</title> | |
<script language="javascript" src="#{TEST_FILE}"></script> | |
<script language="javascript" src="#{path_to_javascript_file(javascript_filename)}"></script> | |
<link rel="stylesheet" type="text/css" href="#{CSS_FILE}" /> | |
</head> | |
<body> | |
<div id="testlog"></div> | |
<script type="text/javascript" language="javascript"> | |
new Test.Unit.Runner({ | |
testTruth: function() { with(this) { | |
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