bmizerany / sinatra

(offically at github.com/sinatra/sinatra) Classy web-development dressed in a DSL

This URL has Read+Write access

sinatra / compat / haml_test.rb
100644 234 lines (157 sloc) 4.278 kb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require File.dirname(__FILE__) + '/helper'
 
context "Haml" do
 
  setup do
    Sinatra.application = nil
  end
 
  context "without layouts" do
 
    setup do
      Sinatra.application = nil
    end
 
    specify "should render" do
 
      get '/no_layout' do
        haml '== #{1+1}'
      end
 
      get_it '/no_layout'
      should.be.ok
      body.should == "2\n"
 
    end
  end
 
  context "with layouts" do
 
    setup do
      Sinatra.application = nil
    end
 
    specify "can be inline" do
 
      layout do
        '== This is #{yield}!'
      end
 
      get '/lay' do
        haml 'Blake'
      end
 
      get_it '/lay'
      should.be.ok
      body.should.equal "This is Blake\n!\n"
 
    end
 
    specify "can use named layouts" do
 
      layout :pretty do
        '%h1== #{yield}'
      end
 
      get '/pretty' do
        haml 'Foo', :layout => :pretty
      end
 
      get '/not_pretty' do
        haml 'Bar'
      end
 
      get_it '/pretty'
      body.should.equal "<h1>Foo</h1>\n"
 
      get_it '/not_pretty'
      body.should.equal "Bar\n"
 
    end
 
    specify "can be read from a file if they're not inlined" do
 
      get '/foo' do
        @title = 'Welcome to the Hello Program'
        haml 'Blake', :layout => :foo_layout,
                      :views_directory => File.dirname(__FILE__) + "/views"
      end
 
      get_it '/foo'
      body.should.equal "Welcome to the Hello Program\nHi Blake\n"
 
    end
 
    specify "can be read from file and layout from text" do
      get '/foo' do
        haml 'Test', :layout => '== Foo #{yield}'
      end
 
      get_it '/foo'
 
      body.should.equal "Foo Test\n"
    end
 
  end
 
  context "Templates (in general)" do
 
    setup do
      Sinatra.application = nil
    end
 
    specify "are read from files if Symbols" do
 
      get '/from_file' do
        @name = 'Alena'
        haml :foo, :views_directory => File.dirname(__FILE__) + "/views"
      end
 
      get_it '/from_file'
 
      body.should.equal "You rock Alena!\n"
 
    end
 
    specify "use layout.ext by default if available" do
 
      get '/' do
        haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
      end
 
      get_it '/'
      should.be.ok
      body.should.equal "x This is foo!\n x\n"
 
    end
 
    specify "renders without layout" do
 
      get '/' do
        haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
      end
 
      get_it '/'
      should.be.ok
      body.should.equal "<h1>No Layout!</h1>\n"
 
    end
 
    specify "can render with no layout" do
      layout do
        "X\n= yield\nX"
      end
 
      get '/' do
        haml 'blake', :layout => false
      end
 
      get_it '/'
 
      body.should.equal "blake\n"
    end
 
    specify "raises error if template not found" do
      get '/' do
        haml :not_found
      end
 
      lambda { get_it '/' }.should.raise(Errno::ENOENT)
    end
 
    specify "use layout.ext by default if available" do
 
      template :foo do
        'asdf'
      end
 
      get '/' do
        haml :foo, :layout => false,
                   :views_directory => File.dirname(__FILE__) + "/views/layout_test"
      end
 
      get_it '/'
      should.be.ok
      body.should.equal "asdf\n"
 
    end
 
  end
 
  describe 'Options passed to the HAML interpreter' do
    setup do
      Sinatra.application = nil
    end
 
    specify 'are empty be default' do
 
      get '/' do
        haml 'foo'
      end
 
      Haml::Engine.expects(:new).with('foo', {}).returns(stub(:render => 'foo'))
 
      get_it '/'
      should.be.ok
 
    end
 
    specify 'can be configured by passing :options to haml' do
 
      get '/' do
        haml 'foo', :options => {:format => :html4}
      end
 
      Haml::Engine.expects(:new).with('foo', {:format => :html4}).returns(stub(:render => 'foo'))
 
      get_it '/'
      should.be.ok
 
    end
 
    specify 'can be configured using set_option :haml' do
 
      configure do
        set_option :haml, :format => :html4,
                          :escape_html => true
      end
 
      get '/' do
        haml 'foo'
      end
 
      Haml::Engine.expects(:new).with('foo', {:format => :html4,
        :escape_html => true}).returns(stub(:render => 'foo'))
 
      get_it '/'
      should.be.ok
 
    end
 
  end
 
end