From ff570c3ddcca3377b6569cf33421d7a66b079ebb Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Wed, 31 Jul 2013 17:41:02 -0400 Subject: [PATCH] Fix clashing method names in enumerable drops --- lib/liquid/drop.rb | 11 ++++++++++- test/liquid/drop_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/liquid/drop.rb b/lib/liquid/drop.rb index e8eb233..2ad33c8 100644 --- a/lib/liquid/drop.rb +++ b/lib/liquid/drop.rb @@ -54,7 +54,16 @@ module Liquid # Check for method existence without invoking respond_to?, which creates symbols def self.invokable?(method_name) - @invokable_methods ||= Set.new(["to_liquid"] + (public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s)) + unless @invokable_methods + blacklist = Liquid::Drop.public_instance_methods + [:each] + if include?(Enumerable) + blacklist += Enumerable.public_instance_methods + blacklist -= [:sort, :count, :first, :min, :max] + end + # Ruby 1.8 compatibility: call to_s on method names (which are strings in 1.8, but already symbols in 1.9) + whitelist = [:to_liquid] + (public_instance_methods.map(&:to_s) - blacklist.map(&:to_s)) + @invokable_methods = Set.new(whitelist.map(&:to_s)) + end @invokable_methods.include?(method_name.to_s) end end diff --git a/test/liquid/drop_test.rb b/test/liquid/drop_test.rb index fc972aa..04290cc 100644 --- a/test/liquid/drop_test.rb +++ b/test/liquid/drop_test.rb @@ -55,6 +55,9 @@ class ProductDrop < Liquid::Drop end class EnumerableDrop < Liquid::Drop + def before_method(method) + method + end def size 3 @@ -67,6 +70,20 @@ class EnumerableDrop < Liquid::Drop end end +class RealEnumerableDrop < Liquid::Drop + include Enumerable + + def before_method(method) + method + end + + def each + yield 1 + yield 2 + yield 3 + end +end + class DropsTest < Test::Unit::TestCase include Liquid @@ -170,6 +187,27 @@ class DropsTest < Test::Unit::TestCase assert_equal '3', Liquid::Template.parse( '{{collection.size}}').render('collection' => EnumerableDrop.new) end + def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names + ["select", "each", "map", "cycle"].each do |method| + assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render('collection' => EnumerableDrop.new) + assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => EnumerableDrop.new) + assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) + assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) + end + end + + def test_some_enumerable_methods_still_get_invoked + [ :count, :max ].each do |method| + assert_equal "3", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) + assert_equal "3", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) + end + + [ :min, :first ].each do |method| + assert_equal "1", Liquid::Template.parse("{{collection.#{method}}}").render('collection' => RealEnumerableDrop.new) + assert_equal "1", Liquid::Template.parse("{{collection[\"#{method}\"]}}").render('collection' => RealEnumerableDrop.new) + end + end + def test_empty_string_value_access assert_equal '', Liquid::Template.parse('{{ product[value] }}').render('product' => ProductDrop.new, 'value' => '') end