Skip to content

Commit

Permalink
Changes static registers to not be frozen (Shopify#1163)
Browse files Browse the repository at this point in the history
* Changes static registers to not be frozen

* Add frozen test to static registers
  • Loading branch information
shopmike authored Sep 19, 2019
1 parent adb40c4 commit ef13343
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib/liquid/static_registers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module Liquid
class StaticRegisters
attr_reader :static_registers, :registers
attr_reader :static, :registers

def initialize(registers = {})
@static_registers = registers.is_a?(StaticRegisters) ? registers.static_registers : registers.freeze
@static = registers.is_a?(StaticRegisters) ? registers.static : registers
@registers = {}
end

Expand All @@ -17,7 +17,7 @@ def [](key)
if @registers.key?(key)
@registers[key]
else
@static_registers[key]
@static[key]
end
end

Expand All @@ -30,7 +30,7 @@ def fetch(key, default = nil)
end

def key?(key)
@registers.key?(key) || @static_registers.key?(key)
@registers.key?(key) || @static.key?(key)
end
end
end
57 changes: 47 additions & 10 deletions test/unit/static_registers_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def set_with_static
static_register["two"] = 4
static_register[true] = "foo"

assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
assert_equal({ nil => false, "two" => 4, true => "foo" }, static_register.registers)

static_register
Expand All @@ -109,7 +109,7 @@ def test_delete_with_static
assert_nil static_register.delete(:one)

assert_equal({}, static_register.registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
end

def test_fetch_with_static
Expand All @@ -135,10 +135,10 @@ def test_key_with_static
assert_equal true, static_register.key?(true)
end

def test_static_register_frozen
def test_static_register_can_be_frozen
static_register = set_with_static

static = static_register.static_registers
static = static_register.static.freeze

assert_raises(RuntimeError) do
static["two"] = "foo"
Expand Down Expand Up @@ -176,9 +176,9 @@ def test_new_static_retains_static
assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, new_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, newest_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, new_register.static)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, newest_register.static)
end

def test_multiple_instances_are_unique
Expand All @@ -204,8 +204,45 @@ def test_multiple_instances_are_unique
assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static_registers)
assert_equal({ foo: :bar }, new_register.static_registers)
assert_equal({ bar: :foo }, newest_register.static_registers)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
assert_equal({ foo: :bar }, new_register.static)
assert_equal({ bar: :foo }, newest_register.static)
end

def test_can_update_static_directly_and_updates_all_instances
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil)
static_register["one"] = 1
static_register["two"] = 2
static_register["three"] = 3

new_register = StaticRegisters.new(static_register)
assert_equal({}, new_register.registers)

assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)

new_register["one"] = 4
new_register["two"] = 5
new_register["three"] = 6
new_register.static["four"] = 10

newest_register = StaticRegisters.new(new_register)
assert_equal({}, newest_register.registers)

assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 10 }, new_register.static)

newest_register["one"] = 7
newest_register["two"] = 8
newest_register["three"] = 9
new_register.static["four"] = 5
new_register.static["five"] = 15

assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)

assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, newest_register.static)

assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, static_register.static)
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, new_register.static)
end
end

0 comments on commit ef13343

Please sign in to comment.