1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/rubyspec/library/erb/result_spec.rb
eregon 95e8c48dd3 Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
  These files can therefore be updated like any other file in MRI.
  Instructions are provided in spec/README.
  [Feature #13156] [ruby-core:79246]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-07 12:04:49 +00:00

86 lines
1.6 KiB
Ruby

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 = '&lt;&gt;'
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 {
MyERB2.new.main2()
}.should raise_error(NameError)
end
end