tennpipes-helper 3.6.6

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