require 'test_helper'
class ErbTest < MiniTest::Unit::TestCase
def test_erb
assert_equal '- foo = bar', render_erb('<% foo = bar %>')
assert_equal '- foo = bar', render_erb('<% foo = bar -%>')
assert_equal '= h @item.title', render_erb('<%=h @item.title %>')
assert_equal '= h @item.title', render_erb('<%=h @item.title -%>')
end
def test_inline_erb
assert_equal("%p= foo", render_erb("
<%= foo %>
"))
end
def test_non_inline_erb
assert_equal(<
<%= foo %>
HTML
assert_equal(<
<%= foo %>
HTML
assert_equal(<<%= foo %>
HTML
end
def test_erb_in_cdata
assert_equal(< baz]]>
HTML
end
def test_erb_in_script
assert_equal(<
function foo() {
return <%= foo.to_json %>;
}
HTML
end
def test_erb_in_style
assert_equal(<
foo {
bar: <%= "baz" %>;
}
HTML
end
def test_erb_in_line
assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')
end
def test_erb_multi_in_line
assert_equal('foo bar #{baz}! Bang #{bop}.',
render_erb('foo bar <%= baz %>! Bang <%= bop %>.'))
assert_equal('foo bar #{baz}#{bop}!',
render_erb('foo bar <%= baz %><%= bop %>!'))
end
def test_erb_with_html_special_chars
assert_equal '= 3 < 5 ? "OK" : "Your computer is b0rken"',
render_erb('<%= 3 < 5 ? "OK" : "Your computer is b0rken" %>')
end
def test_erb_in_class_attribute
assert_equal "%div{:class => dyna_class} I have a dynamic attribute",
render_erb('I have a dynamic attribute
')
end
def test_erb_in_id_attribute
assert_equal "%div{:id => dyna_id} I have a dynamic attribute",
render_erb('I have a dynamic attribute
')
end
def test_erb_in_attribute_results_in_string_interpolation
assert_equal('%div{:id => "item_#{i}"} Ruby string interpolation FTW',
render_erb('Ruby string interpolation FTW
'))
end
def test_erb_in_attribute_with_trailing_content
assert_equal('%div{:class => "#{12}!"} Bang!',
render_erb('Bang!
'))
end
def test_erb_in_html_escaped_attribute
assert_equal '%div{:class => "foo"} Bang!',
render_erb('">Bang!
')
end
def test_erb_in_attribute_to_multiple_interpolations
assert_equal('%div{:class => "#{12} + #{13}"} Math is super',
render_erb('Math is super
'))
end
def test_whitespace_eating_erb_tags
assert_equal '- form_for', render_erb('<%- form_for -%>')
end
def test_interpolation_in_erb
assert_equal('= "Foo #{bar} baz"', render_erb('<%= "Foo #{bar} baz" %>'))
end
def test_interpolation_in_erb_attrs
assert_equal('%p{:foo => "#{bar} baz"}',
render_erb('">
'))
end
def test_multiline_erb_silent_script
assert_equal(<
<%
foo
bar
baz
%>
foo
ERB
end
def test_multiline_erb_loud_script
assert_equal(<
<%=
foo +
bar.baz.bang +
baz
%>
foo
ERB
end
def test_weirdly_indented_multiline_erb_loud_script
assert_equal(<
<%=
foo +
bar.baz.bang +
baz
%>
foo
ERB
end
def test_two_multiline_erb_loud_scripts
assert_equal(<
<%=
foo +
bar.baz.bang +
baz
%>
<%= foo.bar do
bang
end %>
foo
ERB
end
def test_multiline_then_single_line_erb_loud_scripts
assert_equal(<
<%=
foo +
bar.baz.bang +
baz
%>
<%= foo.bar %>
foo
ERB
end
def test_multiline_erb_but_really_single_line
assert_equal(<
<%=
foo
%>
foo
ERB
end
### Block Parsing
def test_block_parsing
assert_equal(<
bar
<% end %>
ERB
end
def test_block_parsing_with_args
assert_equal(<
bar
<% end %>
ERB
end
def test_block_parsing_with_equals
assert_equal(<
bar
<% end %>
ERB
end
def test_block_parsing_with_modified_end
assert_equal(<
blah
<% end.bip %>
ERB
end
def test_block_parsing_with_modified_end_with_block
assert_equal(<
blah
<% end.bip do %>
brang
<% end %>
ERB
end
def test_multiline_block_opener
assert_equal(<
foo
<% end %>
ERB
end
def test_if_elsif_else_parsing
assert_equal(<
bar
<% elsif bar.foo("zip") %>
baz
<% else %>
bibble
<% end %>
ERB
end
def test_case_when_parsing
assert_equal(<
<% when "bip" %>
bip
<% when "bop" %>
BOP
<% when bizzle.bang.boop.blip %>
BIZZLE BANG BOOP BLIP
<% end %>
ERB
assert_equal(<
bip
<% when "bop" %>
BOP
<% when bizzle.bang.boop.blip %>
BIZZLE BANG BOOP BLIP
<% end %>
ERB
end
def test_begin_rescue_ensure
assert_equal(< e
%p b
- ensure
%p c
HAML
<% begin %>
a
<% rescue FooException => e %>
b
<% ensure %>
c
<% end %>
ERB
end
# Regression
def test_tag_inside_block
assert_equal(<
<% foo.each do %>
<% end %>
ERB
end
def test_silent_inside_block_inside_tag
assert_equal(<
<% foo.each do %>
<% haml_puts "foo" %>
<% end %>
ERB
end
def test_commented_erb_should_not_cause_indentation
assert_equal(<
html2haml and multiline titles
<%=# stylesheet_link_tag :first %>
<%#= stylesheet_link_tag :second %>
<%# stylesheet_link_tag :third %>
<%= stylesheet_link_tag 'another file' %>
ERB
end
def test_can_parse_ruby_19_hashes_as_arguments
erb = "<%= foobar 'foo', {bar: 'baz'} %>"
begin
Haml::HTML::ERB.new(erb)
rescue
flunk "should not raise an error"
end
end
def test_should_wrap_in_silent
assert_equal(< some_variable_or_function \n
HTML
<% some_variable_or_function %>
ERB
end
#comment content is removed by erubis
def test_should_wrap_process_comments_as_empty_lines
assert_equal(<\n
HTML
<%# some_variable_or_function %>
ERB
end
end