From 308dfc3cb65d548110c36725908e6af760b461bc Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 6 Sep 2022 16:22:08 -0400 Subject: [PATCH 1/5] Add partials, error_mode and render_errors options to assert_template_result --- test/test_helper.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 3afc8057..aee4d84e 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -37,8 +37,16 @@ module Minitest module Assertions include Liquid - def assert_template_result(expected, template, assigns = {}, message: nil) - assert_equal(expected, Template.parse(template, line_numbers: true).render!(assigns), message) + def assert_template_result( + expected, template, assigns = {}, + message: nil, partials: nil, error_mode: nil, render_errors: false + ) + template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym) + file_system = StubFileSystem.new(partials) if partials + registers = Liquid::Registers.new(file_system: file_system) + context = Liquid::Context.build(environments: assigns, rethrow_errors: !render_errors, registers: registers) + output = template.render(context) + assert_equal(expected, output, message) end def assert_match_syntax_error(match, template) From 57fdc1b5fbb5323145572438d4f92a0c11120db1 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 6 Sep 2022 16:23:13 -0400 Subject: [PATCH 2/5] Add sample use of partials assert_template_result options --- test/integration/blank_test.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/integration/blank_test.rb b/test/integration/blank_test.rb index e3a82c29..b7ca205d 100644 --- a/test/integration/blank_test.rb +++ b/test/integration/blank_test.rb @@ -9,12 +9,6 @@ class FoobarTag < Liquid::Tag end end -class BlankTestFileSystem - def read_template_file(template_path) - template_path - end -end - class BlankTest < Minitest::Test include Liquid N = 10 @@ -95,10 +89,12 @@ class BlankTest < Minitest::Test end def test_include_is_blank - Liquid::Template.file_system = BlankTestFileSystem.new - assert_template_result("foobar" * (N + 1), wrap("{% include 'foobar' %}")) - assert_template_result(" foobar " * (N + 1), wrap("{% include ' foobar ' %}")) - assert_template_result(" " * (N + 1), wrap(" {% include ' ' %} ")) + assert_template_result("foobar" * (N + 1), wrap("{% include 'foobar' %}"), + partials: { 'foobar' => 'foobar' }) + assert_template_result(" foobar " * (N + 1), wrap("{% include ' foobar ' %}"), + partials: { ' foobar ' => ' foobar ' }) + assert_template_result(" " * (N + 1), wrap(" {% include ' ' %} "), + partials: { ' ' => ' ' }) end def test_case_is_blank From 2c51a1922ae82250f368de06980dff444ac61deb Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 6 Sep 2022 16:43:01 -0400 Subject: [PATCH 3/5] Add error_mode option to assert_match_syntax_error --- test/test_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index aee4d84e..de2d534e 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -49,9 +49,9 @@ module Minitest assert_equal(expected, output, message) end - def assert_match_syntax_error(match, template) + def assert_match_syntax_error(match, template, error_mode: nil) exception = assert_raises(Liquid::SyntaxError) do - Template.parse(template, line_numbers: true).render + Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym).render end assert_match(match, exception.message) end From 3a64b3741f9bae1cf78abd4e21b5d528cae1d4c6 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 6 Sep 2022 16:43:16 -0400 Subject: [PATCH 4/5] Add sample usage of error_mode test helper option --- test/integration/assign_test.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/integration/assign_test.rb b/test/integration/assign_test.rb index 07d91e76..ba795fbb 100644 --- a/test/integration/assign_test.rb +++ b/test/integration/assign_test.rb @@ -34,14 +34,9 @@ class AssignTest < Minitest::Test end def test_assign_uses_error_mode - with_error_mode(:strict) do - assert_raises(SyntaxError) do - Template.parse("{% assign foo = ('X' | downcase) %}") - end - end - with_error_mode(:lax) do - assert(Template.parse("{% assign foo = ('X' | downcase) %}")) - end + assert_match_syntax_error("Expected dotdot but found pipe in ", + "{% assign foo = ('X' | downcase) %}", error_mode: :strict) + assert_template_result("", "{% assign foo = ('X' | downcase) %}", error_mode: :lax) end def test_expression_with_whitespace_in_square_brackets From 4648f0fa646f1f35db296d2bc517d951c19340bd Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 6 Sep 2022 16:54:38 -0400 Subject: [PATCH 5/5] Add sample usage of `render_errors: true` assert_template_result option --- test/integration/tags/render_tag_test.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 08625525..28e50d1e 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -134,14 +134,17 @@ class RenderTagTest < Minitest::Test end def test_includes_will_not_render_inside_nested_sibling_tags - Liquid::Template.file_system = StubFileSystem.new( - 'foo' => 'bar', - 'nested_render_with_sibling_include' => '{% render "test_include" %}{% include "foo" %}', - 'test_include' => '{% include "foo" %}' + assert_template_result( + "Liquid error (test_include line 1): include usage is not allowed in this context" \ + "Liquid error (nested_render_with_sibling_include line 1): include usage is not allowed in this context", + '{% render "nested_render_with_sibling_include" %}', + partials: { + 'foo' => 'bar', + 'nested_render_with_sibling_include' => '{% render "test_include" %}{% include "foo" %}', + 'test_include' => '{% include "foo" %}', + }, + render_errors: true ) - - output = Liquid::Template.parse('{% render "nested_render_with_sibling_include" %}').render - assert_equal('Liquid error: include usage is not allowed in this contextLiquid error: include usage is not allowed in this context', output) end def test_render_tag_with