tobi / liquid

Liquid markup language. Save, customer facing template language for flexible web apps.

This URL has Read+Write access

liquid / test / output_test.rb
100644 121 lines (86 sloc) 3.171 kb
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
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/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 < Test::Unit::TestCase
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
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_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