Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partials, error_mode and render_errors options to assert_template_result #1614

Merged
merged 5 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions test/integration/assign_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ def test_assign_syntax_error
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
Expand Down
16 changes: 6 additions & 10 deletions test/integration/blank_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def render_to_output_buffer(_context, output)
end
end

class BlankTestFileSystem
def read_template_file(template_path)
template_path
end
end

class BlankTest < Minitest::Test
include Liquid
N = 10
Expand Down Expand Up @@ -95,10 +89,12 @@ def test_raw_is_not_blank
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
Expand Down
17 changes: 10 additions & 7 deletions test/integration/tags/render_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ def test_includes_will_not_render_inside_render_tag
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
Expand Down
16 changes: 12 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ def fixture(name)
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)
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
Expand Down