forked from Shopify/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplementation of Static Registers (Shopify#1157)
- Loading branch information
Showing
5 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Liquid | ||
class StaticRegisters | ||
attr_reader :static_registers, :registers | ||
|
||
def initialize(registers = {}) | ||
@static_registers = registers.is_a?(StaticRegisters) ? registers.static_registers : registers.freeze | ||
@registers = {} | ||
end | ||
|
||
def []=(key, value) | ||
@registers[key] = value | ||
end | ||
|
||
def [](key) | ||
if @registers.key?(key) | ||
@registers[key] | ||
else | ||
@static_registers[key] | ||
end | ||
end | ||
|
||
def delete(key) | ||
@registers.delete(key) | ||
end | ||
|
||
def fetch(key, default = nil) | ||
key?(key) ? self[key] : default | ||
end | ||
|
||
def key?(key) | ||
@registers.key?(key) || @static_registers.key?(key) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
require 'test_helper' | ||
|
||
class StaticRegistersUnitTest < Minitest::Test | ||
include Liquid | ||
|
||
def set | ||
static_register = StaticRegisters.new | ||
static_register[nil] = true | ||
static_register[1] = :one | ||
static_register[:one] = "one" | ||
static_register["two"] = "three" | ||
static_register["two"] = 3 | ||
static_register[false] = nil | ||
|
||
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.registers) | ||
|
||
static_register | ||
end | ||
|
||
def test_get | ||
static_register = set | ||
|
||
assert_equal true, static_register[nil] | ||
assert_equal :one, static_register[1] | ||
assert_equal "one", static_register[:one] | ||
assert_equal 3, static_register["two"] | ||
assert_nil static_register[false] | ||
assert_nil static_register["unknown"] | ||
end | ||
|
||
def test_delete | ||
static_register = set | ||
|
||
assert_equal true, static_register.delete(nil) | ||
assert_equal :one, static_register.delete(1) | ||
assert_equal "one", static_register.delete(:one) | ||
assert_equal 3, static_register.delete("two") | ||
assert_nil static_register.delete(false) | ||
assert_nil static_register.delete("unknown") | ||
|
||
assert_equal({}, static_register.registers) | ||
end | ||
|
||
def test_fetch | ||
static_register = set | ||
|
||
assert_equal true, static_register.fetch(nil) | ||
assert_equal :one, static_register.fetch(1) | ||
assert_equal "one", static_register.fetch(:one) | ||
assert_equal 3, static_register.fetch("two") | ||
assert_nil static_register.fetch(false) | ||
assert_nil static_register.fetch("unknown") | ||
end | ||
|
||
def test_fetch_default | ||
static_register = StaticRegisters.new | ||
|
||
assert_equal true, static_register.fetch(nil, true) | ||
assert_equal :one, static_register.fetch(1, :one) | ||
assert_equal "one", static_register.fetch(:one, "one") | ||
assert_equal 3, static_register.fetch("two", 3) | ||
assert_nil static_register.fetch(false, nil) | ||
end | ||
|
||
def test_key | ||
static_register = set | ||
|
||
assert_equal true, static_register.key?(nil) | ||
assert_equal true, static_register.key?(1) | ||
assert_equal true, static_register.key?(:one) | ||
assert_equal true, static_register.key?("two") | ||
assert_equal true, static_register.key?(false) | ||
assert_equal false, static_register.key?("unknown") | ||
assert_equal false, static_register.key?(true) | ||
end | ||
|
||
def set_with_static | ||
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil) | ||
static_register[nil] = false | ||
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 => false, "two" => 4, true => "foo" }, static_register.registers) | ||
|
||
static_register | ||
end | ||
|
||
def test_get_with_static | ||
static_register = set_with_static | ||
|
||
assert_equal false, static_register[nil] | ||
assert_equal :one, static_register[1] | ||
assert_equal "one", static_register[:one] | ||
assert_equal 4, static_register["two"] | ||
assert_equal "foo", static_register[true] | ||
assert_nil static_register[false] | ||
end | ||
|
||
def test_delete_with_static | ||
static_register = set_with_static | ||
|
||
assert_equal false, static_register.delete(nil) | ||
assert_equal 4, static_register.delete("two") | ||
assert_equal "foo", static_register.delete(true) | ||
assert_nil static_register.delete("unknown") | ||
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) | ||
end | ||
|
||
def test_fetch_with_static | ||
static_register = set_with_static | ||
|
||
assert_equal false, static_register.fetch(nil) | ||
assert_equal :one, static_register.fetch(1) | ||
assert_equal "one", static_register.fetch(:one) | ||
assert_equal 4, static_register.fetch("two") | ||
assert_equal "foo", static_register.fetch(true) | ||
assert_nil static_register.fetch(false) | ||
end | ||
|
||
def test_key_with_static | ||
static_register = set_with_static | ||
|
||
assert_equal true, static_register.key?(nil) | ||
assert_equal true, static_register.key?(1) | ||
assert_equal true, static_register.key?(:one) | ||
assert_equal true, static_register.key?("two") | ||
assert_equal true, static_register.key?(false) | ||
assert_equal false, static_register.key?("unknown") | ||
assert_equal true, static_register.key?(true) | ||
end | ||
|
||
def test_static_register_frozen | ||
static_register = set_with_static | ||
|
||
static = static_register.static_registers | ||
|
||
assert_raises(RuntimeError) do | ||
static["two"] = "foo" | ||
end | ||
|
||
assert_raises(RuntimeError) do | ||
static["unknown"] = "foo" | ||
end | ||
|
||
assert_raises(RuntimeError) do | ||
static.delete("two") | ||
end | ||
end | ||
|
||
def test_new_static_retains_static | ||
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) | ||
|
||
new_register["one"] = 4 | ||
new_register["two"] = 5 | ||
new_register["three"] = 6 | ||
|
||
newest_register = StaticRegisters.new(new_register) | ||
assert_equal({}, newest_register.registers) | ||
|
||
newest_register["one"] = 7 | ||
newest_register["two"] = 8 | ||
newest_register["three"] = 9 | ||
|
||
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) | ||
end | ||
|
||
def test_multiple_instances_are_unique | ||
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(foo: :bar) | ||
assert_equal({}, new_register.registers) | ||
|
||
new_register["one"] = 4 | ||
new_register["two"] = 5 | ||
new_register["three"] = 6 | ||
|
||
newest_register = StaticRegisters.new(bar: :foo) | ||
assert_equal({}, newest_register.registers) | ||
|
||
newest_register["one"] = 7 | ||
newest_register["two"] = 8 | ||
newest_register["three"] = 9 | ||
|
||
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) | ||
end | ||
end |