From 3f5a7faeb246d5d0b242292976893dedc421c58c Mon Sep 17 00:00:00 2001 From: Guilherme Carreiro Date: Wed, 13 May 2026 10:36:44 +0200 Subject: [PATCH] Adopt 'undef context=' --- lib/liquid/self_drop.rb | 4 ++-- lib/liquid/template.rb | 6 ++++-- test/integration/self_drop_context_test.rb | 15 +++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/liquid/self_drop.rb b/lib/liquid/self_drop.rb index 045653a5..4bfff0e4 100644 --- a/lib/liquid/self_drop.rb +++ b/lib/liquid/self_drop.rb @@ -31,10 +31,10 @@ module Liquid @self_context.variable_defined?(key) end - undef context - def to_liquid self end + + undef context= end end diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index b007765c..70ff0081 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -151,8 +151,10 @@ module Liquid c when Liquid::Drop - drop = args.shift - drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) + drop = args.shift + c = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) + drop.context = c if drop.respond_to?(:context=) + c when Hash Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) when nil diff --git a/test/integration/self_drop_context_test.rb b/test/integration/self_drop_context_test.rb index 9a1c86cf..338719e8 100644 --- a/test/integration/self_drop_context_test.rb +++ b/test/integration/self_drop_context_test.rb @@ -69,12 +69,12 @@ class SelfDropContextTest < Minitest::Test assert_template_result('42', source) end - def test_self_drop_context_writer_is_a_noop + def test_self_drop_context_setter_is_undefined context = Context.new drop = SelfDrop.new(context) - assert(drop.respond_to?(:context=)) - drop.context = Context.new - assert_nil(drop.instance_variable_get(:@context)) + refute(drop.respond_to?(:context=)) + + assert_template_result('42', '{{ self.x }}', { 'x' => 42 }) end def test_self_drop_with_strict_variables_does_not_raise_for_defined_var @@ -88,4 +88,11 @@ class SelfDropContextTest < Minitest::Test result = t.render({}, strict_variables: true) assert_equal('', result) end + + def test_self_drop_can_be_passed_as_bare_drop_to_render + t = Template.parse('{{ self.x }}') + drop = SelfDrop.new(Context.new({ 'x' => 42 })) + result = t.render(drop) + assert_equal('42', result) + end end