File: output_test.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (125 lines) | stat: -rw-r--r-- 3,383 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true

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}</#{tag}>)
  end

  def paragraph(input)
    "<p>#{input}</p>"
  end

  def link_to(name, url)
    %(<a href="#{url}">#{name}</a>)
  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 = %( <span id="bar">bad</span> )

    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 = %( <span id="good">bad</span> )

    assert_equal(expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]))
  end

  def test_multiple_pipings
    text     = %( {{ best_cars | cite_funny | paragraph }} )
    expected = %( <p>LOL: bmw</p> )

    assert_equal(expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]))
  end

  def test_link_to
    text     = %( {{ 'Typo' | link_to: 'http://typo.leetsoft.com' }} )
    expected = %( <a href="http://typo.leetsoft.com">Typo</a> )

    assert_equal(expected, Template.parse(text).render!(@assigns, filters: [FunnyFilter]))
  end
end # OutputTest