1require 'erb'
2require_relative '../../spec_helper'
3
4describe "ERB#result" do
5
6
7  it "return the result of compiled ruby code" do
8    input = <<'END'
9<ul>
10<% for item in list %>
11  <li><%= item %>
12<% end %>
13</ul>
14END
15    expected = <<'END'
16<ul>
17
18  <li>AAA
19
20  <li>BBB
21
22  <li>CCC
23
24</ul>
25END
26    erb = ERB.new(input)
27    list = %w[AAA BBB CCC]
28    actual = erb.result(binding)
29    actual.should == expected
30  end
31
32
33  it "share local variables" do
34    input = "<% var = 456 %>"
35    expected = 456
36    var = 123
37    ERB.new(input).result(binding)
38    var.should == expected
39  end
40
41
42  it "is not able to h() or u() unless including ERB::Util" do
43    input = "<%=h '<>' %>"
44    lambda {
45      ERB.new(input).result()
46    }.should raise_error(NameError)
47  end
48
49
50  it "is able to h() or u() if ERB::Util is included" do
51    myerb1 = Class.new do
52      include ERB::Util
53      def main
54        input = "<%=h '<>' %>"
55        return ERB.new(input).result(binding)
56      end
57    end
58    expected = '&lt;&gt;'
59    actual = myerb1.new.main()
60    actual.should == expected
61  end
62
63
64  it "use TOPLEVEL_BINDING if binding is not passed" do
65    myerb2 = Class.new do
66      include ERB::Util
67      def main1
68        #input = "<%= binding.to_s %>"
69        input = "<%= _xxx_var_ %>"
70        return ERB.new(input).result()
71      end
72      def main2
73        input = "<%=h '<>' %>"
74        return ERB.new(input).result()
75      end
76    end
77
78    eval '_xxx_var_ = 123', TOPLEVEL_BINDING
79    expected = '123'
80    myerb2.new.main1().should == expected
81
82    lambda {
83      myerb2.new.main2()
84    }.should raise_error(NameError)
85  end
86end
87