spec/plugin/render_spec.rb in roda-2.29.0 vs spec/plugin/render_spec.rb in roda-3.0.0
- old
+ new
@@ -1,6 +1,6 @@
-require File.expand_path("spec_helper", File.dirname(File.dirname(__FILE__)))
+require_relative "../spec_helper"
begin
require 'tilt/erb'
require 'tilt/string'
rescue LoadError
@@ -48,17 +48,10 @@
body("/about").strip.must_equal "<h1>About Roda</h1>"
body("/home").strip.must_equal "<title>Roda: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"
body("/inline").strip.must_equal "Hello <%= name %>"
end
- deprecated "with str as ext" do
- app.plugin :render, :ext => "str"
- body("/about").strip.must_equal "<h1>About Roda</h1>"
- body("/home").strip.must_equal "<title>Roda: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"
- body("/inline").strip.must_equal "Hello <%= name %>"
- end
-
it "custom default layout support" do
app.plugin :render, :layout => "layout-alternative"
body("/home").strip.must_equal "<title>Alternative Layout: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"
end
@@ -66,34 +59,10 @@
app.plugin :render, :layout => {:inline=> 'a<%= yield %>b'}
body("/home").strip.must_equal "a<h1>Home</h1>\n<p>Hello Agent Smith</p>\nb"
end
end
-describe "render plugin with :layout_opts=>{:merge_locals=>true}" do
- deprecated "should choose method opts before plugin opts, and layout specific before locals" do
- app(:bare) do
- plugin :render, :views=>"./spec/views", :check_paths=>true, :locals=>{:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}, :layout_opts=>{:inline=>'<%= a %>|<%= b %>|<%= c %>|<%= d %>|<%= e %>|<%= f %>|<%= yield %>', :merge_locals=>true, :locals=>{:a=>-1, :f=>6}}
-
- route do |r|
- r.on "base" do
- view(:inline=>'(<%= a %>|<%= b %>|<%= c %>|<%= d %>|<%= e %>)')
- end
- r.on "override" do
- view(:inline=>'(<%= a %>|<%= b %>|<%= c %>|<%= d %>|<%= e %>)', :locals=>{:b=>-2, :d=>-4, :f=>-6}, :layout_opts=>{:locals=>{:d=>0, :c=>-3, :e=>-5}})
- end
- r.on "no_merge" do
- view(:inline=>'(<%= a %>|<%= b %>|<%= c %>|<%= d %>|<%= e %>)', :locals=>{:b=>-2, :d=>-4, :f=>-6}, :layout_opts=>{:merge_locals=>false, :locals=>{:d=>0, :c=>-3, :e=>-5}})
- end
- end
- end
-
- body("/base").must_equal '-1|2|3|4|5|6|(1|2|3|4|5)'
- body("/override").must_equal '-1|-2|-3|0|-5|-6|(1|-2|3|-4|5)'
- body("/no_merge").must_equal '-1|2|-3|0|-5|6|(1|-2|3|-4|5)'
- end
-end
-
describe "render plugin" do
it "simple layout support" do
app(:bare) do
plugin :render
@@ -142,32 +111,21 @@
end
body.strip.must_equal "<title>Alternative Layout: Home</title>\n<h1>Home</h1>\n<p>Hello Agent Smith</p>"
end
- deprecated "locals overrides" do
+ it ":layout=>true/false/string/hash/not-present respects plugin layout switch and template" do
app(:bare) do
- plugin :render, :views=>"./spec/views", :locals=>{:title=>'Home', :b=>'B'}, :layout_opts=>{:template=>'multiple-layout', :locals=>{:title=>'Roda', :a=>'A'}}
+ plugin :render, :views=>"./spec/views", :layout_opts=>{:template=>'layout-yield'}
route do |r|
- view("multiple", :locals=>{:b=>"BB"}, :layout_opts=>{:locals=>{:a=>'AA'}})
- end
- end
-
- body.strip.must_equal "Roda:AA::Home:BB"
- end
-
- deprecated ":layout=>true/false/string/hash/not-present respects plugin layout switch and template" do
- app(:bare) do
- plugin :render, :views=>"./spec/views", :layout_opts=>{:template=>'layout-yield', :locals=>{:title=>'a'}}
-
- route do |r|
opts = {:content=>'bar'}
opts[:layout] = true if r.path == '/'
opts[:layout] = false if r.path == '/f'
opts[:layout] = 'layout' if r.path == '/s'
opts[:layout] = {:template=>'layout'} if r.path == '/h'
+ opts[:layout_opts] = {:locals=>{:title=>'a'}}
view(opts)
end
end
body.gsub("\n", '').must_equal "HeaderbarFooter"
@@ -371,28 +329,28 @@
app.render_opts[:explicit_cache].must_equal true
app(:render){}
app.render_opts[:explicit_cache].must_equal false
end
- deprecated "Support :cache=>false plugin option to disable template caching, even when :cache=>true method option is given" do
+ it "Support :cache=>false plugin option to disable template caching by default, except :cache=>true method option is given" do
app(:bare) do
plugin :render, :views=>"./spec/views", :cache=>false
route do |r|
@a = 'a'
- r.is('a'){render('iv', :cache=>false)}
- r.is('b'){render('iv', :cache=>true)}
- render('iv')
+ r.is('a'){render('iv', :cache=>false, :cache_key=>:a)}
+ r.is('b'){render('iv', :cache=>true, :cache_key=>:a)}
+ render('iv', :cache_key=>:a)
end
end
body('/a').strip.must_equal "a"
- app.render_opts[:cache].must_equal false
+ app.render_opts[:cache][:a].must_be_nil
body('/b').strip.must_equal "a"
- app.render_opts[:cache].must_equal false
+ app.render_opts[:cache][:a].wont_be_nil
body('/c').strip.must_equal "a"
- app.render_opts[:cache].must_equal false
+ app.render_opts[:cache][:a].wont_be_nil
end
it "Support :cache=>false option to disable template caching" do
app(:bare) do
plugin :render, :views=>"./spec/views"
@@ -517,24 +475,21 @@
end
body.must_equal "1-2"
end
- it "should dup render_opts when subclasses, including an empty cache" do
+ it "should dup render_opts when subclassing" do
c = Class.new(Roda)
- c.plugin :render
- c.render_opts[:cache][:foo] = 1
+ c.plugin :render, :foo=>:bar
sc = Class.new(c)
-
c.render_opts.wont_be_same_as(sc.render_opts)
- c.render_opts[:cache].wont_be_same_as(sc.render_opts[:cache])
- sc.render_opts[:cache][:foo].must_be_nil
+ c.render_opts[:foo].must_equal :bar
end
- it "should use a copy of superclass's cache when inheriting if :inherit_cache option is used" do
+ it "should use a copy of superclass's cache when subclassing" do
c = Class.new(Roda)
- c.plugin :render, :inherit_cache=>true
+ c.plugin :render
c.render_opts[:cache][:foo] = 1
sc = Class.new(c)
c.render_opts.wont_be_same_as(sc.render_opts)
c.render_opts[:cache].wont_be_same_as(sc.render_opts[:cache])
@@ -543,38 +498,41 @@
it "should not modifying existing cache if loading the plugin a separate time" do
c = Class.new(Roda)
c.plugin :render
cache = c.render_opts[:cache]
+ c.render_opts[:explicit_cache].must_equal false
c.plugin :render
c.render_opts[:cache].must_be_same_as cache
+ c.render_opts[:explicit_cache].must_equal false
c.plugin :render, :cache=>false
- c.render_opts[:cache].must_equal false
+ c.render_opts[:cache].must_be_same_as cache
+ c.render_opts[:explicit_cache].must_equal true
c.plugin :render
- c.render_opts[:cache].must_equal false
+ c.render_opts[:cache].must_be_same_as cache
+ c.render_opts[:explicit_cache].must_equal true
end
it "render plugin call should not override existing options" do
c = Class.new(Roda)
c.plugin :render, :layout=>:foo
c.plugin :render
c.render_opts[:layout].must_equal :foo
end
- it "should not use cache in subclass if caching disabled in superclass" do
+ it "should not use cache by default in subclass if not caching by default in superclass" do
app(:bare) do
plugin :render, :views=>"./spec/views", :cache=>false
route do |r|
- view(:inline=>"Hello <%= name %>: <%= render_opts[:cache] %>", :locals=>{:name => "Agent Smith"}, :layout=>nil)
+ view(:inline=>"Hello <%= name %>", :cache_key=>:a, :locals=>{:name => "Agent Smith"}, :layout=>nil)
end
end
- body("/inline").strip.must_equal "Hello Agent Smith: false"
-
- Class.new(app).render_opts[:cache].must_equal false
+ body("/inline").strip.must_equal "Hello Agent Smith"
+ Class.new(app).render_opts[:cache][:a].must_be_nil
end
it "with :check_paths=>true plugin option used" do
render_opts = {}
app(:bare) do
@@ -628,54 +586,10 @@
body.strip.must_equal "b"
proc{req("/a")}.must_raise Roda::RodaError
req("/c")
end
- deprecated "with :check_paths plugin option not set" do
- render_opts = {}
- app(:bare) do
- plugin :render, :views=>"./spec/views"
-
- route do |r|
- r.get 'a' do
- render("a", render_opts)
- end
-
- r.get 'c' do
- render("about/_test", :locals=>{:title=>'a'})
- end
-
- render("b", render_opts)
- end
- end
-
- body.strip.must_equal "b"
- body("/a").strip.must_equal 'a'
- body("/c").strip.must_equal "<h1>Subdir: a</h1>"
-
- app.plugin :render, :allowed_paths=>[]
- body.strip.must_equal "b"
- body("/a").strip.must_equal 'a'
- body("/c").strip.must_equal "<h1>Subdir: a</h1>"
-
- app.plugin :render, :allowed_paths=>['spec/views/about']
- body.strip.must_equal "b"
- body("/a").strip.must_equal 'a'
- body("/c").strip.must_equal "<h1>Subdir: a</h1>"
-
- app.plugin :render, :allowed_paths=>%w'spec/views/about spec/views/b'
- body.strip.must_equal "b"
- body("/a").strip.must_equal 'a'
- body("/c").strip.must_equal "<h1>Subdir: a</h1>"
-
- render_opts[:check_paths] = true
- app.plugin :render, :check_paths=>false
- body.strip.must_equal "b"
- proc{req("/a")}.must_raise Roda::RodaError
- body("/c").strip.must_equal "<h1>Subdir: a</h1>"
- end
-
it "with a cache_class set" do
app(:bare) do
test_cache = Class.new(Roda::RodaCache) do
def [](key)
super
@@ -704,12 +618,12 @@
begin
require 'tilt'
require 'tilt/erubi'
rescue LoadError
- warn "tilt 2 or erubi not installed, skipping render :escape=>:erubi test"
+ warn "tilt 2 or erubi not installed, skipping render :escape=>true test"
else
-describe "_erubis_escaping plugin" do
+describe ":render plugin :escape option" do
before do
if defined?(Tilt::ErubiTemplate) && ::Tilt['erb'] != Tilt::ErubiTemplate
# Set erubi as default erb template handler
Tilt.register(Tilt::ErubiTemplate, 'erb')
end