#!/usr/bin/env ruby
require 'test/unit'
currentPath = File.dirname(__FILE__)
require File.join( currentPath, '../../lib/masterview' )
require File.join( currentPath, '../test_helper' )
class TestTemplate < Test::Unit::TestCase
def setup
MasterView::IOMgr.erb = MasterView::StringHashMIOTree.new({}, '.rhtml', :logging => true)
end
def test_attr
template = <<-END
foo bar
END
expected = {
'foo/bar' => "foo bar
"
}
assert_template_result expected, template
end
def test_attr_erb
template = <<-END
foo bar
END
expected = {
'foo/bar' => "\">foo bar
"
}
assert_template_result expected, template
end
def test_attr_erb2
template = <<-END
foo bar
END
expected = {
'foo/bar' => "\">foo bar
"
}
assert_template_result expected, template
end
def test_multi
template = <<-END
END
expected = {
'foo/bar' => "<%= h product[:price] %>
",
'baz/caz' => "<%= h product[:name] %>
"
}
assert_template_result expected, template
end
def test_multi_gen_partial
template = <<-END
END
expected = {
'foo/bar' => "<%= h product[:price] %><%= render( :partial => 'baz/caz' ) %>
",
'baz/_caz.rhtml' => "<%= h product[:name] %>
"
}
assert_template_result expected, template
end
def test_replace
template = <<-END
foo bar
END
expected = {
'foo/bar' => "<%= h product[:price] %>
"
}
assert_template_result expected, template
end
def test_block_and_content
template = <<-END
END
expected = {
'foo/bar' => "<% @products.each do |product| %>Name: <%= product.name %> <% end %>
"
}
assert_template_result expected, template
end
def test_form
template = <<-END
END
expected = {
'foo/bar' => "<%= form_tag {:action => 'create'}, :method => \"get\" %>hello world <%= end_form_tag %>
"
}
assert_template_result expected, template
end
def test_link_to
template = <<-END
END
expected = {
'foo/bar' => "<%= link_to 'Hello World', :action => 'show', :id => @product %>
"
}
assert_template_result expected, template
end
def test_link_to_if
template = <<-END
END
expected = {
'foo/bar' => "<%= link_to_if @product_pages.current.previous, 'Previous page', {:page => @product_pages.current.previous } %>
"
}
assert_template_result expected, template
end
def test_submit
template = <<-END
END
expected = {
'foo/bar' => "<%= submit_tag 'Hello World', :foo => 'bar' %>
"
}
assert_template_result expected, template
end
def test_text_field
template = <<-END
END
expected = {
'foo/bar' => "<%= text_field 'product', 'price', :maxlength => 21, :size => 10 %>
"
}
assert_template_result expected, template
end
def test_hidden_field
template = <<-END
END
expected = {
'foo/bar' => "<%= hidden_field 'product', 'price' %>
"
}
assert_template_result expected, template
end
def test_password_field
template = <<-END
END
expected = {
'foo/bar' => "<%= password_field 'product', 'price', :maxlength => 21, :size => 10 %>
"
}
assert_template_result expected, template
end
def test_text_area
template = <<-END
END
expected = {
'foo/bar' => "<%= text_area 'product', 'desc', :class => \"foo\", :cols => 21, :disabled => true, :readonly => true, :rows => 10, :style => \"bar\" %>
"
}
assert_template_result expected, template
end
def test_example
template = <<-END
foo bar
END
expected = {
'foo/bar' => "<%= h product[:price] %>
"
}
assert_template_result expected, template
end
def test_import
template = <<-END
foo bar
END
expected = { }
assert_template_result expected, template
end
def test_import2
template = <<-END
END
expected = {
'cat/dog' => "Hello World
"
}
assert_template_result expected, template
end
def test_import3
template = <<-END
END
expected = {
'egg/flower' => "Wow
",
'foo/bar' => "Hello
"
}
assert_template_result expected, template
end
def test_import_render
template = <<-END
END
expected = {
'foo/bar' => "Hello <%= render( :partial => 'cat/dog' ) %>
",
'egg/flower' => "Wow
"
}
assert_template_result expected, template
end
def test_omit_tag
template = <<-END
END
expected = {
'foo/bar' => "bar
"
}
assert_template_result expected, template
end
def test_omit_tag_eval
template = <<-END
END
expected = {
'foo/bar' => "<% if @hello %>
<% end %>bar <% if @hello %>
<% end %>
"
}
assert_template_result expected, template
end
def test_omit_tag_root
template = <<-END
hello world
END
expected = {
'foo/bar' => " hello world "
}
assert_template_result expected, template
end
def test_omit_tag_root_with_children
template = <<-END
hello world
END
expected = {
'foo/bar' => " hello world "
}
assert_template_result expected, template
end
def test_omit_tag_root_with_nested_generate
template = <<-END
first layer
hello world
END
expected = {
'foo/bar' => " first layer ",
'baz/cat' => "hello world"
}
assert_template_result expected, template
end
def test_omit_tag_root_with_nested_gen_partial
template = <<-END
first layer
hello world
END
expected = {
'foo/bar' => " first layer <%= render( :partial => 'baz/cat' ) %>",
'baz/_cat.rhtml' => "hello world"
}
assert_template_result expected, template
end
# Only simplify elements that are specified in the DTD as being empty, collapsing others can cause parsing
# or rendering problems in browsers. Uses constant XHTMLEmptyElementNameSet to find elements that should be
# collapsed.
# http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-Strict
# xhtml-1.0-Strict empty elements are:
# base, meta, link, hr, br, param, img, area, input, col
def test_simplify_empty_elements
template = <<-END
END
expected = {
'foo/bar' => %q{}
}
assert_template_result expected, template
end
def test_keyword_expansion_template_path
template = <<-END
hello world
]
END
expected = {
'one/two_three.rhtml' => "hello world
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_keyword_expansion_template_path_foo_ext
begin
original_default_extension = MasterView::IOMgr.erb.default_extension
MasterView::IOMgr.erb.default_extension = '.foo'
template = <<-END
hello world
]
END
expected = {
'one/two_three.foo' => "hello world
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
rescue
raise
ensure
MasterView::IOMgr.erb.default_extension = original_default_extension
end
end
def test_keyword_expansion_template_path_original
template = <<-END
hello world
]
END
expected = {
'one/two_three.four' => "hello world
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_keyword_expansion_controller_action
template = <<-END
hello world
END
expected = {
'one/two_three' => "hello world
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_keyword_expansion_gen_partial
template = <<-END
END
expected = {
'foo/bar' => "<%= h product[:price] %><%= render( :partial => 'one/two_three' ) %>
",
'one/_two_three.rhtml' => "<%= h product[:name] %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path_use_src
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag '/myimages/cat.jpg' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path_size
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :size => '20x10' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_missing_width
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :height => '10' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_missing_height
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :width => '20' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path_size_alt
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :alt => 'my cat', :size => '20x10' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path_alt
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :alt => 'my cat' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_image_tag_infer_path_class
template = <<-END
END
expected = {
'foo/bar' => "<%= image_tag 'cat.jpg', :alt => 'my cat', :class => 'blue' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_javascript_include
template = <<-END
END
expected = {
'foo/bar' => "<%= javascript_include_tag 'cat.js' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_javascript_include_symbols
template = <<-END
END
expected = {
'foo/bar' => "<%= javascript_include_tag :defaults %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_javascript_include_infer_path
template = <<-END
END
expected = {
'foo/bar' => "<%= javascript_include_tag 'cat.js' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_javascript_include_infer_path_use_src
template = <<-END
END
expected = {
'foo/bar' => "<%= javascript_include_tag '/myjavascripts/cat.js' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
end