support sprockets-style jst templates

This commit is contained in:
John Bintz 2011-11-18 16:00:24 -05:00
parent 537b2e437d
commit 8867d00ac8
13 changed files with 82 additions and 52 deletions

View File

@ -16,4 +16,4 @@ gem 'guard-jasmine-headless-webkit', :git => 'git://github.com/johnbintz/guard-j
gem 'facter'
gem 'jquery-rails'
gem 'ejs'

View File

@ -24,5 +24,5 @@ Gem::Specification.new do |s|
s.add_dependency 'coffee-script', '>= 2.2'
s.add_dependency 'rainbow'
s.add_dependency 'multi_json'
s.add_dependency 'sprockets', '~> 2.0'
s.add_dependency 'sprockets', '>= 2.0'
end

View File

@ -1,4 +1,5 @@
require 'pathname'
require 'sprockets/engines'
module Jasmine::Headless
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
@ -24,3 +25,4 @@ module Jasmine::Headless
end
require 'jasmine/headless/errors'
Sprockets::Engines

View File

@ -57,7 +57,7 @@ module Jasmine::Headless
end
def search_paths
@search_paths ||= [ Jasmine::Core.path, src_dir, spec_dir ] + self.class.vendor_asset_paths
@search_paths ||= [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ] + self.class.vendor_asset_paths
end
def has_spec_outside_scope?
@ -92,6 +92,8 @@ module Jasmine::Headless
TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) }
end
EXTENSION_FILTER = %r{\.(js|css|coffee|jst.*)$}
def add_dependency(type, file, source_root)
case type
when 'require'
@ -99,7 +101,7 @@ module Jasmine::Headless
add_file(*result)
end
when 'require_tree'
Dir[File.join(source_root, file, '**/*.{js,css,coffee}')].each do |tree_path|
Dir[File.join(source_root, file, '**/*')].find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }.sort.each do |tree_path|
if result = find_dependency(tree_path.gsub(%r{^#{source_root}/}, ''))
add_file(*result)
end
@ -109,7 +111,7 @@ module Jasmine::Headless
def find_dependency(file)
search_paths.each do |dir|
if file[%r{\.(js|css|coffee)$}]
if file[EXTENSION_FILTER]
if File.file?(path = File.join(dir, file))
return [ File.expand_path(path), File.expand_path(dir) ]
end
@ -133,7 +135,11 @@ module Jasmine::Headless
alert_time = nil
end
Jasmine::Headless::TestFile.new(file).to_html
search_paths.collect do |path|
if file[path]
Jasmine::Headless::TestFile.new(file, path).to_html
end
end.compact.first
}.flatten.compact.reject(&:empty?)
end
@ -194,7 +200,7 @@ module Jasmine::Headless
end
def expanded_dir(path)
Dir[path].collect { |file| File.expand_path(file) }
Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }
end
def add_file(file, source_root)

View File

@ -1,5 +1,12 @@
require 'rainbow'
require 'sprockets/directive_processor'
require 'sprockets'
%w{haml-sprockets}.each do |library|
begin
require library
rescue LoadError
end
end
module Jasmine::Headless
class TestFile
@ -38,6 +45,13 @@ module Jasmine::Headless
%{<script type="text/javascript" src="#{path}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{path}" type="text/css" />}
when '.jst'
to_jst(read)
else
case path
when %r{\.jst(\..*)$}
to_jst(Sprockets.engines($1).new { read }.evaluate(self, {}))
end
end
end
@ -53,6 +67,19 @@ module Jasmine::Headless
[ type, name ]
end
end
def logical_path
path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '')
end
private
def to_jst(data)
%{<script type="text/javascript">#{Sprockets.engines('.jst').new { data }.evaluate(self, {})}</script>}
end
def read
File.read(path)
end
end
end

View File

@ -146,6 +146,8 @@ describe "jasmine-headless-webkit" do
files.lines.to_a.should contain_in_order_in_file_list(
'vendor/assets/javascripts/jquery.js',
'templates/that.jst.ejs',
'templates/this.jst',
'assets/things/required.js',
'assets/things/code.js',
'assets/things/subcode/more_code.js',

View File

@ -1,3 +1,5 @@
//= require 'jquery'
//= require_tree 'things/templates'
//= require 'things/required'
window.a = '1';

View File

@ -1,2 +1 @@
//= require 'jquery'

View File

@ -5,7 +5,7 @@ spec_files:
- "**/*_spec.js"
src_files:
- "things/**/*.js"
- "things/**/*"
helpers:
- "spec_helper.js"

View File

@ -161,46 +161,6 @@ describe Jasmine::Headless::FilesList do
end
end
describe '#.*files_to_html' do
include FakeFS::SpecHelpers
before do
files_list.instance_variable_set(:@files, [
'test.js',
'test.coffee',
'test.whatever',
'test.css'
])
files_list.instance_variable_set(:@filtered_files, [
'test.js',
'test.coffee'
])
File.stubs(:read)
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle).returns("i compiled")
end
context '#files_to_html' do
it "should create the right HTML" do
files_list.files_to_html.should == [
%{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>},
%{<link rel="stylesheet" href="test.css" type="text/css" />}
]
end
end
context '#filtered_files_to_html' do
it "should create the right HTML" do
files_list.filtered_files_to_html.should == [
%{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>}
]
end
end
end
describe '#spec_file_line_numbers' do
include FakeFS::SpecHelpers
@ -292,7 +252,7 @@ describe Jasmine::Headless::FilesList do
end
it 'should take the src dir and spec dirs' do
files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir ]
files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir) ]
end
end
@ -302,7 +262,7 @@ describe Jasmine::Headless::FilesList do
end
it 'should add the vendor gem paths to the list' do
files_list.search_paths.should == [ Jasmine::Core.path, src_dir, spec_dir, path ]
files_list.search_paths.should == [ Jasmine::Core.path, File.expand_path(src_dir), File.expand_path(spec_dir), path ]
end
end
end

View File

@ -72,6 +72,38 @@ describe Jasmine::Headless::TestFile do
end
end
end
context '.jst' do
include FakeFS::SpecHelpers
let(:path) { 'file.jst' }
let(:content) { 'content' }
before do
File.open(path, 'wb') { |fh| fh.print content }
end
it 'should use the JST template processor to get the processed file' do
subject.should include('JST["file"]')
subject.should include(content)
end
end
context '.jst.*' do
include FakeFS::SpecHelpers
let(:path) { 'file.jst.ejs' }
let(:content) { 'content' }
before do
File.open(path, 'wb') { |fh| fh.print content }
end
it 'should use the JST template processor to get the processed file' do
subject.should include('JST["file"]')
subject.should include(content)
end
end
end
describe '#dependencies' do