diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index c423c1e84..d1b145686 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -56,7 +56,7 @@ def render_to_output_buffer(context, output) block.evaluate(context), ) - if result + if result.respond_to?(:truthy?) ? result.truthy? : result return block.attachment.render_to_output_buffer(context, output) end end diff --git a/lib/liquid/tags/unless.rb b/lib/liquid/tags/unless.rb index a6faa6f93..31ceb8f4e 100644 --- a/lib/liquid/tags/unless.rb +++ b/lib/liquid/tags/unless.rb @@ -26,7 +26,7 @@ def render_to_output_buffer(context, output) first_block.evaluate(context), ) - unless result + unless result.respond_to?(:truthy?) ? result.truthy? : result return first_block.attachment.render_to_output_buffer(context, output) end @@ -36,7 +36,7 @@ def render_to_output_buffer(context, output) block.evaluate(context), ) - if result + if result.respond_to?(:truthy?) ? result.truthy? : result return block.attachment.render_to_output_buffer(context, output) end end diff --git a/test/unit/tags/if_tag_unit_test.rb b/test/unit/tags/if_tag_unit_test.rb index 32243b730..b814aba41 100644 --- a/test/unit/tags/if_tag_unit_test.rb +++ b/test/unit/tags/if_tag_unit_test.rb @@ -7,4 +7,13 @@ def test_if_nodelist template = Liquid::Template.parse('{% if true %}IF{% else %}ELSE{% endif %}') assert_equal(['IF', 'ELSE'], template.root.nodelist[0].nodelist.map(&:nodelist).flatten) end + + def test_support_truthy + falsey = Class.new(Liquid::Drop) { def truthy? = false }.new + truthy = Class.new(Liquid::Drop) { def truthy? = true }.new + template = '{% if obj %}IF{% else %}ELSE{% endif %}' + assert_template_result('ELSE', template, { 'obj' => falsey }) + assert_template_result('IF', template, { 'obj' => truthy }) + assert_template_result('IF', template, { 'obj' => "foo" }) # truthy? not defined + end end diff --git a/test/unit/tags/unless_tag_test.rb b/test/unit/tags/unless_tag_test.rb new file mode 100644 index 000000000..283050d6e --- /dev/null +++ b/test/unit/tags/unless_tag_test.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'test_helper' + +class IfTagUnitTest < Minitest::Test + def test_support_truthy + falsey = Class.new(Liquid::Drop) { def truthy? = false }.new + truthy = Class.new(Liquid::Drop) { def truthy? = true }.new + template = '{% unless obj %}IF{% else %}ELSE{% endunless %}' + assert_template_result('ELSE', template, { 'obj' => truthy }) + assert_template_result('IF', template, { 'obj' => falsey }) + assert_template_result('ELSE', template, { 'obj' => "truthy? not defined" }) + end +end