RAILS_ENV='test'
require 'test/unit'
require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper')
# Re-raise errors caught by the controller.
class WebController < ActionController::Base
before_filter :style_like_home
before_filter :style_like_users!
before_filter :style_like_extra
before_filter :script_like_home
before_filter :script_like_users!
before_filter :script_like_extra
def index
render :text => '
SweetAssets'
end
end
class ShortcutController < ActionController::Base
style_like :home!, :users
script_like :extra!, :home
end
class DefaultsController < ActionController::Base
script_like :defaults
end
class SweetAssetsTest < Test::Unit::TestCase
def setup
ActionController::Base.perform_caching = false
@controller = WebController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def teardown
delete_empty_recent_files
end
def test_each_stylesheet_should_appear
get :index
assert_response :success
assert_tag :link, :attributes => {:href => /stylesheets\/application.css/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
assert_select 'head > link', :count => 5
end
def test_bottom_stylesheets_should_appear_last
get :index
assert_response :success
assert @response.body =~ /\n*?<\/head>/
end
def test_caching_stylesheets
ActionController::Base.perform_caching = true
with_stylesheets :application, :home, :web, :users, :extra do
with_javascripts :application, :home, :web, :users, :extra do
get :index
end
end
assert_response :success
assert_no_tag :link, :attributes => {:href => /stylesheets\/application.css/, :rel => 'stylesheet'}
assert_no_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
assert_no_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
assert_no_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
assert_no_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /sweet_stylesheets_application.css,home.css,extra.css(\?\d*)?/, :rel => 'stylesheet'}
assert_tag :link, :attributes => {:href => /sweet_stylesheets_web.css,users.css(\?\d*)?/, :rel => 'stylesheet'}
# and check the placement
location_of_head = @response.body =~ //
location_of_title = @response.body =~ //
location_of_home_extra_css = @response.body =~ / {:src => /javascripts\/web.js/}
assert_no_tag :script, :attributes => {:src => /javascripts\/home.js/}
assert_no_tag :script, :attributes => {:src => /javascripts\/extra.js/}
assert_no_tag :script, :attributes => {:src => /javascripts\/users.js/}
assert_tag :script, :attributes => {:src => /sweet_javascripts_home.js,extra.js(\?\d*)?/}
assert_tag :script, :attributes => {:src => /sweet_javascripts_web.js,users.js(\?\d*)?/}
# and check the placement
location_of_head = @response.body =~ //
location_of_title = @response.body =~ //
location_of_home_extra_js = @response.body =~ /