From b8d6d82e3df27b0161de28f9c70635a22eb2748f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 28 Nov 2018 20:42:54 -0700 Subject: [PATCH 1/3] refactor: Tweak macros --- tests/conformance_ruby/assign_test.rs | 2 -- tests/conformance_ruby/blank_test.rs | 2 -- tests/conformance_ruby/capture_test.rs | 2 -- tests/conformance_ruby/parsing_quirks_test.rs | 2 -- tests/conformance_ruby/tags/break_tag_test.rs | 2 -- .../tags/continue_tag_test.rs | 2 -- tests/conformance_ruby/tags/for_tag_test.rs | 2 -- .../conformance_ruby/tags/if_else_tag_test.rs | 2 -- .../tags/increment_tag_test.rs | 2 -- tests/conformance_ruby/tags/raw_tag_test.rs | 2 -- tests/conformance_ruby/tags/table_row_test.rs | 2 -- tests/conformance_ruby/template_test.rs | 2 -- tests/conformance_ruby/trim_mode_test.rs | 2 -- tests/conformance_ruby/variable_test.rs | 2 -- tests/test_helper.rs | 32 +++++++++---------- 15 files changed, 16 insertions(+), 44 deletions(-) diff --git a/tests/conformance_ruby/assign_test.rs b/tests/conformance_ruby/assign_test.rs index 76bb9909a..c233c2a7c 100644 --- a/tests/conformance_ruby/assign_test.rs +++ b/tests/conformance_ruby/assign_test.rs @@ -1,7 +1,5 @@ use liquid; -use test_helper::*; - #[test] fn test_assign_with_hyphen_in_variable_name() { let template_source = r#" diff --git a/tests/conformance_ruby/blank_test.rs b/tests/conformance_ruby/blank_test.rs index 723a6e71a..0e07899e7 100644 --- a/tests/conformance_ruby/blank_test.rs +++ b/tests/conformance_ruby/blank_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - const N: usize = 10; fn wrap_in_for>(body: S) -> String { diff --git a/tests/conformance_ruby/capture_test.rs b/tests/conformance_ruby/capture_test.rs index b5908ba59..14a75f5d8 100644 --- a/tests/conformance_ruby/capture_test.rs +++ b/tests/conformance_ruby/capture_test.rs @@ -1,7 +1,5 @@ use liquid; -use test_helper::*; - #[test] #[ignore] fn test_captures_block_content_in_variable() { diff --git a/tests/conformance_ruby/parsing_quirks_test.rs b/tests/conformance_ruby/parsing_quirks_test.rs index 3804d0a84..f03f56990 100644 --- a/tests/conformance_ruby/parsing_quirks_test.rs +++ b/tests/conformance_ruby/parsing_quirks_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] fn test_parsing_css() { let text = " div { font-weight: bold; } "; diff --git a/tests/conformance_ruby/tags/break_tag_test.rs b/tests/conformance_ruby/tags/break_tag_test.rs index f725c6d6d..9c24beed8 100644 --- a/tests/conformance_ruby/tags/break_tag_test.rs +++ b/tests/conformance_ruby/tags/break_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - // tests that no weird errors are raised if break is called outside of a // block #[test] diff --git a/tests/conformance_ruby/tags/continue_tag_test.rs b/tests/conformance_ruby/tags/continue_tag_test.rs index ad7aff888..e304f6d10 100644 --- a/tests/conformance_ruby/tags/continue_tag_test.rs +++ b/tests/conformance_ruby/tags/continue_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - // tests that no weird errors are raised if continue is called outside of a // block #[test] diff --git a/tests/conformance_ruby/tags/for_tag_test.rs b/tests/conformance_ruby/tags/for_tag_test.rs index 68c33fc59..4917af865 100644 --- a/tests/conformance_ruby/tags/for_tag_test.rs +++ b/tests/conformance_ruby/tags/for_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] fn test_for() { assert_template_result!( diff --git a/tests/conformance_ruby/tags/if_else_tag_test.rs b/tests/conformance_ruby/tags/if_else_tag_test.rs index ad9e4d130..7701ea589 100644 --- a/tests/conformance_ruby/tags/if_else_tag_test.rs +++ b/tests/conformance_ruby/tags/if_else_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] fn test_if() { assert_template_result!( diff --git a/tests/conformance_ruby/tags/increment_tag_test.rs b/tests/conformance_ruby/tags/increment_tag_test.rs index c99fcb65b..deb1b92cb 100644 --- a/tests/conformance_ruby/tags/increment_tag_test.rs +++ b/tests/conformance_ruby/tags/increment_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] fn test_inc() { assert_template_result!("0", "{%increment port %}", v!({})); diff --git a/tests/conformance_ruby/tags/raw_tag_test.rs b/tests/conformance_ruby/tags/raw_tag_test.rs index 2493d7afe..6fc11299e 100644 --- a/tests/conformance_ruby/tags/raw_tag_test.rs +++ b/tests/conformance_ruby/tags/raw_tag_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] fn test_tag_in_raw() { assert_template_result!( diff --git a/tests/conformance_ruby/tags/table_row_test.rs b/tests/conformance_ruby/tags/table_row_test.rs index b6db913a4..faee17294 100644 --- a/tests/conformance_ruby/tags/table_row_test.rs +++ b/tests/conformance_ruby/tags/table_row_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - #[test] #[ignore] fn test_table_row() { diff --git a/tests/conformance_ruby/template_test.rs b/tests/conformance_ruby/template_test.rs index 0bb80a66f..0a1dc7e4a 100644 --- a/tests/conformance_ruby/template_test.rs +++ b/tests/conformance_ruby/template_test.rs @@ -1,7 +1,5 @@ use liquid; -use test_helper::*; - #[test] #[should_panic] fn test_instance_assigns_persist_on_same_template_object_between_parses() { diff --git a/tests/conformance_ruby/trim_mode_test.rs b/tests/conformance_ruby/trim_mode_test.rs index 2e3709af5..71293ad81 100644 --- a/tests/conformance_ruby/trim_mode_test.rs +++ b/tests/conformance_ruby/trim_mode_test.rs @@ -1,5 +1,3 @@ -use test_helper::*; - // Make sure the trim isn't applied to standard output #[test] fn test_standard_output() { diff --git a/tests/conformance_ruby/variable_test.rs b/tests/conformance_ruby/variable_test.rs index 832f3b38e..d8fbde83b 100644 --- a/tests/conformance_ruby/variable_test.rs +++ b/tests/conformance_ruby/variable_test.rs @@ -1,7 +1,5 @@ use liquid; -use test_helper::*; - #[test] fn test_simple_variable() { assert_template_result!(r#"worked"#, r#"{{test}}"#, v!({"test": "worked"})); diff --git a/tests/test_helper.rs b/tests/test_helper.rs index a5a0a7068..f80897443 100644 --- a/tests/test_helper.rs +++ b/tests/test_helper.rs @@ -4,18 +4,6 @@ extern crate regex; pub use liquid::value::Value::Nil; -#[allow(dead_code)] -pub fn render_template>( - template: S, - assigns: &liquid::value::Object, -) -> Result { - let template = liquid::ParserBuilder::with_liquid() - .build() - .parse(template.as_ref()) - .unwrap(); - template.render(assigns) -} - #[allow(unused_macros)] #[macro_export] macro_rules! assert_template_result { @@ -30,7 +18,11 @@ macro_rules! assert_template_result { assert_template_result!($expected, $template, $assigns); }; ($expected:expr, $template:expr, $assigns: expr) => { - let rendered = render_template($template, $assigns.as_object().unwrap()).unwrap(); + let template = ::liquid::ParserBuilder::with_liquid() + .build() + .parse($template.as_ref()) + .unwrap(); + let rendered = template.render($assigns.as_object().unwrap()).unwrap(); assert_eq!($expected, rendered); }; } @@ -49,7 +41,11 @@ macro_rules! assert_template_matches { assert_template_matches!($expected, $template, $assigns); }; ($expected:expr, $template:expr, $assigns: expr) => { - let rendered = render_template($template, $assigns.as_object().unwrap()).unwrap(); + let template = ::liquid::ParserBuilder::with_liquid() + .build() + .parse($template.as_ref()) + .unwrap(); + let rendered = template.render($assigns.as_object().unwrap()).unwrap(); let expected = $expected; println!("pattern={}", expected); @@ -81,14 +77,18 @@ macro_rules! assert_render_error { assert_render_error!($template); }; ($template:expr) => { - let assigns = ::liquid::value::Value::default(); + let assigns = ::liquid::value::Value::Object(Default::default()); assert_render_error!($template, assigns); }; ($template:expr, $assigns: expr, ) => { assert_render_error!($template, $assigns); }; ($template:expr, $assigns: expr) => { - render_template($template, $assigns.as_object().unwrap()).unwrap_err(); + let template = ::liquid::ParserBuilder::with_liquid() + .build() + .parse($template.as_ref()) + .unwrap(); + template.render($assigns.as_object().unwrap()).unwrap_err(); }; } From 9b45adc391ade8773617f3bcc7ad4bbcb7281f66 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 30 Nov 2018 16:12:14 -0700 Subject: [PATCH 2/3] test: Port remaining conformance tests --- tests/conformance_ruby/blank_test.rs | 22 +- tests/conformance_ruby/filter_test.rs | 349 +++++----- tests/conformance_ruby/mod.rs | 4 +- tests/conformance_ruby/output_test.rs | 271 ++++---- .../conformance_ruby/tags/include_tag_test.rs | 448 ++++++------- tests/conformance_ruby/tags/mod.rs | 8 +- .../tags/standard_tag_test.rs | 622 +++++++++--------- .../conformance_ruby/tags/statements_test.rs | 231 ++++--- .../tags/unless_else_tag_test.rs | 46 +- tests/test_helper.rs | 23 +- 10 files changed, 1036 insertions(+), 988 deletions(-) diff --git a/tests/conformance_ruby/blank_test.rs b/tests/conformance_ruby/blank_test.rs index 0e07899e7..febbe8c14 100644 --- a/tests/conformance_ruby/blank_test.rs +++ b/tests/conformance_ruby/blank_test.rs @@ -1,3 +1,5 @@ +use liquid; + const N: usize = 10; fn wrap_in_for>(body: S) -> String { @@ -28,6 +30,15 @@ fn repeat>(content: S, count: usize) -> String { (0..count).map(|_| content).collect::() } +#[derive(Clone)] +struct BlankTestFilesystem; + +impl liquid::compiler::Include for BlankTestFilesystem { + fn include(&self, relative_path: &str) -> Result { + Ok(relative_path.to_owned()) + } +} + #[test] fn test_new_tags_are_not_blank_by_default() { assert_template_result!( @@ -128,12 +139,11 @@ fn test_raw_is_not_blank() { #[test] #[ignore] fn test_include_is_blank() { - /* Too lazy to implement atm - Liquid::Template.file_system = BlankTestFileSystem.new - assert_template_result! "foobar" * (N + 1), wrap("{% include 'foobar' %}") - assert_template_result! " foobar " * (N + 1), wrap("{% include ' foobar ' %}") - assert_template_result! " " * (N + 1), wrap(" {% include ' ' %} ") - */ + let liquid = liquid::ParserBuilder::with_liquid().include_source(Box::new(BlankTestFilesystem)).build(); + + assert_template_result!(repeat("foobar", N + 1), wrap("{% include 'foobar' %}"), v!({}), liquid); + assert_template_result!(repeat(" foobar ", N + 1), wrap("{% include ' foobar ' %}"), v!({}), liquid); + assert_template_result!(repeat(" ", N + 1), wrap(" {% include ' ' %} "), v!({}), liquid); } #[test] diff --git a/tests/conformance_ruby/filter_test.rs b/tests/conformance_ruby/filter_test.rs index d3c880e83..76bc2cf78 100644 --- a/tests/conformance_ruby/filter_test.rs +++ b/tests/conformance_ruby/filter_test.rs @@ -1,178 +1,171 @@ -require 'test_helper' - -module MoneyFilter - def money(input) - sprintf(' %d$ ', input) - end - - def money_with_underscore(input) - sprintf(' %d$ ', input) - end -end - -module CanadianMoneyFilter - def money(input) - sprintf(' %d$ CAD ', input) - end -end - -module SubstituteFilter - def substitute(input, params = {}) - input.gsub(/%\{(\w+)\}/) { |match| params[$1] } - end -end - -class FiltersTest < Minitest::Test - include Liquid - - module OverrideObjectMethodFilter - def tap(input) - "tap overridden" - end - end - - def setup - @context = Context.new - end - - def test_local_filter - @context['var'] = 1000 - @context.add_filters(MoneyFilter) - - assert_equal ' 1000$ ', Template.parse("{{var | money}}").render(@context) - end - - def test_underscore_in_filter_name - @context['var'] = 1000 - @context.add_filters(MoneyFilter) - assert_equal ' 1000$ ', Template.parse("{{var | money_with_underscore}}").render(@context) - end - - def test_second_filter_overwrites_first - @context['var'] = 1000 - @context.add_filters(MoneyFilter) - @context.add_filters(CanadianMoneyFilter) - - assert_equal ' 1000$ CAD ', Template.parse("{{var | money}}").render(@context) - end - - def test_size - @context['var'] = 'abcd' - @context.add_filters(MoneyFilter) - - assert_equal '4', Template.parse("{{var | size}}").render(@context) - end - - def test_join - @context['var'] = [1, 2, 3, 4] - - assert_equal "1 2 3 4", Template.parse("{{var | join}}").render(@context) - end - - def test_sort - @context['value'] = 3 - @context['numbers'] = [2, 1, 4, 3] - @context['words'] = ['expected', 'as', 'alphabetic'] - @context['arrays'] = ['flower', 'are'] - @context['case_sensitive'] = ['sensitive', 'Expected', 'case'] - - assert_equal '1 2 3 4', Template.parse("{{numbers | sort | join}}").render(@context) - assert_equal 'alphabetic as expected', Template.parse("{{words | sort | join}}").render(@context) - assert_equal '3', Template.parse("{{value | sort}}").render(@context) - assert_equal 'are flower', Template.parse("{{arrays | sort | join}}").render(@context) - assert_equal 'Expected case sensitive', Template.parse("{{case_sensitive | sort | join}}").render(@context) - end - - def test_sort_natural - @context['words'] = ['case', 'Assert', 'Insensitive'] - @context['hashes'] = [{ 'a' => 'A' }, { 'a' => 'b' }, { 'a' => 'C' }] - @context['objects'] = [TestObject.new('A'), TestObject.new('b'), TestObject.new('C')] - - # Test strings - assert_equal 'Assert case Insensitive', Template.parse("{{words | sort_natural | join}}").render(@context) - - # Test hashes - assert_equal 'A b C', Template.parse("{{hashes | sort_natural: 'a' | map: 'a' | join}}").render(@context) - - # Test objects - assert_equal 'A b C', Template.parse("{{objects | sort_natural: 'a' | map: 'a' | join}}").render(@context) - end - - def test_compact - @context['words'] = ['a', nil, 'b', nil, 'c'] - @context['hashes'] = [{ 'a' => 'A' }, { 'a' => nil }, { 'a' => 'C' }] - @context['objects'] = [TestObject.new('A'), TestObject.new(nil), TestObject.new('C')] - - # Test strings - assert_equal 'a b c', Template.parse("{{words | compact | join}}").render(@context) - - # Test hashes - assert_equal 'A C', Template.parse("{{hashes | compact: 'a' | map: 'a' | join}}").render(@context) - - # Test objects - assert_equal 'A C', Template.parse("{{objects | compact: 'a' | map: 'a' | join}}").render(@context) - end - - def test_strip_html - @context['var'] = "bla blub" - - assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context) - end - - def test_strip_html_ignore_comments_with_html - @context['var'] = "bla blub" - - assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context) - end - - def test_capitalize - @context['var'] = "blub" - - assert_equal "Blub", Template.parse("{{ var | capitalize }}").render(@context) - end - - def test_nonexistent_filter_is_ignored - @context['var'] = 1000 - - assert_equal '1000', Template.parse("{{ var | xyzzy }}").render(@context) - end - - def test_filter_with_keyword_arguments - @context['surname'] = 'john' - @context['input'] = 'hello %{first_name}, %{last_name}' - @context.add_filters(SubstituteFilter) - output = Template.parse(%({{ input | substitute: first_name: surname, last_name: 'doe' }})).render(@context) - assert_equal 'hello john, doe', output - end - - def test_override_object_method_in_filter - assert_equal "tap overridden", Template.parse("{{var | tap}}").render!({ 'var' => 1000 }, filters: [OverrideObjectMethodFilter]) - - # tap still treated as a non-existent filter - assert_equal "1000", Template.parse("{{var | tap}}").render!({ 'var' => 1000 }) - end -end - -class FiltersInTemplate < Minitest::Test - include Liquid - - def test_local_global - with_global_filter(MoneyFilter) do - assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render!(nil, nil) - assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, filters: CanadianMoneyFilter) - assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, filters: [CanadianMoneyFilter]) - end - end - - def test_local_filter_with_deprecated_syntax - assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, CanadianMoneyFilter) - assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, [CanadianMoneyFilter]) - end -end # FiltersTest - -class TestObject < Liquid::Drop - attr_accessor :a - def initialize(a) - @a = a - end -end +use liquid; +use liquid::value::Value; +use liquid::interpreter::FilterResult; + +fn money(input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar(format!(" {}$ ", input))) +} + +fn money_with_underscore(input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar(format!(" {}$ ", input))) +} + +fn liquid_money() -> liquid::Parser { + liquid::ParserBuilder::with_liquid() + .filter("money", money as liquid::interpreter::FnFilterValue) + .filter("money_with_underscore", money_with_underscore as liquid::interpreter::FnFilterValue) + .build() +} + +fn substitute(input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar(format!("No keyword argument support: {}", input))) +} + +fn liquid_sub() -> liquid::Parser { + liquid::ParserBuilder::with_liquid() + .filter("substitute", substitute as liquid::interpreter::FnFilterValue) + .build() +} + +#[test] +fn test_local_filter() { + let assigns = v!({"var": 1000}); + + assert_template_result!(" 1000$ ", "{{var | money}}", assigns, liquid_money()); +} + +#[test] +fn test_underscore_in_filter_name() { + let assigns = v!({"var": 1000}); + + assert_template_result!(" 1000$ ", "{{var | money_with_underscore}}", assigns, liquid_money()); +} + +#[test] +#[should_panic] +fn test_second_filter_overwrites_first() { + panic!("Implementation specific: API for adding filters"); +} + +#[test] +fn test_size() { + let assigns = v!({"var": "abcd"}); + + assert_template_result!("4", "{{var | size}}", assigns); +} + +#[test] +fn test_join() { + let assigns = v!({"var": [1, 2, 3, 4]}); + + assert_template_result!("1 2 3 4", "{{var | join}}", assigns); +} + +#[test] +#[ignore] +fn test_sort() { + let assigns = v!({ + "value": 3, + "numbers": [2, 1, 4, 3], + "words": ["expected", "as", "alphabetic"], + "arrays": ["flower", "are"], + "case_sensitive": ["sensitive", "Expected", "case"], + }); + + assert_template_result!("1 2 3 4", "{{numbers | sort | join}}", assigns); + assert_template_result!("alphabetic as expected", "{{words | sort | join}}", assigns); + assert_template_result!("3", "{{value | sort}}", assigns); + assert_template_result!("are flower", "{{arrays | sort | join}}", assigns); + assert_template_result!("Expected case sensitive", "{{case_sensitive | sort | join}}", assigns); +} + +#[test] +#[ignore] +fn test_sort_natural() { + let assigns = v!({ + "words": ["case", "Assert", "Insensitive"], + "hashes": [{ "a": "A" }, { "a": "b" }, { "a": "C" }], + }); + + // Test strings + assert_template_result!("Assert case Insensitive", "{{words | sort_natural | join}}", assigns); + + // Test hashes + assert_template_result!("A b C", "{{hashes | sort_natural: 'a' | map: 'a' | join}}", assigns); + + // Test objects + // Implementation specific: API support objects for variables. +} + +#[test] +#[ignore] +fn test_compact() { + let assigns = v!({ + "words": ["a", nil, "b", nil, "c"], + "hashes": [{ "a": "A" }, { "a": nil }, { "a": "C" }], + }); + + // Test strings + assert_template_result!("a b c", "{{words | compact | join}}", assigns); + + // Test hashes + assert_template_result!("A C", "{{hashes | compact: 'a' | map: 'a' | join}}", assigns); + + // Test objects + // Implementation specific: API support objects for variables. +} + +#[test] +fn test_strip_html() { + let assigns = v!({"var": "bla blub"}); + + assert_template_result!("bla blub", "{{var | strip_html }}", assigns); +} + +#[test] +fn test_strip_html_ignore_comments_with_html() { + let assigns = v!({"var": "bla blub"}); + + assert_template_result!("bla blub", "{{var | strip_html }}", assigns); +} + +#[test] +fn test_capitalize() { + let assigns = v!({"var": "blub"}); + + assert_template_result!("Blub", "{{var | capitalize }}", assigns); +} + +#[test] +#[should_panic] +fn test_nonexistent_filter_is_ignored() { + panic!("Implementation specific: strict_filters"); +} + +#[test] +#[ignore] +fn test_filter_with_keyword_arguments() { + let assigns = v!({ + "surname": "john", + "input": "hello %{first_name}, %{last_name}", + }); + assert_template_result!("hello john, doe", "{{ input | substitute: first_name: surname, last_name: 'doe' }}", assigns, liquid_sub()); +} + +#[test] +#[should_panic] +fn test_override_object_method_in_filter() { + panic!("Implementation specific: object API"); +} + +#[test] +#[should_panic] +fn test_local_global() { + panic!("Implementation specific: local/global API"); +} + +#[test] +#[should_panic] +fn test_local_filter_with_deprecated_syntax() { + panic!("Implementation specific: local/global API"); +} diff --git a/tests/conformance_ruby/mod.rs b/tests/conformance_ruby/mod.rs index bd825edb2..9b0f387e0 100644 --- a/tests/conformance_ruby/mod.rs +++ b/tests/conformance_ruby/mod.rs @@ -8,9 +8,9 @@ mod context_test; mod document_test; mod drop_test; mod error_handling_test; -// mod filter_test; +mod filter_test; mod hash_ordering_test; -// mod output_test; +mod output_test; mod parse_tree_visitor_test; mod parsing_quirks_test; mod render_profiling_test; diff --git a/tests/conformance_ruby/output_test.rs b/tests/conformance_ruby/output_test.rs index b4cf9d707..bc3aa4f56 100644 --- a/tests/conformance_ruby/output_test.rs +++ b/tests/conformance_ruby/output_test.rs @@ -1,123 +1,148 @@ -require 'test_helper' - -module FunnyFilter - def make_funny(input) - 'LOL' - end - - def cite_funny(input) - "LOL: #{input}" - end - - def add_smiley(input, smiley = ":-)") - "#{input} #{smiley}" - end - - def add_tag(input, tag = "p", id = "foo") - %(<#{tag} id="#{id}">#{input}) - end - - def paragraph(input) - "

#{input}

" - end - - def link_to(name, url) - %(#{name}) - end -end - -class OutputTest < Minitest::Test - include Liquid - - def setup - @assigns = { - 'best_cars' => 'bmw', - 'car' => { 'bmw' => 'good', 'gm' => 'bad' } - } - end - - def test_variable - text = %( {{best_cars}} ) - - expected = %( bmw ) - assert_equal expected, Template.parse(text).render!(@assigns) - end - - def test_variable_traversing_with_two_brackets - text = %({{ site.data.menu[include.menu][include.locale] }}) - assert_equal "it works!", Template.parse(text).render!( - "site" => { "data" => { "menu" => { "foo" => { "bar" => "it works!" } } } }, - "include" => { "menu" => "foo", "locale" => "bar" } - ) - end - - def test_variable_traversing - text = %( {{car.bmw}} {{car.gm}} {{car.bmw}} ) - - expected = %( good bad good ) - assert_equal expected, Template.parse(text).render!(@assigns) - end - - def test_variable_piping - text = %( {{ car.gm | make_funny }} ) - expected = %( LOL ) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_variable_piping_with_input - text = %( {{ car.gm | cite_funny }} ) - expected = %( LOL: bad ) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_variable_piping_with_args - text = %! {{ car.gm | add_smiley : ':-(' }} ! - expected = %| bad :-( | - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_variable_piping_with_no_args - text = %( {{ car.gm | add_smiley }} ) - expected = %| bad :-) | - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_multiple_variable_piping_with_args - text = %! {{ car.gm | add_smiley : ':-(' | add_smiley : ':-('}} ! - expected = %| bad :-( :-( | - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_variable_piping_with_multiple_args - text = %( {{ car.gm | add_tag : 'span', 'bar'}} ) - expected = %( bad ) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_variable_piping_with_variable_args - text = %( {{ car.gm | add_tag : 'span', car.bmw}} ) - expected = %( bad ) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_multiple_pipings - text = %( {{ best_cars | cite_funny | paragraph }} ) - expected = %(

LOL: bmw

) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end - - def test_link_to - text = %( {{ 'Typo' | link_to: 'http://typo.leetsoft.com' }} ) - expected = %( Typo ) - - assert_equal expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]) - end -end # OutputTest +use liquid; +use liquid::value::Value; +use liquid::interpreter::FilterResult; + +fn make_funny(_input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar("LOL")) +} + +fn cite_funny(input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar(format!("LOL: {}", input))) +} + +fn add_smiley(input: &Value, args: &[Value]) -> FilterResult { + let smiley = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| ":-)".to_owned()); + Ok(Value::scalar(format!("{} {}", input, smiley))) +} + +fn add_tag(input: &Value, args: &[Value]) -> FilterResult { + let tag = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| "p".to_owned()); + let id = args.get(1).map(|s| s.to_string()).unwrap_or_else(|| "foo".to_owned()); + Ok(Value::scalar(format!(r#"<{} id="{}">{}"#, tag, id, input, tag))) +} + +fn paragraph(input: &Value, _args: &[Value]) -> FilterResult { + Ok(Value::scalar(format!("

{}

", input))) +} + +fn link_to(input: &Value, args: &[Value]) -> FilterResult { + let name = input; + let url = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| ":-)".to_owned()); + Ok(Value::scalar(format!(r#"{}"#, url, name))) +} + +fn liquid() -> liquid::Parser { + liquid::ParserBuilder::new() + .filter("make_funny", make_funny as liquid::interpreter::FnFilterValue) + .filter("cite_funny", cite_funny as liquid::interpreter::FnFilterValue) + .filter("add_smiley", add_smiley as liquid::interpreter::FnFilterValue) + .filter("add_tag", add_tag as liquid::interpreter::FnFilterValue) + .filter("paragraph", paragraph as liquid::interpreter::FnFilterValue) + .filter("link_to", link_to as liquid::interpreter::FnFilterValue) + .build() +} + +fn assigns() -> liquid::value::Value { + v!({ + "best_cars": "bmw", + "car": { "bmw": "good", "gm": "bad" } + }) +} + +#[test] +fn test_variable() { + let text = " {{best_cars}} "; + + let expected = " bmw "; + assert_template_result!(expected, text, assigns()); +} + +#[test] +#[ignore] +fn test_variable_traversing_with_two_brackets() { + let text = "{{ site.data.menu[include.menu][include.locale] }}"; + assert_template_result!("it works", text, v!({ + "site": { "data": { "menu": { "foo": { "bar": "it works!" } } } }, + "include": { "menu": "foo", "locale": "bar" } + })); +} + +#[test] +fn test_variable_traversing() { + let text = " {{car.bmw}} {{car.gm}} {{car.bmw}} "; + + let expected = " good bad good "; + assert_template_result!(expected, text, assigns()); +} + +#[test] +fn test_variable_piping() { + let text = " {{ car.gm | make_funny }} "; + let expected = " LOL "; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_variable_piping_with_input() { + let text = " {{ car.gm | cite_funny }} "; + let expected = " LOL: bad "; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_variable_piping_with_args() { + let text = r#" {{ car.gm | add_smiley : ":-(" }} "#; + let expected = " bad :-( "; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_variable_piping_with_no_args() { + let text = " {{ car.gm | add_smiley }} "; + let expected = " bad :-) "; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_multiple_variable_piping_with_args() { + let text = r#" {{ car.gm | add_smiley : ":-(" | add_smiley : ":-("}} "#; + let expected = " bad :-( :-( "; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_variable_piping_with_multiple_args() { + let text = r#" {{ car.gm | add_tag : "span", "bar"}} "#; + let expected = r#" bad "#; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_variable_piping_with_variable_args() { + let text = r#" {{ car.gm | add_tag : "span", car.bmw}} "#; + let expected = r#" bad "#; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_multiple_pipings() { + let text = " {{ best_cars | cite_funny | paragraph }} "; + let expected = "

LOL: bmw

"; + + assert_template_result!(expected, text, assigns(), liquid()); +} + +#[test] +fn test_link_to() { + let text = r#" {{ "Typo" | link_to: "http://typo.leetsoft.com" }} "#; + let expected = r#" Typo "#; + + assert_template_result!(expected, text, assigns(), liquid()); +} diff --git a/tests/conformance_ruby/tags/include_tag_test.rs b/tests/conformance_ruby/tags/include_tag_test.rs index 1d854b0c7..6f66d5305 100644 --- a/tests/conformance_ruby/tags/include_tag_test.rs +++ b/tests/conformance_ruby/tags/include_tag_test.rs @@ -1,245 +1,205 @@ -require 'test_helper' - -class TestFileSystem - def read_template_file(template_path) - case template_path - when "product" - "Product: {{ product.title }} " - - when "locale_variables" - "Locale: {{echo1}} {{echo2}}" - - when "variant" - "Variant: {{ variant.title }}" - - when "nested_template" - "{% include 'header' %} {% include 'body' %} {% include 'footer' %}" - - when "body" - "body {% include 'body_detail' %}" - - when "nested_product_template" - "Product: {{ nested_product_template.title }} {%include 'details'%} " - - when "recursively_nested_template" - "-{% include 'recursively_nested_template' %}" - - when "pick_a_source" - "from TestFileSystem" - - when 'assignments' - "{% assign foo = 'bar' %}" - - else - template_path - end - end -end - -class OtherFileSystem - def read_template_file(template_path) - 'from OtherFileSystem' - end -end - -class CountingFileSystem - attr_reader :count - def read_template_file(template_path) - @count ||= 0 - @count += 1 - 'from CountingFileSystem' - end -end - -class CustomInclude < Liquid::Tag - Syntax = /(#{Liquid::QuotedFragment}+)(\s+(?:with|for)\s+(#{Liquid::QuotedFragment}+))?/o - - def initialize(tag_name, markup, tokens) - markup =~ Syntax - @template_name = $1 - super - end - - def parse(tokens) - end - - def render(context) - @template_name[1..-2] - end -end - -class IncludeTagTest < Minitest::Test - include Liquid - - def setup - Liquid::Template.file_system = TestFileSystem.new - end - - def test_include_tag_looks_for_file_system_in_registers_first - assert_equal 'from OtherFileSystem', - Template.parse("{% include 'pick_a_source' %}").render!({}, registers: { file_system: OtherFileSystem.new }) - end - - def test_include_tag_with - assert_template_result "Product: Draft 151cm ", - "{% include 'product' with products[0] %}", "products" => [ { 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' } ] - end - - def test_include_tag_with_default_name - assert_template_result "Product: Draft 151cm ", - "{% include 'product' %}", "product" => { 'title' => 'Draft 151cm' } - end - - def test_include_tag_for - assert_template_result "Product: Draft 151cm Product: Element 155cm ", - "{% include 'product' for products %}", "products" => [ { 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' } ] - end - - def test_include_tag_with_local_variables - assert_template_result "Locale: test123 ", "{% include 'locale_variables' echo1: 'test123' %}" - end - - def test_include_tag_with_multiple_local_variables - assert_template_result "Locale: test123 test321", - "{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}" - end - - def test_include_tag_with_multiple_local_variables_from_context - assert_template_result "Locale: test123 test321", +use liquid; + +#[derive(Clone)] +struct TestFileSystem; + +impl liquid::compiler::Include for TestFileSystem { + fn include(&self, relative_path: &str) -> Result { + let template = match relative_path { + "product" => "Product: {{ product.title }} ", + "locale_variables" => "Locale: {{echo1}} {{echo2}}", + "variant" => "Variant: {{ variant.title }}", + "nested_template" => "{% include 'header' %} {% include 'body' %} {% include 'footer' %}", + "body" => "body {% include 'body_detail' %}", + "nested_product_template" => "Product: {{ nested_product_template.title }} {%include 'details'%} ", + "recursively_nested_template" => "-{% include 'recursively_nested_template' %}", + "pick_a_source" => "from TestFileSystem", + "assignments" => "{% assign foo = 'bar' %}", + _ => relative_path, + }; + Ok(template.to_owned()) + } +} + +fn liquid() -> liquid::Parser { + liquid::ParserBuilder::with_liquid().include_source(Box::new(TestFileSystem)).build() +} + +#[test] +#[should_panic] +fn test_include_tag_looks_for_file_system_in_registers_first() { + panic!("Implementation specific: exposing of registers API"); +} + +#[test] +#[ignore] +fn test_include_tag_with() { + assert_template_result!("Product: Draft 151cm ", + "{% include 'product' with products[0] %}", + v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), + liquid()); +} + +#[test] +fn test_include_tag_with_default_name() { + assert_template_result!("Product: Draft 151cm ", + "{% include 'product' %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid()); +} + +#[test] +#[ignore] +fn test_include_tag_for() { + assert_template_result!("Product: Draft 151cm Product: Element 155cm ", + "{% include 'product' for products %}", + v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), + liquid()); +} + +#[test] +#[ignore] +fn test_include_tag_with_local_variables() { + assert_template_result!("Locale: test123 ", "{% include 'locale_variables' echo1: 'test123' %}", v!({}), liquid()); +} + +#[test] +#[ignore] +fn test_include_tag_with_multiple_local_variables() { + assert_template_result!("Locale: test123 test321", + "{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}", v!({}), liquid()); +} + +#[test] +#[ignore] +fn test_include_tag_with_multiple_local_variables_from_context() { + assert_template_result!("Locale: test123 test321", "{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}", - 'echo1' => 'test123', 'more_echos' => { "echo2" => 'test321' } - end - - def test_included_templates_assigns_variables - assert_template_result "bar", "{% include 'assignments' %}{{ foo }}" - end - - def test_nested_include_tag - assert_template_result "body body_detail", "{% include 'body' %}" - - assert_template_result "header body body_detail footer", "{% include 'nested_template' %}" - end - - def test_nested_include_with_variable - assert_template_result "Product: Draft 151cm details ", - "{% include 'nested_product_template' with product %}", "product" => { "title" => 'Draft 151cm' } - - assert_template_result "Product: Draft 151cm details Product: Element 155cm details ", - "{% include 'nested_product_template' for products %}", "products" => [{ "title" => 'Draft 151cm' }, { "title" => 'Element 155cm' }] - end - - def test_recursively_included_template_does_not_produce_endless_loop - infinite_file_system = Class.new do - def read_template_file(template_path) - "-{% include 'loop' %}" - end - end - - Liquid::Template.file_system = infinite_file_system.new - - assert_raises(Liquid::StackLevelError) do - Template.parse("{% include 'loop' %}").render! - end - end - - def test_dynamically_choosen_template - assert_template_result "Test123", "{% include template %}", "template" => 'Test123' - assert_template_result "Test321", "{% include template %}", "template" => 'Test321' - - assert_template_result "Product: Draft 151cm ", "{% include template for product %}", - "template" => 'product', 'product' => { 'title' => 'Draft 151cm' } - end - - def test_include_tag_caches_second_read_of_same_partial - file_system = CountingFileSystem.new - assert_equal 'from CountingFileSystemfrom CountingFileSystem', - Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render!({}, registers: { file_system: file_system }) - assert_equal 1, file_system.count - end - - def test_include_tag_doesnt_cache_partials_across_renders - file_system = CountingFileSystem.new - assert_equal 'from CountingFileSystem', - Template.parse("{% include 'pick_a_source' %}").render!({}, registers: { file_system: file_system }) - assert_equal 1, file_system.count - - assert_equal 'from CountingFileSystem', - Template.parse("{% include 'pick_a_source' %}").render!({}, registers: { file_system: file_system }) - assert_equal 2, file_system.count - end - - def test_include_tag_within_if_statement - assert_template_result "foo_if_true", "{% if true %}{% include 'foo_if_true' %}{% endif %}" - end - - def test_custom_include_tag - original_tag = Liquid::Template.tags['include'] - Liquid::Template.tags['include'] = CustomInclude - begin - assert_equal "custom_foo", - Template.parse("{% include 'custom_foo' %}").render! - ensure - Liquid::Template.tags['include'] = original_tag - end - end - - def test_custom_include_tag_within_if_statement - original_tag = Liquid::Template.tags['include'] - Liquid::Template.tags['include'] = CustomInclude - begin - assert_equal "custom_foo_if_true", - Template.parse("{% if true %}{% include 'custom_foo_if_true' %}{% endif %}").render! - ensure - Liquid::Template.tags['include'] = original_tag - end - end - - def test_does_not_add_error_in_strict_mode_for_missing_variable - Liquid::Template.file_system = TestFileSystem.new - - a = Liquid::Template.parse(' {% include "nested_template" %}') - a.render! - assert_empty a.errors - end - - def test_passing_options_to_included_templates - assert_raises(Liquid::SyntaxError) do - Template.parse("{% include template %}", error_mode: :strict).render!("template" => '{{ "X" || downcase }}') - end - with_error_mode(:lax) do - assert_equal 'x', Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: true).render!("template" => '{{ "X" || downcase }}') - end - assert_raises(Liquid::SyntaxError) do - Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: [:locale]).render!("template" => '{{ "X" || downcase }}') - end - with_error_mode(:lax) do - assert_equal 'x', Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: [:error_mode]).render!("template" => '{{ "X" || downcase }}') - 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 %}" - - assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page %}", "product" => { 'title' => 'Draft 151cm' } - - assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => { 'title' => 'Draft 151cm' } - end - - def test_including_with_strict_variables - template = Liquid::Template.parse("{% include 'simple' %}", error_mode: :warn) - template.render(nil, strict_variables: true) - - assert_equal [], template.errors - end -end # IncludeTagTest + v!({"echo1": "test123", "more_echos": { "echo2": "test321" }}), + liquid()); +} + +#[test] +fn test_included_templates_assigns_variables() { + assert_template_result!("bar", "{% include 'assignments' %}{{ foo }}", v!({}), liquid()); +} + +#[test] +fn test_nested_include_tag() { + assert_template_result!("body body_detail", "{% include 'body' %}", v!({}), liquid()); + + assert_template_result!("header body body_detail footer", "{% include 'nested_template' %}", v!({}), liquid()); +} + +#[test] +#[ignore] +fn test_nested_include_with_variable() { + assert_template_result!("Product: Draft 151cm details ", + "{% include 'nested_product_template' with product %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid()); + + assert_template_result!("Product: Draft 151cm details Product: Element 155cm details ", + "{% include 'nested_product_template' for products %}", + v!({"products": [{ "title": "Draft 151cm" }, { "title": "Element 155cm" }]}), + liquid()); +} + +#[derive(Clone)] +struct InfiniteFileSystem; + +impl liquid::compiler::Include for InfiniteFileSystem { + fn include(&self, _relative_path: &str) -> Result { + Ok("-{% include 'loop' %}".to_owned()) + } +} + +#[test] +#[ignore] +#[should_panic] +fn test_recursively_included_template_does_not_produce_endless_loop() { + panic!("We don't check recursion depth"); + /* + let parser = liquid::ParserBuilder::with_liquid().include_source(Box::new(InfiniteFileSystem)).build(); + parser.parse("{% include 'loop' %}").unwrap(); + */ +} + +#[test] +#[ignore] +fn test_dynamically_choosen_template() { + assert_template_result!("Test123", "{% include template %}", v!({"template": "Test123"}), liquid()); + assert_template_result!("Test321", "{% include template %}", v!({"template": "Test321"}), liquid()); + + assert_template_result!("Product: Draft 151cm ", "{% include template for product %}", + v!({"template": "product", "product": { "title": "Draft 151cm" }}), + liquid()); +} + +#[test] +#[should_panic] +fn test_include_tag_caches_second_read_of_same_partial() { + panic!("Implementation specific: caching policies"); +} + +#[test] +#[should_panic] +fn test_include_tag_doesnt_cache_partials_across_renders() { + panic!("Implementation specific: caching policies"); +} + +#[test] +fn test_include_tag_within_if_statement() { + assert_template_result!("foo_if_true", "{% if true %}{% include 'foo_if_true' %}{% endif %}", v!({}), liquid()); +} + +#[test] +#[should_panic] +fn test_custom_include_tag() { + panic!("Implementation specific: API customization"); +} + +#[test] +#[should_panic] +fn test_custom_include_tag_within_if_statement() { + panic!("Implementation specific: API customization"); +} + +#[test] +fn test_does_not_add_error_in_strict_mode_for_missing_variable() { + let template = liquid().parse(r#" {% include "nested_template" %}"#).unwrap(); + template.render(v!({}).as_object().unwrap()).unwrap(); +} + +#[test] +#[should_panic] +fn test_passing_options_to_included_templates() { + panic!("Implementation specific: API options"); +} + +#[test] +#[ignore] +fn test_render_raise_argument_error_when_template_is_undefined() { + assert_parse_error!("{% include undefined_variable %}", liquid()); + assert_parse_error!("{% include nil %}", liquid()); +} + +#[test] +#[ignore] +fn test_including_via_variable_value() { + assert_template_result!("from TestFileSystem", "{% assign page = 'pick_a_source' %}{% include page %}", v!({}), liquid()); + + assert_template_result!("Product: Draft 151cm ", "{% assign page = 'product' %}{% include page %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid()); + + assert_template_result!("Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", + v!({"foo": { "title": "Draft 151cm" }}), + liquid()); +} + +#[test] +fn test_including_with_strict_variables() { + let template = liquid().parse("{% include 'simple' %}").unwrap(); + template.render(v!({}).as_object().unwrap()).unwrap(); +} diff --git a/tests/conformance_ruby/tags/mod.rs b/tests/conformance_ruby/tags/mod.rs index 4a6d31c36..b444509e1 100644 --- a/tests/conformance_ruby/tags/mod.rs +++ b/tests/conformance_ruby/tags/mod.rs @@ -2,10 +2,10 @@ mod break_tag_test; mod continue_tag_test; mod for_tag_test; mod if_else_tag_test; -// mod include_tag_test; +mod include_tag_test; mod increment_tag_test; mod raw_tag_test; -// mod standard_tag_test; -// mod statements_test; +mod standard_tag_test; +mod statements_test; mod table_row_test; -// mod unless_else_tag_test; +mod unless_else_tag_test; diff --git a/tests/conformance_ruby/tags/standard_tag_test.rs b/tests/conformance_ruby/tags/standard_tag_test.rs index 4b4703a6f..3a9f20b38 100644 --- a/tests/conformance_ruby/tags/standard_tag_test.rs +++ b/tests/conformance_ruby/tags/standard_tag_test.rs @@ -1,296 +1,326 @@ -require 'test_helper' - -class StandardTagTest < Minitest::Test - include Liquid - - def test_no_transform - assert_template_result('this text should come out of the template without change...', - 'this text should come out of the template without change...') - - assert_template_result('blah', 'blah') - assert_template_result('', '') - assert_template_result('|,.:', '|,.:') - assert_template_result('', '') - - text = %(this shouldnt see any transformation either but has multiple lines - as you can clearly see here ...) - assert_template_result(text, text) - end - - def test_has_a_block_which_does_nothing - assert_template_result(%(the comment block should be removed .. right?), - %(the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?)) - - assert_template_result('', '{%comment%}{%endcomment%}') - assert_template_result('', '{%comment%}{% endcomment %}') - assert_template_result('', '{% comment %}{%endcomment%}') - assert_template_result('', '{% comment %}{% endcomment %}') - assert_template_result('', '{%comment%}comment{%endcomment%}') - assert_template_result('', '{% comment %}comment{% endcomment %}') - assert_template_result('', '{% comment %} 1 {% comment %} 2 {% endcomment %} 3 {% endcomment %}') - - assert_template_result('', '{%comment%}{%blabla%}{%endcomment%}') - assert_template_result('', '{% comment %}{% blabla %}{% endcomment %}') - assert_template_result('', '{%comment%}{% endif %}{%endcomment%}') - assert_template_result('', '{% comment %}{% endwhatever %}{% endcomment %}') - assert_template_result('', '{% comment %}{% raw %} {{%%%%}} }} { {% endcomment %} {% comment {% endraw %} {% endcomment %}') - - assert_template_result('foobar', 'foo{%comment%}comment{%endcomment%}bar') - assert_template_result('foobar', 'foo{% comment %}comment{% endcomment %}bar') - assert_template_result('foobar', 'foo{%comment%} comment {%endcomment%}bar') - assert_template_result('foobar', 'foo{% comment %} comment {% endcomment %}bar') - - assert_template_result('foo bar', 'foo {%comment%} {%endcomment%} bar') - assert_template_result('foo bar', 'foo {%comment%}comment{%endcomment%} bar') - assert_template_result('foo bar', 'foo {%comment%} comment {%endcomment%} bar') - - assert_template_result('foobar', 'foo{%comment%} - {%endcomment%}bar') - end - - def test_hyphenated_assign - assigns = { 'a-b' => '1' } - assert_template_result('a-b:1 a-b:2', 'a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}', assigns) - end - - def test_assign_with_colon_and_spaces - assigns = { 'var' => { 'a:b c' => { 'paged' => '1' } } } - assert_template_result('var2: 1', '{%assign var2 = var["a:b c"].paged %}var2: {{var2}}', assigns) - end - - def test_capture - assigns = { 'var' => 'content' } - assert_template_result('content foo content foo ', - '{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}', - assigns) - end - - def test_capture_detects_bad_syntax - assert_raises(SyntaxError) do - assert_template_result('content foo content foo ', - '{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}', - { 'var' => 'content' }) - end - end - - def test_case - assigns = { 'condition' => 2 } - assert_template_result(' its 2 ', - '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', - assigns) - - assigns = { 'condition' => 1 } - assert_template_result(' its 1 ', - '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', - assigns) - - assigns = { 'condition' => 3 } - assert_template_result('', - '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}', - assigns) - - assigns = { 'condition' => "string here" } - assert_template_result(' hit ', - '{% case condition %}{% when "string here" %} hit {% endcase %}', - assigns) - - assigns = { 'condition' => "bad string here" } - assert_template_result('', - '{% case condition %}{% when "string here" %} hit {% endcase %}',\ - assigns) - end - - def test_case_with_else - assigns = { 'condition' => 5 } - assert_template_result(' hit ', - '{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', - assigns) - - assigns = { 'condition' => 6 } - assert_template_result(' else ', - '{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}', - assigns) - - assigns = { 'condition' => 6 } - assert_template_result(' else ', - '{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}', - assigns) - end - - def test_case_on_size - assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => []) - assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1]) - assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1]) - assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1]) - assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1]) - assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1]) - end - - def test_case_on_size_with_else - assert_template_result('else', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => []) - - assert_template_result('1', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => [1]) - - assert_template_result('2', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => [1, 1]) - - assert_template_result('else', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => [1, 1, 1]) - - assert_template_result('else', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => [1, 1, 1, 1]) - - assert_template_result('else', - '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}', - 'a' => [1, 1, 1, 1, 1]) - end - - def test_case_on_length_with_else - assert_template_result('else', - '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', - {}) - - assert_template_result('false', - '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', - {}) - - assert_template_result('true', - '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', - {}) - - assert_template_result('else', - '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}', - {}) - end - - def test_assign_from_case - # Example from the shopify forums - code = "{% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }}" - template = Liquid::Template.parse(code) - assert_equal "menswear", template.render!("collection" => { 'handle' => 'menswear-jackets' }) - assert_equal "menswear", template.render!("collection" => { 'handle' => 'menswear-t-shirts' }) - assert_equal "womenswear", template.render!("collection" => { 'handle' => 'x' }) - assert_equal "womenswear", template.render!("collection" => { 'handle' => 'y' }) - assert_equal "womenswear", template.render!("collection" => { 'handle' => 'z' }) - end - - def test_case_when_or - code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}' - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 1 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 2 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 3 }) - assert_template_result(' its 4 ', code, { 'condition' => 4 }) - assert_template_result('', code, { 'condition' => 5 }) - - code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}' - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 1 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 'string' }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => nil }) - assert_template_result('', code, { 'condition' => 'something else' }) - end - - def test_case_when_comma - code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}' - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 1 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 2 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 3 }) - assert_template_result(' its 4 ', code, { 'condition' => 4 }) - assert_template_result('', code, { 'condition' => 5 }) - - code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}' - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 1 }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => 'string' }) - assert_template_result(' its 1 or 2 or 3 ', code, { 'condition' => nil }) - assert_template_result('', code, { 'condition' => 'something else' }) - end - - def test_assign - assert_template_result 'variable', '{% assign a = "variable"%}{{a}}' - end - - def test_assign_unassigned - assigns = { 'var' => 'content' } - assert_template_result('var2: var2:content', 'var2:{{var2}} {%assign var2 = var%} var2:{{var2}}', assigns) - end - - def test_assign_an_empty_string - assert_template_result '', '{% assign a = ""%}{{a}}' - end - - def test_assign_is_global - assert_template_result 'variable', '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' - end - - def test_case_detects_bad_syntax - assert_raises(SyntaxError) do - assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {}) - end - - assert_raises(SyntaxError) do - assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {}) - end - end - - def test_cycle - assert_template_result('one', '{%cycle "one", "two"%}') - assert_template_result('one two', '{%cycle "one", "two"%} {%cycle "one", "two"%}') - assert_template_result(' two', '{%cycle "", "two"%} {%cycle "", "two"%}') - - assert_template_result('one two one', '{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}') - - assert_template_result('text-align: left text-align: right', - '{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}') - end - - def test_multiple_cycles - assert_template_result('1 2 1 1 2 3 1', - '{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}') - end - - def test_multiple_named_cycles - assert_template_result('one one two two one one', - '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}') - end - - def test_multiple_named_cycles_with_names_from_context - assigns = { "var1" => 1, "var2" => 2 } - assert_template_result('one one two two one one', - '{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns) - end - - def test_size_of_array - assigns = { "array" => [1, 2, 3, 4] } - assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns) - end - - def test_size_of_hash - assigns = { "hash" => { a: 1, b: 2, c: 3, d: 4 } } - assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns) - end - - def test_illegal_symbols - assert_template_result('', '{% if true == empty %}?{% endif %}', {}) - assert_template_result('', '{% if true == null %}?{% endif %}', {}) - assert_template_result('', '{% if empty == true %}?{% endif %}', {}) - assert_template_result('', '{% if null == true %}?{% endif %}', {}) - end - - def test_ifchanged - assigns = { 'array' => [ 1, 1, 2, 2, 3, 3] } - assert_template_result('123', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns) - - assigns = { 'array' => [ 1, 1, 1, 1] } - assert_template_result('1', '{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}', assigns) - end - - def test_multiline_tag - assert_template_result '0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}" - end -end # StandardTagTest +use liquid; + +#[test] +fn test_no_transform() { + assert_template_result!("this text should come out of the template without change...", + "this text should come out of the template without change..."); + + assert_template_result!("blah", "blah"); + assert_template_result!("", ""); + assert_template_result!("|,.:", "|,.:"); + assert_template_result!("", ""); + + let text = r#"this shouldnt see any transformation either but has multiple lines + as you can clearly see here ..."#; + assert_template_result!(text, text); +} + +#[test] +#[ignore] +fn test_has_a_block_which_does_nothing() { + assert_template_result!("the comment block should be removed .. right?", + "the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?"); + + assert_template_result!("", "{%comment%}{%endcomment%}"); + assert_template_result!("", "{%comment%}{% endcomment %}"); + assert_template_result!("", "{% comment %}{%endcomment%}"); + assert_template_result!("", "{% comment %}{% endcomment %}"); + assert_template_result!("", "{%comment%}comment{%endcomment%}"); + assert_template_result!("", "{% comment %}comment{% endcomment %}"); + assert_template_result!("", "{% comment %} 1 {% comment %} 2 {% endcomment %} 3 {% endcomment %}"); + + assert_template_result!("", "{%comment%}{%blabla%}{%endcomment%}"); + assert_template_result!("", "{% comment %}{% blabla %}{% endcomment %}"); + assert_template_result!("", "{%comment%}{% endif %}{%endcomment%}"); + assert_template_result!("", "{% comment %}{% endwhatever %}{% endcomment %}"); + assert_template_result!("", "{% comment %}{% raw %} {{%%%%}} }} { {% endcomment %} {% comment {% endraw %} {% endcomment %}"); + + assert_template_result!("foobar", "foo{%comment%}comment{%endcomment%}bar"); + assert_template_result!("foobar", "foo{% comment %}comment{% endcomment %}bar"); + assert_template_result!("foobar", "foo{%comment%} comment {%endcomment%}bar"); + assert_template_result!("foobar", "foo{% comment %} comment {% endcomment %}bar"); + + assert_template_result!("foo bar", "foo {%comment%} {%endcomment%} bar"); + assert_template_result!("foo bar", "foo {%comment%}comment{%endcomment%} bar"); + assert_template_result!("foo bar", "foo {%comment%} comment {%endcomment%} bar"); + + assert_template_result!("foobar", r#"foo{%comment%} + {%endcomment%}bar"#); +} + +#[test] +fn test_hyphenated_assign() { + let assigns = v!({ "a-b": "1" }); + assert_template_result!("a-b:1 a-b:2", "a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}", assigns); +} + +#[test] +fn test_assign_with_colon_and_spaces() { + let assigns = v!({ "var": { "a:b c": { "paged": "1" } } }); + assert_template_result!("var2: 1", r#"{%assign var2 = var["a:b c"].paged %}var2: {{var2}}"#, assigns); +} + +#[test] +fn test_capture() { + // Implementation specific: strict_variables is enabled, testing that instead. + let assigns = v!({ "var": "content" }); + assert_template_result!("content foo content foo ", + "{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}", + assigns); +} + +#[test] +fn test_capture_detects_bad_syntax() { + // Implementation specific: strict_variables is enabled, testing that instead. + assert_parse_error!( + "{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}"); +} + +#[test] +fn test_case() { + let assigns = v!({ "condition": 2 }); + assert_template_result!(" its 2 ", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns); + + let assigns = v!({ "condition": 1 }); + assert_template_result!(" its 1 ", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns); + + let assigns = v!({ "condition": 3 }); + assert_template_result!("", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns); + + let assigns = v!({ "condition": "string here" }); + assert_template_result!(" hit ", + r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, + assigns); + + let assigns = v!({ "condition": "bad string here" }); + assert_template_result!("", + r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, + assigns); +} + +#[test] +fn test_case_with_else() { + let assigns = v!({ "condition": 5 }); + assert_template_result!(" hit ", + "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", + assigns); + + let assigns = v!({ "condition": 6 }); + assert_template_result!(" else ", + "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", + assigns); + + let assigns = v!({ "condition": 6 }); + assert_template_result!(" else ", + "{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}", + assigns); +} + +#[test] +#[ignore] +fn test_case_on_size() { + assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": []})); + assert_template_result!("1", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1]})); + assert_template_result!("2", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1]})); + assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1]})); + assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1, 1]})); + assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1, 1, 1]})); +} + +#[test] +#[ignore] +fn test_case_on_size_with_else() { + assert_template_result!("else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": []})); + + assert_template_result!("1", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1]})); + + assert_template_result!("2", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1]})); + + assert_template_result!("else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1]})); + + assert_template_result!("else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1, 1]})); + + assert_template_result!("else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1, 1, 1]})); +} + +#[test] +#[ignore] +fn test_case_on_length_with_else() { + assert_template_result!("else", + "{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); + + assert_template_result!("false", + "{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); + + assert_template_result!("true", + "{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); + + assert_template_result!("else", + "{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); +} + +#[test] +fn test_assign_from_case() { + // Example from the shopify forums + let code = r#"{% case collection.handle %}{% when "menswear-jackets" %}{% assign ptitle = "menswear" %}{% when "menswear-t-shirts" %}{% assign ptitle = "menswear" %}{% else %}{% assign ptitle = "womenswear" %}{% endcase %}{{ ptitle }}"#; + let template = liquid::ParserBuilder::with_liquid() + .build() + .parse(code) + .unwrap(); + + assert_eq!("menswear", template.render(v!({"collection": { "handle": "menswear-jackets" }}).as_object().unwrap()).unwrap()); + assert_eq!("menswear", template.render(v!({"collection": { "handle": "menswear-t-shirts" }}).as_object().unwrap()).unwrap()); + assert_eq!("womenswear", template.render(v!({"collection": { "handle": "x" }}).as_object().unwrap()).unwrap()); + assert_eq!("womenswear", template.render(v!({"collection": { "handle": "y" }}).as_object().unwrap()).unwrap()); + assert_eq!("womenswear", template.render(v!({"collection": { "handle": "z" }}).as_object().unwrap()).unwrap()); +} + +#[test] +#[ignore] +fn test_case_when_or() { + let code = "{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"; + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 1 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 2 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 3 })); + assert_template_result!(" its 4 ", code, v!({ "condition": 4 })); + assert_template_result!("", code, v!({ "condition": 5 })); + + let code = r#"{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"#; + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 1 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": "string" })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": nil })); + assert_template_result!("", code, v!({ "condition": "something else" })); +} + +#[test] +#[ignore] +fn test_case_when_comma() { + let code = "{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"; + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 1 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 2 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 3 })); + assert_template_result!(" its 4 ", code, v!({ "condition": 4 })); + assert_template_result!("", code, v!({ "condition": 5 })); + + let code = r#"{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"#; + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 1 })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": "string" })); + assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": nil })); + assert_template_result!("", code, v!({ "condition": "something else" })); +} + +#[test] +fn test_assign() { + assert_template_result!("variable", r#"{% assign a = "variable"%}{{a}}"#); +} + +#[test] +fn test_assign_unassigned() { + // Implementation specific: strict_variables is enabled, testing that instead. + let assigns = v!({ "var": "content" }); + assert_render_error!("var2:{{var2}} {%assign var2 = var%} var2:{{var2}}", assigns); +} + +#[test] +fn test_assign_an_empty_string() { + assert_template_result!("", r#"{% assign a = ""%}{{a}}"#); +} + +#[test] +fn test_assign_is_global() { + assert_template_result!("variable", r#"{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}"#); +} + +#[test] +fn test_case_detects_bad_syntax() { + assert_parse_error!("{% case false %}{% when %}true{% endcase %}"); + + assert_parse_error!("{% case false %}{% huh %}true{% endcase %}"); +} + +#[test] +fn test_cycle() { + assert_template_result!("one", r#"{%cycle "one", "two"%}"#); + assert_template_result!("one two", r#"{%cycle "one", "two"%} {%cycle "one", "two"%}"#); + assert_template_result!(" two", r#"{%cycle "", "two"%} {%cycle "", "two"%}"#); + + assert_template_result!("one two one", r#"{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}"#); + + assert_template_result!("text-align: left text-align: right", + r#"{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}"#); +} + +#[test] +fn test_multiple_cycles() { + assert_template_result!("1 2 1 1 2 3 1", + "{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}"); +} + +#[test] +fn test_multiple_named_cycles() { + assert_template_result!("one one two two one one", + r#"{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}"#); +} + +#[test] +fn test_multiple_named_cycles_with_names_from_context() { + let assigns = v!({ "var1": 1, "var2": 2 }); + assert_template_result!("one one two two one one", + r#"{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}"#, assigns); +} + +#[test] +#[ignore] +fn test_size_of_array() { + let assigns = v!({ "array": [1, 2, 3, 4] }); + assert_template_result!("array has 4 elements", "array has {{ array.size }} elements", assigns); +} + +#[test] +#[ignore] +fn test_size_of_hash() { + let assigns = v!({ "hash": { "a": 1, "b": 2, "c": 3, "d": 4 } }); + assert_template_result!("hash has 4 elements", "hash has {{ hash.size }} elements", assigns); +} + +#[test] +fn test_illegal_symbols() { + // Implementation specific: strict_variables is enabled, testing that instead. + assert_render_error!("{% if true == empty %}?{% endif %}"); + assert_render_error!("{% if true == null %}?{% endif %}"); + assert_render_error!("{% if empty == true %}?{% endif %}"); + assert_render_error!("{% if null == true %}?{% endif %}"); +} + +#[test] +fn test_ifchanged() { + let assigns = v!({ "array": [ 1, 1, 2, 2, 3, 3] }); + assert_template_result!("123", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); + + let assigns = v!({ "array": [ 1, 1, 1, 1] }); + assert_template_result!("1", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); +} + +#[test] +#[ignore] +fn test_multiline_tag() { + assert_template_result!("0 1 2 3", "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}"); +} diff --git a/tests/conformance_ruby/tags/statements_test.rs b/tests/conformance_ruby/tags/statements_test.rs index eeff166f5..14fb37f13 100644 --- a/tests/conformance_ruby/tags/statements_test.rs +++ b/tests/conformance_ruby/tags/statements_test.rs @@ -1,111 +1,130 @@ -require 'test_helper' - -class StatementsTest < Minitest::Test - include Liquid - - def test_true_eql_true - text = ' {% if true == true %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text - end - - def test_true_not_eql_true - text = ' {% if true != true %} true {% else %} false {% endif %} ' - assert_template_result ' false ', text - end - - def test_true_lq_true - text = ' {% if 0 > 0 %} true {% else %} false {% endif %} ' - assert_template_result ' false ', text - end - - def test_one_lq_zero - text = ' {% if 1 > 0 %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text - end - - def test_zero_lq_one - text = ' {% if 0 < 1 %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text - end - - def test_zero_lq_or_equal_one - text = ' {% if 0 <= 0 %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text - end - - def test_zero_lq_or_equal_one_involving_nil - text = ' {% if null <= 0 %} true {% else %} false {% endif %} ' - assert_template_result ' false ', text - - text = ' {% if 0 <= null %} true {% else %} false {% endif %} ' - assert_template_result ' false ', text - end - - def test_zero_lqq_or_equal_one - text = ' {% if 0 >= 0 %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text - end - - def test_strings - text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} " - assert_template_result ' true ', text - end - - def test_strings_not_equal - text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} " - assert_template_result ' false ', text - end - - def test_var_strings_equal - text = ' {% if var == "hello there!" %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => 'hello there!' - end - - def test_var_strings_are_not_equal - text = ' {% if "hello there!" == var %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => 'hello there!' - end - - def test_var_and_long_string_are_equal - text = " {% if var == 'hello there!' %} true {% else %} false {% endif %} " - assert_template_result ' true ', text, 'var' => 'hello there!' - end - - def test_var_and_long_string_are_equal_backwards - text = " {% if 'hello there!' == var %} true {% else %} false {% endif %} " - assert_template_result ' true ', text, 'var' => 'hello there!' - end - +#[test] +fn test_true_eql_true() { + let text = " {% if true == true %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +fn test_true_not_eql_true() { + let text = " {% if true != true %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text); +} + +#[test] +fn test_true_lq_true() { + let text = " {% if 0 > 0 %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text); +} + +#[test] +fn test_one_lq_zero() { + let text = " {% if 1 > 0 %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +fn test_zero_lq_one() { + let text = " {% if 0 < 1 %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +fn test_zero_lq_or_equal_one() { + let text = " {% if 0 <= 0 %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +#[ignore] +fn test_zero_lq_or_equal_one_involving_nil() { + let text = " {% if null <= 0 %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text); + + let text = " {% if 0 <= null %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text); +} + +#[test] +fn test_zero_lqq_or_equal_one() { + let text = " {% if 0 >= 0 %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +fn test_strings() { + let text = " {% if 'test' == 'test' %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text); +} + +#[test] +fn test_strings_not_equal() { + let text = " {% if 'test' != 'test' %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text); +} + +#[test] +fn test_var_strings_equal() { + let text = r#" {% if var == "hello there!" %} true {% else %} false {% endif %} "#; + assert_template_result!(" true ", text, v!({"var": "hello there!"})); +} + +#[test] +fn test_var_strings_are_not_equal() { + let text = r#" {% if "hello there!" == var %} true {% else %} false {% endif %} "#; + assert_template_result!(" true ", text, v!({"var": "hello there!"})); +} + +#[test] +fn test_var_and_long_string_are_equal() { + let text = r#" {% if var == "hello there!" %} true {% else %} false {% endif %} "#; + assert_template_result!(" true ", text, v!({"var": "hello there!"})); +} + +#[test] +fn test_var_and_long_string_are_equal_backwards() { + let text = r#" {% if "hello there!" == var %} true {% else %} false {% endif %} "#; + assert_template_result!(" true ", text, v!({"var": "hello there!"})); +} + +/* # def test_is_nil # text = %| {% if var != nil %} true {% else %} false {% end %} | - # @template.assigns = { 'var' => 'hello there!'} + # @template.assigns = { "var": "hello there!"} # expected = %| true | # assert_equal expected, @template.parse(text) # end - - def test_is_collection_empty - text = ' {% if array == empty %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'array' => [] - end - - def test_is_not_collection_empty - text = ' {% if array == empty %} true {% else %} false {% endif %} ' - assert_template_result ' false ', text, 'array' => [1, 2, 3] - end - - def test_nil - text = ' {% if var == nil %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => nil - - text = ' {% if var == null %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => nil - end - - def test_not_nil - text = ' {% if var != nil %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => 1 - - text = ' {% if var != null %} true {% else %} false {% endif %} ' - assert_template_result ' true ', text, 'var' => 1 - end -end # StatementsTest +*/ + +#[test] +#[ignore] +fn test_is_collection_empty() { + let text = " {% if array == empty %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text, v!({"array": []})); +} + +#[test] +#[ignore] +fn test_is_not_collection_empty() { + let text = " {% if array == empty %} true {% else %} false {% endif %} "; + assert_template_result!(" false ", text, v!({"array": [1, 2, 3]})); +} + +#[test] +#[ignore] +fn test_nil() { + let text = " {% if var == nil %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text, v!({"var": nil})); + + let text = " {% if var == null %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text, v!({"var": nil})); +} + +#[test] +#[ignore] +fn test_not_nil() { + let text = " {% if var != nil %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text, v!({"var": 1})); + + let text = " {% if var != null %} true {% else %} false {% endif %} "; + assert_template_result!(" true ", text, v!({"var": 1})); +} diff --git a/tests/conformance_ruby/tags/unless_else_tag_test.rs b/tests/conformance_ruby/tags/unless_else_tag_test.rs index c414a715b..2b8d3af4c 100644 --- a/tests/conformance_ruby/tags/unless_else_tag_test.rs +++ b/tests/conformance_ruby/tags/unless_else_tag_test.rs @@ -1,26 +1,26 @@ -require 'test_helper' +#[test] +fn test_unless() { + assert_template_result!(" ", " {% unless true %} this text should not go into the output {% endunless %} "); + assert_template_result!(" this text should go into the output ", + " {% unless false %} this text should go into the output {% endunless %} "); + assert_template_result!(" you rock ?", "{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?"); +} -class UnlessElseTagTest < Minitest::Test - include Liquid +#[test] +#[ignore] +fn test_unless_else() { + assert_template_result!(" YES ", "{% unless true %} NO {% else %} YES {% endunless %}"); + assert_template_result!(" YES ", "{% unless false %} YES {% else %} NO {% endunless %}"); + assert_template_result!(" YES ", r#"{% unless "foo" %} NO {% else %} YES {% endunless %}"#); +} - def test_unless - assert_template_result(' ', ' {% unless true %} this text should not go into the output {% endunless %} ') - assert_template_result(' this text should go into the output ', - ' {% unless false %} this text should go into the output {% endunless %} ') - assert_template_result(' you rock ?', '{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?') - end +#[test] +fn test_unless_in_loop() { + assert_template_result!("23", "{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}", v!({"choices": [1, nil, false]})); +} - def test_unless_else - assert_template_result(' YES ', '{% unless true %} NO {% else %} YES {% endunless %}') - assert_template_result(' YES ', '{% unless false %} YES {% else %} NO {% endunless %}') - assert_template_result(' YES ', '{% unless "foo" %} NO {% else %} YES {% endunless %}') - end - - def test_unless_in_loop - assert_template_result '23', '{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}', 'choices' => [1, nil, false] - end - - def test_unless_else_in_loop - assert_template_result ' TRUE 2 3 ', '{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}', 'choices' => [1, nil, false] - end -end # UnlessElseTest +#[test] +#[ignore] +fn test_unless_else_in_loop() { + assert_template_result!(" TRUE 2 3 ", "{% for i in choices %}{% unless i %} {{ forloop.index }} {% else %} TRUE {% endunless %}{% endfor %}", v!({"choices": [1, nil, false]})); +} diff --git a/tests/test_helper.rs b/tests/test_helper.rs index f80897443..7387bbe1c 100644 --- a/tests/test_helper.rs +++ b/tests/test_helper.rs @@ -18,8 +18,14 @@ macro_rules! assert_template_result { assert_template_result!($expected, $template, $assigns); }; ($expected:expr, $template:expr, $assigns: expr) => { - let template = ::liquid::ParserBuilder::with_liquid() - .build() + let liquid = ::liquid::ParserBuilder::with_liquid().build(); + assert_template_result!($expected, $template, $assigns, liquid); + }; + ($expected:expr, $template:expr, $assigns: expr, $liquid: expr, ) => { + assert_template_result!($expected, $template, $assigns, $liquid); + }; + ($expected:expr, $template:expr, $assigns: expr, $liquid: expr) => { + let template = $liquid .parse($template.as_ref()) .unwrap(); let rendered = template.render($assigns.as_object().unwrap()).unwrap(); @@ -59,12 +65,17 @@ macro_rules! assert_template_matches { #[macro_export] macro_rules! assert_parse_error { ($template:expr, ) => { - assert_parse_error!($template); + assert_parse_error!($template) }; ($template:expr) => {{ - let template = ::liquid::ParserBuilder::with_liquid() - .build() - .parse($template); + let liquid = ::liquid::ParserBuilder::with_liquid().build(); + assert_parse_error!($template, liquid) + }}; + ($template:expr, $liquid:expr, ) => {{ + assert_parse_error!($template, $liquid) + }}; + ($template:expr, $liquid:expr) => {{ + let template = $liquid.parse($template); assert!(template.is_err()); template.err().unwrap() }}; From fe98508a4b3d099cf6dd5e01d3b5ffb726f679c9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 30 Nov 2018 16:12:38 -0700 Subject: [PATCH 3/3] chore: Re-format conformance tests --- tests/conformance_ruby/blank_test.rs | 25 +- tests/conformance_ruby/filter_test.rs | 57 +++- tests/conformance_ruby/output_test.rs | 49 ++- .../conformance_ruby/tags/include_tag_test.rs | 168 ++++++--- .../tags/standard_tag_test.rs | 323 +++++++++++++----- .../conformance_ruby/tags/statements_test.rs | 4 +- .../tags/unless_else_tag_test.rs | 37 +- tests/test_helper.rs | 4 +- 8 files changed, 484 insertions(+), 183 deletions(-) diff --git a/tests/conformance_ruby/blank_test.rs b/tests/conformance_ruby/blank_test.rs index febbe8c14..65a19e879 100644 --- a/tests/conformance_ruby/blank_test.rs +++ b/tests/conformance_ruby/blank_test.rs @@ -139,11 +139,28 @@ fn test_raw_is_not_blank() { #[test] #[ignore] fn test_include_is_blank() { - let liquid = liquid::ParserBuilder::with_liquid().include_source(Box::new(BlankTestFilesystem)).build(); + let liquid = liquid::ParserBuilder::with_liquid() + .include_source(Box::new(BlankTestFilesystem)) + .build(); - assert_template_result!(repeat("foobar", N + 1), wrap("{% include 'foobar' %}"), v!({}), liquid); - assert_template_result!(repeat(" foobar ", N + 1), wrap("{% include ' foobar ' %}"), v!({}), liquid); - assert_template_result!(repeat(" ", N + 1), wrap(" {% include ' ' %} "), v!({}), liquid); + assert_template_result!( + repeat("foobar", N + 1), + wrap("{% include 'foobar' %}"), + v!({}), + liquid + ); + assert_template_result!( + repeat(" foobar ", N + 1), + wrap("{% include ' foobar ' %}"), + v!({}), + liquid + ); + assert_template_result!( + repeat(" ", N + 1), + wrap(" {% include ' ' %} "), + v!({}), + liquid + ); } #[test] diff --git a/tests/conformance_ruby/filter_test.rs b/tests/conformance_ruby/filter_test.rs index 76bc2cf78..1727af1e0 100644 --- a/tests/conformance_ruby/filter_test.rs +++ b/tests/conformance_ruby/filter_test.rs @@ -1,6 +1,6 @@ use liquid; -use liquid::value::Value; use liquid::interpreter::FilterResult; +use liquid::value::Value; fn money(input: &Value, _args: &[Value]) -> FilterResult { Ok(Value::scalar(format!(" {}$ ", input))) @@ -13,18 +13,25 @@ fn money_with_underscore(input: &Value, _args: &[Value]) -> FilterResult { fn liquid_money() -> liquid::Parser { liquid::ParserBuilder::with_liquid() .filter("money", money as liquid::interpreter::FnFilterValue) - .filter("money_with_underscore", money_with_underscore as liquid::interpreter::FnFilterValue) - .build() + .filter( + "money_with_underscore", + money_with_underscore as liquid::interpreter::FnFilterValue, + ).build() } fn substitute(input: &Value, _args: &[Value]) -> FilterResult { - Ok(Value::scalar(format!("No keyword argument support: {}", input))) + Ok(Value::scalar(format!( + "No keyword argument support: {}", + input + ))) } fn liquid_sub() -> liquid::Parser { liquid::ParserBuilder::with_liquid() - .filter("substitute", substitute as liquid::interpreter::FnFilterValue) - .build() + .filter( + "substitute", + substitute as liquid::interpreter::FnFilterValue, + ).build() } #[test] @@ -38,7 +45,12 @@ fn test_local_filter() { fn test_underscore_in_filter_name() { let assigns = v!({"var": 1000}); - assert_template_result!(" 1000$ ", "{{var | money_with_underscore}}", assigns, liquid_money()); + assert_template_result!( + " 1000$ ", + "{{var | money_with_underscore}}", + assigns, + liquid_money() + ); } #[test] @@ -76,7 +88,11 @@ fn test_sort() { assert_template_result!("alphabetic as expected", "{{words | sort | join}}", assigns); assert_template_result!("3", "{{value | sort}}", assigns); assert_template_result!("are flower", "{{arrays | sort | join}}", assigns); - assert_template_result!("Expected case sensitive", "{{case_sensitive | sort | join}}", assigns); + assert_template_result!( + "Expected case sensitive", + "{{case_sensitive | sort | join}}", + assigns + ); } #[test] @@ -88,10 +104,18 @@ fn test_sort_natural() { }); // Test strings - assert_template_result!("Assert case Insensitive", "{{words | sort_natural | join}}", assigns); + assert_template_result!( + "Assert case Insensitive", + "{{words | sort_natural | join}}", + assigns + ); // Test hashes - assert_template_result!("A b C", "{{hashes | sort_natural: 'a' | map: 'a' | join}}", assigns); + assert_template_result!( + "A b C", + "{{hashes | sort_natural: 'a' | map: 'a' | join}}", + assigns + ); // Test objects // Implementation specific: API support objects for variables. @@ -109,7 +133,11 @@ fn test_compact() { assert_template_result!("a b c", "{{words | compact | join}}", assigns); // Test hashes - assert_template_result!("A C", "{{hashes | compact: 'a' | map: 'a' | join}}", assigns); + assert_template_result!( + "A C", + "{{hashes | compact: 'a' | map: 'a' | join}}", + assigns + ); // Test objects // Implementation specific: API support objects for variables. @@ -149,7 +177,12 @@ fn test_filter_with_keyword_arguments() { "surname": "john", "input": "hello %{first_name}, %{last_name}", }); - assert_template_result!("hello john, doe", "{{ input | substitute: first_name: surname, last_name: 'doe' }}", assigns, liquid_sub()); + assert_template_result!( + "hello john, doe", + "{{ input | substitute: first_name: surname, last_name: 'doe' }}", + assigns, + liquid_sub() + ); } #[test] diff --git a/tests/conformance_ruby/output_test.rs b/tests/conformance_ruby/output_test.rs index bc3aa4f56..60602c665 100644 --- a/tests/conformance_ruby/output_test.rs +++ b/tests/conformance_ruby/output_test.rs @@ -1,6 +1,6 @@ use liquid; -use liquid::value::Value; use liquid::interpreter::FilterResult; +use liquid::value::Value; fn make_funny(_input: &Value, _args: &[Value]) -> FilterResult { Ok(Value::scalar("LOL")) @@ -11,14 +11,26 @@ fn cite_funny(input: &Value, _args: &[Value]) -> FilterResult { } fn add_smiley(input: &Value, args: &[Value]) -> FilterResult { - let smiley = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| ":-)".to_owned()); + let smiley = args + .get(0) + .map(|s| s.to_string()) + .unwrap_or_else(|| ":-)".to_owned()); Ok(Value::scalar(format!("{} {}", input, smiley))) } fn add_tag(input: &Value, args: &[Value]) -> FilterResult { - let tag = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| "p".to_owned()); - let id = args.get(1).map(|s| s.to_string()).unwrap_or_else(|| "foo".to_owned()); - Ok(Value::scalar(format!(r#"<{} id="{}">{}"#, tag, id, input, tag))) + let tag = args + .get(0) + .map(|s| s.to_string()) + .unwrap_or_else(|| "p".to_owned()); + let id = args + .get(1) + .map(|s| s.to_string()) + .unwrap_or_else(|| "foo".to_owned()); + Ok(Value::scalar(format!( + r#"<{} id="{}">{}"#, + tag, id, input, tag + ))) } fn paragraph(input: &Value, _args: &[Value]) -> FilterResult { @@ -27,16 +39,25 @@ fn paragraph(input: &Value, _args: &[Value]) -> FilterResult { fn link_to(input: &Value, args: &[Value]) -> FilterResult { let name = input; - let url = args.get(0).map(|s| s.to_string()).unwrap_or_else(|| ":-)".to_owned()); + let url = args + .get(0) + .map(|s| s.to_string()) + .unwrap_or_else(|| ":-)".to_owned()); Ok(Value::scalar(format!(r#"{}"#, url, name))) } fn liquid() -> liquid::Parser { liquid::ParserBuilder::new() - .filter("make_funny", make_funny as liquid::interpreter::FnFilterValue) - .filter("cite_funny", cite_funny as liquid::interpreter::FnFilterValue) - .filter("add_smiley", add_smiley as liquid::interpreter::FnFilterValue) - .filter("add_tag", add_tag as liquid::interpreter::FnFilterValue) + .filter( + "make_funny", + make_funny as liquid::interpreter::FnFilterValue, + ).filter( + "cite_funny", + cite_funny as liquid::interpreter::FnFilterValue, + ).filter( + "add_smiley", + add_smiley as liquid::interpreter::FnFilterValue, + ).filter("add_tag", add_tag as liquid::interpreter::FnFilterValue) .filter("paragraph", paragraph as liquid::interpreter::FnFilterValue) .filter("link_to", link_to as liquid::interpreter::FnFilterValue) .build() @@ -61,10 +82,14 @@ fn test_variable() { #[ignore] fn test_variable_traversing_with_two_brackets() { let text = "{{ site.data.menu[include.menu][include.locale] }}"; - assert_template_result!("it works", text, v!({ + assert_template_result!( + "it works", + text, + v!({ "site": { "data": { "menu": { "foo": { "bar": "it works!" } } } }, "include": { "menu": "foo", "locale": "bar" } - })); + }) + ); } #[test] diff --git a/tests/conformance_ruby/tags/include_tag_test.rs b/tests/conformance_ruby/tags/include_tag_test.rs index 6f66d5305..d0dd9b38e 100644 --- a/tests/conformance_ruby/tags/include_tag_test.rs +++ b/tests/conformance_ruby/tags/include_tag_test.rs @@ -9,9 +9,13 @@ impl liquid::compiler::Include for TestFileSystem { "product" => "Product: {{ product.title }} ", "locale_variables" => "Locale: {{echo1}} {{echo2}}", "variant" => "Variant: {{ variant.title }}", - "nested_template" => "{% include 'header' %} {% include 'body' %} {% include 'footer' %}", + "nested_template" => { + "{% include 'header' %} {% include 'body' %} {% include 'footer' %}" + } "body" => "body {% include 'body_detail' %}", - "nested_product_template" => "Product: {{ nested_product_template.title }} {%include 'details'%} ", + "nested_product_template" => { + "Product: {{ nested_product_template.title }} {%include 'details'%} " + } "recursively_nested_template" => "-{% include 'recursively_nested_template' %}", "pick_a_source" => "from TestFileSystem", "assignments" => "{% assign foo = 'bar' %}", @@ -22,7 +26,9 @@ impl liquid::compiler::Include for TestFileSystem { } fn liquid() -> liquid::Parser { - liquid::ParserBuilder::with_liquid().include_source(Box::new(TestFileSystem)).build() + liquid::ParserBuilder::with_liquid() + .include_source(Box::new(TestFileSystem)) + .build() } #[test] @@ -34,75 +40,106 @@ fn test_include_tag_looks_for_file_system_in_registers_first() { #[test] #[ignore] fn test_include_tag_with() { - assert_template_result!("Product: Draft 151cm ", - "{% include 'product' with products[0] %}", - v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), - liquid()); + assert_template_result!( + "Product: Draft 151cm ", + "{% include 'product' with products[0] %}", + v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), + liquid() + ); } #[test] fn test_include_tag_with_default_name() { - assert_template_result!("Product: Draft 151cm ", - "{% include 'product' %}", - v!({"product": { "title": "Draft 151cm" }}), - liquid()); + assert_template_result!( + "Product: Draft 151cm ", + "{% include 'product' %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid() + ); } #[test] #[ignore] fn test_include_tag_for() { - assert_template_result!("Product: Draft 151cm Product: Element 155cm ", - "{% include 'product' for products %}", - v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), - liquid()); + assert_template_result!( + "Product: Draft 151cm Product: Element 155cm ", + "{% include 'product' for products %}", + v!({"products": [ { "title": "Draft 151cm" }, { "title": "Element 155cm" } ]}), + liquid() + ); } #[test] #[ignore] fn test_include_tag_with_local_variables() { - assert_template_result!("Locale: test123 ", "{% include 'locale_variables' echo1: 'test123' %}", v!({}), liquid()); + assert_template_result!( + "Locale: test123 ", + "{% include 'locale_variables' echo1: 'test123' %}", + v!({}), + liquid() + ); } #[test] #[ignore] fn test_include_tag_with_multiple_local_variables() { - assert_template_result!("Locale: test123 test321", - "{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}", v!({}), liquid()); + assert_template_result!( + "Locale: test123 test321", + "{% include 'locale_variables' echo1: 'test123', echo2: 'test321' %}", + v!({}), + liquid() + ); } #[test] #[ignore] fn test_include_tag_with_multiple_local_variables_from_context() { - assert_template_result!("Locale: test123 test321", - "{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}", - v!({"echo1": "test123", "more_echos": { "echo2": "test321" }}), - liquid()); + assert_template_result!( + "Locale: test123 test321", + "{% include 'locale_variables' echo1: echo1, echo2: more_echos.echo2 %}", + v!({"echo1": "test123", "more_echos": { "echo2": "test321" }}), + liquid() + ); } #[test] fn test_included_templates_assigns_variables() { - assert_template_result!("bar", "{% include 'assignments' %}{{ foo }}", v!({}), liquid()); + assert_template_result!( + "bar", + "{% include 'assignments' %}{{ foo }}", + v!({}), + liquid() + ); } #[test] fn test_nested_include_tag() { assert_template_result!("body body_detail", "{% include 'body' %}", v!({}), liquid()); - assert_template_result!("header body body_detail footer", "{% include 'nested_template' %}", v!({}), liquid()); + assert_template_result!( + "header body body_detail footer", + "{% include 'nested_template' %}", + v!({}), + liquid() + ); } #[test] #[ignore] fn test_nested_include_with_variable() { - assert_template_result!("Product: Draft 151cm details ", - "{% include 'nested_product_template' with product %}", - v!({"product": { "title": "Draft 151cm" }}), - liquid()); - - assert_template_result!("Product: Draft 151cm details Product: Element 155cm details ", - "{% include 'nested_product_template' for products %}", - v!({"products": [{ "title": "Draft 151cm" }, { "title": "Element 155cm" }]}), - liquid()); + assert_template_result!( + "Product: Draft 151cm details ", + "{% include 'nested_product_template' with product %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid() + ); + + assert_template_result!( + "Product: Draft 151cm details Product: Element 155cm details ", + "{% include 'nested_product_template' for products %}", + v!({"products": [{ "title": "Draft 151cm" }, { "title": "Element 155cm" }]}), + liquid() + ); } #[derive(Clone)] @@ -128,12 +165,25 @@ fn test_recursively_included_template_does_not_produce_endless_loop() { #[test] #[ignore] fn test_dynamically_choosen_template() { - assert_template_result!("Test123", "{% include template %}", v!({"template": "Test123"}), liquid()); - assert_template_result!("Test321", "{% include template %}", v!({"template": "Test321"}), liquid()); - - assert_template_result!("Product: Draft 151cm ", "{% include template for product %}", - v!({"template": "product", "product": { "title": "Draft 151cm" }}), - liquid()); + assert_template_result!( + "Test123", + "{% include template %}", + v!({"template": "Test123"}), + liquid() + ); + assert_template_result!( + "Test321", + "{% include template %}", + v!({"template": "Test321"}), + liquid() + ); + + assert_template_result!( + "Product: Draft 151cm ", + "{% include template for product %}", + v!({"template": "product", "product": { "title": "Draft 151cm" }}), + liquid() + ); } #[test] @@ -150,7 +200,12 @@ fn test_include_tag_doesnt_cache_partials_across_renders() { #[test] fn test_include_tag_within_if_statement() { - assert_template_result!("foo_if_true", "{% if true %}{% include 'foo_if_true' %}{% endif %}", v!({}), liquid()); + assert_template_result!( + "foo_if_true", + "{% if true %}{% include 'foo_if_true' %}{% endif %}", + v!({}), + liquid() + ); } #[test] @@ -167,7 +222,9 @@ fn test_custom_include_tag_within_if_statement() { #[test] fn test_does_not_add_error_in_strict_mode_for_missing_variable() { - let template = liquid().parse(r#" {% include "nested_template" %}"#).unwrap(); + let template = liquid() + .parse(r#" {% include "nested_template" %}"#) + .unwrap(); template.render(v!({}).as_object().unwrap()).unwrap(); } @@ -187,15 +244,26 @@ fn test_render_raise_argument_error_when_template_is_undefined() { #[test] #[ignore] fn test_including_via_variable_value() { - assert_template_result!("from TestFileSystem", "{% assign page = 'pick_a_source' %}{% include page %}", v!({}), liquid()); - - assert_template_result!("Product: Draft 151cm ", "{% assign page = 'product' %}{% include page %}", - v!({"product": { "title": "Draft 151cm" }}), - liquid()); - - assert_template_result!("Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", - v!({"foo": { "title": "Draft 151cm" }}), - liquid()); + assert_template_result!( + "from TestFileSystem", + "{% assign page = 'pick_a_source' %}{% include page %}", + v!({}), + liquid() + ); + + assert_template_result!( + "Product: Draft 151cm ", + "{% assign page = 'product' %}{% include page %}", + v!({"product": { "title": "Draft 151cm" }}), + liquid() + ); + + assert_template_result!( + "Product: Draft 151cm ", + "{% assign page = 'product' %}{% include page for foo %}", + v!({"foo": { "title": "Draft 151cm" }}), + liquid() + ); } #[test] diff --git a/tests/conformance_ruby/tags/standard_tag_test.rs b/tests/conformance_ruby/tags/standard_tag_test.rs index 3a9f20b38..564fa4779 100644 --- a/tests/conformance_ruby/tags/standard_tag_test.rs +++ b/tests/conformance_ruby/tags/standard_tag_test.rs @@ -2,8 +2,10 @@ use liquid; #[test] fn test_no_transform() { - assert_template_result!("this text should come out of the template without change...", - "this text should come out of the template without change..."); + assert_template_result!( + "this text should come out of the template without change...", + "this text should come out of the template without change..." + ); assert_template_result!("blah", "blah"); assert_template_result!("", ""); @@ -18,8 +20,10 @@ fn test_no_transform() { #[test] #[ignore] fn test_has_a_block_which_does_nothing() { - assert_template_result!("the comment block should be removed .. right?", - "the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?"); + assert_template_result!( + "the comment block should be removed .. right?", + "the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?" + ); assert_template_result!("", "{%comment%}{%endcomment%}"); assert_template_result!("", "{%comment%}{% endcomment %}"); @@ -27,7 +31,10 @@ fn test_has_a_block_which_does_nothing() { assert_template_result!("", "{% comment %}{% endcomment %}"); assert_template_result!("", "{%comment%}comment{%endcomment%}"); assert_template_result!("", "{% comment %}comment{% endcomment %}"); - assert_template_result!("", "{% comment %} 1 {% comment %} 2 {% endcomment %} 3 {% endcomment %}"); + assert_template_result!( + "", + "{% comment %} 1 {% comment %} 2 {% endcomment %} 3 {% endcomment %}" + ); assert_template_result!("", "{%comment%}{%blabla%}{%endcomment%}"); assert_template_result!("", "{% comment %}{% blabla %}{% endcomment %}"); @@ -44,137 +51,209 @@ fn test_has_a_block_which_does_nothing() { assert_template_result!("foo bar", "foo {%comment%}comment{%endcomment%} bar"); assert_template_result!("foo bar", "foo {%comment%} comment {%endcomment%} bar"); - assert_template_result!("foobar", r#"foo{%comment%} - {%endcomment%}bar"#); + assert_template_result!( + "foobar", + r#"foo{%comment%} + {%endcomment%}bar"# + ); } #[test] fn test_hyphenated_assign() { let assigns = v!({ "a-b": "1" }); - assert_template_result!("a-b:1 a-b:2", "a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}", assigns); + assert_template_result!( + "a-b:1 a-b:2", + "a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}", + assigns + ); } #[test] fn test_assign_with_colon_and_spaces() { let assigns = v!({ "var": { "a:b c": { "paged": "1" } } }); - assert_template_result!("var2: 1", r#"{%assign var2 = var["a:b c"].paged %}var2: {{var2}}"#, assigns); + assert_template_result!( + "var2: 1", + r#"{%assign var2 = var["a:b c"].paged %}var2: {{var2}}"#, + assigns + ); } #[test] fn test_capture() { // Implementation specific: strict_variables is enabled, testing that instead. let assigns = v!({ "var": "content" }); - assert_template_result!("content foo content foo ", - "{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}", - assigns); + assert_template_result!( + "content foo content foo ", + "{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}", + assigns + ); } #[test] fn test_capture_detects_bad_syntax() { // Implementation specific: strict_variables is enabled, testing that instead. - assert_parse_error!( - "{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}"); + assert_parse_error!("{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}"); } #[test] fn test_case() { let assigns = v!({ "condition": 2 }); - assert_template_result!(" its 2 ", - "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", - assigns); + assert_template_result!( + " its 2 ", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns + ); let assigns = v!({ "condition": 1 }); - assert_template_result!(" its 1 ", - "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", - assigns); + assert_template_result!( + " its 1 ", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns + ); let assigns = v!({ "condition": 3 }); - assert_template_result!("", - "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", - assigns); + assert_template_result!( + "", + "{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}", + assigns + ); let assigns = v!({ "condition": "string here" }); - assert_template_result!(" hit ", - r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, - assigns); + assert_template_result!( + " hit ", + r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, + assigns + ); let assigns = v!({ "condition": "bad string here" }); - assert_template_result!("", - r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, - assigns); + assert_template_result!( + "", + r#"{% case condition %}{% when "string here" %} hit {% endcase %}"#, + assigns + ); } #[test] fn test_case_with_else() { let assigns = v!({ "condition": 5 }); - assert_template_result!(" hit ", - "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", - assigns); + assert_template_result!( + " hit ", + "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", + assigns + ); let assigns = v!({ "condition": 6 }); - assert_template_result!(" else ", - "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", - assigns); + assert_template_result!( + " else ", + "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", + assigns + ); let assigns = v!({ "condition": 6 }); - assert_template_result!(" else ", - "{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}", - assigns); + assert_template_result!( + " else ", + "{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}", + assigns + ); } #[test] #[ignore] fn test_case_on_size() { - assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": []})); - assert_template_result!("1", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1]})); - assert_template_result!("2", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1]})); - assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1]})); - assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1, 1]})); - assert_template_result!("", "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", v!({"a": [1, 1, 1, 1, 1]})); + assert_template_result!( + "", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": []}) + ); + assert_template_result!( + "1", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": [1]}) + ); + assert_template_result!( + "2", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": [1, 1]}) + ); + assert_template_result!( + "", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": [1, 1, 1]}) + ); + assert_template_result!( + "", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": [1, 1, 1, 1]}) + ); + assert_template_result!( + "", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}", + v!({"a": [1, 1, 1, 1, 1]}) + ); } #[test] #[ignore] fn test_case_on_size_with_else() { - assert_template_result!("else", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": []})); - - assert_template_result!("1", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": [1]})); - - assert_template_result!("2", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": [1, 1]})); - - assert_template_result!("else", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": [1, 1, 1]})); - - assert_template_result!("else", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": [1, 1, 1, 1]})); - - assert_template_result!("else", - "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", - v!({"a": [1, 1, 1, 1, 1]})); + assert_template_result!( + "else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": []}) + ); + + assert_template_result!( + "1", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1]}) + ); + + assert_template_result!( + "2", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1]}) + ); + + assert_template_result!( + "else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1]}) + ); + + assert_template_result!( + "else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1, 1]}) + ); + + assert_template_result!( + "else", + "{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}", + v!({"a": [1, 1, 1, 1, 1]}) + ); } #[test] #[ignore] fn test_case_on_length_with_else() { - assert_template_result!("else", - "{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); - - assert_template_result!("false", - "{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); - - assert_template_result!("true", - "{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); - - assert_template_result!("else", - "{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}"); + assert_template_result!( + "else", + "{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}" + ); + + assert_template_result!( + "false", + "{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}" + ); + + assert_template_result!( + "true", + "{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}" + ); + + assert_template_result!( + "else", + "{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}" + ); } #[test] @@ -186,11 +265,42 @@ fn test_assign_from_case() { .parse(code) .unwrap(); - assert_eq!("menswear", template.render(v!({"collection": { "handle": "menswear-jackets" }}).as_object().unwrap()).unwrap()); - assert_eq!("menswear", template.render(v!({"collection": { "handle": "menswear-t-shirts" }}).as_object().unwrap()).unwrap()); - assert_eq!("womenswear", template.render(v!({"collection": { "handle": "x" }}).as_object().unwrap()).unwrap()); - assert_eq!("womenswear", template.render(v!({"collection": { "handle": "y" }}).as_object().unwrap()).unwrap()); - assert_eq!("womenswear", template.render(v!({"collection": { "handle": "z" }}).as_object().unwrap()).unwrap()); + assert_eq!( + "menswear", + template + .render( + v!({"collection": { "handle": "menswear-jackets" }}) + .as_object() + .unwrap() + ).unwrap() + ); + assert_eq!( + "menswear", + template + .render( + v!({"collection": { "handle": "menswear-t-shirts" }}) + .as_object() + .unwrap() + ).unwrap() + ); + assert_eq!( + "womenswear", + template + .render(v!({"collection": { "handle": "x" }}).as_object().unwrap()) + .unwrap() + ); + assert_eq!( + "womenswear", + template + .render(v!({"collection": { "handle": "y" }}).as_object().unwrap()) + .unwrap() + ); + assert_eq!( + "womenswear", + template + .render(v!({"collection": { "handle": "z" }}).as_object().unwrap()) + .unwrap() + ); } #[test] @@ -213,7 +323,8 @@ fn test_case_when_or() { #[test] #[ignore] fn test_case_when_comma() { - let code = "{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"; + let code = + "{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}"; assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 1 })); assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 2 })); assert_template_result!(" its 1 or 2 or 3 ", code, v!({ "condition": 3 })); @@ -246,7 +357,10 @@ fn test_assign_an_empty_string() { #[test] fn test_assign_is_global() { - assert_template_result!("variable", r#"{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}"#); + assert_template_result!( + "variable", + r#"{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}"# + ); } #[test] @@ -259,10 +373,16 @@ fn test_case_detects_bad_syntax() { #[test] fn test_cycle() { assert_template_result!("one", r#"{%cycle "one", "two"%}"#); - assert_template_result!("one two", r#"{%cycle "one", "two"%} {%cycle "one", "two"%}"#); + assert_template_result!( + "one two", + r#"{%cycle "one", "two"%} {%cycle "one", "two"%}"# + ); assert_template_result!(" two", r#"{%cycle "", "two"%} {%cycle "", "two"%}"#); - assert_template_result!("one two one", r#"{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}"#); + assert_template_result!( + "one two one", + r#"{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}"# + ); assert_template_result!("text-align: left text-align: right", r#"{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}"#); @@ -291,14 +411,22 @@ fn test_multiple_named_cycles_with_names_from_context() { #[ignore] fn test_size_of_array() { let assigns = v!({ "array": [1, 2, 3, 4] }); - assert_template_result!("array has 4 elements", "array has {{ array.size }} elements", assigns); + assert_template_result!( + "array has 4 elements", + "array has {{ array.size }} elements", + assigns + ); } #[test] #[ignore] fn test_size_of_hash() { let assigns = v!({ "hash": { "a": 1, "b": 2, "c": 3, "d": 4 } }); - assert_template_result!("hash has 4 elements", "hash has {{ hash.size }} elements", assigns); + assert_template_result!( + "hash has 4 elements", + "hash has {{ hash.size }} elements", + assigns + ); } #[test] @@ -313,14 +441,25 @@ fn test_illegal_symbols() { #[test] fn test_ifchanged() { let assigns = v!({ "array": [ 1, 1, 2, 2, 3, 3] }); - assert_template_result!("123", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); + assert_template_result!( + "123", + "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", + assigns + ); let assigns = v!({ "array": [ 1, 1, 1, 1] }); - assert_template_result!("1", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); + assert_template_result!( + "1", + "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", + assigns + ); } #[test] #[ignore] fn test_multiline_tag() { - assert_template_result!("0 1 2 3", "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}"); + assert_template_result!( + "0 1 2 3", + "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}" + ); } diff --git a/tests/conformance_ruby/tags/statements_test.rs b/tests/conformance_ruby/tags/statements_test.rs index 14fb37f13..47560ce0a 100644 --- a/tests/conformance_ruby/tags/statements_test.rs +++ b/tests/conformance_ruby/tags/statements_test.rs @@ -113,10 +113,10 @@ fn test_is_not_collection_empty() { #[ignore] fn test_nil() { let text = " {% if var == nil %} true {% else %} false {% endif %} "; - assert_template_result!(" true ", text, v!({"var": nil})); + assert_template_result!(" true ", text, v!({ "var": nil })); let text = " {% if var == null %} true {% else %} false {% endif %} "; - assert_template_result!(" true ", text, v!({"var": nil})); + assert_template_result!(" true ", text, v!({ "var": nil })); } #[test] diff --git a/tests/conformance_ruby/tags/unless_else_tag_test.rs b/tests/conformance_ruby/tags/unless_else_tag_test.rs index 2b8d3af4c..cc9ab93a5 100644 --- a/tests/conformance_ruby/tags/unless_else_tag_test.rs +++ b/tests/conformance_ruby/tags/unless_else_tag_test.rs @@ -1,22 +1,43 @@ #[test] fn test_unless() { - assert_template_result!(" ", " {% unless true %} this text should not go into the output {% endunless %} "); - assert_template_result!(" this text should go into the output ", - " {% unless false %} this text should go into the output {% endunless %} "); - assert_template_result!(" you rock ?", "{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?"); + assert_template_result!( + " ", + " {% unless true %} this text should not go into the output {% endunless %} " + ); + assert_template_result!( + " this text should go into the output ", + " {% unless false %} this text should go into the output {% endunless %} " + ); + assert_template_result!( + " you rock ?", + "{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?" + ); } #[test] #[ignore] fn test_unless_else() { - assert_template_result!(" YES ", "{% unless true %} NO {% else %} YES {% endunless %}"); - assert_template_result!(" YES ", "{% unless false %} YES {% else %} NO {% endunless %}"); - assert_template_result!(" YES ", r#"{% unless "foo" %} NO {% else %} YES {% endunless %}"#); + assert_template_result!( + " YES ", + "{% unless true %} NO {% else %} YES {% endunless %}" + ); + assert_template_result!( + " YES ", + "{% unless false %} YES {% else %} NO {% endunless %}" + ); + assert_template_result!( + " YES ", + r#"{% unless "foo" %} NO {% else %} YES {% endunless %}"# + ); } #[test] fn test_unless_in_loop() { - assert_template_result!("23", "{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}", v!({"choices": [1, nil, false]})); + assert_template_result!( + "23", + "{% for i in choices %}{% unless i %}{{ forloop.index }}{% endunless %}{% endfor %}", + v!({"choices": [1, nil, false]}) + ); } #[test] diff --git a/tests/test_helper.rs b/tests/test_helper.rs index 7387bbe1c..4213c2fd1 100644 --- a/tests/test_helper.rs +++ b/tests/test_helper.rs @@ -25,9 +25,7 @@ macro_rules! assert_template_result { assert_template_result!($expected, $template, $assigns, $liquid); }; ($expected:expr, $template:expr, $assigns: expr, $liquid: expr) => { - let template = $liquid - .parse($template.as_ref()) - .unwrap(); + let template = $liquid.parse($template.as_ref()).unwrap(); let rendered = template.render($assigns.as_object().unwrap()).unwrap(); assert_eq!($expected, rendered); };