From b18930f2ea425ae570ae4bc0d4d41d1fd86bb260 Mon Sep 17 00:00:00 2001 From: Guilherme Carreiro Date: Tue, 2 Jun 2026 16:41:59 +0200 Subject: [PATCH] Fix `SelfDrop` equality --- lib/liquid/self_drop.rb | 14 ++++++++++++++ test/integration/self_drop_context_test.rb | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/liquid/self_drop.rb b/lib/liquid/self_drop.rb index 4bfff0e4..b753f383 100644 --- a/lib/liquid/self_drop.rb +++ b/lib/liquid/self_drop.rb @@ -35,6 +35,20 @@ module Liquid self end + def ==(other) + other.is_a?(SelfDrop) && other.self_context.equal?(@self_context) + end + + alias_method :eql?, :== + + def hash + @self_context.object_id.hash + end + + protected + + attr_reader :self_context + undef context= end end diff --git a/test/integration/self_drop_context_test.rb b/test/integration/self_drop_context_test.rb index 338719e8..6fd7ec88 100644 --- a/test/integration/self_drop_context_test.rb +++ b/test/integration/self_drop_context_test.rb @@ -77,6 +77,24 @@ class SelfDropContextTest < Minitest::Test assert_template_result('42', '{{ self.x }}', { 'x' => 42 }) end + def test_self_drop_repeated_lookups_compare_equal_for_same_context + context = Context.new + + assert_equal(context.find_variable("self"), context.find_variable("self")) + end + + def test_assigned_self_drop_compares_equal_to_itself + assert_template_result('T', '{% assign s = self %}{% if s == s %}T{% else %}F{% endif %}') + end + + def test_distinct_self_assignments_compare_equal_for_same_context + assert_template_result('T', '{% assign a = self %}{% assign b = self %}{% if a == b %}T{% else %}F{% endif %}') + end + + def test_bare_self_compares_equal_to_bare_self + assert_template_result('T', '{% if self == self %}T{% else %}F{% endif %}') + end + def test_self_drop_with_strict_variables_does_not_raise_for_defined_var t = Template.parse('{{ self.x }}') result = t.render({ 'x' => 42 }, strict_variables: true)