-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhandlebars_spec.rb
More file actions
273 lines (218 loc) · 9.21 KB
/
Copy pathhandlebars_spec.rb
File metadata and controls
273 lines (218 loc) · 9.21 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require_relative 'spec_helper'
require_relative '../lib/ruby-handlebars'
require_relative '../lib/ruby-handlebars/escapers/dummy_escaper'
describe Handlebars::Handlebars do
let(:hbs) {Handlebars::Handlebars.new}
def evaluate(template, args = {}, **compiling_options)
hbs.compile(template, **compiling_options).call(args)
end
context 'evaluating' do
it 'a dummy template' do
expect(evaluate('My simple template')).to eq('My simple template')
end
it 'a simple replacement' do
expect(evaluate('Hello {{name}}', {name: 'world'})).to eq('Hello world')
end
it 'a double braces replacement with unsafe characters' do
expect(evaluate('Hello {{name}}', {name: '<"\'>&'})).to eq('Hello <"'>&')
end
it 'a double braces replacement with nil' do
expect(evaluate('Hello {{name}}', {name: nil})).to eq('Hello ')
end
it 'a triple braces replacement with unsafe characters' do
expect(evaluate('Hello {{{name}}}', {name: '<"\'>&'})).to eq('Hello <"\'>&')
end
it 'allows values specified by methods' do
expect(evaluate('Hello {{name}}', double(name: 'world'))).to eq('Hello world')
end
it 'prefers hash value over method value' do
expect(evaluate('Hello {{name}}', double(name: 'world', '[]': 'dog', has_key?: true))).to eq('Hello dog')
end
it 'handles object that implement #[] but not #has_key?' do
expect(evaluate('Hello {{name}}', double(name: 'world', '[]': 'dog'))).to eq('Hello world')
end
it 'a replacement with a path' do
expect(evaluate('My simple template: {{person.name}}', {person: {name: 'Another name'}})).to eq('My simple template: Another name')
end
it 'handles a parameter with a dash' do
expect(evaluate('Hello {{first-name}}', double("first-name": 'world'))).to eq('Hello world')
end
context 'strict mode' do
it 'raises an error' do
expect{evaluate('Hello {{you}}', {}, strict: true)}.to raise_error(Handlebars::Context::AttributeNotFoundError)
end
end
context 'partials' do
it 'simple' do
hbs.register_partial('plic', "Plic")
expect(evaluate("Hello {{> plic}}")).to eq("Hello Plic")
end
it 'using a name with a slash' do
hbs.register_partial('parent/plic', "Plic")
expect(evaluate("Hello {{> parent/plic}}")).to eq("Hello Plic")
end
it 'using a name that begins with a slash' do
hbs.register_partial('/parent/plic', "Plic")
expect(evaluate("Hello {{> /parent/plic}}")).to eq("Hello Plic")
end
it 'using context' do
hbs.register_partial('brackets', "[{{name}}]")
expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]")
end
it 'with a string argument' do
hbs.register_partial('with_args', "[{{name}}]")
expect(evaluate("Hello {{> with_args name='jon'}}")).to eq("Hello [jon]")
end
it 'with string arguments' do
hbs.register_partial('with_args', "[{{fname}} {{lname}}]")
expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]")
end
it 'with variables in arguments' do
hbs.register_partial('with_args', "[{{fname}} {{lname}}]")
expect(evaluate("Hello {{> with_args fname='jon' lname=last_name}}", {last_name: 'doe'})).to eq("Hello [jon doe]")
end
it 'with a helper as an argument' do
hbs.register_helper('wrap_parens') {|context, value| "(#{value})"}
hbs.register_partial('with_args', "[{{fname}} {{lname}}]")
expect(evaluate("Hello {{> with_args fname='jon' lname=(wrap_parens 'doe')}}")).to eq("Hello [jon (doe)]")
end
end
context 'helpers' do
it 'without any argument' do
hbs.register_helper('rainbow') {|context| "-"}
expect(evaluate("{{rainbow}}")).to eq("-")
end
it 'with a single argument' do
hbs.register_helper('noah') {|context, value| value.gsub(/a/, '')}
expect(evaluate("{{noah country}}", {country: 'Canada'})).to eq("Cnd")
end
it 'with multiple arguments, including strings' do
hbs.register_helper('add') {|context, left, op, right| "#{left} #{op} #{right}"}
expect(evaluate("{{add left '&' right}}", {left: 'Law', right: 'Order'})).to eq("Law & Order")
expect(evaluate("{{{add left '&' right}}}", {left: 'Law', right: 'Order'})).to eq("Law & Order")
end
it 'with an empty string argument' do
hbs.register_helper('noah') {|context, value| value.to_s.gsub(/a/, '')}
expect(evaluate("hey{{noah ''}}there", {})).to eq("heythere")
end
it 'with helpers as arguments' do
hbs.register_helper('wrap_parens') {|context, value| "(#{value})"}
hbs.register_helper('wrap_dashes') {|context, value| "-#{value}-"}
expect(evaluate('{{wrap_dashes (wrap_parens "hello")}}', {})).to eq("-(hello)-")
expect(evaluate('{{wrap_dashes (wrap_parens world)}}', {world: "world"})).to eq("-(world)-")
end
it 'block' do
hbs.register_helper('comment') do |context, commenter, block|
block.fn(context).split("\n").map do |line|
"#{commenter} #{line}"
end.join("\n")
expect(evaluate([
"{{comment '//'}}",
"Author: {{author.name}}, {{author.company}}",
"Date: {{commit_date}}",
"{{/comment}}"
].join("\n"), {author: {name: 'Vincent', company: 'Hiptest'}, commit_date: 'today'})).to eq([
"// Author: Vincent, Hiptest",
"// Date: today"
].join("\n"))
end
end
it 'block without arguments' do
template = [
"<tr>{{#indent}}",
"{{#each items}}<td>{{{ this }}}</td>",
"{{/each}}",
"{{/indent}}",
"</tr>"
].join("\n")
hbs.register_helper('indent') do |context, block|
block.fn(context).split("\n").map do |line|
" #{line}"
end.join("\n")
end
expect(evaluate(template, {items: ['a', 'b', 'c']})).to eq([
"<tr> ",
" <td>a</td>",
" <td>b</td>",
" <td>c</td>",
"</tr>"
].join("\n"))
end
it 'block parameters can be paths' do
data = {company: {people: ['a', 'b', 'c']}}
expect(evaluate("{{#each company.people}}{{{this}}}{{/each}}", data)).to eq('abc')
end
it 'a else keyword out of a helper will raise an error' do
expect { evaluate('My {{ else }} template') }.to raise_exception(Parslet::ParseFailed)
end
it '"else" can be part of a path' do
expect(evaluate('My {{ something.else }} template', { something: { else: 'awesome' }})).to eq('My awesome template')
end
end
context 'as_helpers' do
it 'can be used to have names parameters inside the block' do
hbs.register_as_helper('test_with') do |context, value, name, block|
context.with_temporary_context(name => value) do
block.fn(context)
end
end
expect(evaluate("{{#test_with name as |duck|}}Duck name is: {{duck}}{{/test_with}}", {name: "Dewey"})).to eq('Duck name is: Dewey')
end
it 'can have multiple "as" parameters' do
hbs.register_as_helper('test_with') do |context, value1, value2, name1, name2, block|
mapping = {}
mapping[name1] = value1
mapping[name2] = value2
context.with_temporary_context(mapping) do
block.fn(context)
end
end
expect(evaluate("{{#test_with name1 name2 as |duck1 duck2|}}Duck names are {{duck1}} and {{duck2}}{{/test_with}}", {name1: "Huey", name2: "Dewey"})).to eq('Duck names are Huey and Dewey')
end
end
end
context 'escaping characters' do
let(:escaper) { nil }
let(:name) { '<"\'>&' }
let(:replacement_escaped) { evaluate('Hello {{ name }}', {name: name}) }
let(:helper_replacement_escaped) {
hbs.register_helper('wrap_parens') {|context, value| "(#{value})"}
evaluate('Hello {{wrap_parens name}}', {name: name})
}
before do
hbs.set_escaper(escaper)
end
context 'default escaper' do
it 'escapes HTML characters in simple replacements' do
expect(replacement_escaped).to eq('Hello <"'>&')
end
it 'escapes HTML characters in helpers' do
expect(helper_replacement_escaped).to eq('Hello (<"'>&)')
end
end
context 'DummyEscaper' do
let(:escaper) { Handlebars::Escapers::DummyEscaper }
it 'escapes nothing' do
expect(replacement_escaped).to eq('Hello <"\'>&')
end
it 'escapes nothing in helpers' do
expect(helper_replacement_escaped).to eq('Hello (<"\'>&)')
end
end
context 'custom escaper' do
class VowelEscaper
def self.escape(value)
value.gsub(/([aeiuo])/, '-\1')
end
end
let(:escaper) { VowelEscaper }
let(:name) { 'Her Serene Highness' }
it 'applies the escaping' do
expect(replacement_escaped).to eq('Hello H-er S-er-en-e H-ighn-ess')
end
it 'applies the escaping in helpers' do
expect(helper_replacement_escaped).to eq('Hello (H-er S-er-en-e H-ighn-ess)')
end
end
end
end