PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/padrino-core/test/test_rendering.rb

https://github.com/ganesan/padrino-framework
Ruby | 368 lines | 330 code | 35 blank | 3 comment | 0 complexity | 20b78ec7f5007608e8b6c4469b488603 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/helper')
  2. require 'i18n'
  3. class TestRendering < Test::Unit::TestCase
  4. def teardown
  5. remove_views
  6. end
  7. context 'for application layout functionality' do
  8. should 'get no layout' do
  9. mock_app do
  10. get("/"){ "no layout" }
  11. end
  12. get "/"
  13. assert_equal "no layout", body
  14. end
  15. should 'be compatible with sinatra layout' do
  16. mock_app do
  17. layout do
  18. "this is a <%= yield %>"
  19. end
  20. get("/"){ render :erb, "sinatra layout" }
  21. end
  22. get "/"
  23. assert_equal "this is a sinatra layout", body
  24. end
  25. should 'use rails way layout' do
  26. with_layout :application, "this is a <%= yield %>" do
  27. mock_app do
  28. get("/"){ render :erb, "rails way layout" }
  29. end
  30. get "/"
  31. assert_equal "this is a rails way layout", body
  32. end
  33. end
  34. should 'use rails way for a custom layout' do
  35. with_layout "layouts/custom", "this is a <%= yield %>" do
  36. mock_app do
  37. layout :custom
  38. get("/"){ render :erb, "rails way custom layout" }
  39. end
  40. get "/"
  41. assert_equal "this is a rails way custom layout", body
  42. end
  43. end
  44. should 'not use layout' do
  45. with_layout :application, "this is a <%= yield %>" do
  46. with_view :index, "index" do
  47. mock_app do
  48. get("/with/layout"){ render :index }
  49. get("/without/layout"){ render :index, :layout => false }
  50. end
  51. get "/with/layout"
  52. assert_equal "this is a index", body
  53. get "/without/layout"
  54. assert_equal "index", body
  55. end
  56. end
  57. end
  58. should 'not use layout with js format' do
  59. create_layout :application, "this is an <%= yield %>"
  60. create_view :foo, "erb file"
  61. create_view :foo, "js file", :format => :js
  62. mock_app do
  63. get('/layout_test', :provides => [:html, :js]){ render :foo }
  64. end
  65. get "/layout_test"
  66. assert_equal "this is an erb file", body
  67. get "/layout_test.js"
  68. assert_equal "js file", body
  69. end
  70. should 'use correct layout for each format' do
  71. create_layout :application, "this is an <%= yield %>"
  72. create_layout :application, "document start <%= yield %> end", :format => :xml
  73. create_view :foo, "erb file"
  74. create_view :foo, "xml file", :format => :xml
  75. mock_app do
  76. get('/layout_test', :provides => [:html, :xml]){ render :foo }
  77. end
  78. get "/layout_test"
  79. assert_equal "this is an erb file", body
  80. get "/layout_test.xml"
  81. assert_equal "document start xml file end", body
  82. end
  83. should 'by default use html file when no other is given' do
  84. create_layout :foo, "html file", :format => :html
  85. mock_app do
  86. get('/content_type_test', :provides => [:html, :xml]) { render :foo }
  87. end
  88. get "/content_type_test"
  89. assert_equal "html file", body
  90. get "/content_type_test.xml"
  91. assert_equal "html file", body
  92. end
  93. should 'not use html file when DEFAULT_RENDERING_OPTIONS[:strict_format] == true' do
  94. create_layout :foo, "html file", :format => :html
  95. mock_app do
  96. get('/default_rendering_test', :provides => [:html, :xml]) { render :foo }
  97. end
  98. @save = Padrino::Rendering::DEFAULT_RENDERING_OPTIONS
  99. Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = true
  100. get "/default_rendering_test"
  101. assert_equal "html file", body
  102. assert_raise Padrino::Rendering::TemplateNotFound do
  103. get "/default_rendering_test.xml"
  104. end
  105. Padrino::Rendering::DEFAULT_RENDERING_OPTIONS.merge(@save)
  106. end
  107. should 'use correct layout with each controller' do
  108. create_layout :foo, "foo layout at <%= yield %>"
  109. create_layout :bar, "bar layout at <%= yield %>"
  110. create_layout :application, "default layout at <%= yield %>"
  111. mock_app do
  112. get("/"){ render :erb, "application" }
  113. controller :foo do
  114. layout :foo
  115. get("/"){ render :erb, "foo" }
  116. end
  117. controller :bar do
  118. layout :bar
  119. get("/"){ render :erb, "bar" }
  120. end
  121. controller :none do
  122. get("/") { render :erb, "none" }
  123. get("/with_foo_layout") { render :erb, "none with layout", :layout => :foo }
  124. end
  125. end
  126. get "/foo"
  127. assert_equal "foo layout at foo", body
  128. get "/bar"
  129. assert_equal "bar layout at bar", body
  130. get "/none"
  131. assert_equal "default layout at none", body
  132. get "/none/with_foo_layout"
  133. assert_equal "foo layout at none with layout", body
  134. get "/"
  135. assert_equal "default layout at application", body
  136. end
  137. end
  138. context 'for application render functionality' do
  139. should 'be compatible with sinatra render' do
  140. mock_app do
  141. get("/"){ render :erb, "<%= 1+2 %>" }
  142. end
  143. get "/"
  144. assert_equal "3", body
  145. end
  146. should 'be compatible with sinatra views' do
  147. with_view :index, "<%= 1+2 %>" do
  148. mock_app do
  149. get("/foo") { render :erb, :index }
  150. get("/bar") { erb :index }
  151. get("/dir") { "3" }
  152. get("/inj") { erb "<%= 2+1 %>" }
  153. get("/rnj") { render :erb, "<%= 2+1 %>" }
  154. end
  155. get "/foo"
  156. assert_equal "3", body
  157. get "/bar"
  158. assert_equal "3", body
  159. get "/dir"
  160. assert_equal "3", body
  161. get "/inj"
  162. assert_equal "3", body
  163. get "/rnj"
  164. assert_equal "3", body
  165. end
  166. end
  167. should 'resolve template engine' do
  168. with_view :index, "<%= 1+2 %>" do
  169. mock_app do
  170. get("/foo") { render :index }
  171. get("/bar") { render "/index" }
  172. end
  173. get "/foo"
  174. assert_equal "3", body
  175. get "/bar"
  176. assert_equal "3", body
  177. end
  178. end
  179. should 'resolve template content type' do
  180. create_view :foo, "Im Js", :format => :js
  181. create_view :foo, "Im Erb"
  182. mock_app do
  183. get("/foo", :provides => :js) { render :foo }
  184. get("/bar.js") { render :foo }
  185. end
  186. get "/foo.js"
  187. assert_equal "Im Js", body
  188. # TODO: implement this!
  189. # get "/bar.js"
  190. # assert_equal "Im Js", body
  191. end
  192. should 'resolve with explicit template format' do
  193. create_view :foo, "Im Js", :format => :js
  194. create_view :foo, "Im Haml", :format => :haml
  195. create_view :foo, "Im Xml", :format => :xml
  196. mock_app do
  197. get("/foo_normal", :provides => :js) { render 'foo' }
  198. get("/foo_haml", :provides => :js) { render 'foo.haml' }
  199. get("/foo_xml", :provides => :js) { render 'foo.xml' }
  200. end
  201. get "/foo_normal.js"
  202. assert_equal "Im Js", body
  203. get "/foo_haml.js"
  204. assert_equal "Im Haml\n", body
  205. get "/foo_xml.js"
  206. assert_equal "Im Xml", body
  207. end
  208. should 'resolve without explict template format' do
  209. create_view :foo, "Im Html"
  210. create_view :foo, "xml.rss", :format => :rss
  211. mock_app do
  212. get(:index, :map => "/", :provides => [:html, :rss]){ render 'foo' }
  213. end
  214. get "/", {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
  215. assert_equal "Im Html", body
  216. get ".rss"
  217. assert_equal "<rss/>\n", body
  218. end
  219. should "ignore files ending in tilde and not render them" do
  220. create_view :foo, "Im Wrong", :format => 'haml~'
  221. create_view :foo, "Im Haml", :format => :haml
  222. create_view :bar, "Im Haml backup", :format => 'haml~'
  223. mock_app do
  224. get('/foo') { render 'foo' }
  225. get('/bar') { render 'bar' }
  226. end
  227. get '/foo'
  228. assert_equal "Im Haml\n", body
  229. assert_raises(Padrino::Rendering::TemplateNotFound) { get '/bar' }
  230. end
  231. should 'resolve template locale' do
  232. create_view :foo, "Im English", :locale => :en
  233. create_view :foo, "Im Italian", :locale => :it
  234. mock_app do
  235. get("/foo") { render :foo }
  236. end
  237. I18n.locale = :en
  238. get "/foo"
  239. assert_equal "Im English", body
  240. I18n.locale = :it
  241. get "/foo"
  242. assert_equal "Im Italian", body
  243. end
  244. should 'resolve template content_type and locale' do
  245. create_view :foo, "Im Js", :format => :js
  246. create_view :foo, "Im Erb"
  247. create_view :foo, "Im English Erb", :locale => :en
  248. create_view :foo, "Im Italian Erb", :locale => :it
  249. create_view :foo, "Im English Js", :format => :js, :locale => :en
  250. create_view :foo, "Im Italian Js", :format => :js, :locale => :it
  251. mock_app do
  252. get("/foo", :provides => [:html, :js]) { render :foo }
  253. end
  254. I18n.locale = :none
  255. get "/foo.js"
  256. assert_equal "Im Js", body
  257. get "/foo"
  258. assert_equal "Im Erb", body
  259. I18n.locale = :en
  260. get "/foo"
  261. assert_equal "Im English Erb", body
  262. I18n.locale = :it
  263. get "/foo"
  264. assert_equal "Im Italian Erb", body
  265. I18n.locale = :en
  266. get "/foo.js"
  267. assert_equal "Im English Js", body
  268. I18n.locale = :it
  269. get "/foo.js"
  270. assert_equal "Im Italian Js", body
  271. I18n.locale = :en
  272. get "/foo.pk"
  273. assert_equal 404, status
  274. end
  275. should 'resolve template content_type and locale with layout' do
  276. create_layout :foo, "Hello <%= yield %> in a Js layout", :format => :js
  277. create_layout :foo, "Hello <%= yield %> in a Js-En layout", :format => :js, :locale => :en
  278. create_layout :foo, "Hello <%= yield %> in a Js-It layout", :format => :js, :locale => :it
  279. create_layout :foo, "Hello <%= yield %> in a Erb-En layout", :locale => :en
  280. create_layout :foo, "Hello <%= yield %> in a Erb-It layout", :locale => :it
  281. create_layout :foo, "Hello <%= yield %> in a Erb layout"
  282. create_view :bar, "Im Js", :format => :js
  283. create_view :bar, "Im Erb"
  284. create_view :bar, "Im English Erb", :locale => :en
  285. create_view :bar, "Im Italian Erb", :locale => :it
  286. create_view :bar, "Im English Js", :format => :js, :locale => :en
  287. create_view :bar, "Im Italian Js", :format => :js, :locale => :it
  288. create_view :bar, "Im a json", :format => :json
  289. mock_app do
  290. layout :foo
  291. get("/bar", :provides => [:html, :js, :json]) { render :bar }
  292. end
  293. I18n.locale = :none
  294. get "/bar.js"
  295. assert_equal "Hello Im Js in a Js layout", body
  296. get "/bar"
  297. assert_equal "Hello Im Erb in a Erb layout", body
  298. I18n.locale = :en
  299. get "/bar"
  300. assert_equal "Hello Im English Erb in a Erb-En layout", body
  301. I18n.locale = :it
  302. get "/bar"
  303. assert_equal "Hello Im Italian Erb in a Erb-It layout", body
  304. I18n.locale = :en
  305. get "/bar.js"
  306. assert_equal "Hello Im English Js in a Js-En layout", body
  307. I18n.locale = :it
  308. get "/bar.js"
  309. assert_equal "Hello Im Italian Js in a Js-It layout", body
  310. I18n.locale = :en
  311. get "/bar.json"
  312. assert_equal "Im a json", body
  313. get "/bar.pk"
  314. assert_equal 404, status
  315. end
  316. should 'renders erb with blocks' do
  317. mock_app do
  318. def container
  319. @_out_buf << "THIS."
  320. yield
  321. @_out_buf << "SPARTA!"
  322. end
  323. def is; "IS."; end
  324. get '/' do
  325. render :erb, '<% container do %> <%= is %> <% end %>'
  326. end
  327. end
  328. get '/'
  329. assert ok?
  330. assert_equal 'THIS. IS. SPARTA!', body
  331. end
  332. end
  333. end