padrino-helpers 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-helpers.rb +4 -1
  3. data/lib/padrino-helpers/asset_tag_helpers.rb +17 -14
  4. data/lib/padrino-helpers/breadcrumb_helpers.rb +6 -6
  5. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +119 -163
  6. data/lib/padrino-helpers/form_builder/deprecated_builder_methods.rb +92 -0
  7. data/lib/padrino-helpers/form_helpers.rb +66 -347
  8. data/lib/padrino-helpers/form_helpers/errors.rb +138 -0
  9. data/lib/padrino-helpers/form_helpers/options.rb +97 -0
  10. data/lib/padrino-helpers/form_helpers/security.rb +70 -0
  11. data/lib/padrino-helpers/output_helpers.rb +1 -1
  12. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +1 -1
  13. data/lib/padrino-helpers/render_helpers.rb +10 -9
  14. data/lib/padrino-helpers/tag_helpers.rb +2 -1
  15. data/lib/padrino/rendering.rb +378 -0
  16. data/lib/padrino/rendering/extensions/erubis.rb +74 -0
  17. data/lib/padrino/rendering/extensions/haml.rb +29 -0
  18. data/lib/padrino/rendering/extensions/slim.rb +21 -0
  19. data/padrino-helpers.gemspec +2 -1
  20. data/test/fixtures/apps/.components +6 -0
  21. data/test/fixtures/apps/.gitignore +7 -0
  22. data/test/fixtures/apps/render.rb +25 -0
  23. data/test/fixtures/apps/views/article/comment/show.slim +1 -0
  24. data/test/fixtures/apps/views/blog/post.erb +1 -0
  25. data/test/fixtures/apps/views/layouts/specific.erb +1 -0
  26. data/test/fixtures/apps/views/test/post.erb +1 -0
  27. data/test/fixtures/layouts/layout.erb +1 -0
  28. data/test/fixtures/markup_app/app.rb +0 -1
  29. data/test/fixtures/render_app/app.rb +25 -1
  30. data/test/fixtures/render_app/views/_unsafe.html.builder +2 -0
  31. data/test/fixtures/render_app/views/_unsafe_object.html.builder +2 -0
  32. data/test/fixtures/render_app/views/ruby_block_capture_erb.erb +1 -0
  33. data/test/fixtures/render_app/views/ruby_block_capture_haml.haml +1 -0
  34. data/test/fixtures/render_app/views/ruby_block_capture_slim.slim +1 -0
  35. data/test/helper.rb +65 -1
  36. data/test/test_asset_tag_helpers.rb +83 -79
  37. data/test/test_breadcrumb_helpers.rb +20 -20
  38. data/test/test_form_builder.rb +196 -196
  39. data/test/test_form_helpers.rb +163 -163
  40. data/test/test_format_helpers.rb +65 -65
  41. data/test/test_locale.rb +1 -1
  42. data/test/test_number_helpers.rb +10 -11
  43. data/test/test_output_helpers.rb +28 -28
  44. data/test/test_render_helpers.rb +89 -35
  45. data/test/test_rendering.rb +683 -0
  46. data/test/test_rendering_extensions.rb +14 -0
  47. data/test/test_tag_helpers.rb +23 -23
  48. metadata +57 -5
@@ -6,70 +6,109 @@ describe "RenderHelpers" do
6
6
  RenderDemo
7
7
  end
8
8
 
9
- context 'for #partial method and object' do
10
- setup { visit '/partial/object' }
11
- should "render partial html with object" do
9
+ describe 'for #partial method and object' do
10
+ before { visit '/partial/object' }
11
+ it 'should render partial html with object' do
12
12
  assert_have_selector "h1", :content => "User name is John"
13
13
  end
14
- should "have no counter index for single item" do
14
+ it 'should have no counter index for single item' do
15
15
  assert_have_no_selector "p", :content => "My counter is 1", :count => 1
16
16
  end
17
- should "include extra locals information" do
17
+ it 'should include extra locals information' do
18
18
  assert_have_selector 'p', :content => "Extra is bar"
19
19
  end
20
20
  end
21
21
 
22
- context 'for #partial method and collection' do
23
- setup { visit '/partial/collection' }
24
- should "render partial html with collection" do
22
+ describe 'for #partial method and collection' do
23
+ before { visit '/partial/collection' }
24
+ it 'should render partial html with collection' do
25
25
  assert_have_selector "h1", :content => "User name is John"
26
26
  assert_have_selector "h1", :content => "User name is Billy"
27
27
  end
28
- should "include counter which contains item index" do
28
+ it 'should include counter which contains item index' do
29
29
  assert_have_selector "p", :content => "My counter is 1"
30
30
  assert_have_selector "p", :content => "My counter is 2"
31
31
  end
32
- should "include extra locals information" do
32
+ it 'should include extra locals information' do
33
33
  assert_have_selector 'p', :content => "Extra is bar"
34
34
  end
35
35
  end
36
36
 
37
- context 'for #partial method and locals' do
38
- setup { visit '/partial/locals' }
39
- should "render partial html with locals" do
37
+ describe 'for #partial with ext and collection' do
38
+ before { visit '/partial/collection.ext' }
39
+ it 'should not fail horribly with `invalid locals key` RuntimeError' do
40
40
  assert_have_selector "h1", :content => "User name is John"
41
41
  end
42
- should "have no counter index for single item" do
42
+ end
43
+
44
+ describe 'for #partial method and locals' do
45
+ before { visit '/partial/locals' }
46
+ it 'should render partial html with locals' do
47
+ assert_have_selector "h1", :content => "User name is John"
48
+ end
49
+ it 'should have no counter index for single item' do
43
50
  assert_have_no_selector "p", :content => "My counter is 1", :count => 1
44
51
  end
45
- should "include extra locals information" do
52
+ it 'should include extra locals information' do
46
53
  assert_have_selector 'p', :content => "Extra is bar"
47
54
  end
48
55
  end
49
56
 
50
- context 'for #partial method taking a path starting with forward slash' do
51
- setup { visit '/partial/foward_slash' }
52
- should "render partial without throwing an error" do
57
+ describe 'for #partial method taking a path starting with forward slash' do
58
+ before { visit '/partial/foward_slash' }
59
+ it 'should render partial without throwing an error' do
60
+ assert_have_selector "h1", :content => "User name is John"
61
+ end
62
+ end
63
+
64
+ describe 'for #partial method with unsafe engine' do
65
+ it 'should render partial without escaping it' do
66
+ visit '/partial/unsafe'
67
+ assert_have_selector "h1", :content => "User name is John"
68
+ end
69
+ it 'should render partial object without escaping it' do
70
+ visit '/partial/unsafe_one'
71
+ assert_have_selector "h1", :content => "User name is Mary"
72
+ end
73
+ it 'should render partial collection without escaping it' do
74
+ visit '/partial/unsafe_many'
75
+ assert_have_selector "h1", :content => "User name is John"
76
+ assert_have_selector "h1", :content => "User name is Mary"
77
+ end
78
+ it 'should render unsafe partial without escaping it' do
79
+ visit '/partial/unsafe?block=%3Cevil%3E'
80
+ assert_have_selector "h1", :content => "User name is John"
81
+ assert_have_selector "evil"
82
+ end
83
+ it 'should render unsafe partial object without escaping it' do
84
+ visit '/partial/unsafe_one?block=%3Cevil%3E'
85
+ assert_have_selector "h1", :content => "User name is Mary"
86
+ assert_have_selector "evil"
87
+ end
88
+ it 'should render unsafe partial collection without escaping it' do
89
+ visit '/partial/unsafe_many?block=%3Cevil%3E'
53
90
  assert_have_selector "h1", :content => "User name is John"
91
+ assert_have_selector "h1", :content => "User name is Mary"
92
+ assert_have_selector "evil"
54
93
  end
55
94
  end
56
95
 
57
- context 'render with block' do
58
- should 'render slim with block' do
96
+ describe 'render with block' do
97
+ it 'should render slim with block' do
59
98
  visit '/render_block_slim'
60
99
  assert_have_selector 'h1', :content => 'prefix'
61
100
  assert_have_selector 'h3', :content => 'postfix'
62
101
  assert_have_selector '.slim-block'
63
102
  assert_have_selector 'div', :content => 'go block!'
64
103
  end
65
- should 'render erb with block' do
104
+ it 'should render erb with block' do
66
105
  visit '/render_block_erb'
67
106
  assert_have_selector 'h1', :content => 'prefix'
68
107
  assert_have_selector 'h3', :content => 'postfix'
69
108
  assert_have_selector '.erb-block'
70
109
  assert_have_selector 'div', :content => 'go block!'
71
110
  end
72
- should 'render haml with block' do
111
+ it 'should render haml with block' do
73
112
  visit '/render_block_haml'
74
113
  assert_have_selector 'h1', :content => 'prefix'
75
114
  assert_have_selector 'h3', :content => 'postfix'
@@ -78,8 +117,8 @@ describe "RenderHelpers" do
78
117
  end
79
118
  end
80
119
 
81
- context 'partial with block' do
82
- should 'show partial slim with block' do
120
+ describe 'partial with block' do
121
+ it 'should show partial slim with block' do
83
122
  visit '/partial_block_slim'
84
123
  assert_have_selector 'h1', :content => 'prefix'
85
124
  assert_have_selector 'h3', :content => 'postfix'
@@ -87,7 +126,7 @@ describe "RenderHelpers" do
87
126
  assert_have_selector 'div', :content => 'go block!'
88
127
  assert_have_selector 'div.deep', :content => 'Done'
89
128
  end
90
- should 'show partial erb with block' do
129
+ it 'should show partial erb with block' do
91
130
  visit '/partial_block_erb'
92
131
  assert_have_selector 'h1', :content => 'prefix'
93
132
  assert_have_selector 'h3', :content => 'postfix'
@@ -95,7 +134,7 @@ describe "RenderHelpers" do
95
134
  assert_have_selector 'div', :content => 'go block!'
96
135
  assert_have_selector 'div.deep', :content => 'Done'
97
136
  end
98
- should 'show partial haml with block' do
137
+ it 'should show partial haml with block' do
99
138
  visit '/partial_block_haml'
100
139
  assert_have_selector 'h1', :content => 'prefix'
101
140
  assert_have_selector 'h3', :content => 'postfix'
@@ -105,8 +144,8 @@ describe "RenderHelpers" do
105
144
  end
106
145
  end
107
146
 
108
- context 'for #current_engine method' do
109
- should 'detect correctly current engine for a padrino application' do
147
+ describe 'for #current_engine method' do
148
+ it 'should detect correctly current engine for a padrino application' do
110
149
  visit '/current_engine'
111
150
  assert_have_selector 'p.start', :content => "haml"
112
151
  assert_have_selector 'p.haml span', :content => "haml"
@@ -115,7 +154,7 @@ describe "RenderHelpers" do
115
154
  assert_have_selector 'p.end', :content => "haml"
116
155
  end
117
156
 
118
- should "detect correctly current engine for explicit engine on partials" do
157
+ it 'should detect correctly current engine for explicit engine on partials' do
119
158
  visit '/explicit_engine'
120
159
  assert_have_selector 'p.start', :content => "haml"
121
160
  assert_have_selector 'p.haml span', :content => "haml"
@@ -124,38 +163,53 @@ describe "RenderHelpers" do
124
163
  assert_have_selector 'p.end', :content => "haml"
125
164
  end
126
165
 
127
- should "capture slim template once and only once" do
166
+ it 'should capture slim template once and only once' do
128
167
  $number_of_captures = 0
129
168
  visit '/double_capture_slim'
130
169
  assert_equal 1,$number_of_captures
131
170
  end
132
171
 
133
- should "capture haml template once and only once" do
172
+ it 'should capture haml template once and only once' do
134
173
  $number_of_captures = 0
135
174
  visit '/double_capture_haml'
136
175
  assert_equal 1,$number_of_captures
137
176
  end
138
177
 
139
- should "capture erb template once and only once" do
178
+ it 'should capture erb template once and only once' do
140
179
  $number_of_captures = 0
141
180
  visit '/double_capture_erb'
142
181
  assert_equal 1,$number_of_captures
143
182
  end
144
183
 
145
- should "fail on wrong erb usage" do
184
+ it 'should fail on wrong erb usage' do
146
185
  assert_raises(SyntaxError) do
147
186
  visit '/wrong_capture_erb'
148
187
  end
149
188
  end
150
189
 
151
- should "ignore wrong haml usage" do
190
+ it 'should ignore wrong haml usage' do
152
191
  visit '/wrong_capture_haml'
153
192
  assert_have_no_selector 'p', :content => 'this is wrong'
154
193
  end
155
194
 
156
- should "ignore wrong slim usage" do
195
+ it 'should ignore wrong slim usage' do
157
196
  visit '/wrong_capture_slim'
158
197
  assert_have_no_selector 'p', :content => 'this is wrong'
159
198
  end
199
+
200
+ it 'should support weird ruby blocks in erb' do
201
+ visit '/ruby_block_capture_erb'
202
+ assert_have_selector 'b', :content => 'c'
203
+ end
204
+
205
+ it 'should support weird ruby blocks in haml' do
206
+ visit '/ruby_block_capture_haml'
207
+ assert_have_selector 'b', :content => 'c'
208
+ end
209
+
210
+ it 'should support weird ruby blocks in slim' do
211
+ visit '/ruby_block_capture_slim'
212
+ assert_have_selector 'b', :content => 'c'
213
+ end
160
214
  end
161
215
  end
@@ -0,0 +1,683 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require 'slim'
3
+
4
+ describe "Rendering" do
5
+ def setup
6
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = false
7
+ I18n.enforce_available_locales = true
8
+ end
9
+
10
+ def teardown
11
+ I18n.locale = :en
12
+ remove_views
13
+ end
14
+
15
+ describe 'for application layout functionality' do
16
+
17
+ it 'should get no layout' do
18
+ mock_app do
19
+ get("/"){ "no layout" }
20
+ end
21
+
22
+ get "/"
23
+ assert_equal "no layout", body
24
+ end
25
+
26
+ it 'should be compatible with sinatra layout' do
27
+ mock_app do
28
+ layout do
29
+ "this is a <%= yield %>"
30
+ end
31
+
32
+ get("/"){ render :erb, "sinatra layout", :layout => true }
33
+ end
34
+
35
+ get "/"
36
+ assert_equal "this is a sinatra layout", body
37
+ end
38
+
39
+ it 'should use rails way layout' do
40
+ with_layout :application, "this is a <%= yield %>" do
41
+ mock_app do
42
+ get("/"){ render :erb, "rails way layout" }
43
+ end
44
+
45
+ get "/"
46
+ assert_equal "this is a rails way layout", body
47
+ end
48
+ end
49
+
50
+ it 'should use rails way for a custom layout' do
51
+ with_layout "layouts/custom", "this is a <%= yield %>" do
52
+ mock_app do
53
+ layout :custom
54
+ get("/"){ render :erb, "rails way custom layout" }
55
+ end
56
+
57
+ get "/"
58
+ assert_equal "this is a rails way custom layout", body
59
+ end
60
+ end
61
+
62
+ it 'should not use layout' do
63
+ with_layout :application, "this is an <%= yield %>" do
64
+ with_view :index, "index" do
65
+ mock_app do
66
+ get("/with/layout"){ render :index }
67
+ get("/without/layout"){ render :index, :layout => false }
68
+ end
69
+ get "/with/layout"
70
+ assert_equal "this is an index", body
71
+ get "/without/layout"
72
+ assert_equal "index", body
73
+ end
74
+ end
75
+ end
76
+
77
+ it 'should not use layout with js format' do
78
+ create_layout :application, "this is an <%= yield %>"
79
+ create_view :foo, "erb file"
80
+ create_view :foo, "js file", :format => :js
81
+ mock_app do
82
+ get('/layout_test', :provides => [:html, :js]){ render :foo }
83
+ end
84
+ get "/layout_test"
85
+ assert_equal "this is an erb file", body
86
+ get "/layout_test.js"
87
+ assert_equal "js file", body
88
+ end
89
+
90
+ it 'should use correct layout for each format' do
91
+ create_layout :application, "this is an <%= yield %>"
92
+ create_layout :application, "document start <%= yield %> end", :format => :xml
93
+ create_view :foo, "erb file"
94
+ create_view :foo, "xml file", :format => :xml
95
+ mock_app do
96
+ get('/layout_test', :provides => [:html, :xml]){ render :foo }
97
+ end
98
+ get "/layout_test"
99
+ assert_equal "this is an erb file", body
100
+ get "/layout_test.xml"
101
+ assert_equal "document start xml file end", body
102
+ end
103
+
104
+ it 'should by default use html file when no other is given' do
105
+ create_layout :baz, "html file", :format => :html
106
+
107
+ mock_app do
108
+ get('/content_type_test', :provides => [:html, :xml]) { render :baz }
109
+ end
110
+
111
+ get "/content_type_test"
112
+ assert_equal "html file", body
113
+ get "/content_type_test.html"
114
+ assert_equal "html file", body
115
+ get "/content_type_test.xml"
116
+ assert_equal "html file", body
117
+ end
118
+
119
+ it 'should not use html file when DEFAULT_RENDERING_OPTIONS[:strict_format] == true' do
120
+ create_layout :foo, "html file", :format => :html
121
+
122
+ mock_app do
123
+ get('/default_rendering_test', :provides => [:html, :xml]) { render :foo }
124
+ end
125
+
126
+ @save = Padrino::Rendering::DEFAULT_RENDERING_OPTIONS
127
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = true
128
+
129
+ get "/default_rendering_test"
130
+ assert_equal "html file", body
131
+ assert_raises Padrino::Rendering::TemplateNotFound do
132
+ get "/default_rendering_test.xml"
133
+ end
134
+
135
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS.merge!(@save)
136
+ end
137
+
138
+ it 'should use correct layout with each controller' do
139
+ create_layout :foo, "foo layout at <%= yield %>"
140
+ create_layout :bar, "bar layout at <%= yield %>"
141
+ create_layout :baz, "baz layout at <%= yield %>"
142
+ create_layout :application, "default layout at <%= yield %>"
143
+ mock_app do
144
+ get("/"){ render :erb, "application" }
145
+ controller :foo do
146
+ layout :foo
147
+ get("/"){ render :erb, "foo" }
148
+ end
149
+ controller :bar do
150
+ layout :bar
151
+ get("/"){ render :erb, "bar" }
152
+ end
153
+ controller :baz do
154
+ layout :baz
155
+ get("/"){ render :erb, "baz", :layout => true }
156
+ end
157
+ controller :none do
158
+ get("/") { render :erb, "none" }
159
+ get("/with_foo_layout") { render :erb, "none with layout", :layout => :foo }
160
+ end
161
+ end
162
+ get "/foo"
163
+ assert_equal "foo layout at foo", body
164
+ get "/bar"
165
+ assert_equal "bar layout at bar", body
166
+ get "/baz"
167
+ assert_equal "baz layout at baz", body
168
+ get "/none"
169
+ assert_equal "default layout at none", body
170
+ get "/none/with_foo_layout"
171
+ assert_equal "foo layout at none with layout", body
172
+ get "/"
173
+ assert_equal "default layout at application", body
174
+ end
175
+ end
176
+
177
+ it 'should solve layout in layouts paths' do
178
+ create_layout :foo, "foo layout <%= yield %>"
179
+ create_layout :"layouts/bar", "bar layout <%= yield %>"
180
+ mock_app do
181
+ get("/") { render :erb, "none" }
182
+ get("/foo") { render :erb, "foo", :layout => :foo }
183
+ get("/bar") { render :erb, "bar", :layout => :bar }
184
+ end
185
+ get "/"
186
+ assert_equal "none", body
187
+ get "/foo"
188
+ assert_equal "foo layout foo", body
189
+ get "/bar"
190
+ assert_equal "bar layout bar", body
191
+ end
192
+
193
+ it 'should render correctly if layout was not found or not exist' do
194
+ create_layout :application, "application layout for <%= yield %>"
195
+ create_view :foo, "index", :format => :html
196
+ create_view :foo, "xml.rss", :format => :rss
197
+ mock_app do
198
+ get("/foo", :provides => [:html, :rss]) { render('foo') }
199
+ get("/baz", :provides => :js) { render(:erb, 'baz') }
200
+ get("/bar") { render :haml, "haml" }
201
+ end
202
+ get "/foo"
203
+ assert_equal "application layout for index", body
204
+ get "/foo.rss"
205
+ assert_equal "<rss/>", body.chomp
206
+ get "/baz.js"
207
+ assert_equal "baz", body
208
+ get "/bar"
209
+ assert_equal "haml", body.chomp
210
+ end
211
+
212
+ it 'should allow to render template with layout option that using other template engine.' do
213
+ create_layout :"layouts/foo", "application layout for <%= yield %>", :format => :erb
214
+ create_view :slim, "| slim", :format => :slim
215
+ create_view :haml, "haml", :format => :haml
216
+ create_view :erb, "erb", :format => :erb
217
+ mock_app do
218
+ get("/slim") { render("slim.slim", :layout => "foo.erb") }
219
+ get("/haml") { render("haml.haml", :layout => "foo.erb") }
220
+ get("/erb") { render("erb.erb", :layout => "foo.erb") }
221
+ end
222
+ get "/slim"
223
+ assert_equal "application layout for slim", body.chomp
224
+ get "/haml"
225
+ assert_equal "application layout for haml", body.chomp
226
+ get "/erb"
227
+ assert_equal "application layout for erb", body.chomp
228
+ end
229
+
230
+ it 'should allow to use extension with layout method.' do
231
+ create_layout :"layouts/bar", "application layout for <%= yield %>", :format => :erb
232
+ create_view :slim, "| slim", :format => :slim
233
+ create_view :haml, "haml", :format => :haml
234
+ create_view :erb, "erb", :format => :erb
235
+ mock_app do
236
+ layout "bar.erb"
237
+ get("/slim") { render("slim.slim") }
238
+ get("/haml") { render("haml.haml") }
239
+ get("/erb") { render("erb.erb") }
240
+ end
241
+ get "/slim"
242
+ assert_equal "application layout for slim", body.chomp
243
+ get "/haml"
244
+ assert_equal "application layout for haml", body.chomp
245
+ get "/erb"
246
+ assert_equal "application layout for erb", body.chomp
247
+ end
248
+
249
+ describe 'for application render functionality' do
250
+
251
+ it 'should work properly with logging and missing layout' do
252
+ create_view :index, "<%= foo %>"
253
+ mock_app do
254
+ enable :logging
255
+ get("/") { render "index", { :layout => nil }, { :foo => "bar" } }
256
+ end
257
+ get "/"
258
+ assert_equal "bar", body
259
+ end
260
+
261
+ it 'should work properly with logging and layout' do
262
+ create_layout :application, "layout <%= yield %>"
263
+ create_view :index, "<%= foo %>"
264
+ mock_app do
265
+ enable :logging
266
+ get("/") { render "index", { :layout => true }, { :foo => "bar" } }
267
+ end
268
+ get "/"
269
+ assert_equal "layout bar", body
270
+ end
271
+
272
+ it 'should be compatible with sinatra render' do
273
+ mock_app do
274
+ get("/"){ render :erb, "<%= 1+2 %>" }
275
+ end
276
+ get "/"
277
+ assert_equal "3", body
278
+ end
279
+
280
+ it 'should support passing locals into render' do
281
+ create_layout :application, "layout <%= yield %>"
282
+ create_view :index, "<%= foo %>"
283
+ mock_app do
284
+ get("/") { render "index", { :layout => true }, { :foo => "bar" } }
285
+ end
286
+ get "/"
287
+ assert_equal "layout bar", body
288
+ end
289
+
290
+ it 'should support passing locals into sinatra render' do
291
+ create_layout :application, "layout <%= yield %>"
292
+ create_view :index, "<%= foo %>"
293
+ mock_app do
294
+ get("/") { render :erb, :index, { :layout => true }, { :foo => "bar" } }
295
+ end
296
+ get "/"
297
+ assert_equal "layout bar", body
298
+ end
299
+
300
+ it 'should support passing locals into special nil engine render' do
301
+ create_layout :application, "layout <%= yield %>"
302
+ create_view :index, "<%= foo %>"
303
+ mock_app do
304
+ get("/") { render nil, :index, { :layout => true }, { :foo => "bar" } }
305
+ end
306
+ get "/"
307
+ assert_equal "layout bar", body
308
+ end
309
+
310
+ it 'should be compatible with sinatra views' do
311
+ with_view :index, "<%= 1+2 %>" do
312
+ mock_app do
313
+ get("/foo") { render :erb, :index }
314
+ get("/bar") { erb :index }
315
+ get("/dir") { "3" }
316
+ get("/inj") { erb "<%= 2+1 %>" }
317
+ get("/rnj") { render :erb, "<%= 2+1 %>" }
318
+ end
319
+ get "/foo"
320
+ assert_equal "3", body
321
+ get "/bar"
322
+ assert_equal "3", body
323
+ get "/dir"
324
+ assert_equal "3", body
325
+ get "/inj"
326
+ assert_equal "3", body
327
+ get "/rnj"
328
+ assert_equal "3", body
329
+ end
330
+ end
331
+
332
+ it 'should resolve template engine' do
333
+ with_view :index, "<%= 1+2 %>" do
334
+ mock_app do
335
+ get("/foo") { render :index }
336
+ get("/bar") { render "/index" }
337
+ end
338
+ get "/foo"
339
+ assert_equal "3", body
340
+ get "/bar"
341
+ assert_equal "3", body
342
+ end
343
+ end
344
+
345
+ it 'should resolve template content type' do
346
+ create_view :foo, "Im Js", :format => :js
347
+ create_view :foo, "Im Erb"
348
+ mock_app do
349
+ get("/foo", :provides => :js) { render :foo }
350
+ get("/bar.js") { render :foo }
351
+ end
352
+ get "/foo.js"
353
+ assert_equal "Im Js", body
354
+ # TODO: implement this!
355
+ # get "/bar.js"
356
+ # assert_equal "Im Js", body
357
+ end
358
+
359
+ it 'should resolve with explicit template format' do
360
+ create_view :foo, "Im Js", :format => :js
361
+ create_view :foo, "Im Haml", :format => :haml
362
+ create_view :foo, "Im Xml", :format => :xml
363
+ mock_app do
364
+ get("/foo_normal", :provides => :js) { render 'foo' }
365
+ get("/foo_haml", :provides => :js) { render 'foo.haml' }
366
+ get("/foo_xml", :provides => :js) { render 'foo.xml' }
367
+ end
368
+ get "/foo_normal.js"
369
+ assert_equal "Im Js", body
370
+ get "/foo_haml.js"
371
+ assert_equal "Im Haml\n", body
372
+ get "/foo_xml.js"
373
+ assert_equal "Im Xml", body
374
+ end
375
+
376
+ it 'should resolve without explict template format' do
377
+ create_view :foo, "Im Html"
378
+ create_view :foo, "xml.rss", :format => :rss
379
+ mock_app do
380
+ get(:index, :map => "/", :provides => [:html, :rss]){ render 'foo' }
381
+ end
382
+ get "/", {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
383
+ assert_equal "Im Html", body
384
+ get ".rss"
385
+ assert_equal "<rss/>\n", body
386
+ end
387
+
388
+ it 'should ignore files ending in tilde and not render them' do
389
+ create_view :foo, "Im Wrong", :format => 'haml~'
390
+ create_view :foo, "Im Haml", :format => :haml
391
+ create_view :bar, "Im Haml backup", :format => 'haml~'
392
+ mock_app do
393
+ get('/foo') { render 'foo' }
394
+ get('/bar') { render 'bar' }
395
+ end
396
+ get '/foo'
397
+ assert_equal "Im Haml\n", body
398
+ assert_raises(Padrino::Rendering::TemplateNotFound) { get '/bar' }
399
+ end
400
+
401
+ it 'should resolve template locale' do
402
+ create_view :foo, "Im English", :locale => :en
403
+ create_view :foo, "Im Italian", :locale => :it
404
+ mock_app do
405
+ get("/foo") { render :foo }
406
+ end
407
+ I18n.locale = :en
408
+ get "/foo"
409
+ assert_equal "Im English", body
410
+ I18n.locale = :it
411
+ get "/foo"
412
+ assert_equal "Im Italian", body
413
+ end
414
+
415
+ it 'should resolve template content_type and locale' do
416
+ create_view :foo, "Im Js", :format => :js
417
+ create_view :foo, "Im Erb"
418
+ create_view :foo, "Im English Erb", :locale => :en
419
+ create_view :foo, "Im Italian Erb", :locale => :it
420
+ create_view :foo, "Im English Js", :format => :js, :locale => :en
421
+ create_view :foo, "Im Italian Js", :format => :js, :locale => :it
422
+ mock_app do
423
+ get("/foo", :provides => [:html, :js]) { render :foo }
424
+ end
425
+
426
+ I18n.enforce_available_locales = false
427
+ I18n.locale = :none
428
+ get "/foo.js"
429
+ assert_equal "Im Js", body
430
+ get "/foo"
431
+ assert_equal "Im Erb", body
432
+ I18n.enforce_available_locales = true
433
+
434
+ I18n.locale = :en
435
+ get "/foo"
436
+ assert_equal "Im English Erb", body
437
+ I18n.locale = :it
438
+ get "/foo"
439
+ assert_equal "Im Italian Erb", body
440
+ I18n.locale = :en
441
+ get "/foo.js"
442
+ assert_equal "Im English Js", body
443
+ I18n.locale = :it
444
+ get "/foo.js"
445
+ assert_equal "Im Italian Js", body
446
+ I18n.locale = :en
447
+ get "/foo.pk"
448
+ assert_equal 404, status
449
+ end
450
+
451
+ it 'should resolve layouts from specific application' do
452
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
453
+ @app = RenderDemo2
454
+ get '/blog/override'
455
+ assert_equal 'otay', body
456
+ end
457
+
458
+ it 'should resolve templates and layouts located in absolute paths' do
459
+ mock_app do
460
+ get("/foo") { render 'apps/views/blog/post', :layout => 'layout', :views => File.dirname(__FILE__)+'/fixtures' }
461
+ end
462
+ get '/foo'
463
+ assert_match /okay absolute layout/, body
464
+ end
465
+
466
+ it 'should resolve template content_type and locale with layout' do
467
+ create_layout :foo, "Hello <%= yield %> in a Js layout", :format => :js
468
+ create_layout :foo, "Hello <%= yield %> in a Js-En layout", :format => :js, :locale => :en
469
+ create_layout :foo, "Hello <%= yield %> in a Js-It layout", :format => :js, :locale => :it
470
+ create_layout :foo, "Hello <%= yield %> in a Erb-En layout", :locale => :en
471
+ create_layout :foo, "Hello <%= yield %> in a Erb-It layout", :locale => :it
472
+ create_layout :foo, "Hello <%= yield %> in a Erb layout"
473
+ create_view :bar, "Im Js", :format => :js
474
+ create_view :bar, "Im Erb"
475
+ create_view :bar, "Im English Erb", :locale => :en
476
+ create_view :bar, "Im Italian Erb", :locale => :it
477
+ create_view :bar, "Im English Js", :format => :js, :locale => :en
478
+ create_view :bar, "Im Italian Js", :format => :js, :locale => :it
479
+ create_view :bar, "Im a json", :format => :json
480
+ mock_app do
481
+ layout :foo
482
+ get("/bar", :provides => [:html, :js, :json]) { render :bar }
483
+ end
484
+
485
+ I18n.enforce_available_locales = false
486
+ I18n.locale = :none
487
+ get "/bar.js"
488
+ assert_equal "Hello Im Js in a Js layout", body
489
+ get "/bar"
490
+ assert_equal "Hello Im Erb in a Erb layout", body
491
+ I18n.enforce_available_locales = true
492
+
493
+ I18n.locale = :en
494
+ get "/bar"
495
+ assert_equal "Hello Im English Erb in a Erb-En layout", body
496
+ I18n.locale = :it
497
+ get "/bar"
498
+ assert_equal "Hello Im Italian Erb in a Erb-It layout", body
499
+ I18n.locale = :en
500
+ get "/bar.js"
501
+ assert_equal "Hello Im English Js in a Js-En layout", body
502
+ I18n.locale = :it
503
+ get "/bar.js"
504
+ assert_equal "Hello Im Italian Js in a Js-It layout", body
505
+ I18n.locale = :en
506
+ get "/bar.json"
507
+ assert_equal "Im a json", body
508
+ get "/bar.pk"
509
+ assert_equal 404, status
510
+
511
+ end
512
+
513
+ it 'should resolve template location relative to controller name' do
514
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
515
+ @app = RenderDemo2
516
+ get '/blog'
517
+ assert_equal 'okay', body
518
+ end
519
+
520
+ it 'should resolve nested template location relative to controller name' do
521
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
522
+ @app = RenderDemo2
523
+ get '/article/comment'
524
+ assert_equal 'okay comment', body
525
+ end
526
+
527
+ it 'should renders erb with blocks' do
528
+ mock_app do
529
+ def container
530
+ @_out_buf << "THIS."
531
+ yield
532
+ @_out_buf << "SPARTA!"
533
+ end
534
+ def is; "IS."; end
535
+ get '/' do
536
+ render :erb, '<% container do %> <%= is %> <% end %>'
537
+ end
538
+ end
539
+ get '/'
540
+ assert ok?
541
+ assert_equal 'THIS. IS. SPARTA!', body
542
+ end
543
+
544
+ it 'should render erb to a SafeBuffer' do
545
+ mock_app do
546
+ layout do
547
+ "this is a <%= yield %>"
548
+ end
549
+ get '/' do
550
+ render :erb, '<p><%= %q{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>} %></p>', :layout => false
551
+ end
552
+ get '/with_layout' do
553
+ render :erb, '<span>span</span>', :layout => true
554
+ end
555
+ end
556
+ get '/'
557
+ assert ok?
558
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body
559
+
560
+ get '/with_layout'
561
+ assert ok?
562
+ assert_equal 'this is a <span>span</span>', body
563
+ end
564
+
565
+ it 'should render haml to a SafeBuffer' do
566
+ mock_app do
567
+ layout do
568
+ "%p= yield"
569
+ end
570
+ get '/' do
571
+ render :haml, '%p= %s{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>}', :layout => false
572
+ end
573
+ get '/with_layout' do
574
+ render :haml, "%div\n foo", :layout => true
575
+ end
576
+ end
577
+ get '/'
578
+ assert ok?
579
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body.strip
580
+
581
+ get 'with_layout'
582
+ assert ok?
583
+ assert_equal '<p><div>foo</div></p>', body.gsub(/\s+/, "")
584
+ end
585
+
586
+ it 'should render slim to a SafeBuffer' do
587
+ mock_app do
588
+ layout do
589
+ "p= yield"
590
+ end
591
+ get '/' do
592
+ render :slim, 'p = %q{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>}', :layout => false
593
+ end
594
+ get "/with_layout" do
595
+ render :slim, 'div foo', :layout => true
596
+ end
597
+ end
598
+ get '/'
599
+ assert ok?
600
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body.strip
601
+
602
+ get '/with_layout'
603
+ assert ok?
604
+ assert_equal '<p><div>foo</div></p>', body.strip
605
+ end
606
+
607
+ it 'should render correct erb when use sinatra as middleware' do
608
+ class Bar < Sinatra::Base
609
+ get "/" do
610
+ render :erb, "<&'>"
611
+ end
612
+ end
613
+ mock_app do
614
+ use Bar
615
+ end
616
+ get "/"
617
+ assert_equal "<&'>", body
618
+ end
619
+ end
620
+
621
+ describe 'standalone Sinatra usage of Rendering' do
622
+ before do
623
+ Sinatra::Request.class_eval{ alias_method :monkey_controller, :controller; undef :controller }
624
+ end
625
+ after do
626
+ Sinatra::Request.class_eval{ alias_method :controller, :monkey_controller; undef :monkey_controller }
627
+ end
628
+ it 'should work with Sinatra::Base' do
629
+ class Application < Sinatra::Base
630
+ register Padrino::Rendering
631
+ get '/' do
632
+ render :post, :views => File.dirname(__FILE__)+'/fixtures/apps/views/blog'
633
+ end
634
+ end
635
+ @app = Application.new
636
+ get '/'
637
+ assert_equal 'okay', body
638
+ end
639
+ end
640
+
641
+ describe 'locating of template paths' do
642
+ it 'should locate controller templates' do
643
+ mock_app do
644
+ disable :reload_templates
645
+ set :views, File.dirname(__FILE__)+'/fixtures/apps/views'
646
+ controller :test do
647
+ get :index do
648
+ render 'test/post'
649
+ end
650
+ end
651
+ end
652
+ get '/test'
653
+ end
654
+
655
+ it 'should properly cache template path' do
656
+ mock_app do
657
+ disable :reload_templates
658
+ set :views, File.dirname(__FILE__)+'/fixtures/apps/views'
659
+ controller :blog do
660
+ get :index do
661
+ render :post
662
+ end
663
+ end
664
+ controller :test do
665
+ get :index do
666
+ render 'post'
667
+ end
668
+ end
669
+ end
670
+ get '/blog'
671
+ get '/test'
672
+ assert_equal 'test', body
673
+ end
674
+ end
675
+
676
+ describe 'rendering bug in some' do
677
+ it 'should raise error on registering things to Padrino::Application' do
678
+ assert_raises(RuntimeError) do
679
+ Padrino::Application.register Padrino::Rendering
680
+ end
681
+ end
682
+ end
683
+ end