From f3270183a778d5bc7dcfb0874d4c0543c7877814 Mon Sep 17 00:00:00 2001 From: Josh Faigan Date: Tue, 1 Oct 2024 14:23:55 -0400 Subject: [PATCH] tests passing --- lib/liquid/tags/render.rb | 9 ++++--- test/integration/tags/snippet_test.rb | 37 ++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 14274c06..520e4c0f 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -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) diff --git a/test/integration/tags/snippet_test.rb b/test/integration/tags/snippet_test.rb index 4cf045f8..fd1cbcdb 100644 --- a/test/integration/tags/snippet_test.rb +++ b/test/integration/tags/snippet_test.rb @@ -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| %} + + {% endsnippet %} + + {% snippet "inputs" |type, value| %} + + {% endsnippet %} + + {%- render "input", type: "text" -%} + {%- render "inputs", type: "password", value: "pass" -%} + LIQUID + expected = <<~OUTPUT + + + + + + + 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| %} {% endsnippet %} - - {% snippet "banner"%} - {{ type }} + {% snippet "no_leak" %} + {% endsnippet %} {%- render "input", type: "text" -%} - {%- render "banner" -%} + {%- render "no_leak" -%} LIQUID - expected = <<~OUTPUT.strip + expected = <<~OUTPUT + - + OUTPUT assert_template_result(expected, template)