Skip to content

Commit

Permalink
Always sort locals by strings (Fixes rtomayko#306) (rtomayko#307)
Browse files Browse the repository at this point in the history
Fixes issues when mixing string locals with symbol locals.
  • Loading branch information
jeremyevans authored and judofyr committed Dec 30, 2016
1 parent 4e40682 commit 9a00ee3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 1 addition & 7 deletions lib/tilt/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Tilt
TOPOBJECT = Object.superclass || Object
# @private
LOCK = Mutex.new
# @private
SYMBOL_ARRAY_SORTABLE = RUBY_VERSION >= '1.9'

# Base class for template implementations. Subclasses must implement
# the #prepare method and one of the #evaluate or #precompiled_template
Expand Down Expand Up @@ -158,11 +156,7 @@ def prepare
# override render() may not support all features.
def evaluate(scope, locals, &block)
locals_keys = locals.keys
if SYMBOL_ARRAY_SORTABLE
locals_keys.sort!
else
locals_keys.sort!{|x, y| x.to_s <=> y.to_s}
end
locals_keys.sort!{|x, y| x.to_s <=> y.to_s}
method = compiled_method(locals_keys)
method.bind(scope).call(locals, &block)
end
Expand Down
6 changes: 6 additions & 0 deletions test/tilt_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def precompiled_template(locals)
assert inst.prepared?
end

test "template_source with locals of strings" do
inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
assert_equal "Hey Joe!", inst.render(Object.new, 'name' => 'Joe', :name=>'Joe')
assert inst.prepared?
end

test "template_source with locals having non-variable keys raises error" do
inst = SourceGeneratingMockTemplate.new { |t| '1 + 2 = #{_answer}' }
err = assert_raises(RuntimeError) { inst.render(Object.new, 'ANSWER' => 3) }
Expand Down

0 comments on commit 9a00ee3

Please sign in to comment.