tests passing

This commit is contained in:
Josh Faigan
2024-10-01 14:23:55 -04:00
parent 3399981b89
commit f3270183a7
2 changed files with 36 additions and 10 deletions
+5 -4
View File
@@ -69,15 +69,16 @@ module Liquid
# Inline snippets take precedence over external snippets
if (inline_snippet = context.registers[:inline_snippet][template_name])
inner_context = context.new_isolated_subcontext
# binding.irb
snippet_body = inline_snippet[:body]
snippet_args = inline_snippet[:args]
# Validate and set the arguments in the inner context
@attributes.each do |key, value|
if snippet_args.include?(key)
inner_context[key] = context.evaluate(value)
unless snippet_args.include?(key)
raise Liquid::ArgumentError, "Invalid argument `#{key}` for snippet `#{template_name}`"
end
inner_context[key] = context.evaluate(value)
end
return output << snippet_body.render(inner_context)
+31 -6
View File
@@ -104,6 +104,31 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippets_using_same_argument_name
template = <<~LIQUID.strip
{% snippet "input" |type| %}
<input type="{{ type }}" />
{% endsnippet %}
{% snippet "inputs" |type, value| %}
<input type="{{ type }}" value="{{ value }}" />
{% endsnippet %}
{%- render "input", type: "text" -%}
{%- render "inputs", type: "password", value: "pass" -%}
LIQUID
expected = <<~OUTPUT
<input type="text" />
<input type="password" value="pass" />
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_empty_string_when_missing_argument
template = <<~LIQUID.strip
{% snippet "input" |type| %}
@@ -145,19 +170,19 @@ class SnippetTest < Minitest::Test
{% snippet "input" |type| %}
<input type="{{ type }}" />
{% endsnippet %}
{% snippet "banner"%}
{{ type }}
{% snippet "no_leak" %}
<input type="{{ type }}" />
{% endsnippet %}
{%- render "input", type: "text" -%}
{%- render "banner" -%}
{%- render "no_leak" -%}
LIQUID
expected = <<~OUTPUT.strip
expected = <<~OUTPUT
<input type="text" />
<input type="" />
OUTPUT
assert_template_result(expected, template)