|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class HybridTagUnitTest < Minitest::Test |
| 6 | + include Liquid |
| 7 | + |
| 8 | + class TestHybridTag < Liquid::HybridTag |
| 9 | + def blank? |
| 10 | + true |
| 11 | + end |
| 12 | + |
| 13 | + private |
| 14 | + |
| 15 | + def render_self_closing_to_output_buffer(_context, output) |
| 16 | + output << "self-closing" |
| 17 | + end |
| 18 | + |
| 19 | + def render_block_form_to_output_buffer(context, output) |
| 20 | + output << "block[" |
| 21 | + @body.render_to_output_buffer(context, output) |
| 22 | + output << "]" |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def setup |
| 27 | + @environment = Liquid::Environment.build do |env| |
| 28 | + env.register_tag("hybrid", TestHybridTag) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def test_hybrid_tag_is_subclass_of_block |
| 33 | + assert(TestHybridTag < Liquid::Block) |
| 34 | + end |
| 35 | + |
| 36 | + def test_self_closing_renders_correctly |
| 37 | + template = Liquid::Template.parse("{% hybrid %}", environment: @environment) |
| 38 | + assert_equal("self-closing", template.render) |
| 39 | + end |
| 40 | + |
| 41 | + def test_self_closing_block_form_predicate_is_false |
| 42 | + tag = parse_hybrid_tag("{% hybrid %}") |
| 43 | + refute(tag.block_form?) |
| 44 | + end |
| 45 | + |
| 46 | + def test_self_closing_does_not_consume_subsequent_tokens |
| 47 | + template = Liquid::Template.parse("{% hybrid %}after", environment: @environment) |
| 48 | + assert_equal("self-closingafter", template.render) |
| 49 | + end |
| 50 | + |
| 51 | + def test_block_form_renders_correctly |
| 52 | + template = Liquid::Template.parse("{% hybrid %}body{% endhybrid %}", environment: @environment) |
| 53 | + assert_equal("block[body]", template.render) |
| 54 | + end |
| 55 | + |
| 56 | + def test_block_form_predicate_is_true |
| 57 | + tag = parse_hybrid_tag("{% hybrid %}body{% endhybrid %}") |
| 58 | + assert(tag.block_form?) |
| 59 | + end |
| 60 | + |
| 61 | + def test_block_form_body_accessible_via_nodelist |
| 62 | + tag = parse_hybrid_tag("{% hybrid %}hello world{% endhybrid %}") |
| 63 | + assert(tag.block_form?) |
| 64 | + refute_empty(tag.nodelist) |
| 65 | + assert_equal("hello world", tag.nodelist.map(&:to_s).join) |
| 66 | + end |
| 67 | + |
| 68 | + def test_empty_block_form |
| 69 | + template = Liquid::Template.parse("{% hybrid %}{% endhybrid %}", environment: @environment) |
| 70 | + assert_equal("block[]", template.render) |
| 71 | + end |
| 72 | + |
| 73 | + def test_block_form_with_liquid_content |
| 74 | + template = Liquid::Template.parse( |
| 75 | + "{% hybrid %}before{{ var }}after{% endhybrid %}", |
| 76 | + environment: @environment, |
| 77 | + ) |
| 78 | + assert_equal("block[beforeVafter]", template.render({ "var" => "V" })) |
| 79 | + end |
| 80 | + |
| 81 | + def test_sequential_block_forms |
| 82 | + template = Liquid::Template.parse( |
| 83 | + "{% hybrid %}a{% endhybrid %}{% hybrid %}b{% endhybrid %}", |
| 84 | + environment: @environment, |
| 85 | + ) |
| 86 | + assert_equal("block[a]block[b]", template.render) |
| 87 | + end |
| 88 | + |
| 89 | + def test_self_closing_followed_by_block_form |
| 90 | + template = Liquid::Template.parse( |
| 91 | + "{% hybrid %}{% hybrid %}body{% endhybrid %}", |
| 92 | + environment: @environment, |
| 93 | + ) |
| 94 | + assert_equal("self-closingblock[body]", template.render) |
| 95 | + end |
| 96 | + |
| 97 | + def test_block_form_followed_by_self_closing |
| 98 | + template = Liquid::Template.parse( |
| 99 | + "{% hybrid %}body{% endhybrid %}{% hybrid %}", |
| 100 | + environment: @environment, |
| 101 | + ) |
| 102 | + assert_equal("block[body]self-closing", template.render) |
| 103 | + end |
| 104 | + |
| 105 | + def test_mixed_forms |
| 106 | + template = Liquid::Template.parse( |
| 107 | + "{% hybrid %}{% hybrid %}body{% endhybrid %}{% hybrid %}", |
| 108 | + environment: @environment, |
| 109 | + ) |
| 110 | + assert_equal("self-closingblock[body]self-closing", template.render) |
| 111 | + end |
| 112 | + |
| 113 | + def test_self_closing_inside_block_tag |
| 114 | + template = Liquid::Template.parse( |
| 115 | + "{% if true %}{% hybrid %}{% endif %}", |
| 116 | + environment: @environment, |
| 117 | + ) |
| 118 | + assert_equal("self-closing", template.render) |
| 119 | + end |
| 120 | + |
| 121 | + def test_block_form_inside_block_tag |
| 122 | + template = Liquid::Template.parse( |
| 123 | + "{% if true %}{% hybrid %}body{% endhybrid %}{% endif %}", |
| 124 | + environment: @environment, |
| 125 | + ) |
| 126 | + assert_equal("block[body]", template.render) |
| 127 | + end |
| 128 | + |
| 129 | + def test_nested_same_type_raises_syntax_error |
| 130 | + error = assert_raises(Liquid::SyntaxError) do |
| 131 | + Liquid::Template.parse( |
| 132 | + "{% hybrid %}{% hybrid %}inner{% endhybrid %}{% endhybrid %}", |
| 133 | + environment: @environment, |
| 134 | + ) |
| 135 | + end |
| 136 | + assert_match(/cannot be nested/, error.message) |
| 137 | + end |
| 138 | + |
| 139 | + def test_orphan_end_tag_raises_syntax_error |
| 140 | + error = assert_raises(Liquid::SyntaxError) do |
| 141 | + Liquid::Template.parse( |
| 142 | + "{% endhybrid %}", |
| 143 | + environment: @environment, |
| 144 | + ) |
| 145 | + end |
| 146 | + assert_match(/no matching/, error.message) |
| 147 | + end |
| 148 | + |
| 149 | + private |
| 150 | + |
| 151 | + def parse_hybrid_tag(source) |
| 152 | + template = Liquid::Template.parse(source, environment: @environment) |
| 153 | + template.root.nodelist.find { |node| node.is_a?(TestHybridTag) } |
| 154 | + end |
| 155 | +end |
0 commit comments