|
require 'minitest/autorun' |
|
require 'erb' |
|
|
|
class ErbTest < Minitest::Test |
|
def test_all_trim_modes_on_single_line_ruby_code_and_expressions |
|
erb_template = "<% if true %> |
|
<%= hello_world_method %> |
|
<% end %>" |
|
|
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, nil) |
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-') |
|
assert_equal "hello world", output_with_trim_mode(erb_template, '>') |
|
assert_equal "hello world", output_with_trim_mode(erb_template, '<>') |
|
end |
|
|
|
def test_all_trim_modes_on_multi_line_ruby_code_and_expressions |
|
erb_template = "<% |
|
if true |
|
%> |
|
<%= |
|
hello_world_method |
|
%> |
|
<% |
|
end |
|
%>" |
|
|
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, nil) |
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-') |
|
assert_equal "hello world", output_with_trim_mode(erb_template, '>') |
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '<>') |
|
end |
|
|
|
def test_explicit_trim_mode_on_ruby_expression |
|
erb_template = "<% if true %> |
|
<%= hello_world_method -%> |
|
<% end %> |
|
" |
|
|
|
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-') |
|
end |
|
|
|
def test_explicit_trim_mode_on_ruby_expression_and_if_ruby_statement |
|
erb_template = "<% if true -%> |
|
<%= hello_world_method -%> |
|
<% end %> |
|
" |
|
|
|
assert_equal "hello world\n", output_with_trim_mode(erb_template, '-') |
|
end |
|
|
|
def test_explicit_trim_mode_on_ruby_expression_and_end_ruby_statement |
|
erb_template = "<% if true %> |
|
<%= hello_world_method -%> |
|
<% end -%> |
|
" |
|
|
|
assert_equal "\nhello world", output_with_trim_mode(erb_template, '-') |
|
end |
|
|
|
def test_explicit_trim_mode_on_ruby_expression_and_if_and_end_ruby_statements |
|
erb_template = "<% if true -%> |
|
<%= hello_world_method -%> |
|
<% end -%> |
|
" |
|
|
|
assert_equal 'hello world', output_with_trim_mode(erb_template, '-') |
|
end |
|
|
|
def test_combining_percentage_and_no_trim_mode |
|
erb_template = "% if true |
|
<%= hello_world_method %> |
|
% end" |
|
|
|
assert_equal "hello world\n", output_with_trim_mode(erb_template, '%') |
|
end |
|
|
|
def test_combining_percentage_and_trim_line_1 |
|
erb_template = "% if true |
|
<%= hello_world_method %> |
|
% end" |
|
|
|
assert_equal 'hello world', output_with_trim_mode(erb_template, '%>') |
|
end |
|
|
|
def test_combining_percentage_and_trim_line_2 |
|
erb_template = "% if true |
|
<%= hello_world_method %> |
|
% end" |
|
|
|
assert_equal 'hello world', output_with_trim_mode(erb_template, '%<>') |
|
end |
|
|
|
def test_combining_percentage_and_explicit_trim_line |
|
erb_template = "% if true |
|
<%= hello_world_method -%> |
|
% end" |
|
|
|
assert_equal 'hello world', output_with_trim_mode(erb_template, '%-') |
|
end |
|
|
|
private |
|
|
|
def hello_world_method |
|
'hello world' |
|
end |
|
|
|
def output_with_trim_mode(erb_template, trim_mode) |
|
safe_mode = nil |
|
ERB.new(erb_template, safe_mode, trim_mode).result(binding) |
|
end |
|
end |