require File.dirname(__FILE__) + '/abstract_unit'
require File.dirname(__FILE__) + '/../lib/multi_asset_locations'
module BaseTest
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::AssetTagHelper
include MultiAssetLocations::Helpers::AssetTagHelper
def setup
@controller = Class.new do
attr_accessor :request
def url_for(options, *parameters_for_method_reference)
"http://www.example.com"
end
end.new
@request = Class.new do
def relative_url_root
""
end
end.new
@controller.request = @request
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
end
def teardown
Object.send(:remove_const, :RAILS_ROOT) if defined?(RAILS_ROOT)
ENV["RAILS_ASSET_ID"] = nil
end
end
class MultiAssetLocationsTest < Test::Unit::TestCase
include BaseTest
AutoDiscoveryToTag = {
%(auto_discovery_link_tag) => %(),
%(auto_discovery_link_tag(:atom)) => %(),
%(auto_discovery_link_tag(:rss, :action => "feed")) => %(),
%(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(),
%(auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"})) => %(),
%(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(),
%(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(),
%(auto_discovery_link_tag(nil, {}, {:title => "No stream.. really", :type => "text/html"})) => %(),
%(auto_discovery_link_tag(:rss, {}, {:title => "My RSS", :type => "text/html"})) => %(),
%(auto_discovery_link_tag(:atom, {}, {:rel => "Not so alternate"})) => %(),
}
JavascriptPathToTag = {
%(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
%(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js)
}
JavascriptIncludeToTag = {
%(javascript_include_tag("xmlhr")) => %(),
%(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(),
%(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(\n),
%(javascript_include_tag(:defaults)) => %(\n\n\n),
%(javascript_include_tag(:defaults, "test")) => %(\n\n\n\n),
%(javascript_include_tag("test", :defaults)) => %(\n\n\n\n)
}
StylePathToTag = {
%(stylesheet_path("style")) => %(/stylesheets/style.css),
%(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
%(stylesheet_path('/dir/file')) => %(/dir/file.css)
}
StyleLinkToTag = {
%(stylesheet_link_tag("style")) => %(),
%(stylesheet_link_tag("/dir/file")) => %(),
%(stylesheet_link_tag("dir/file")) => %(),
%(stylesheet_link_tag("style", :media => "all")) => %(),
%(stylesheet_link_tag("random.styles", "/css/stylish")) => %(\n)
}
ImagePathToTag = {
%(image_path("xml.png")) => %(/images/xml.png),
}
ImageLinkToTag = {
%(image_tag("xml.png")) => %(
),
%(image_tag("rss.png", :alt => "rss syndication")) => %(
),
%(image_tag("gold.png", :size => "45x70")) => %(
),
%(image_tag("symbolize.png", "size" => "45x70")) => %(
),
%(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(
)
}
def test_auto_discovery
set_to_empty
AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_path
set_to_empty
JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_include
set_to_empty
JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_register_javascript_include_default
ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
assert_dom_equal %(\n\n\n\n), javascript_include_tag(:defaults)
ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2'
assert_dom_equal %(\n\n\n\n\n\n), javascript_include_tag(:defaults)
end
def test_style_path
set_to_empty
StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_style_link
set_to_empty
StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_path
set_to_empty
ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_tag
set_to_empty
ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_should_allow_for_default_settings
set_to_empty
assert_equal '', javascript_include_tag("prototype")
end
def test_should_allow_for_symbols
set_to_empty
assert_equal '', javascript_include_tag(:prototype)
end
def test_should_allow_for_string_settings
set_to_string
assert_equal '', javascript_include_tag("prototype")
end
def test_should_allow_for_hash_settings
set_to_full_hash
assert_equal '', javascript_include_tag("prototype")
assert_equal '', stylesheet_link_tag('scaffold')
assert_equal '
', image_tag("myimage.png")
end
def test_should_go_back_to_defaults_on_partial_hash_settings
set_to_partial_hash
assert_equal '', javascript_include_tag("prototype")
assert_equal '', stylesheet_link_tag('scaffold')
assert_equal '
', image_tag("myimage.png")
end
private
def set_to_string
set_base_host "http://assets.domain.com"
end
def set_to_empty
set_base_host ""
end
def set_to_full_hash
set_base_host(:images => "http://images.domain.com",
:javascripts => "http://javascripts.domain.com",
:stylesheets => "http://stylesheets.domain.com")
end
def set_to_partial_hash
set_base_host(:stylesheets => "http://stylesheets.domain.com")
end
def set_base_host(value)
ActionController::Base.asset_host = value
end
end