mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-14 16:30:40 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc5855d619 |
@@ -1,6 +0,0 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: weekly
|
||||
@@ -31,8 +31,8 @@ jobs:
|
||||
- { ruby: ruby-head, allowed-failure: false, rubyopt: "--yjit" }
|
||||
name: Test Ruby ${{ matrix.entry.ruby }}
|
||||
steps:
|
||||
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
|
||||
- uses: ruby/setup-ruby@dffc446db9ba5a0c4446edb5bca1c5c473a806c5 # v1.235.0
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: ${{ matrix.entry.ruby }}
|
||||
bundler-cache: true
|
||||
@@ -45,8 +45,8 @@ jobs:
|
||||
memory_profile:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
|
||||
- uses: ruby/setup-ruby@dffc446db9ba5a0c4446edb5bca1c5c473a806c5 # v1.235.0
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
bundler-cache: true
|
||||
- run: bundle exec rake memory_profile:run
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
require 'cgi'
|
||||
require 'base64'
|
||||
require 'bigdecimal'
|
||||
|
||||
module Liquid
|
||||
module StandardFilters
|
||||
MAX_I32 = (1 << 31) - 1
|
||||
@@ -711,16 +712,7 @@ module Liquid
|
||||
input.gsub(/\r?\n/, "<br />\n")
|
||||
end
|
||||
|
||||
# @liquid_public_docs
|
||||
# @liquid_type filter
|
||||
# @liquid_category date
|
||||
# @liquid_summary
|
||||
# Formats a date according to a specified format string.
|
||||
# @liquid_description
|
||||
# This filter formats a date using various format specifiers. If the format string is empty,
|
||||
# the original input is returned. If the input cannot be converted to a date, the original input is returned.
|
||||
#
|
||||
# The following format specifiers can be used:
|
||||
# Reformat a date using Ruby's core Time#strftime( string ) -> string
|
||||
#
|
||||
# %a - The abbreviated weekday name (``Sun'')
|
||||
# %A - The full weekday name (``Sunday'')
|
||||
@@ -749,8 +741,8 @@ module Liquid
|
||||
# %Y - Year with century
|
||||
# %Z - Time zone name
|
||||
# %% - Literal ``%'' character
|
||||
# @liquid_syntax date | date: string
|
||||
# @liquid_return [string]
|
||||
#
|
||||
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
|
||||
def date(input, format)
|
||||
str_format = Utils.to_s(format)
|
||||
return input if str_format.empty?
|
||||
@@ -997,7 +989,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def raise_property_error(property)
|
||||
raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'"
|
||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||
end
|
||||
|
||||
def apply_operation(input, operand, operation)
|
||||
@@ -1069,10 +1061,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def uniq(&block)
|
||||
to_a.uniq do |item|
|
||||
item = Utils.to_liquid_value(item)
|
||||
block ? yield(item) : item
|
||||
end
|
||||
to_a.uniq(&block)
|
||||
end
|
||||
|
||||
def compact
|
||||
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# @liquid_description
|
||||
# Variables that are declared with `decrement` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||
# [snippets](/themes/architecture/snippets) included in the file.
|
||||
# [snippets](/themes/architecture#snippets) included in the file.
|
||||
#
|
||||
# Similarly, variables that are created with `decrement` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `decrement` and [`increment`](/docs/api/liquid/tags/increment) share
|
||||
|
||||
@@ -13,16 +13,12 @@ module Liquid
|
||||
# Liquid code inside will be parsed but not executed. This facilitates
|
||||
# tooling support for features like code completion, linting, and inline
|
||||
# documentation.
|
||||
#
|
||||
# For detailed documentation syntax and examples, see the
|
||||
# [`LiquidDoc` reference](/docs/storefronts/themes/tools/liquid-doc).
|
||||
#
|
||||
# @liquid_syntax
|
||||
# {% doc %}
|
||||
# Renders a message.
|
||||
#
|
||||
# @param {string} foo - A string value.
|
||||
# @param {string} [bar] - An optional string value.
|
||||
# @param {string} foo - A foo value.
|
||||
# @param {string} [bar] - An optional bar value.
|
||||
#
|
||||
# @example
|
||||
# {% render 'message', foo: 'Hello', bar: 'World' %}
|
||||
|
||||
@@ -6,7 +6,7 @@ module Liquid
|
||||
# @liquid_category theme
|
||||
# @liquid_name include
|
||||
# @liquid_summary
|
||||
# Renders a [snippet](/themes/architecture/snippets).
|
||||
# Renders a [snippet](/themes/architecture#snippets).
|
||||
# @liquid_description
|
||||
# Inside the snippet, you can access and alter variables that are [created](/docs/api/liquid/tags/variable-tags) outside of the
|
||||
# snippet.
|
||||
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# @liquid_description
|
||||
# Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||
# [snippets](/themes/architecture/snippets) included in the file.
|
||||
# [snippets](/themes/architecture#snippets) included in the file.
|
||||
#
|
||||
# Similarly, variables that are created with `increment` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `increment` and [`decrement`](/docs/api/liquid/tags/decrement) share
|
||||
|
||||
@@ -6,7 +6,7 @@ module Liquid
|
||||
# @liquid_category theme
|
||||
# @liquid_name render
|
||||
# @liquid_summary
|
||||
# Renders a [snippet](/themes/architecture/snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
||||
# Renders a [snippet](/themes/architecture#snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
||||
# @liquid_description
|
||||
# Inside snippets and app blocks, you can't directly access variables that are [created](/docs/api/liquid/tags/variable-tags) outside
|
||||
# of the snippet or app block. However, you can [specify variables as parameters](/docs/api/liquid/tags/render#render-passing-variables-to-a-snippet)
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
VERSION = "5.8.6"
|
||||
VERSION = "5.8.1"
|
||||
end
|
||||
|
||||
@@ -560,23 +560,12 @@ class StandardFiltersTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_map_with_value_property
|
||||
array = [
|
||||
{ "handle" => "alpha", "value" => "A" },
|
||||
{ "handle" => "beta", "value" => "B" },
|
||||
{ "handle" => "gamma", "value" => "C" }
|
||||
]
|
||||
|
||||
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
|
||||
end
|
||||
|
||||
def test_map_returns_input_with_no_property
|
||||
def test_map_returns_empty_with_no_property
|
||||
foo = [
|
||||
[1],
|
||||
[2],
|
||||
[3],
|
||||
]
|
||||
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
@filters.map(foo, nil)
|
||||
end
|
||||
@@ -1044,23 +1033,6 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result(expected_output, template, { "array" => array })
|
||||
end
|
||||
|
||||
def test_where_with_empty_string_is_a_no_op
|
||||
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||
expected_output = "alpha beta gamma"
|
||||
template = "{{ array | where: '' | join: ' ' }}"
|
||||
|
||||
assert_template_result(expected_output, template, environment)
|
||||
end
|
||||
|
||||
def test_where_with_nil_is_a_no_op
|
||||
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||
template = "{{ array | where: nil | join: ' ' }}"
|
||||
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
assert_template_result("alpha beta gamma", template, environment)
|
||||
end
|
||||
end
|
||||
|
||||
def test_where_with_value
|
||||
array = [
|
||||
{ "handle" => "alpha", "ok" => true },
|
||||
@@ -1296,33 +1268,6 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
|
||||
end
|
||||
|
||||
def test_sum_with_non_string_property
|
||||
input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]
|
||||
|
||||
assert_equal(1, @filters.sum(input, true))
|
||||
assert_equal(0.2, @filters.sum(input, 1.0))
|
||||
assert_equal(-0.3, @filters.sum(input, 1))
|
||||
assert_equal(0.4, @filters.sum(input, (1..5)))
|
||||
assert_equal(0, @filters.sum(input, nil))
|
||||
assert_equal(0, @filters.sum(input, ""))
|
||||
end
|
||||
|
||||
def test_uniq_with_to_liquid_value
|
||||
input = [StringDrop.new("foo"), StringDrop.new("bar"), "foo"]
|
||||
expected = [StringDrop.new("foo"), StringDrop.new("bar")]
|
||||
result = @filters.uniq(input)
|
||||
|
||||
assert_equal(expected, result)
|
||||
end
|
||||
|
||||
def test_uniq_with_to_liquid_value_pick_correct_classes
|
||||
input = ["foo", StringDrop.new("foo"), StringDrop.new("bar")]
|
||||
expected = [String, StringDrop]
|
||||
result = @filters.uniq(input).map(&:class)
|
||||
|
||||
assert_equal(expected, result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_timezone(tz)
|
||||
|
||||
@@ -146,35 +146,6 @@ class BooleanDrop < Liquid::Drop
|
||||
end
|
||||
end
|
||||
|
||||
class StringDrop < Liquid::Drop
|
||||
include Comparable
|
||||
|
||||
def initialize(value)
|
||||
super()
|
||||
@value = value
|
||||
end
|
||||
|
||||
def to_liquid_value
|
||||
@value
|
||||
end
|
||||
|
||||
def to_s
|
||||
@value
|
||||
end
|
||||
|
||||
def to_str
|
||||
@value
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<StringDrop @value=#{@value.inspect}>"
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
to_liquid_value <=> Liquid::Utils.to_liquid_value(other)
|
||||
end
|
||||
end
|
||||
|
||||
class ErrorDrop < Liquid::Drop
|
||||
def standard_error
|
||||
raise Liquid::StandardError, 'standard error'
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class InfixOperatorsUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_addition_operator
|
||||
# Filter syntax
|
||||
filter_template = Liquid::Template.parse("{{ num | plus: 3 }}")
|
||||
# Infix syntax
|
||||
infix_template = Liquid::Template.parse("{{ num + 3 }}")
|
||||
|
||||
assert_equal(filter_template.render("num" => 5), infix_template.render("num" => 5))
|
||||
assert_equal("8", infix_template.render("num" => 5))
|
||||
end
|
||||
|
||||
def test_subtraction_operator
|
||||
# Filter syntax
|
||||
filter_template = Liquid::Template.parse("{{ num | minus: 3 }}")
|
||||
# Infix syntax
|
||||
infix_template = Liquid::Template.parse("{{ num - 3 }}")
|
||||
|
||||
assert_equal(filter_template.render("num" => 5), infix_template.render("num" => 5))
|
||||
assert_equal("2", infix_template.render("num" => 5))
|
||||
end
|
||||
|
||||
def test_multiplication_operator
|
||||
# Filter syntax
|
||||
filter_template = Liquid::Template.parse("{{ num | times: 3 }}")
|
||||
# Infix syntax
|
||||
infix_template = Liquid::Template.parse("{{ num * 3 }}")
|
||||
|
||||
assert_equal(filter_template.render("num" => 5), infix_template.render("num" => 5))
|
||||
assert_equal("15", infix_template.render("num" => 5))
|
||||
end
|
||||
|
||||
def test_division_operator
|
||||
# Filter syntax
|
||||
filter_template = Liquid::Template.parse("{{ num | divided_by: 2 }}")
|
||||
# Infix syntax
|
||||
infix_template = Liquid::Template.parse("{{ num / 2 }}")
|
||||
|
||||
assert_equal(filter_template.render("num" => 10), infix_template.render("num" => 10))
|
||||
assert_equal("5", infix_template.render("num" => 10))
|
||||
end
|
||||
|
||||
def test_comparison_operators
|
||||
# Greater than
|
||||
assert_equal("true", Liquid::Template.parse("{{ 5 > 3 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 3 > 5 }}").render)
|
||||
|
||||
# Greater than or equal
|
||||
assert_equal("true", Liquid::Template.parse("{{ 5 >= 5 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 3 >= 5 }}").render)
|
||||
|
||||
# Equal to
|
||||
assert_equal("true", Liquid::Template.parse("{{ 5 == 5 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 3 == 5 }}").render)
|
||||
|
||||
# Less than or equal
|
||||
assert_equal("true", Liquid::Template.parse("{{ 5 <= 5 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 6 <= 5 }}").render)
|
||||
|
||||
# Less than
|
||||
assert_equal("true", Liquid::Template.parse("{{ 3 < 5 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 5 < 3 }}").render)
|
||||
end
|
||||
|
||||
def test_logical_operators
|
||||
# AND operator
|
||||
assert_equal("true", Liquid::Template.parse("{{ true && true }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ true && false }}").render)
|
||||
|
||||
# OR operator
|
||||
assert_equal("true", Liquid::Template.parse("{{ true || false }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ false || false }}").render)
|
||||
end
|
||||
|
||||
def test_xor_operator
|
||||
assert_equal("true", Liquid::Template.parse("{{ true ^ false }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ true ^ true }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ false ^ false }}").render)
|
||||
end
|
||||
|
||||
def test_operator_precedence
|
||||
# (10 - 2) * 3 = 24
|
||||
assert_equal("24", Liquid::Template.parse("{{ (10 - 2) * 3 }}").render)
|
||||
# 10 - (2 * 3) = 4
|
||||
assert_equal("4", Liquid::Template.parse("{{ 10 - (2 * 3) }}").render)
|
||||
# Without parentheses, multiplication has higher precedence
|
||||
# 10 - 2 * 3 = 10 - 6 = 4
|
||||
assert_equal("4", Liquid::Template.parse("{{ 10 - 2 * 3 }}").render)
|
||||
end
|
||||
|
||||
def test_complex_expressions
|
||||
# Multiple operations
|
||||
assert_equal("9", Liquid::Template.parse("{{ 3 + 2 * 3 }}").render)
|
||||
assert_equal("15", Liquid::Template.parse("{{ (3 + 2) * 3 }}").render)
|
||||
|
||||
# Mixed arithmetic and comparison
|
||||
assert_equal("true", Liquid::Template.parse("{{ 3 + 2 > 4 }}").render)
|
||||
assert_equal("false", Liquid::Template.parse("{{ 3 + 2 < 4 }}").render)
|
||||
|
||||
# Mixed arithmetic and logical
|
||||
assert_equal("true", Liquid::Template.parse("{{ 3 + 2 > 4 && 10 / 2 == 5 }}").render)
|
||||
end
|
||||
|
||||
def test_combined_operations
|
||||
# In the proposed example
|
||||
infix_template = Liquid::Template.parse("{% assign media_count = media_count - variant_images.size + 1 %}")
|
||||
filter_template = Liquid::Template.parse("{% assign media_count = media_count | minus: variant_images.size | plus: 1 %}")
|
||||
|
||||
# Check that both templates have the same effect
|
||||
context1 = Context.new("media_count" => 10, "variant_images" => [1, 2, 3])
|
||||
context2 = Context.new("media_count" => 10, "variant_images" => [1, 2, 3])
|
||||
|
||||
infix_template.render(context1)
|
||||
filter_template.render(context2)
|
||||
|
||||
assert_equal(context1["media_count"], context2["media_count"])
|
||||
assert_equal(8, context1["media_count"])
|
||||
end
|
||||
|
||||
def test_with_variables
|
||||
template = Liquid::Template.parse("{{ a + b * c }}")
|
||||
assert_equal("11", template.render("a" => 5, "b" => 2, "c" => 3))
|
||||
|
||||
template = Liquid::Template.parse("{{ (a + b) * c }}")
|
||||
assert_equal("21", template.render("a" => 5, "b" => 2, "c" => 3))
|
||||
end
|
||||
|
||||
def test_chained_comparisons
|
||||
template = Liquid::Template.parse("{{ a < b && b < c }}")
|
||||
assert_equal("true", template.render("a" => 1, "b" => 5, "c" => 10))
|
||||
assert_equal("false", template.render("a" => 1, "b" => 15, "c" => 10))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user