1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
require 'erb'
require File.expand_path('../../../spec_helper', __FILE__)
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"
[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 = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\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 = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\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 = "<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
ERB.new(input, nil, '-').result.should == expected
end
it "not support '<%-= expr %> even when trim_mode is '-'" do
input = <<'END'
<p>
<%= expr -%>
<%-= expr -%>
</p>
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 = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n\n</ul>\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 = "<ul>\n <li>1 <li>2 <li>3 </ul>\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 = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n</ul>\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 = "<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
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 = "<b><%=- 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 %>
<b><%#= item %></b>
<%# end %>
END
ERB.new(input).result.should == "\n<b></b>\n\n"
ERB.new(input, nil, '<>').result.should == "<b></b>\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
|