Ensure that partial caches are shared with subcontexts

Make Context use StaticRegisters by default. This makes it easier to
ensure that all subcontexts share the same static registers.

Co-authored-by: Dylan Thacker-Smith <[email protected]>
This commit is contained in:
Chris AtLee
2022-04-08 10:05:58 -04:00
co-authored by Dylan Thacker-Smith
parent 1cae1e497f
commit 6c2c621712
7 changed files with 66 additions and 9 deletions
+14
View File
@@ -618,6 +618,20 @@ class ContextTest < Minitest::Test
end
end
def test_context_always_uses_static_registers
registers = {
my_register: :my_value,
}
c = Context.new({}, {}, registers)
assert_instance_of(StaticRegisters, c.registers)
assert_equal(:my_value, c.registers[:my_register])
r = StaticRegisters.new(registers)
c = Context.new({}, {}, r)
assert_instance_of(StaticRegisters, c.registers)
assert_equal(:my_value, c.registers[:my_register])
end
private
def assert_no_object_allocations
+2 -2
View File
@@ -32,7 +32,7 @@ class TestDrop < Liquid::Drop
attr_reader :value
def registers
@context.registers
{ @value => @context.registers[@value] }
end
end
@@ -440,7 +440,7 @@ class StandardFiltersTest < Minitest::Test
end
def test_map_calls_context=
model = TestModel.new(value: "test")
model = TestModel.new(value: :test)
template = Template.parse('{{ foo | map: "registers" }}')
template.registers[:test] = 1234
+31
View File
@@ -125,4 +125,35 @@ class PartialCacheUnitTest < Minitest::Test
assert_equal('my partial body', partial.render)
assert_equal(1, template_factory.count)
end
def test_cache_state_is_shared_for_subcontexts
parse_context = Liquid::ParseContext.new
shared_file_system = StubFileSystem.new(
'my_partial' => 'my shared value'
)
context = Liquid::Context.build(
registers: Liquid::StaticRegisters.new(
file_system: shared_file_system,
)
)
subcontext = context.new_isolated_subcontext
assert_equal(subcontext.registers[:cached_partials].object_id, context.registers[:cached_partials].object_id)
2.times do
Liquid::PartialCache.load(
'my_partial',
context: context,
parse_context: parse_context
)
Liquid::PartialCache.load(
'my_partial',
context: subcontext,
parse_context: parse_context
)
end
assert_equal(1, shared_file_system.file_read_count)
end
end