Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 234 lines (157 sloc) 4.406 kb
520ac2e Haml
Blake Mizerany authored
1 require File.dirname(__FILE__) + '/helper'
2
3 context "Haml" do
4
5 setup do
6 Sinatra.application = nil
7 end
8
9 context "without layouts" do
10
11 setup do
12 Sinatra.application = nil
13 end
14
15 specify "should render" do
16
17 get '/no_layout' do
18 haml '== #{1+1}'
19 end
20
21 get_it '/no_layout'
22 should.be.ok
23 body.should == "2\n"
24
25 end
26 end
27
28 context "with layouts" do
29
30 setup do
31 Sinatra.application = nil
32 end
33
34 specify "can be inline" do
35
36 layout do
7d5bc1f Haml & Erb redo
Blake Mizerany authored
37 '== This is #{yield}!'
520ac2e Haml
Blake Mizerany authored
38 end
39
40 get '/lay' do
41 haml 'Blake'
42 end
43
44 get_it '/lay'
45 should.be.ok
46 body.should.equal "This is Blake\n!\n"
47
48 end
49
50 specify "can use named layouts" do
51
52 layout :pretty do
7d5bc1f Haml & Erb redo
Blake Mizerany authored
53 '%h1== #{yield}'
520ac2e Haml
Blake Mizerany authored
54 end
55
56 get '/pretty' do
57 haml 'Foo', :layout => :pretty
58 end
59
60 get '/not_pretty' do
61 haml 'Bar'
62 end
63
64 get_it '/pretty'
65 body.should.equal "<h1>Foo</h1>\n"
66
67 get_it '/not_pretty'
68 body.should.equal "Bar\n"
69
70 end
71
72 specify "can be read from a file if they're not inlined" do
73
74 get '/foo' do
75 @title = 'Welcome to the Hello Program'
76 haml 'Blake', :layout => :foo_layout,
77 :views_directory => File.dirname(__FILE__) + "/views"
78 end
79
80 get_it '/foo'
81 body.should.equal "Welcome to the Hello Program\nHi Blake\n"
82
83 end
7d5bc1f Haml & Erb redo
Blake Mizerany authored
84
85 specify "can be read from file and layout from text" do
86 get '/foo' do
87 haml 'Test', :layout => '== Foo #{yield}'
88 end
89
90 get_it '/foo'
91
92 body.should.equal "Foo Test\n"
93 end
520ac2e Haml
Blake Mizerany authored
94
95 end
96
7d5bc1f Haml & Erb redo
Blake Mizerany authored
97 context "Templates (in general)" do
98
ccbb070 FIX: Render without layout
Blake Mizerany authored
99 setup do
100 Sinatra.application = nil
101 end
102
7d5bc1f Haml & Erb redo
Blake Mizerany authored
103 specify "are read from files if Symbols" do
104
105 get '/from_file' do
106 @name = 'Alena'
107 haml :foo, :views_directory => File.dirname(__FILE__) + "/views"
108 end
109
110 get_it '/from_file'
111
112 body.should.equal "You rock Alena!\n"
113
114 end
115
116 specify "use layout.ext by default if available" do
117
ccbb070 FIX: Render without layout
Blake Mizerany authored
118 get '/' do
7d5bc1f Haml & Erb redo
Blake Mizerany authored
119 haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
120 end
121
ccbb070 FIX: Render without layout
Blake Mizerany authored
122 get_it '/'
7d5bc1f Haml & Erb redo
Blake Mizerany authored
123 should.be.ok
124 body.should.equal "x This is foo!\n x\n"
125
126 end
127
ccbb070 FIX: Render without layout
Blake Mizerany authored
128 specify "renders without layout" do
129
130 get '/' do
131 haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
132 end
133
134 get_it '/'
135 should.be.ok
136 body.should.equal "<h1>No Layout!</h1>\n"
137
138 end
139
898b36e @bmizerany allow user to prevent layout render
bmizerany authored
140 specify "can render with no layout" do
141 layout do
142 "X\n= yield\nX"
143 end
144
145 get '/' do
146 haml 'blake', :layout => false
147 end
148
149 get_it '/'
150
151 body.should.equal "blake\n"
152 end
153
ccbb070 FIX: Render without layout
Blake Mizerany authored
154 specify "raises error if template not found" do
155 get '/' do
156 haml :not_found
157 end
158
159 lambda { get_it '/' }.should.raise(Errno::ENOENT)
160 end
161
6bda16b @bmizerany added `template` to DSL
bmizerany authored
162 specify "use layout.ext by default if available" do
163
164 template :foo do
165 'asdf'
166 end
167
168 get '/' do
169 haml :foo, :layout => false,
170 :views_directory => File.dirname(__FILE__) + "/views/layout_test"
171 end
172
173 get_it '/'
174 should.be.ok
175 body.should.equal "asdf\n"
176
177 end
178
7d5bc1f Haml & Erb redo
Blake Mizerany authored
179 end
180
2399905 @sr Add the possibility to configure options that are passed to Haml::Engine...
sr authored
181 describe 'Options passed to the HAML interpreter' do
182 setup do
183 Sinatra.application = nil
184 end
185
186 specify 'are empty be default' do
187
188 get '/' do
189 haml 'foo'
190 end
191
192 Haml::Engine.expects(:new).with('foo', {}).returns(stub(:render => 'foo'))
193
194 get_it '/'
195 should.be.ok
196
197 end
198
199 specify 'can be configured by passing :options to haml' do
200
201 get '/' do
202 haml 'foo', :options => {:format => :html4}
203 end
204
205 Haml::Engine.expects(:new).with('foo', {:format => :html4}).returns(stub(:render => 'foo'))
206
207 get_it '/'
208 should.be.ok
209
210 end
211
212 specify 'can be configured using set_option :haml' do
213
214 configure do
215 set_option :haml, :format => :html4,
216 :escape_html => true
217 end
218
219 get '/' do
220 haml 'foo'
221 end
222
223 Haml::Engine.expects(:new).with('foo', {:format => :html4,
224 :escape_html => true}).returns(stub(:render => 'foo'))
225
226 get_it '/'
227 should.be.ok
228
229 end
230
231 end
232
520ac2e Haml
Blake Mizerany authored
233 end
Something went wrong with that request. Please try again.