require 'erb'
describe "ERB.new" do
before :all do
@eruby_str = <<'END'
<% list = [1,2,3] %>
<% for item in list %>
<% if item %>
- <%= item %>
<% end %>
<% end %>
END
@eruby_str2 = <<'END'
% list = [1,2,3]
%for item in list
% if item
- <%= item %>
<% end %>
<% end %>
%%%
END
end
it "compiles eRuby script into ruby code when trim mode is 0 or not specified" do
expected = "\n\n\n\n- 1
\n\n\n\n- 2
\n\n\n\n- 3
\n\n\n
\n"
[0, '', nil].each do |trim_mode|
ERB.new(@eruby_str, nil, trim_mode).result.should == expected
end
end
it "removes '\n' when trim_mode is 1 or '>'" do
expected = "\n"
[1, '>'].each do |trim_mode|
ERB.new(@eruby_str, nil, trim_mode).result.should == expected
end
end
it "removes spaces at beginning of line and '\n' when trim_mode is 2 or '<>'" do
expected = "\n"
[2, '<>'].each do |trim_mode|
ERB.new(@eruby_str, nil, trim_mode).result.should == expected
end
end
it "removes spaces around '<%- -%>' when trim_mode is '-'" do
expected = "\n"
input = <<'END'
<%- for item in [1,2,3] -%>
<%- if item -%>
- <%= item -%>
<%- end -%>
<%- end -%>
END
ERB.new(input, nil, '-').result.should == expected
end
it "not support '<%-= expr %> even when trim_mode is '-'" do
input = <<'END'
<%= expr -%>
<%-= expr -%>
END
lambda { ERB.new(input, nil, '-').result }.should raise_error
end
ruby_bug "#213", "1.8.7" do
it "regards lines starting with '%' as '<% ... %>' when trim_mode is '%'" do
expected = "\n - 1\n \n
- 2\n \n
- 3\n \n\n
\n%%\n"
ERB.new(@eruby_str2, nil, "%").result.should == expected
end
end
it "regards lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%>'" do
expected = "\n%%\n"
ERB.new(@eruby_str2, nil, '%>').result.should == expected
end
it "regard lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%<>'" do
expected = "\n%%\n"
ERB.new(@eruby_str2, nil, '%<>').result.should == expected
end
it "regard lines starting with '%' as '<% ... %>' and spaces around '<%- -%>' when trim_mode is '%-'" do
expected = "\n%%\n"
input = <<'END'
%list = [1,2]
%for item in list
- <%= item %>
<% end %>
%%%
END
trim_mode = '%-'
ERB.new(input, nil, '%-').result.should == expected
end
not_compliant_on :rubinius do
it "accepts a safe level as second argument" do
input = "<%=- 2+2 %>"
safe_level = 3
lambda { ERB.new(input, safe_level).result }.should_not raise_error
end
end
it "changes '_erbout' variable name in the produced source" do
input = @eruby_str
match_erbout = ERB.new(input, nil, nil).src
match_buf = ERB.new(input, nil, nil, 'buf').src
match_erbout.gsub("_erbout", "buf").should == match_buf
end
it "ignores '<%# ... %>'" do
input = <<'END'
<%# for item in list %>
<%#= item %>
<%# end %>
END
ERB.new(input).result.should == "\n\n\n"
ERB.new(input, nil, '<>').result.should == "\n"
end
ruby_version_is ""..."2.0" do
it "remember local variables defined previous one" do
ERB.new(@eruby_str).result
ERB.new("<%= list.inspect %>").result.should == "[1, 2, 3]"
end
end
ruby_version_is "2.0" do
it "forget local variables defined previous one" do
ERB.new(@eruby_str).result
lambda{ ERB.new("<%= list %>").result }.should raise_error(NameError)
end
end
end