Skip to content

Commit

Permalink
Merge pull request #258 from myabc/fix/render-nil-scope
Browse files Browse the repository at this point in the history
Ruby 2.2: Tilt::HamlTemplate#render shouldn't blow up on explicit nil
  • Loading branch information
judofyr committed Jul 6, 2015
2 parents ab12350 + fbf56cc commit a5e7895
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/tilt/haml.rb
Expand Up @@ -13,6 +13,8 @@ def prepare
end

def evaluate(scope, locals, &block)
raise ArgumentError, 'invalid scope: must not be frozen' if scope.frozen?

if @engine.respond_to?(:precompiled_method_return_value, true)
super
else
Expand Down Expand Up @@ -54,4 +56,3 @@ def precompiled_postamble(locals)
end
end
end

3 changes: 2 additions & 1 deletion lib/tilt/template.rb
Expand Up @@ -92,7 +92,8 @@ def initialize(file=nil, line=1, options={}, &block)
# Render the template in the given scope with the locals specified. If a
# block is given, it is typically available within the template via
# +yield+.
def render(scope=Object.new, locals={}, &block)
def render(scope=nil, locals={}, &block)
scope ||= Object.new
current_template = Thread.current[:tilt_current_template]
Thread.current[:tilt_current_template] = self
evaluate(scope, locals || {}, &block)
Expand Down
22 changes: 22 additions & 0 deletions test/tilt_hamltemplate_test.rb
Expand Up @@ -27,6 +27,17 @@ class HamlTemplateTest < Minitest::Test
assert_equal "<p>Hey Joe!</p>\n", template.render(Object.new, :name => 'Joe')
end

test 'evaluating in default/nil scope' do
template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' }
assert_equal "<p>Hey unknown!</p>\n", template.render
assert_equal "<p>Hey unknown!</p>\n", template.render(nil)
end

test 'evaluating in invalid, frozen scope' do
template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' }
assert_raises(ArgumentError) { template.render(Object.new.freeze) }
end

test "evaluating in an object scope" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
scope = Object.new
Expand Down Expand Up @@ -87,6 +98,17 @@ class Scope
assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new, :name => 'Joe')
end

test 'evaluating in default/nil scope' do
template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' }
assert_equal "<p>Hey unknown!</p>\n", template.render
assert_equal "<p>Hey unknown!</p>\n", template.render(nil)
end

test 'evaluating in invalid, frozen scope' do
template = Tilt::HamlTemplate.new { |t| '%p Hey unknown!' }
assert_raises(ArgumentError) { template.render(Object.new.freeze) }
end

test "evaluating in an object scope" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
scope = Scope.new
Expand Down
6 changes: 6 additions & 0 deletions test/tilt_template_test.rb
Expand Up @@ -122,6 +122,12 @@ def evaluate(scope, locals, &block)
assert inst.prepared?
end

test 'prepares and evaluates the template on #render with nil arg' do
inst = SimpleMockTemplate.new { |t| "Hello World!" }
assert_equal '<em>Hello World!</em>', inst.render(nil)
assert inst.prepared?
end

class SourceGeneratingMockTemplate < PreparingMockTemplate
def precompiled_template(locals)
"foo = [] ; foo << %Q{#{data}} ; foo.join"
Expand Down

0 comments on commit a5e7895

Please sign in to comment.