From edf7b5577b5aa60567335dff1e6bc1d2568eea68 Mon Sep 17 00:00:00 2001 From: James MacAulay Date: Wed, 15 Oct 2008 15:16:29 -0400 Subject: [PATCH] filtered variables for assign, case, and cycle --- lib/liquid/tags/assign.rb | 2 +- lib/liquid/tags/case.rb | 4 ++-- lib/liquid/tags/cycle.rb | 6 +++--- test/assign_test.rb | 11 +++++++++++ test/if_else_test.rb | 12 ++++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 test/assign_test.rb diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index a68cf30d..7b48b84f 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -9,7 +9,7 @@ module Liquid # {{ monkey }} # class Assign < Tag - Syntax = /(#{VariableSignature}+)\s*=\s*(#{QuotedFragment}+)/ + Syntax = /(#{VariableSignature}+)\s*=\s*(#{Expression}+)/ def initialize(tag_name, markup, tokens) if markup =~ Syntax diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index 0733c51a..f6f24d4d 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -1,7 +1,7 @@ module Liquid class Case < Block - Syntax = /(#{QuotedFragment})/ - WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/ + Syntax = /(#{Expression})/ + WhenSyntax = /(#{Expression})(?:(?:\s+or\s+|\s*\,\s*)(#{Expression}.*))?/ def initialize(tag_name, markup, tokens) @blocks = [] diff --git a/lib/liquid/tags/cycle.rb b/lib/liquid/tags/cycle.rb index a34f29af..71965ca2 100644 --- a/lib/liquid/tags/cycle.rb +++ b/lib/liquid/tags/cycle.rb @@ -13,8 +13,8 @@ module Liquid #
Item five
# class Cycle < Tag - SimpleSyntax = /#{QuotedFragment}/ - NamedSyntax = /(#{QuotedFragment})\s*\:\s*(.*)/ + SimpleSyntax = /#{Expression}/ + NamedSyntax = /(#{Expression})\s*\:\s*(.*)/ def initialize(tag_name, markup, tokens) case markup @@ -49,7 +49,7 @@ module Liquid def variables_from_string(markup) markup.split(',').collect do |var| - var =~ /\s*(#{QuotedFragment})\s*/ + var =~ /\s*(#{Expression})\s*/ $1 ? $1 : nil end.compact end diff --git a/test/assign_test.rb b/test/assign_test.rb new file mode 100644 index 00000000..15d00e53 --- /dev/null +++ b/test/assign_test.rb @@ -0,0 +1,11 @@ +require File.dirname(__FILE__) + '/helper' + +class IfElseTest < Test::Unit::TestCase + include Liquid + + def test_with_filtered_expressions + assert_template_result('foo','{% assign foo = values|sort|last %}{{ foo }}', 'values' => %w{foo bar baz}) + assert_template_result('foo','{% assign sorted = values|sort %}{{ sorted | last }}', 'values' => %w{foo bar baz}) + end + +end \ No newline at end of file diff --git a/test/if_else_test.rb b/test/if_else_test.rb index 538c7850..8e52d00a 100644 --- a/test/if_else_test.rb +++ b/test/if_else_test.rb @@ -117,6 +117,18 @@ class IfElseTest < Test::Unit::TestCase assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--" == "FOO--" %}yes{% endif %}') assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--"|downcase == "foo--" %}yes{% endif %}') assert_template_result('yes','{% if "foo--" == "FOO BAR"|truncatewords:1,"--"|downcase %}yes{% endif %}') + # array transformation, to make sure we aren't converting arrays to strings somewhere along the way: + assert_template_result('yes','{% if values|sort == sorted %}yes{% endif %}', 'values' => %w{foo bar baz}, 'sorted' => %w{bar baz foo}) + end + + def test_allow_no_spaces_in_filtered_expressions + assert_template_result('','{% if "foo--" == "FOO BAR" |truncatewords:1,"--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"| truncatewords:1,"--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords :1,"--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords: 1,"--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1 ,"--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1, "--"|downcase %}yes{% endif %}') + assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1,"--" |downcase %}yes{% endif %}') end def test_syntax_error_no_variable