File: tilt_hamltemplate_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 (144 lines) | stat: -rw-r--r-- 4,638 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
require 'contest'
require 'tilt'

begin
  class ::MockError < NameError
  end

  require 'haml'

  class HamlTemplateTest < Test::Unit::TestCase
    test "registered for '.haml' files" do
      assert_equal Tilt::HamlTemplate, Tilt['test.haml']
    end

    test "preparing and evaluating templates on #render" do
      template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
      assert_equal "<p>Hello World!</p>\n", template.render
    end

    test "can be rendered more than once" do
      template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
      3.times { assert_equal "<p>Hello World!</p>\n", template.render }
    end

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

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

    test "passing a block for yield" do
      template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
      assert_equal "<p>Hey Joe!</p>\n", 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::HamlTemplate.new('test.haml', 10) { data }
      begin
        template.render
        fail 'should have raised an exception'
      rescue => boom
        assert_kind_of NameError, boom
        line = boom.backtrace.grep(/^test\.haml:/).first
        assert line, "Backtrace didn't contain test.haml"
        file, line, meth = line.split(":")
        assert_equal '12', 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::HamlTemplate.new('test.haml') { data }
      begin
        res = template.render(Object.new, :name => 'Joe', :foo => 'bar')
      rescue => boom
        assert_kind_of MockError, boom
        line = boom.backtrace.first
        file, line, meth = line.split(":")
        assert_equal 'test.haml', file
        assert_equal '5', line
      end
    end
  end

  class CompiledHamlTemplateTest < Test::Unit::TestCase
    class Scope
    end

    test "compiling template source to a method" do
      template = Tilt::HamlTemplate.new { |t| "Hello World!" }
      template.render(Scope.new)
      method = template.send(:compiled_method, [])
      assert_kind_of UnboundMethod, method
    end

    test "passing locals" do
      template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
      assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new, :name => 'Joe')
    end

    test "evaluating in an object scope" do
      template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
      scope = Scope.new
      scope.instance_variable_set :@name, 'Joe'
      assert_equal "<p>Hey Joe!</p>\n", template.render(scope)
    end

    test "passing a block for yield" do
      template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
      assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new) { '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::HamlTemplate.new('test.haml', 10) { data }
      begin
        template.render(Scope.new)
        fail 'should have raised an exception'
      rescue => boom
        assert_kind_of NameError, boom
        line = boom.backtrace.grep(/^test\.haml:/).first
        assert line, "Backtrace didn't contain test.haml"
        file, line, meth = line.split(":")
        assert_equal '12', 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::HamlTemplate.new('test.haml') { data }
      begin
        res = template.render(Scope.new, :name => 'Joe', :foo => 'bar')
      rescue => boom
        assert_kind_of MockError, boom
        line = boom.backtrace.first
        file, line, meth = line.split(":")
        assert_equal 'test.haml', file
        assert_equal '5', line
      end
    end
  end
rescue LoadError => boom
  warn "Tilt::HamlTemplate (disabled)"
end

__END__
%html
  %body
    %h1= "Hey #{name}"

    = raise MockError

    %p we never get here