File: tilt_erubistemplate_test.rb

package info (click to toggle)
ruby-tilt 2.0.0%2Breally1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 476 kB
  • ctags: 411
  • sloc: ruby: 3,546; makefile: 5
file content (151 lines) | stat: -rw-r--r-- 5,279 bytes parent folder | download | duplicates (2)
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
require 'contest'
require 'tilt'

begin
  require 'erubis'
  class ErubisTemplateTest < Test::Unit::TestCase
    test "registered for '.erubis' files" do
      assert_equal Tilt::ErubisTemplate, Tilt['test.erubis']
      assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis']
    end

    test "registered above ERB" do
      %w[erb rhtml].each do |ext|
        mappings = Tilt.mappings[ext]
        erubis_idx = mappings.index(Tilt::ErubisTemplate)
        erb_idx = mappings.index(Tilt::ERBTemplate)
        assert erubis_idx < erb_idx,
          "#{erubis_idx} should be lower than #{erb_idx}"
      end
    end

    test "preparing and evaluating templates on #render" do
      template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
      assert_equal "Hello World!", template.render
    end

    test "can be rendered more than once" do
      template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
      3.times { assert_equal "Hello World!", template.render }
    end

    test "passing locals" do
      template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' }
      assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
    end

    test "evaluating in an object scope" do
      template = Tilt::ErubisTemplate.new { 'Hey <%= @name %>!' }
      scope = Object.new
      scope.instance_variable_set :@name, 'Joe'
      assert_equal "Hey Joe!", template.render(scope)
    end

    class MockOutputVariableScope
      attr_accessor :exposed_buffer
    end

    test "exposing the buffer to the template by default" do
      begin
        Tilt::ErubisTemplate.default_output_variable = '@_out_buf'
        template = Tilt::ErubisTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
        scope = MockOutputVariableScope.new
        template.render(scope)
        assert_not_nil scope.exposed_buffer
        assert_equal scope.exposed_buffer, 'hey'
      ensure
        Tilt::ErubisTemplate.default_output_variable = '_erbout'
      end
    end

    test "passing a block for yield" do
      template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
      assert_equal "Hey Joe!", template.render { 'Joe' }
    end

    test "backtrace file and line reporting without locals" do
      data = File.read(__FILE__).split("\n__END__\n").last
      fail unless data[0] == ?<
      template = Tilt::ErubisTemplate.new('test.erubis', 11) { data }
      begin
        template.render
        fail 'should have raised an exception'
      rescue => boom
        assert_kind_of NameError, boom
        line = boom.backtrace.grep(/^test\.erubis:/).first
        assert line, "Backtrace didn't contain test.erubis"
        file, line, meth = line.split(":")
        assert_equal '13', line
      end
    end

    test "backtrace file and line reporting with locals" do
      data = File.read(__FILE__).split("\n__END__\n").last
      fail unless data[0] == ?<
      template = Tilt::ErubisTemplate.new('test.erubis', 1) { data }
      begin
        template.render(nil, :name => 'Joe', :foo => 'bar')
        fail 'should have raised an exception'
      rescue => boom
        assert_kind_of RuntimeError, boom
        line = boom.backtrace.first
        file, line, meth = line.split(":")
        assert_equal 'test.erubis', file
        assert_equal '6', line
      end
    end

    test "erubis template options" do
      template = Tilt::ErubisTemplate.new(nil, :pattern => '\{% %\}') { 'Hey {%= @name %}!' }
      scope = Object.new
      scope.instance_variable_set :@name, 'Joe'
      assert_equal "Hey Joe!", template.render(scope)
    end

    test "using an instance variable as the outvar" do
      template = Tilt::ErubisTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
      scope = Object.new
      scope.instance_variable_set(:@buf, 'original value')
      assert_equal '2', template.render(scope)
      assert_equal 'original value', scope.instance_variable_get(:@buf)
    end

    test "using Erubis::EscapedEruby subclass via :engine_class option" do
      template = Tilt::ErubisTemplate.new(nil, :engine_class => ::Erubis::EscapedEruby) { |t| %(<%= "<p>Hello World!</p>" %>) }
      assert_equal "&lt;p&gt;Hello World!&lt;/p&gt;", template.render
    end

    test "using :escape_html => true option" do
      template = Tilt::ErubisTemplate.new(nil, :escape_html => true) { |t| %(<%= "<p>Hello World!</p>" %>) }
      assert_equal "&lt;p&gt;Hello World!&lt;/p&gt;", template.render
    end

    test "using :escape_html => false option" do
      template = Tilt::ErubisTemplate.new(nil, :escape_html => false) { |t| %(<%= "<p>Hello World!</p>" %>) }
      assert_equal "<p>Hello World!</p>", template.render
    end

    test "erubis default does not escape html" do
      template = Tilt::ErubisTemplate.new { |t| %(<%= "<p>Hello World!</p>" %>) }
      assert_equal "<p>Hello World!</p>", template.render
    end

    test "does not modify options argument" do
      options_hash = {:escape_html => true}
      template = Tilt::ErubisTemplate.new(nil, options_hash) { |t| "Hello World!" }
      assert_equal({:escape_html => true}, options_hash)
    end
  end
rescue LoadError => boom
  warn "Tilt::ErubisTemplate (disabled)"
end

__END__
<html>
<body>
  <h1>Hey <%= name %>!</h1>


  <p><% fail %></p>
</body>
</html>