Permalink
Browse files

Always sort locals by strings (Fixes #306) (#307)

Fixes issues when mixing string locals with symbol locals.
  • Loading branch information...
1 parent 4e40682 commit 9a00ee3c67eb660093005abaa692710d78f0a17f @jeremyevans jeremyevans committed with judofyr Dec 30, 2016
Showing with 7 additions and 7 deletions.
  1. +1 −7 lib/tilt/template.rb
  2. +6 −0 test/tilt_template_test.rb
@@ -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
@@ -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
@@ -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) }

0 comments on commit 9a00ee3

Please sign in to comment.