mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
FiltersTest
- added test that asserts nonexistent filters are ignored Liquid - Bill's mind blowing liquid patch to support filter separators (|) in quoted strings (svn r7516). - This is a consolidation effort based on newrelic's liquid fork commit 88a5b891d009054d56b994c9448725c74e2b1e13
This commit is contained in:
+1
-1
@@ -38,7 +38,7 @@ module Liquid
|
|||||||
StrictQuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
|
StrictQuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
|
||||||
FirstFilterArgument = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
|
FirstFilterArgument = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||||
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
|
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
|
||||||
SpacelessFilter = /#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
|
SpacelessFilter = /^(?:'[^']+'|"[^"]+"|[^'"])*#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
|
||||||
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
|
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
|
||||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/
|
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/
|
||||||
AnyStartingTag = /\{\{|\{\%/
|
AnyStartingTag = /\{\{|\{\%/
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class FiltersTest < Test::Unit::TestCase
|
|||||||
def test_local_filter
|
def test_local_filter
|
||||||
@context['var'] = 1000
|
@context['var'] = 1000
|
||||||
@context.add_filters(MoneyFilter)
|
@context.add_filters(MoneyFilter)
|
||||||
|
|
||||||
assert_equal ' 1000$ ', Variable.new("var | money").render(@context)
|
assert_equal ' 1000$ ', Variable.new("var | money").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -39,17 +40,20 @@ class FiltersTest < Test::Unit::TestCase
|
|||||||
@context['var'] = 1000
|
@context['var'] = 1000
|
||||||
@context.add_filters(MoneyFilter)
|
@context.add_filters(MoneyFilter)
|
||||||
@context.add_filters(CanadianMoneyFilter)
|
@context.add_filters(CanadianMoneyFilter)
|
||||||
|
|
||||||
assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context)
|
assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_size
|
def test_size
|
||||||
@context['var'] = 'abcd'
|
@context['var'] = 'abcd'
|
||||||
@context.add_filters(MoneyFilter)
|
@context.add_filters(MoneyFilter)
|
||||||
|
|
||||||
assert_equal 4, Variable.new("var | size").render(@context)
|
assert_equal 4, Variable.new("var | size").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_join
|
def test_join
|
||||||
@context['var'] = [1,2,3,4]
|
@context['var'] = [1,2,3,4]
|
||||||
|
|
||||||
assert_equal "1 2 3 4", Variable.new("var | join").render(@context)
|
assert_equal "1 2 3 4", Variable.new("var | join").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -58,22 +62,30 @@ class FiltersTest < Test::Unit::TestCase
|
|||||||
@context['numbers'] = [2,1,4,3]
|
@context['numbers'] = [2,1,4,3]
|
||||||
@context['words'] = ['expected', 'as', 'alphabetic']
|
@context['words'] = ['expected', 'as', 'alphabetic']
|
||||||
@context['arrays'] = [['flattened'], ['are']]
|
@context['arrays'] = [['flattened'], ['are']]
|
||||||
|
|
||||||
assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
|
assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
|
||||||
assert_equal ['alphabetic', 'as', 'expected'],
|
assert_equal ['alphabetic', 'as', 'expected'], Variable.new("words | sort").render(@context)
|
||||||
Variable.new("words | sort").render(@context)
|
|
||||||
assert_equal [3], Variable.new("value | sort").render(@context)
|
assert_equal [3], Variable.new("value | sort").render(@context)
|
||||||
assert_equal ['are', 'flattened'], Variable.new("arrays | sort").render(@context)
|
assert_equal ['are', 'flattened'], Variable.new("arrays | sort").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strip_html
|
def test_strip_html
|
||||||
@context['var'] = "<b>bla blub</a>"
|
@context['var'] = "<b>bla blub</a>"
|
||||||
|
|
||||||
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
|
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_capitalize
|
def test_capitalize
|
||||||
@context['var'] = "blub"
|
@context['var'] = "blub"
|
||||||
|
|
||||||
assert_equal "Blub", Variable.new("var | capitalize").render(@context)
|
assert_equal "Blub", Variable.new("var | capitalize").render(@context)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_nonexistent_filter_is_ignored
|
||||||
|
@context['var'] = 1000
|
||||||
|
|
||||||
|
assert_equal 1000, Variable.new("var | xyzzy").render(@context)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FiltersInTemplate < Test::Unit::TestCase
|
class FiltersInTemplate < Test::Unit::TestCase
|
||||||
|
|||||||
Reference in New Issue
Block a user