mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Move VariableLookup parsing logic to .parse instead of initializer
Goal is to get rid of it entirely, but baby steps.
This commit is contained in:
@@ -7,10 +7,6 @@ module Liquid
|
|||||||
attr_reader :name, :lookups
|
attr_reader :name, :lookups
|
||||||
|
|
||||||
def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil)
|
def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil)
|
||||||
new(markup, string_scanner, cache)
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil)
|
|
||||||
lookups = markup.scan(VariableParser)
|
lookups = markup.scan(VariableParser)
|
||||||
|
|
||||||
name = lookups.shift
|
name = lookups.shift
|
||||||
@@ -21,12 +17,10 @@ module Liquid
|
|||||||
cache,
|
cache,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@name = name
|
|
||||||
|
|
||||||
@lookups = lookups
|
command_flags = 0
|
||||||
@command_flags = 0
|
|
||||||
|
|
||||||
@lookups.each_index do |i|
|
lookups.each_index do |i|
|
||||||
lookup = lookups[i]
|
lookup = lookups[i]
|
||||||
if lookup&.start_with?('[') && lookup&.end_with?(']')
|
if lookup&.start_with?('[') && lookup&.end_with?(']')
|
||||||
lookups[i] = Expression.parse(
|
lookups[i] = Expression.parse(
|
||||||
@@ -35,9 +29,17 @@ module Liquid
|
|||||||
cache,
|
cache,
|
||||||
)
|
)
|
||||||
elsif COMMAND_METHODS.include?(lookup)
|
elsif COMMAND_METHODS.include?(lookup)
|
||||||
@command_flags |= 1 << i
|
command_flags |= 1 << i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
new(name, lookups, command_flags)
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(name, lookups, command_flags)
|
||||||
|
@name = name
|
||||||
|
@lookups = lookups
|
||||||
|
@command_flags = command_flags
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_command?(lookup_index)
|
def lookup_command?(lookup_index)
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ class ConditionUnitTest < Minitest::Test
|
|||||||
def test_contains_works_on_arrays
|
def test_contains_works_on_arrays
|
||||||
@context = Liquid::Context.new
|
@context = Liquid::Context.new
|
||||||
@context['array'] = [1, 2, 3, 4, 5]
|
@context['array'] = [1, 2, 3, 4, 5]
|
||||||
array_expr = VariableLookup.new("array")
|
array_expr = VariableLookup.parse("array")
|
||||||
|
|
||||||
assert_evaluates_false(array_expr, 'contains', 0)
|
assert_evaluates_false(array_expr, 'contains', 0)
|
||||||
assert_evaluates_true(array_expr, 'contains', 1)
|
assert_evaluates_true(array_expr, 'contains', 1)
|
||||||
@@ -96,8 +96,8 @@ class ConditionUnitTest < Minitest::Test
|
|||||||
|
|
||||||
def test_contains_returns_false_for_nil_operands
|
def test_contains_returns_false_for_nil_operands
|
||||||
@context = Liquid::Context.new
|
@context = Liquid::Context.new
|
||||||
assert_evaluates_false(VariableLookup.new('not_assigned'), 'contains', '0')
|
assert_evaluates_false(VariableLookup.parse('not_assigned'), 'contains', '0')
|
||||||
assert_evaluates_false(0, 'contains', VariableLookup.new('not_assigned'))
|
assert_evaluates_false(0, 'contains', VariableLookup.parse('not_assigned'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_contains_return_false_on_wrong_data_type
|
def test_contains_return_false_on_wrong_data_type
|
||||||
@@ -149,7 +149,7 @@ class ConditionUnitTest < Minitest::Test
|
|||||||
@context = Liquid::Context.new
|
@context = Liquid::Context.new
|
||||||
@context['one'] = @context['another'] = "gnomeslab-and-or-liquid"
|
@context['one'] = @context['another'] = "gnomeslab-and-or-liquid"
|
||||||
|
|
||||||
assert_evaluates_true(VariableLookup.new("one"), '==', VariableLookup.new("another"))
|
assert_evaluates_true(VariableLookup.parse("one"), '==', VariableLookup.parse("another"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_context_is_deprecated
|
def test_default_context_is_deprecated
|
||||||
|
|||||||
@@ -7,20 +7,20 @@ class VariableUnitTest < Minitest::Test
|
|||||||
|
|
||||||
def test_variable
|
def test_variable
|
||||||
var = create_variable('hello')
|
var = create_variable('hello')
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_filters
|
def test_filters
|
||||||
var = create_variable('hello | textileze')
|
var = create_variable('hello | textileze')
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['textileze', []]], var.filters)
|
assert_equal([['textileze', []]], var.filters)
|
||||||
|
|
||||||
var = create_variable('hello | textileze | paragraph')
|
var = create_variable('hello | textileze | paragraph')
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
||||||
|
|
||||||
var = create_variable(%( hello | strftime: '%Y'))
|
var = create_variable(%( hello | strftime: '%Y'))
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['strftime', ['%Y']]], var.filters)
|
assert_equal([['strftime', ['%Y']]], var.filters)
|
||||||
|
|
||||||
var = create_variable(%( 'typo' | link_to: 'Typo', true ))
|
var = create_variable(%( 'typo' | link_to: 'Typo', true ))
|
||||||
@@ -44,11 +44,11 @@ class VariableUnitTest < Minitest::Test
|
|||||||
assert_equal([['repeat', [3, 3, 3]]], var.filters)
|
assert_equal([['repeat', [3, 3, 3]]], var.filters)
|
||||||
|
|
||||||
var = create_variable(%( hello | strftime: '%Y, okay?'))
|
var = create_variable(%( hello | strftime: '%Y, okay?'))
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['strftime', ['%Y, okay?']]], var.filters)
|
assert_equal([['strftime', ['%Y, okay?']]], var.filters)
|
||||||
|
|
||||||
var = create_variable(%( hello | things: "%Y, okay?", 'the other one'))
|
var = create_variable(%( hello | things: "%Y, okay?", 'the other one'))
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['things', ['%Y, okay?', 'the other one']]], var.filters)
|
assert_equal([['things', ['%Y, okay?', 'the other one']]], var.filters)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -60,15 +60,15 @@ class VariableUnitTest < Minitest::Test
|
|||||||
|
|
||||||
def test_filters_without_whitespace
|
def test_filters_without_whitespace
|
||||||
var = create_variable('hello | textileze | paragraph')
|
var = create_variable('hello | textileze | paragraph')
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
||||||
|
|
||||||
var = create_variable('hello|textileze|paragraph')
|
var = create_variable('hello|textileze|paragraph')
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
assert_equal([['textileze', []], ['paragraph', []]], var.filters)
|
||||||
|
|
||||||
var = create_variable("hello|replace:'foo','bar'|textileze")
|
var = create_variable("hello|replace:'foo','bar'|textileze")
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['replace', ['foo', 'bar']], ['textileze', []]], var.filters)
|
assert_equal([['replace', ['foo', 'bar']], ['textileze', []]], var.filters)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -99,8 +99,8 @@ class VariableUnitTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_dashes
|
def test_dashes
|
||||||
assert_equal(VariableLookup.new('foo-bar'), create_variable('foo-bar').name)
|
assert_equal(VariableLookup.parse('foo-bar'), create_variable('foo-bar').name)
|
||||||
assert_equal(VariableLookup.new('foo-bar-2'), create_variable('foo-bar-2').name)
|
assert_equal(VariableLookup.parse('foo-bar-2'), create_variable('foo-bar-2').name)
|
||||||
|
|
||||||
assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') }
|
assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') }
|
||||||
assert_raises(Liquid::SyntaxError) { create_variable('-foo') }
|
assert_raises(Liquid::SyntaxError) { create_variable('-foo') }
|
||||||
@@ -114,12 +114,12 @@ class VariableUnitTest < Minitest::Test
|
|||||||
|
|
||||||
def test_string_dot
|
def test_string_dot
|
||||||
var = create_variable(%( test.test ))
|
var = create_variable(%( test.test ))
|
||||||
assert_equal(VariableLookup.new('test.test'), var.name)
|
assert_equal(VariableLookup.parse('test.test'), var.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_filter_with_keyword_arguments
|
def test_filter_with_keyword_arguments
|
||||||
var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye'))
|
var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye'))
|
||||||
assert_equal(VariableLookup.new('hello'), var.name)
|
assert_equal(VariableLookup.parse('hello'), var.name)
|
||||||
assert_equal([['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters)
|
assert_equal([['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ class VariableUnitTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_variable_lookup_interface
|
def test_variable_lookup_interface
|
||||||
lookup = VariableLookup.new('a.b.c')
|
lookup = VariableLookup.parse('a.b.c')
|
||||||
assert_equal('a', lookup.name)
|
assert_equal('a', lookup.name)
|
||||||
assert_equal(['b', 'c'], lookup.lookups)
|
assert_equal(['b', 'c'], lookup.lookups)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user