2017-05-07 08:04:49 -04:00
|
|
|
require 'erb'
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "ERB.new" do
|
|
|
|
before :all do
|
|
|
|
@eruby_str = <<'END'
|
|
|
|
<ul>
|
|
|
|
<% list = [1,2,3] %>
|
|
|
|
<% for item in list %>
|
|
|
|
<% if item %>
|
|
|
|
<li><%= item %></li>
|
|
|
|
<% end %>
|
|
|
|
<% end %>
|
|
|
|
</ul>
|
|
|
|
END
|
|
|
|
|
|
|
|
@eruby_str2 = <<'END'
|
|
|
|
<ul>
|
|
|
|
% list = [1,2,3]
|
|
|
|
%for item in list
|
|
|
|
% if item
|
|
|
|
<li><%= item %>
|
|
|
|
<% end %>
|
|
|
|
<% end %>
|
|
|
|
</ul>
|
|
|
|
%%%
|
|
|
|
END
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "compiles eRuby script into ruby code when trim mode is 0 or not specified" do
|
|
|
|
expected = "<ul>\n\n\n\n<li>1</li>\n\n\n\n<li>2</li>\n\n\n\n<li>3</li>\n\n\n</ul>\n"
|
2018-11-12 02:32:21 -05:00
|
|
|
[0, nil].each do |trim_mode|
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "warns invalid trim_mode" do
|
|
|
|
-> do
|
|
|
|
ERBSpecs.new_erb(@eruby_str, trim_mode: '')
|
|
|
|
end.should complain(/Invalid ERB trim mode/)
|
2018-11-12 02:32:21 -05:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "removes '\n' when trim_mode is 1 or '>'" do
|
|
|
|
expected = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\n"
|
|
|
|
[1, '>'].each do |trim_mode|
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes spaces at beginning of line and '\n' when trim_mode is 2 or '<>'" do
|
|
|
|
expected = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\n"
|
|
|
|
[2, '<>'].each do |trim_mode|
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes spaces around '<%- -%>' when trim_mode is '-'" do
|
|
|
|
expected = "<ul>\n <li>1 <li>2 <li>3</ul>\n"
|
|
|
|
input = <<'END'
|
|
|
|
<ul>
|
|
|
|
<%- for item in [1,2,3] -%>
|
|
|
|
<%- if item -%>
|
|
|
|
<li><%= item -%>
|
|
|
|
<%- end -%>
|
|
|
|
<%- end -%>
|
|
|
|
</ul>
|
|
|
|
END
|
|
|
|
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(input, trim_mode: '-').result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-12-01 10:41:50 -05:00
|
|
|
it "does not support '<%-= expr %> even when trim_mode is '-'" do
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
input = <<'END'
|
|
|
|
<p>
|
|
|
|
<%= expr -%>
|
|
|
|
<%-= expr -%>
|
|
|
|
</p>
|
|
|
|
END
|
|
|
|
|
2019-07-27 06:40:09 -04:00
|
|
|
-> {
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(input, trim_mode: '-').result
|
2017-12-01 10:41:50 -05:00
|
|
|
}.should raise_error(SyntaxError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "regards lines starting with '%' as '<% ... %>' when trim_mode is '%'" do
|
|
|
|
expected = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n\n</ul>\n%%\n"
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str2, trim_mode: "%").result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
it "regards lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%>'" do
|
|
|
|
expected = "<ul>\n <li>1 <li>2 <li>3 </ul>\n%%\n"
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str2, trim_mode: '%>').result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it "regard lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%<>'" do
|
|
|
|
expected = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n</ul>\n%%\n"
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(@eruby_str2, trim_mode: '%<>').result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it "regard lines starting with '%' as '<% ... %>' and spaces around '<%- -%>' when trim_mode is '%-'" do
|
|
|
|
expected = "<ul>\n<li>1</li>\n<li>2</li>\n</ul>\n%%\n"
|
|
|
|
input = <<'END'
|
|
|
|
<ul>
|
|
|
|
%list = [1,2]
|
|
|
|
%for item in list
|
|
|
|
<li><%= item %></li>
|
|
|
|
<% end %></ul>
|
|
|
|
%%%
|
|
|
|
END
|
|
|
|
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(input, trim_mode: '%-').result.should == expected
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "changes '_erbout' variable name in the produced source" do
|
|
|
|
input = @eruby_str
|
2021-07-29 16:11:21 -04:00
|
|
|
match_erbout = ERB.new(input, trim_mode: nil).src
|
|
|
|
match_buf = ERB.new(input, trim_mode: nil, eoutvar: 'buf').src
|
2017-05-07 08:04:49 -04:00
|
|
|
match_erbout.gsub("_erbout", "buf").should == match_buf
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it "ignores '<%# ... %>'" do
|
|
|
|
input = <<'END'
|
|
|
|
<%# for item in list %>
|
|
|
|
<b><%#= item %></b>
|
|
|
|
<%# end %>
|
|
|
|
END
|
2018-02-27 15:21:25 -05:00
|
|
|
ERBSpecs.new_erb(input).result.should == "\n<b></b>\n\n"
|
|
|
|
ERBSpecs.new_erb(input, trim_mode: '<>').result.should == "<b></b>\n"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "forget local variables defined previous one" do
|
|
|
|
ERB.new(@eruby_str).result
|
2019-07-27 06:40:09 -04:00
|
|
|
->{ ERB.new("<%= list %>").result }.should raise_error(NameError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|