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

Use assert_template_result & assert_match_syntax_error in more places #1611

Merged
merged 1 commit into from Sep 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions test/integration/assign_test.rb
Expand Up @@ -6,13 +6,11 @@ class AssignTest < Minitest::Test
include Liquid

def test_assign_with_hyphen_in_variable_name
template_source = <<-END_TEMPLATE
{% assign this-thing = 'Print this-thing' %}
{{ this-thing }}
template_source = <<~END_TEMPLATE
{% assign this-thing = 'Print this-thing' -%}
{{ this-thing -}}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render!
assert_equal("Print this-thing", rendered.strip)
assert_template_result("Print this-thing", template_source)
end

def test_assigned_variable
Expand Down
6 changes: 2 additions & 4 deletions test/integration/block_test.rb
Expand Up @@ -6,10 +6,8 @@ class BlockTest < Minitest::Test
include Liquid

def test_unexpected_end_tag
exc = assert_raises(SyntaxError) do
Template.parse("{% if true %}{% endunless %}")
end
assert_equal(exc.message, "Liquid syntax error: 'endunless' is not a valid delimiter for if tags. use endif")
source = '{% if true %}{% endunless %}'
assert_match_syntax_error("Liquid syntax error (line 1): 'endunless' is not a valid delimiter for if tags. use endif", source)
end

def test_with_custom_tag
Expand Down
52 changes: 23 additions & 29 deletions test/integration/capture_test.rb
Expand Up @@ -10,44 +10,38 @@ def test_captures_block_content_in_variable
end

def test_capture_with_hyphen_in_variable_name
template_source = <<-END_TEMPLATE
{% capture this-thing %}Print this-thing{% endcapture %}
{{ this-thing }}
template_source = <<~END_TEMPLATE
{% capture this-thing %}Print this-thing{% endcapture -%}
{{ this-thing -}}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render!
assert_equal("Print this-thing", rendered.strip)
assert_template_result("Print this-thing", template_source)
end

def test_capture_to_variable_from_outer_scope_if_existing
template_source = <<-END_TEMPLATE
{% assign var = '' %}
{% if true %}
{% capture var %}first-block-string{% endcapture %}
{% endif %}
{% if true %}
{% capture var %}test-string{% endcapture %}
{% endif %}
{{var}}
template_source = <<~END_TEMPLATE
{% assign var = '' -%}
{% if true -%}
{% capture var %}first-block-string{% endcapture -%}
{% endif -%}
{% if true -%}
{% capture var %}test-string{% endcapture -%}
{% endif -%}
{{var-}}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render!
assert_equal("test-string", rendered.gsub(/\s/, ''))
assert_template_result("test-string", template_source)
end

def test_assigning_from_capture
template_source = <<-END_TEMPLATE
{% assign first = '' %}
{% assign second = '' %}
{% for number in (1..3) %}
{% capture first %}{{number}}{% endcapture %}
{% assign second = first %}
{% endfor %}
{{ first }}-{{ second }}
template_source = <<~END_TEMPLATE
{% assign first = '' -%}
{% assign second = '' -%}
{% for number in (1..3) -%}
{% capture first %}{{number}}{% endcapture -%}
{% assign second = first -%}
{% endfor -%}
{{ first }}-{{ second -}}
END_TEMPLATE
template = Template.parse(template_source)
rendered = template.render!
assert_equal("3-3", rendered.gsub(/\s/, ''))
assert_template_result("3-3", template_source)
end

def test_increment_assign_score_by_bytes_not_characters
Expand Down
153 changes: 71 additions & 82 deletions test/integration/context_test.rb
Expand Up @@ -102,7 +102,7 @@ def test_variables
end

def test_variables_not_existing
assert_nil(@context['does_not_exist'])
assert_template_result("true", "{% if does_not_exist == nil %}true{% endif %}")
end

def test_scoping
Expand All @@ -121,22 +121,18 @@ def test_scoping
end

def test_length_query
@context['numbers'] = [1, 2, 3, 4]
assert_template_result("true", "{% if numbers.size == 4 %}true{% endif %}",
{ "numbers" => [1, 2, 3, 4] })

assert_equal(4, @context['numbers.size'])
assert_template_result("true", "{% if numbers.size == 4 %}true{% endif %}",
{ "numbers" => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 } })

@context['numbers'] = { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }

assert_equal(4, @context['numbers.size'])

@context['numbers'] = { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 'size' => 1000 }

assert_equal(1000, @context['numbers.size'])
assert_template_result("true", "{% if numbers.size == 1000 %}true{% endif %}",
{ "numbers" => { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 'size' => 1000 } })
end

def test_hyphenated_variable
@context['oh-my'] = 'godz'
assert_equal('godz', @context['oh-my'])
assert_template_result("godz", "{{ oh-my }}", { "oh-my" => 'godz' })
end

def test_add_filter
Expand Down Expand Up @@ -188,24 +184,24 @@ def test_add_item_in_inner_scope
end

def test_hierachical_data
@context['hash'] = { "name" => 'tobi' }
assert_equal('tobi', @context['hash.name'])
assert_equal('tobi', @context['hash["name"]'])
assigns = { 'hash' => { "name" => 'tobi' } }
assert_template_result("tobi", "{{ hash.name }}", assigns)
assert_template_result("tobi", '{{ hash["name"] }}', assigns)
end

def test_keywords
assert_equal(true, @context['true'])
assert_equal(false, @context['false'])
assert_template_result("pass", "{% if true == expect %}pass{% endif %}", { "expect" => true })
assert_template_result("pass", "{% if false == expect %}pass{% endif %}", { "expect" => false })
end

def test_digits
assert_equal(100, @context['100'])
assert_equal(100.00, @context['100.00'])
assert_template_result("pass", "{% if 100 == expect %}pass{% endif %}", { "expect" => 100 })
assert_template_result("pass", "{% if 100.00 == expect %}pass{% endif %}", { "expect" => 100.00 })
end

def test_strings
assert_equal("hello!", @context['"hello!"'])
assert_equal("hello!", @context["'hello!'"])
assert_template_result("hello!", '{{ "hello!" }}')
assert_template_result("hello!", "{{ 'hello!' }}")
end

def test_merge
Expand All @@ -217,99 +213,90 @@ def test_merge
end

def test_array_notation
@context['test'] = [1, 2, 3, 4, 5]

assert_equal(1, @context['test[0]'])
assert_equal(2, @context['test[1]'])
assert_equal(3, @context['test[2]'])
assert_equal(4, @context['test[3]'])
assert_equal(5, @context['test[4]'])
assigns = { "test" => ["a", "b"] }
assert_template_result("a", "{{ test[0] }}", assigns)
assert_template_result("b", "{{ test[1] }}", assigns)
assert_template_result("pass", "{% if test[2] == nil %}pass{% endif %}", assigns)
end

def test_recoursive_array_notation
@context['test'] = { 'test' => [1, 2, 3, 4, 5] }

assert_equal(1, @context['test.test[0]'])

@context['test'] = [{ 'test' => 'worked' }]
assigns = { "test" => { 'test' => [1, 2, 3, 4, 5] } }
assert_template_result("1", "{{ test.test[0] }}", assigns)

assert_equal('worked', @context['test[0].test'])
assigns = { "test" => [{ 'test' => 'worked' }] }
assert_template_result("worked", "{{ test[0].test }}", assigns)
end

def test_hash_to_array_transition
@context['colors'] = {
assigns = { 'colors' => {
'Blue' => ['003366', '336699', '6699CC', '99CCFF'],
'Green' => ['003300', '336633', '669966', '99CC99'],
'Yellow' => ['CC9900', 'FFCC00', 'FFFF99', 'FFFFCC'],
'Red' => ['660000', '993333', 'CC6666', 'FF9999'],
}
} }

assert_equal('003366', @context['colors.Blue[0]'])
assert_equal('FF9999', @context['colors.Red[3]'])
assert_template_result("003366", "{{ colors.Blue[0] }}", assigns)
assert_template_result("FF9999", "{{ colors.Red[3] }}", assigns)
end

def test_try_first
@context['test'] = [1, 2, 3, 4, 5]

assert_equal(1, @context['test.first'])
assert_equal(5, @context['test.last'])

@context['test'] = { 'test' => [1, 2, 3, 4, 5] }
assigns = { 'test' => [1, 2, 3, 4, 5] }
assert_template_result("1", "{{ test.first }}", assigns)
assert_template_result("pass", "{% if test.last == 5 %}pass{% endif %}", assigns)

assert_equal(1, @context['test.test.first'])
assert_equal(5, @context['test.test.last'])
assigns = { "test" => { "test" => [1, 2, 3, 4, 5] } }
assert_template_result("1", "{{ test.test.first }}", assigns)
assert_template_result("5", "{{ test.test.last }}", assigns)

@context['test'] = [1]
assert_equal(1, @context['test.first'])
assert_equal(1, @context['test.last'])
assigns = { "test" => [1] }
assert_template_result("1", "{{ test.first }}", assigns)
assert_template_result("1", "{{ test.last }}", assigns)
end

def test_access_hashes_with_hash_notation
@context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
@context['product'] = { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] }
assigns = { 'products' => { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] } }
assert_template_result("5", '{{ products["count"] }}', assigns)
assert_template_result("deepsnow", '{{ products["tags"][0] }}', assigns)
assert_template_result("deepsnow", '{{ products["tags"].first }}', assigns)

assert_equal(5, @context['products["count"]'])
assert_equal('deepsnow', @context['products["tags"][0]'])
assert_equal('deepsnow', @context['products["tags"].first'])
assert_equal('draft151cm', @context['product["variants"][0]["title"]'])
assert_equal('element151cm', @context['product["variants"][1]["title"]'])
assert_equal('draft151cm', @context['product["variants"][0]["title"]'])
assert_equal('element151cm', @context['product["variants"].last["title"]'])
assigns = { 'product' => { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] } }
assert_template_result("draft151cm", '{{ product["variants"][0]["title"] }}', assigns)
assert_template_result("element151cm", '{{ product["variants"][1]["title"] }}', assigns)
assert_template_result("draft151cm", '{{ product["variants"][0]["title"] }}', assigns)
assert_template_result("element151cm", '{{ product["variants"].last["title"] }}', assigns)
end

def test_access_variable_with_hash_notation
@context['foo'] = 'baz'
@context['bar'] = 'foo'

assert_equal('baz', @context['["foo"]'])
assert_equal('baz', @context['[bar]'])
assert_template_result('baz', '{{ ["foo"] }}', { "foo" => "baz" })
assert_template_result('baz', '{{ [bar] }}', { 'foo' => 'baz', 'bar' => 'foo' })
end

def test_access_hashes_with_hash_access_variables
@context['var'] = 'tags'
@context['nested'] = { 'var' => 'tags' }
@context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
assigns = {
'var' => 'tags',
'nested' => { 'var' => 'tags' },
'products' => { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] },
}

assert_equal('deepsnow', @context['products[var].first'])
assert_equal('freestyle', @context['products[nested.var].last'])
assert_template_result('deepsnow', '{{ products[var].first }}', assigns)
assert_template_result('freestyle', '{{ products[nested.var].last }}', assigns)
end

def test_hash_notation_only_for_hash_access
@context['array'] = [1, 2, 3, 4, 5]
@context['hash'] = { 'first' => 'Hello' }
assigns = { "array" => [1, 2, 3, 4, 5] }
assert_template_result("1", "{{ array.first }}", assigns)
assert_template_result("pass", '{% if array["first"] == nil %}pass{% endif %}', assigns)

assert_equal(1, @context['array.first'])
assert_nil(@context['array["first"]'])
assert_equal('Hello', @context['hash["first"]'])
assert_template_result("Hello", '{{ hash["first"] }}', { "hash" => { "first" => "Hello" } })
end

def test_first_can_appear_in_middle_of_callchain
@context['product'] = { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] }
assigns = { "product" => { 'variants' => [{ 'title' => 'draft151cm' }, { 'title' => 'element151cm' }] } }

assert_equal('draft151cm', @context['product.variants[0].title'])
assert_equal('element151cm', @context['product.variants[1].title'])
assert_equal('draft151cm', @context['product.variants.first.title'])
assert_equal('element151cm', @context['product.variants.last.title'])
assert_template_result('draft151cm', '{{ product.variants[0].title }}', assigns)
assert_template_result('element151cm', '{{ product.variants[1].title }}', assigns)
assert_template_result('draft151cm', '{{ product.variants.first.title }}', assigns)
assert_template_result('element151cm', '{{ product.variants.last.title }}', assigns)
end

def test_cents
Expand Down Expand Up @@ -351,10 +338,12 @@ def test_nested_context_from_within_drop
end

def test_ranges
@context.merge("test" => '5')
assert_equal((1..5), @context['(1..5)'])
assert_equal((1..5), @context['(1..test)'])
assert_equal((5..5), @context['(test..test)'])
assert_template_result("1..5", '{{ (1..5) }}')
assert_template_result("pass", '{% if (1..5) == expect %}pass{% endif %}', { "expect" => (1..5) })

assigns = { "test" => '5' }
assert_template_result("1..5", "{{ (1..test) }}", assigns)
assert_template_result("5..5", "{{ (test..test) }}", assigns)
end

def test_cents_through_drop_nestedly
Expand Down
12 changes: 4 additions & 8 deletions test/integration/document_test.rb
Expand Up @@ -6,16 +6,12 @@ class DocumentTest < Minitest::Test
include Liquid

def test_unexpected_outer_tag
exc = assert_raises(SyntaxError) do
Template.parse("{% else %}")
end
assert_equal(exc.message, "Liquid syntax error: Unexpected outer 'else' tag")
source = "{% else %}"
assert_match_syntax_error("Liquid syntax error (line 1): Unexpected outer 'else' tag", source)
end

def test_unknown_tag
exc = assert_raises(SyntaxError) do
Template.parse("{% foo %}")
end
assert_equal(exc.message, "Liquid syntax error: Unknown tag 'foo'")
source = "{% foo %}"
assert_match_syntax_error("Liquid syntax error (line 1): Unknown tag 'foo'", source)
end
end