Files
liquid/test/unit/parse_context_unit_test.rb
T
Charles-P. Clermont cf3d845315 Move safe_parse_expression into Parser.expression_node
- Make `Parser` accept the expression cache
- Remove `safe_parse_expression` from `ParseContext`
- Replace all usage of `safe_parse_expression` with `parser.expression_node`
2026-01-26 16:52:17 -05:00

87 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'test_helper'
class ParseContextUnitTest < Minitest::Test
include Liquid
def test_safe_parse_expression_with_variable_lookup
parser = parse_context.new_parser('product.title')
result = parser.expression_node
assert_instance_of(VariableLookup, result)
assert_equal('product', result.name)
assert_equal(['title'], result.lookups)
end
def test_safe_parse_expression_raises_syntax_error_for_invalid_expression
parser = parse_context.new_parser('')
error = assert_raises(Liquid::SyntaxError) do
parser.expression_node
end
assert_match(/is not a valid expression/, error.message)
end
def test_parse_expression_with_variable_lookup
error = assert_raises(Liquid::InternalError) do
parse_context.parse_expression('product.title')
end
assert_match(/unsafe parse_expression cannot be used/, error.message)
end
def test_parse_expression_with_safe_true
result = parse_context.parse_expression('product.title', safe: true)
assert_instance_of(VariableLookup, result)
assert_equal('product', result.name)
assert_equal(['title'], result.lookups)
end
def test_parse_expression_with_empty_string
error = assert_raises(Liquid::InternalError) do
parse_context.parse_expression('')
end
assert_match(/unsafe parse_expression cannot be used/, error.message)
end
def test_parse_expression_with_empty_string_and_safe_true
result = parse_context.parse_expression('', safe: true)
assert_nil(result)
end
def test_safe_parse_expression_advances_parser_pointer
parser = parse_context.new_parser('foo, bar')
# parser.expression_node consumes "foo"
first_result = parser.expression_node
assert_instance_of(VariableLookup, first_result)
assert_equal('foo', first_result.name)
parser.consume(:comma)
# parser.expression_node consumes "bar"
second_result = parser.expression_node
assert_instance_of(VariableLookup, second_result)
assert_equal('bar', second_result.name)
parser.consume(:end_of_string)
end
def test_parse_expression_with_whitespace
result = parse_context.parse_expression(' ', safe: true)
assert_nil(result)
end
private
def parse_context
@parse_context ||= ParseContext.new(
environment: Environment.build,
)
end
end