mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Compare commits
33 Commits
range-to_l
...
fix-hash-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7e7285fcb | ||
|
|
ffb0ace303 | ||
|
|
ad00998ef8 | ||
|
|
869dbc7ebf | ||
|
|
fae3a2de7b | ||
|
|
f27bd619b9 | ||
|
|
a9b84b7806 | ||
|
|
6cc2c567c5 | ||
|
|
812e3c51b9 | ||
|
|
9dd0801f5c | ||
|
|
b146b49f46 | ||
|
|
86944fe7b7 | ||
|
|
a549d289d7 | ||
|
|
b2feeacbce | ||
|
|
143ba39a08 | ||
|
|
43e59796f6 | ||
|
|
bb3624b799 | ||
|
|
64fca66ef5 | ||
|
|
e9d7486758 | ||
|
|
2bb98c1431 | ||
|
|
95d5c24bfc | ||
|
|
b7ee1a2176 | ||
|
|
0eca61a977 | ||
|
|
9bfd04da2d | ||
|
|
302185a7fc | ||
|
|
50c85afc35 | ||
|
|
5876dff326 | ||
|
|
f25185631d | ||
|
|
283f1bad18 | ||
|
|
e1d40c7d89 | ||
|
|
19c6eb426a | ||
|
|
f87b06095d | ||
|
|
b81d54e789 |
@@ -1,9 +1,9 @@
|
||||
language: ruby
|
||||
|
||||
rvm:
|
||||
- 2.0
|
||||
- 2.1
|
||||
- 2.2
|
||||
- 2.3.3
|
||||
- ruby-head
|
||||
- jruby-head
|
||||
# - rbx-2
|
||||
|
||||
2
Gemfile
2
Gemfile
@@ -9,6 +9,6 @@ group :test do
|
||||
gem 'rubocop', '0.34.2'
|
||||
|
||||
platform :mri do
|
||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '2570693d8d03faa0df9160ec74348a7149436df3'
|
||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'bd53db95de3d44d631e7c5a267c3d934e66107dd'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
# Liquid Change Log
|
||||
|
||||
## 4.0.0 / not yet released / branch "master"
|
||||
## 4.0.0 / 2016-12-14 / branch "4-0-stable"
|
||||
|
||||
### Changed
|
||||
* Render an opaque internal error by default for non-Liquid::Error (#835) [Dylan Thacker-Smith]
|
||||
* Ruby 2.0 support dropped (#832) [Dylan Thacker-Smith]
|
||||
* Add to_number Drop method to allow custom drops to work with number filters (#731)
|
||||
* Add strict_variables and strict_filters options to detect undefined references (#691)
|
||||
* Improve loop performance (#681) [Florian Weingarten]
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
machine:
|
||||
ruby:
|
||||
version: ruby-2.0
|
||||
version: ruby-2.1
|
||||
|
||||
@@ -24,6 +24,7 @@ module Liquid
|
||||
ArgumentSeparator = ','.freeze
|
||||
FilterArgumentSeparator = ':'.freeze
|
||||
VariableAttributeSeparator = '.'.freeze
|
||||
WhitespaceControl = '-'.freeze
|
||||
TagStart = /\{\%/
|
||||
TagEnd = /\%\}/
|
||||
VariableSignature = /\(?[\w\-\.\[\]]\)?/
|
||||
@@ -34,7 +35,7 @@ module Liquid
|
||||
QuotedString = /"[^"]*"|'[^']*'/
|
||||
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
|
||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
|
||||
AnyStartingTag = /\{\{|\{\%/
|
||||
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
|
||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
|
||||
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module Liquid
|
||||
class BlockBody
|
||||
FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||
ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/om
|
||||
FullToken = /\A#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
|
||||
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
|
||||
TAGSTART = "{%".freeze
|
||||
VARSTART = "{{".freeze
|
||||
|
||||
@@ -18,6 +18,7 @@ module Liquid
|
||||
unless token.empty?
|
||||
case
|
||||
when token.start_with?(TAGSTART)
|
||||
whitespace_handler(token, parse_context)
|
||||
if token =~ FullToken
|
||||
tag_name = $1
|
||||
markup = $2
|
||||
@@ -35,9 +36,14 @@ module Liquid
|
||||
raise_missing_tag_terminator(token, parse_context)
|
||||
end
|
||||
when token.start_with?(VARSTART)
|
||||
whitespace_handler(token, parse_context)
|
||||
@nodelist << create_variable(token, parse_context)
|
||||
@blank = false
|
||||
else
|
||||
if parse_context.trim_whitespace
|
||||
token.lstrip!
|
||||
end
|
||||
parse_context.trim_whitespace = false
|
||||
@nodelist << token
|
||||
@blank &&= !!(token =~ /\A\s*\z/)
|
||||
end
|
||||
@@ -48,6 +54,16 @@ module Liquid
|
||||
yield nil, nil
|
||||
end
|
||||
|
||||
def whitespace_handler(token, parse_context)
|
||||
if token[2] == WhitespaceControl
|
||||
previous_token = @nodelist.last
|
||||
if previous_token.is_a? String
|
||||
previous_token.rstrip!
|
||||
end
|
||||
end
|
||||
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
|
||||
end
|
||||
|
||||
def blank?
|
||||
@blank
|
||||
end
|
||||
@@ -77,10 +93,10 @@ module Liquid
|
||||
rescue MemoryError => e
|
||||
raise e
|
||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||
context.handle_error(e, token.line_number)
|
||||
context.handle_error(e, token.line_number, token.raw)
|
||||
output << nil
|
||||
rescue ::StandardError => e
|
||||
output << context.handle_error(e, token.line_number)
|
||||
output << context.handle_error(e, token.line_number, token.raw)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ module Liquid
|
||||
elsif left.respond_to?(operation) && right.respond_to?(operation)
|
||||
begin
|
||||
left.send(operation, right)
|
||||
rescue ::ArgumentError => e
|
||||
rescue ::ArgumentError, TypeError => e
|
||||
raise Liquid::ArgumentError.new(e.message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,7 +13,7 @@ module Liquid
|
||||
# context['bob'] #=> nil class Context
|
||||
class Context
|
||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
||||
attr_accessor :exception_handler, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
||||
attr_accessor :exception_renderer, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
||||
|
||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
||||
@environments = [environments].flatten
|
||||
@@ -27,8 +27,9 @@ module Liquid
|
||||
|
||||
@this_stack_used = false
|
||||
|
||||
self.exception_renderer = Template.default_exception_renderer
|
||||
if rethrow_errors
|
||||
self.exception_handler = ->(e) { raise }
|
||||
self.exception_renderer = ->(e) { raise }
|
||||
end
|
||||
|
||||
@interrupts = []
|
||||
@@ -73,31 +74,12 @@ module Liquid
|
||||
@interrupts.pop
|
||||
end
|
||||
|
||||
def handle_error(e, line_number = nil)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.template_name ||= template_name
|
||||
e.line_number ||= line_number
|
||||
end
|
||||
|
||||
output = nil
|
||||
|
||||
if exception_handler
|
||||
result = exception_handler.call(e)
|
||||
case result
|
||||
when Exception
|
||||
e = result
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.template_name ||= template_name
|
||||
e.line_number ||= line_number
|
||||
end
|
||||
when String
|
||||
output = result
|
||||
else
|
||||
raise if result
|
||||
end
|
||||
end
|
||||
def handle_error(e, line_number = nil, raw_token = nil)
|
||||
e = internal_error unless e.is_a?(Liquid::Error)
|
||||
e.template_name ||= template_name
|
||||
e.line_number ||= line_number
|
||||
errors.push(e)
|
||||
output || Liquid::Error.render(e)
|
||||
exception_renderer.call(e).to_s
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
@@ -221,6 +203,13 @@ module Liquid
|
||||
|
||||
private
|
||||
|
||||
def internal_error
|
||||
# raise and catch to set backtrace and cause on exception
|
||||
raise Liquid::InternalError, 'internal'
|
||||
rescue Liquid::InternalError => exc
|
||||
exc
|
||||
end
|
||||
|
||||
def squash_instance_assigns_with_environments
|
||||
@scopes.last.each_key do |k|
|
||||
@environments.each do |env|
|
||||
|
||||
@@ -17,14 +17,6 @@ module Liquid
|
||||
str
|
||||
end
|
||||
|
||||
def self.render(e)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.to_s
|
||||
else
|
||||
"Liquid error: #{e}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def message_prefix
|
||||
@@ -60,4 +52,5 @@ module Liquid
|
||||
UndefinedDropMethod = Class.new(Error)
|
||||
UndefinedFilter = Class.new(Error)
|
||||
MethodOverrideError = Class.new(Error)
|
||||
InternalError = Class.new(Error)
|
||||
end
|
||||
|
||||
@@ -8,8 +8,8 @@ module Liquid
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_path)
|
||||
# liquid = Liquid::Template.parse(template)
|
||||
# Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_path)
|
||||
# liquid = Liquid::Template.parse(template)
|
||||
#
|
||||
# This will parse the template with a LocalFileSystem implementation rooted at 'template_path'.
|
||||
class BlankFileSystem
|
||||
@@ -26,10 +26,10 @@ module Liquid
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# file_system = Liquid::LocalFileSystem.new("/some/path")
|
||||
# file_system = Liquid::LocalFileSystem.new("/some/path")
|
||||
#
|
||||
# file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid"
|
||||
# file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid"
|
||||
# file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid"
|
||||
# file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid"
|
||||
#
|
||||
# Optionally in the second argument you can specify a custom pattern for template filenames.
|
||||
# The Kernel::sprintf format specification is used.
|
||||
@@ -37,9 +37,9 @@ module Liquid
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
|
||||
# file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
|
||||
#
|
||||
# file_system.full_path("index") # => "/some/path/index.html"
|
||||
# file_system.full_path("index") # => "/some/path/index.html"
|
||||
#
|
||||
class LocalFileSystem
|
||||
attr_accessor :root
|
||||
|
||||
@@ -22,3 +22,5 @@
|
||||
tag_never_closed: "'%{block_name}' tag was never closed"
|
||||
meta_syntax_error: "Liquid syntax error: #{e.message}"
|
||||
table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
|
||||
argument:
|
||||
include: "Argument error in tag 'include' - Illegal template name"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module Liquid
|
||||
class ParseContext
|
||||
attr_accessor :locale, :line_number
|
||||
attr_accessor :locale, :line_number, :trim_whitespace
|
||||
attr_reader :partial, :warnings, :error_mode
|
||||
|
||||
def initialize(options = {})
|
||||
|
||||
@@ -65,9 +65,10 @@ module Liquid
|
||||
return if input.nil?
|
||||
input_str = input.to_s
|
||||
length = Utils.to_integer(length)
|
||||
l = length - truncate_string.length
|
||||
truncate_string_str = truncate_string.to_s
|
||||
l = length - truncate_string_str.length
|
||||
l = 0 if l < 0
|
||||
input_str.length > length ? input_str[0...l] + truncate_string : input_str
|
||||
input_str.length > length ? input_str[0...l] + truncate_string_str : input_str
|
||||
end
|
||||
|
||||
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
||||
@@ -76,7 +77,7 @@ module Liquid
|
||||
words = Utils.to_integer(words)
|
||||
l = words - 1
|
||||
l = 0 if l < 0
|
||||
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
|
||||
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string.to_s : input
|
||||
end
|
||||
|
||||
# Split input string into an array of substrings separated by given pattern.
|
||||
@@ -85,7 +86,7 @@ module Liquid
|
||||
# <div class="summary">{{ post | split '//' | first }}</div>
|
||||
#
|
||||
def split(input, pattern)
|
||||
input.to_s.split(pattern)
|
||||
input.to_s.split(pattern.to_s)
|
||||
end
|
||||
|
||||
def strip(input)
|
||||
@@ -124,7 +125,15 @@ module Liquid
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||
ary.sort { |a, b| a[property] <=> b[property] }
|
||||
ary.sort do |a, b|
|
||||
a = a[property]
|
||||
b = b[property]
|
||||
if a && b
|
||||
a <=> b
|
||||
else
|
||||
a ? -1 : 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def self.global_filter(filter)
|
||||
@@strainer_class_cache.clear
|
||||
@@global_strainer.add_filter(filter)
|
||||
end
|
||||
|
||||
|
||||
@@ -42,8 +42,9 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
partial = load_cached_partial(template_name, context)
|
||||
raise ArgumentError.new(options[:locale].t("errors.argument.include")) unless template_name
|
||||
|
||||
partial = load_cached_partial(template_name, context)
|
||||
context_variable_name = template_name.split('/'.freeze).last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
|
||||
@@ -69,6 +69,11 @@ module Liquid
|
||||
# :error raises an error when tainted output is used
|
||||
attr_writer :taint_mode
|
||||
|
||||
attr_accessor :default_exception_renderer
|
||||
Template.default_exception_renderer = lambda do |exception|
|
||||
exception
|
||||
end
|
||||
|
||||
def file_system
|
||||
@@file_system
|
||||
end
|
||||
@@ -167,7 +172,7 @@ module Liquid
|
||||
c = args.shift
|
||||
|
||||
if @rethrow_errors
|
||||
c.exception_handler = ->(e) { raise }
|
||||
c.exception_renderer = ->(e) { raise }
|
||||
end
|
||||
|
||||
c
|
||||
@@ -241,7 +246,7 @@ module Liquid
|
||||
def apply_options_to_context(context, options)
|
||||
context.add_filters(options[:filters]) if options[:filters]
|
||||
context.global_filter = options[:global_filter] if options[:global_filter]
|
||||
context.exception_handler = options[:exception_handler] if options[:exception_handler]
|
||||
context.exception_renderer = options[:exception_renderer] if options[:exception_renderer]
|
||||
context.strict_variables = options[:strict_variables] if options[:strict_variables]
|
||||
context.strict_filters = options[:strict_filters] if options[:strict_filters]
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# encoding: utf-8
|
||||
module Liquid
|
||||
VERSION = "4.0.0.rc2"
|
||||
VERSION = "4.0.0"
|
||||
end
|
||||
|
||||
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
|
||||
s.license = "MIT"
|
||||
# s.description = "A secure, non-evaling end user template engine with aesthetic markup."
|
||||
|
||||
s.required_ruby_version = ">= 2.1.0"
|
||||
s.required_rubygems_version = ">= 1.3.7"
|
||||
|
||||
s.test_files = Dir.glob("{test}/**/*")
|
||||
@@ -24,6 +25,6 @@ Gem::Specification.new do |s|
|
||||
|
||||
s.require_path = "lib"
|
||||
|
||||
s.add_development_dependency 'rake'
|
||||
s.add_development_dependency 'rake', '~> 11.3'
|
||||
s.add_development_dependency 'minitest'
|
||||
end
|
||||
|
||||
@@ -97,6 +97,22 @@ class ErrorHandlingTest < Minitest::Test
|
||||
assert_match(/Liquid syntax error \(line 4\)/, err.message)
|
||||
end
|
||||
|
||||
def test_with_line_numbers_adds_numbers_to_parser_errors_with_whitespace_trim
|
||||
err = assert_raises(SyntaxError) do
|
||||
Liquid::Template.parse(%q(
|
||||
foobar
|
||||
|
||||
{%- "cat" | foobar -%}
|
||||
|
||||
bla
|
||||
),
|
||||
line_numbers: true
|
||||
)
|
||||
end
|
||||
|
||||
assert_match(/Liquid syntax error \(line 4\)/, err.message)
|
||||
end
|
||||
|
||||
def test_parsing_warn_with_line_numbers_adds_numbers_to_lexer_errors
|
||||
template = Liquid::Template.parse('
|
||||
foobar
|
||||
@@ -186,22 +202,40 @@ class ErrorHandlingTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_exception_handler_with_string_result
|
||||
template = Liquid::Template.parse('This is an argument error: {{ errors.argument_error }}')
|
||||
output = template.render({ 'errors' => ErrorDrop.new }, exception_handler: ->(e) { '' })
|
||||
assert_equal 'This is an argument error: ', output
|
||||
assert_equal [ArgumentError], template.errors.map(&:class)
|
||||
end
|
||||
|
||||
class InternalError < Liquid::Error
|
||||
end
|
||||
|
||||
def test_exception_handler_with_exception_result
|
||||
def test_default_exception_renderer_with_internal_error
|
||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
||||
handler = ->(e) { e.is_a?(Liquid::Error) ? e : InternalError.new('internal') }
|
||||
output = template.render({ 'errors' => ErrorDrop.new }, exception_handler: handler)
|
||||
|
||||
output = template.render({ 'errors' => ErrorDrop.new })
|
||||
|
||||
assert_equal 'This is a runtime error: Liquid error (line 1): internal', output
|
||||
assert_equal [InternalError], template.errors.map(&:class)
|
||||
assert_equal [Liquid::InternalError], template.errors.map(&:class)
|
||||
end
|
||||
|
||||
def test_setting_default_exception_renderer
|
||||
old_exception_renderer = Liquid::Template.default_exception_renderer
|
||||
exceptions = []
|
||||
Liquid::Template.default_exception_renderer = ->(e) { exceptions << e; '' }
|
||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.argument_error }}')
|
||||
|
||||
output = template.render({ 'errors' => ErrorDrop.new })
|
||||
|
||||
assert_equal 'This is a runtime error: ', output
|
||||
assert_equal [Liquid::ArgumentError], template.errors.map(&:class)
|
||||
ensure
|
||||
Liquid::Template.default_exception_renderer = old_exception_renderer if old_exception_renderer
|
||||
end
|
||||
|
||||
def test_exception_renderer_exposing_non_liquid_error
|
||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
||||
exceptions = []
|
||||
handler = ->(e) { exceptions << e; e.cause }
|
||||
|
||||
output = template.render({ 'errors' => ErrorDrop.new }, exception_renderer: handler)
|
||||
|
||||
assert_equal 'This is a runtime error: runtime error', output
|
||||
assert_equal [Liquid::InternalError], exceptions.map(&:class)
|
||||
assert_equal exceptions, template.errors
|
||||
assert_equal '#<RuntimeError: runtime error>', exceptions.first.cause.inspect
|
||||
end
|
||||
|
||||
class TestFileSystem
|
||||
|
||||
@@ -115,15 +115,15 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal '...', @filters.truncate('1234567890', 0)
|
||||
assert_equal '1234567890', @filters.truncate('1234567890')
|
||||
assert_equal "测试...", @filters.truncate("测试测试测试测试", 5)
|
||||
assert_equal '12341', @filters.truncate("1234567890", 5, 1)
|
||||
end
|
||||
|
||||
def test_split
|
||||
assert_equal ['12', '34'], @filters.split('12~34', '~')
|
||||
assert_equal ['A? ', ' ,Z'], @filters.split('A? ~ ~ ~ ,Z', '~ ~ ~')
|
||||
assert_equal ['A?Z'], @filters.split('A?Z', '~')
|
||||
# Regexp works although Liquid does not support.
|
||||
assert_equal ['A', 'Z'], @filters.split('AxZ', /x/)
|
||||
assert_equal [], @filters.split(nil, ' ')
|
||||
assert_equal ['A', 'Z'], @filters.split('A1Z', 1)
|
||||
end
|
||||
|
||||
def test_escape
|
||||
@@ -154,6 +154,7 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal 'one two three', @filters.truncatewords('one two three')
|
||||
assert_equal 'Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13”...', @filters.truncatewords('Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13” x 16” x 10.5” high) with cover.', 15)
|
||||
assert_equal "测试测试测试测试", @filters.truncatewords('测试测试测试测试', 5)
|
||||
assert_equal 'one two1', @filters.truncatewords("one two three", 2, 1)
|
||||
end
|
||||
|
||||
def test_strip_html
|
||||
@@ -176,6 +177,24 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
||||
end
|
||||
|
||||
def test_sort_when_property_is_sometimes_missing_puts_nils_last
|
||||
input = [
|
||||
{ "price" => 4, "handle" => "alpha" },
|
||||
{ "handle" => "beta" },
|
||||
{ "price" => 1, "handle" => "gamma" },
|
||||
{ "handle" => "delta" },
|
||||
{ "price" => 2, "handle" => "epsilon" }
|
||||
]
|
||||
expectation = [
|
||||
{ "price" => 1, "handle" => "gamma" },
|
||||
{ "price" => 2, "handle" => "epsilon" },
|
||||
{ "price" => 4, "handle" => "alpha" },
|
||||
{ "handle" => "delta" },
|
||||
{ "handle" => "beta" }
|
||||
]
|
||||
assert_equal expectation, @filters.sort(input, "price")
|
||||
end
|
||||
|
||||
def test_sort_empty_array
|
||||
assert_equal [], @filters.sort([], "a")
|
||||
end
|
||||
|
||||
@@ -217,6 +217,17 @@ class IncludeTagTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_raise_argument_error_when_template_is_undefined
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
template = Liquid::Template.parse('{% include undefined_variable %}')
|
||||
template.render!
|
||||
end
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
template = Liquid::Template.parse('{% include nil %}')
|
||||
template.render!
|
||||
end
|
||||
end
|
||||
|
||||
def test_including_via_variable_value
|
||||
assert_template_result "from TestFileSystem", "{% assign page = 'pick_a_source' %}{% include page %}"
|
||||
|
||||
|
||||
@@ -215,16 +215,20 @@ class TemplateTest < Minitest::Test
|
||||
assert_equal 'ruby error in drop', e.message
|
||||
end
|
||||
|
||||
def test_exception_handler_doesnt_reraise_if_it_returns_false
|
||||
def test_exception_renderer_that_returns_string
|
||||
exception = nil
|
||||
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; false })
|
||||
handler = ->(e) { exception = e; '<!-- error -->' }
|
||||
|
||||
output = Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: handler)
|
||||
|
||||
assert exception.is_a?(Liquid::ZeroDivisionError)
|
||||
assert_equal '<!-- error -->', output
|
||||
end
|
||||
|
||||
def test_exception_handler_does_reraise_if_it_returns_true
|
||||
def test_exception_renderer_that_raises
|
||||
exception = nil
|
||||
assert_raises(Liquid::ZeroDivisionError) do
|
||||
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; true })
|
||||
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: ->(e) { exception = e; raise })
|
||||
end
|
||||
assert exception.is_a?(Liquid::ZeroDivisionError)
|
||||
end
|
||||
|
||||
525
test/integration/trim_mode_test.rb
Normal file
525
test/integration/trim_mode_test.rb
Normal file
@@ -0,0 +1,525 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TrimModeTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
# Make sure the trim isn't applied to standard output
|
||||
def test_standard_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{ 'John' }}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
John
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_variable_output_with_multiple_blank_lines
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
|
||||
|
||||
{{- 'John' -}}
|
||||
|
||||
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>John</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_tag_output_with_multiple_blank_lines
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
|
||||
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
|
||||
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
# Make sure the trim isn't applied to standard tags
|
||||
def test_standard_tags
|
||||
whitespace = ' '
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if true %}
|
||||
yes
|
||||
{% endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
#{whitespace}
|
||||
yes
|
||||
#{whitespace}
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if false %}
|
||||
no
|
||||
{% endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
#{whitespace}
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
# Make sure the trim isn't too agressive
|
||||
def test_no_trim_output
|
||||
text = '<p>{{- \'John\' -}}</p>'
|
||||
expected = '<p>John</p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
# Make sure the trim isn't too agressive
|
||||
def test_no_trim_tags
|
||||
text = '<p>{%- if true -%}yes{%- endif -%}</p>'
|
||||
expected = '<p>yes</p>'
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = '<p>{%- if false -%}no{%- endif -%}</p>'
|
||||
expected = '<p></p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_single_line_outer_tag
|
||||
text = '<p> {%- if true %} yes {% endif -%} </p>'
|
||||
expected = '<p> yes </p>'
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = '<p> {%- if false %} no {% endif -%} </p>'
|
||||
expected = '<p></p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_single_line_inner_tag
|
||||
text = '<p> {% if true -%} yes {%- endif %} </p>'
|
||||
expected = '<p> yes </p>'
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = '<p> {% if false -%} no {%- endif %} </p>'
|
||||
expected = '<p> </p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_single_line_post_tag
|
||||
text = '<p> {% if true -%} yes {% endif -%} </p>'
|
||||
expected = '<p> yes </p>'
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = '<p> {% if false -%} no {% endif -%} </p>'
|
||||
expected = '<p> </p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_single_line_pre_tag
|
||||
text = '<p> {%- if true %} yes {%- endif %} </p>'
|
||||
expected = '<p> yes </p>'
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = '<p> {%- if false %} no {%- endif %} </p>'
|
||||
expected = '<p> </p>'
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_pre_trim_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' }}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>John
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_pre_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if true %}
|
||||
yes
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if false %}
|
||||
no
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_post_trim_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{ 'John' -}}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
John</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_post_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if true -%}
|
||||
yes
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if false -%}
|
||||
no
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_pre_and_post_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if true %}
|
||||
yes
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if false %}
|
||||
no
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_post_and_pre_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if true -%}
|
||||
yes
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
whitespace = ' '
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{% if false -%}
|
||||
no
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>
|
||||
#{whitespace}
|
||||
</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_trim_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>John</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if false -%}
|
||||
no
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_whitespace_trim_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}},
|
||||
{{- '30' -}}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>John,30</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_whitespace_trim_tags
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{%- if false -%}
|
||||
no
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_complex_trim_output
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
{{- '30' -}}
|
||||
</p>
|
||||
<b>
|
||||
{{ 'John' -}}
|
||||
{{- '30' }}
|
||||
</b>
|
||||
<i>
|
||||
{{- 'John' }}
|
||||
{{ '30' -}}
|
||||
</i>
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
<p>John30</p>
|
||||
<b>
|
||||
John30
|
||||
</b>
|
||||
<i>John
|
||||
30</i>
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_complex_trim
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
{%- if true -%}
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div><p>John</p></div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
|
||||
def test_raw_output
|
||||
whitespace = ' '
|
||||
text = <<-END_TEMPLATE
|
||||
<div>
|
||||
{% raw %}
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
{% endraw %}
|
||||
</div>
|
||||
END_TEMPLATE
|
||||
expected = <<-END_EXPECTED
|
||||
<div>
|
||||
#{whitespace}
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
#{whitespace}
|
||||
</div>
|
||||
END_EXPECTED
|
||||
assert_template_result(expected, text)
|
||||
end
|
||||
end # TrimModeTest
|
||||
@@ -57,6 +57,11 @@ class ConditionUnitTest < Minitest::Test
|
||||
assert_evaluates_argument_error 1, '~~', 0
|
||||
end
|
||||
|
||||
def test_comparing_hash_and_integer
|
||||
assert_evaluates_argument_error({a: 1}, '>', 1)
|
||||
assert_evaluates_argument_error(1, '>', {a: 1})
|
||||
end
|
||||
|
||||
def test_comparation_of_int_and_str
|
||||
assert_evaluates_argument_error '1', '>', 0
|
||||
assert_evaluates_argument_error '1', '<', 0
|
||||
|
||||
@@ -133,4 +133,16 @@ class StrainerUnitTest < Minitest::Test
|
||||
strainer.class.add_filter(PublicMethodOverrideFilter)
|
||||
assert strainer.class.filter_methods.include?('public_filter')
|
||||
end
|
||||
|
||||
module LateAddedFilter
|
||||
def late_added_filter(input)
|
||||
"filtered"
|
||||
end
|
||||
end
|
||||
|
||||
def test_global_filter_clears_cache
|
||||
assert_equal 'input', Strainer.create(nil).invoke('late_added_filter', 'input')
|
||||
Strainer.global_filter(LateAddedFilter)
|
||||
assert_equal 'filtered', Strainer.create(nil).invoke('late_added_filter', 'input')
|
||||
end
|
||||
end # StrainerTest
|
||||
|
||||
Reference in New Issue
Block a user