#!/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
def test_check_box
template = <<-END
END
expected = {
'foo/bar' => "<%= check_box 'cat', 'dog', {:egg => 'chicken'}, 'yes', 'no' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_check_box_object_method
template = <<-END
END
expected = {
'foo/bar' => "<%= check_box 'cat', 'dog' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_check_box_object_method_options
template = <<-END
END
expected = {
'foo/bar' => "<%= check_box 'cat', 'dog', :egg => 'chicken' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_check_box_object_method_class_id
template = <<-END
END
expected = {
'foo/bar' => "<%= check_box 'cat', 'dog', :class => \"green\", :id => \"boo\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_check_box_object_method_options_class_id
template = <<-END
END
expected = {
'foo/bar' => "<%= check_box 'cat', 'dog', :hello => 'world', :class => \"green\", :id => \"boo\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_radio_button
template = <<-END
END
expected = {
'foo/bar' => "<%= radio_button 'cat', 'dog', 'egg' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_radio_button_quoted
template = <<-END
END
expected = {
'foo/bar' => "<%= radio_button 'cat', 'dog', 'egg' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_radio_button_object_method_class_id
template = <<-END
END
expected = {
'foo/bar' => "<%= radio_button 'cat', 'dog', 'egg', :class => \"green\", :id => \"boo\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_radio_button_object_method_options_class_id
template = <<-END
END
expected = {
'foo/bar' => "<%= radio_button 'cat', 'dog', 'egg', :hello => 'world', :class => \"green\", :id => \"boo\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_select
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= select 'product', 'category_id', ['hardware', 'software'], {:hello => 'world'}, :readonly => true %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_select_obj_method_choices
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= select 'product', 'category_id', ['hardware', 'software'] %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_select_obj_method_choices_find
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= select 'product', 'category_id', Categories.find_all.collect{|c| [c.name, c.id]} %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_select_obj_method_choices_find_class_id
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= select 'product', 'category_id', Categories.find_all.collect{|c| [c.name, c.id]}, {:hello => :world}, :readonly => true, :class => \"green\", :id => \"sel1\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_collection_select
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= collection_select 'product', 'category_id', Category.find(:all), 'id', 'name', {:hello => 'world'}, :readonly => true %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_collection_select_short
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= collection_select 'product', 'category_id', Category.find(:all), 'id', 'name' %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_collection_select_symbols
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= collection_select 'product', 'category_id', Category.find(:all), :id, :name, {:hello => 'world'}, :readonly => true %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_collection_select_merging_html
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= collection_select 'product', 'category_id', Category.find(:all), 'id', 'name', {:hello => 'world'}, :readonly => true, :class => \"green\", :id => \"sel1\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
def test_collection_select_no_options_merging_html
template = <<-END
Foo
Bar
END
expected = {
'foo/bar' => "<%= collection_select 'product', 'category_id', Category.find(:all), 'id', 'name', {}, :class => \"green\", :id => \"sel1\" %>
"
}
assert_template_result expected, template, :template_pathname => 'one/two_three.four'
end
end