require File.dirname(__FILE__) + '/../abstract_unit'
class AssetTagHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::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
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)
}
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")) => %(/images/xml.png),
}
ImageLinkToTag = {
%(image_tag("xml")) => %(),
%(image_tag("rss", :alt => "rss syndication")) => %(),
%(image_tag("gold", :size => "45x70")) => %(),
}
def test_auto_discovery
AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_path
JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_include
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
StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_style_link
StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_path
ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_tag
ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
end
class AssetTagHelperNonVhostTest < Test::Unit::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::AssetTagHelper
def setup
@controller = Class.new do
attr_accessor :request
def url_for(options, *parameters_for_method_reference)
"http://www.example.com/calloboration/hieraki"
end
end.new
@request = Class.new do
def relative_url_root
"/calloboration/hieraki"
end
end.new
@controller.request = @request
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
end
AutoDiscoveryToTag = {
%(auto_discovery_link_tag(:rss, :action => "feed")) => %(),
%(auto_discovery_link_tag(:atom)) => %(),
%(auto_discovery_link_tag) => %(),
}
JavascriptPathToTag = {
%(javascript_path("xmlhr")) => %(/calloboration/hieraki/javascripts/xmlhr.js),
}
JavascriptIncludeToTag = {
%(javascript_include_tag("xmlhr")) => %(),
%(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(\n),
%(javascript_include_tag(:defaults)) => %(\n\n\n)
}
StylePathToTag = {
%(stylesheet_path("style")) => %(/calloboration/hieraki/stylesheets/style.css),
}
StyleLinkToTag = {
%(stylesheet_link_tag("style")) => %(),
%(stylesheet_link_tag("random.styles", "/css/stylish")) => %(\n)
}
ImagePathToTag = {
%(image_path("xml")) => %(/calloboration/hieraki/images/xml.png),
}
ImageLinkToTag = {
%(image_tag("xml")) => %(),
%(image_tag("rss", :alt => "rss syndication")) => %(),
%(image_tag("gold", :size => "45x70")) => %(),
%(image_tag("http://www.example.com/images/icon.gif")) => %(),
}
def test_auto_discovery
AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_path
JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_javascript_include
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
StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_style_link
StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_path
ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
def test_image_tag
ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
# Assigning a default alt tag should not cause an exception to be raised
assert_nothing_raised { image_tag('') }
end
def test_stylesheet_with_asset_host_already_encoded
ActionController::Base.asset_host = "http://foo.example.com"
result = stylesheet_link_tag("http://bar.example.com/stylesheets/style.css")
assert_dom_equal(
%(),
result)
ensure
ActionController::Base.asset_host = ""
end
end