jasmine-headless-webkit/lib/jasmine/headless/test_file.rb
2011-11-20 11:56:25 -05:00

66 lines
1.6 KiB
Ruby

require 'rainbow'
require 'sprockets'
module Jasmine::Headless
class TestFile
attr_reader :path, :source_root
def initialize(path, source_root = nil)
@path, @source_root = path, source_root
end
def ==(other)
self.path == other.path
end
def to_html
process_data_by_filename(path)
end
def dependencies
return @dependencies if @dependencies
processor = Sprockets::DirectiveProcessor.new(path)
@dependencies = processor.directives.collect do |_, type, name|
if name[%r{^\.}]
name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '')
else
raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree'
end
[ type, name ]
end
end
def logical_path
path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '')
end
private
def read
File.read(path)
end
def process_data_by_filename(path, data = nil)
case extension = File.extname(path)
when ''
data || ''
when '.js'
data || %{<script type="text/javascript" src="#{path}"></script>}
when '.css'
data || %{<link rel="stylesheet" href="#{path}" type="text/css" />}
else
if engine = Sprockets.engines(extension)
data = engine.new(path) { data || read }.render(self)
data = %{<script type="text/javascript">#{data}</script>} if extension == '.jst'
process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data)
else
data || ''
end
end
end
end
end