Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/liquid/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def find_variable(key, raise_on_not_found: true)

# `self` resolves to a SelfDrop (enabling `self['var']` lookups),
# but only when it hasn't been explicitly assigned as a local variable.
return SelfDrop.new(self) if key == Expression::SELF && !index
return @self_drop ||= SelfDrop.new(self) if key == Expression::SELF && !index

variable = if index
lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found)
Expand Down
14 changes: 14 additions & 0 deletions lib/liquid/self_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def to_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
22 changes: 22 additions & 0 deletions test/integration/self_drop_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ def test_self_drop_context_setter_is_undefined
assert_template_result('42', '{{ self.x }}', { 'x' => 42 })
end

def test_self_drop_repeated_lookups_compare_equal_for_same_context
context = Context.new
drop = context.find_variable("self")
cached_drop = context.find_variable("self")

assert_same(drop, cached_drop)
assert_equal(drop.object_id, cached_drop.object_id)
assert_equal(drop, cached_drop)
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)
Expand Down
Loading