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
|
require 'erb'
require File.expand_path('../../../spec_helper', __FILE__)
describe "ERB#result" do
it "return the result of compiled ruby code" do
input = <<'END'
<ul>
<% for item in list %>
<li><%= item %>
<% end %>
</ul>
END
expected = <<'END'
<ul>
<li>AAA
<li>BBB
<li>CCC
</ul>
END
erb = ERB.new(input)
list = %w[AAA BBB CCC]
actual = erb.result(binding)
actual.should == expected
end
it "share local variables" do
input = "<% var = 456 %>"
expected = 456
var = 123
ERB.new(input).result(binding)
var.should == expected
end
it "is not able to h() or u() unless including ERB::Util" do
input = "<%=h '<>' %>"
lambda {
ERB.new(input).result()
}.should raise_error(NameError)
end
it "is able to h() or u() if ERB::Util is included" do
class MyERB1
include ERB::Util
def main
input = "<%=h '<>' %>"
return ERB.new(input).result(binding)
end
end
expected = '<>'
actual = MyERB1.new.main()
actual.should == expected
end
it "use TOPLEVEL_BINDING if binding is not passed" do
class MyERB2
include ERB::Util
def main1
#input = "<%= binding.to_s %>"
input = "<%= _xxx_var_ %>"
return ERB.new(input).result()
end
def main2
input = "<%=h '<>' %>"
return ERB.new(input).result()
end
end
eval '_xxx_var_ = 123', TOPLEVEL_BINDING
expected = '123'
MyERB2.new.main1().should == expected
lambda {
actual = MyERB2.new.main2()
}.should raise_error(NameError)
end
#--
#it "does not change current $SAFE even if safe_level is specifiled at ERB#initialize" do
# input = "$SAFE=<%=$SAFE.inspect%>"
# expected = "$SAFE=2"
# safe_level = 2
# erb = ERB.new(input, safe_level)
# curr_safe_level = $SAFE
# erb.result(binding()).should == expected
# $SAFE.should == curr_safe_level # BUG: $SAFE will be changed in current Rubinius
#end
#++
end
|