tilt-handlebars 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +12 -0
  3. data/.github/workflows/ci.yml +76 -0
  4. data/.github/workflows/publish.yml +58 -0
  5. data/.gitignore +14 -15
  6. data/.rspec +1 -0
  7. data/.rubocop.yml +189 -0
  8. data/Appraisals +9 -0
  9. data/CONTRIBUTING.md +75 -0
  10. data/Gemfile +3 -1
  11. data/LICENSE +22 -0
  12. data/README.md +34 -19
  13. data/RELEASE_NOTES.md +50 -8
  14. data/Rakefile +4 -8
  15. data/bin/audit +8 -0
  16. data/bin/build +8 -0
  17. data/bin/bundle +114 -0
  18. data/bin/bundler-audit +29 -0
  19. data/bin/console +15 -0
  20. data/bin/doc +7 -0
  21. data/bin/lint +8 -0
  22. data/bin/rake +29 -0
  23. data/bin/release +25 -0
  24. data/bin/rspec +29 -0
  25. data/bin/rubocop +29 -0
  26. data/bin/setup +13 -0
  27. data/bin/tag +25 -0
  28. data/bin/test +7 -0
  29. data/bin/version +24 -0
  30. data/gemfiles/tilt_1.gemfile +7 -0
  31. data/gemfiles/tilt_2.gemfile +7 -0
  32. data/lib/sinatra/handlebars.rb +10 -6
  33. data/lib/tilt/handlebars/version.rb +3 -1
  34. data/lib/tilt/handlebars.rb +57 -46
  35. data/lib/tilt-handlebars.rb +3 -0
  36. data/spec/fixtures/helpers.rb +21 -0
  37. data/spec/fixtures/views/hello.hbs +1 -0
  38. data/spec/fixtures/views/hello_missing.hbs +1 -0
  39. data/spec/fixtures/views/hello_partial.hbs +1 -0
  40. data/spec/fixtures/views/hello_partial2.hbs +1 -0
  41. data/spec/fixtures/views/partial.hbs +1 -0
  42. data/spec/fixtures/views/partial2.handlebars +1 -0
  43. data/spec/sinatra/handlebars_spec.rb +47 -0
  44. data/spec/spec_coverage.rb +19 -0
  45. data/spec/spec_helper.rb +23 -0
  46. data/spec/tilt/handlebars_template_spec.rb +543 -0
  47. data/spec/tilt_spec.rb +40 -0
  48. data/tasks/doc.rake +8 -0
  49. data/tasks/gem.rake +7 -0
  50. data/tasks/lint.rake +7 -0
  51. data/tasks/test.rake +10 -0
  52. data/tilt-handlebars.gemspec +48 -23
  53. metadata +261 -65
  54. data/.travis.yml +0 -6
  55. data/LICENSE.txt +0 -22
  56. data/test/fixtures/views/hello.hbs +0 -1
  57. data/test/fixtures/views/missing_partial.hbs +0 -1
  58. data/test/fixtures/views/partial.hbs +0 -1
  59. data/test/fixtures/views/partial2.handlebars +0 -1
  60. data/test/fixtures/views/partial_test.hbs +0 -1
  61. data/test/fixtures/views/partial_test2.handlebars +0 -1
  62. data/test/fixtures/views/two_partials.hbs +0 -1
  63. data/test/sinatra_test.rb +0 -38
  64. data/test/test_helper.rb +0 -9
  65. data/test/tilt_handlebarstemplate_test.rb +0 -276
@@ -1,276 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'tilt'
4
- require 'tilt/handlebars'
5
-
6
- def make_template(text)
7
- Tilt::HandlebarsTemplate.new { |t| text }
8
- end
9
-
10
- describe Tilt::HandlebarsTemplate do
11
- it 'renders from file' do
12
- template = Tilt.new('test/fixtures/views/hello.hbs')
13
- template.render(nil, name: 'Joe', emotion: 'happy').must_equal "Hello, Joe. I'm happy to meet you.\n"
14
- end
15
-
16
- it 'is registered for .hbs files' do
17
- Tilt['test.hbs'].must_equal Tilt::HandlebarsTemplate
18
- end
19
-
20
- it 'is registered for .handlebars files' do
21
- Tilt['test.handlebars'].must_equal Tilt::HandlebarsTemplate
22
- end
23
-
24
- it 'renders static template' do
25
- template = make_template "Hello World!"
26
- template.render.must_equal "Hello World!"
27
- end
28
-
29
- it "can be rendered more than once" do
30
- template = make_template "Hello World!"
31
- 3.times { template.render.must_equal "Hello World!" }
32
- end
33
-
34
- it "substitutes locals in template" do
35
- template = make_template "Hey {{ name }}!"
36
- template.render(nil, :name => 'Joe').must_equal "Hey Joe!"
37
- end
38
-
39
- it "displays nested properties" do
40
- template = make_template "Hey {{ person.name }}!"
41
- template.render(nil, :person => { :name => 'Joe' }).must_equal "Hey Joe!"
42
- end
43
-
44
- it "displays text from block" do
45
- template = make_template "Deck the halls. Fa {{ yield }}."
46
- rendered = template.render(nil) { "la la la la"}
47
- rendered.must_equal "Deck the halls. Fa la la la la."
48
- end
49
-
50
-
51
- describe "arrays" do
52
- shopping_list = %w[milk eggs flour sugar]
53
-
54
- it "displays element from array" do
55
- template = make_template "Don't forget the {{ shopping_list.[1] }}!"
56
- template.render(nil, :shopping_list => shopping_list).must_equal "Don't forget the eggs!"
57
- end
58
-
59
- it "displays elements in an array in a loop" do
60
- template = make_template 'Items to buy:{{#each shopping_list}} {{ this }}{{/each}}'
61
- template.render(nil, :shopping_list => shopping_list).must_equal "Items to buy: milk eggs flour sugar"
62
- end
63
-
64
- it "displays alternate text when array is empty" do
65
- template = make_template 'Items to buy:{{#each shopping_list}} {{ this }}{{else}} All done!{{/each}}'
66
- template.render(nil, :shopping_list => []).must_equal "Items to buy: All done!"
67
- end
68
- end
69
-
70
- describe "conditionals" do
71
- template = make_template '{{#if morning}}Good morning{{else}}Hello{{/if}}, {{ name }}'
72
-
73
- it "displays text if value is true" do
74
- template.render(nil, :name => 'Joe', :morning => true).must_equal "Good morning, Joe"
75
- end
76
-
77
- it "displays alternate text if value is false" do
78
- template.render(nil, :name => 'Joe', :morning => false).must_equal "Hello, Joe"
79
- end
80
-
81
- it "displays alternate text if value is missing" do
82
- template.render(nil, :name => 'Joe').must_equal "Hello, Joe"
83
- end
84
- end
85
-
86
- describe "unless expressions" do
87
- template = make_template 'Hello, {{ name }}.{{#unless weekend}} Time to go to work.{{/unless}}'
88
-
89
- it "displays text if value is false" do
90
- template.render(nil, :name => 'Joe', :weekend => false).must_equal "Hello, Joe. Time to go to work."
91
- end
92
-
93
- it "does not display text if value is true" do
94
- template.render(nil, :name => 'Joe', :weekend => true).must_equal "Hello, Joe."
95
- end
96
-
97
- it "displays text if value is missing" do
98
- template.render(nil, :name => 'Joe').must_equal "Hello, Joe. Time to go to work."
99
- end
100
- end
101
-
102
- describe "escape HTML" do
103
- it "escapes HTML characters" do
104
- template = make_template "Hey {{ name }}!"
105
- template.render(nil, :name => '<b>Joe</b>' ).must_equal "Hey &lt;b&gt;Joe&lt;/b&gt;!"
106
- end
107
-
108
- it "does not escape HTML characters in triple-stash" do
109
- template = make_template "Hey {{{ name }}}!"
110
- template.render(nil, :name => '<b>Joe</b>' ).must_equal "Hey <b>Joe</b>!"
111
- end
112
- end
113
-
114
- describe "helpers" do
115
-
116
- it "applies simple helper to value" do
117
- template = make_template '{{upper greeting}}, world'
118
- template.register_helper(:upper) do |this, *args|
119
- "#{args[0].upcase}"
120
- end
121
-
122
- template.render(nil, :greeting => 'hello').must_equal "HELLO, world"
123
- end
124
-
125
- it "applies simple helper to quoted value" do
126
- template = make_template '{{upper "greetings and salutations"}}, world'
127
- template.register_helper(:upper) do |this, *args|
128
- "#{args[0].upcase}"
129
- end
130
-
131
- template.render.must_equal "GREETINGS AND SALUTATIONS, world"
132
- end
133
-
134
- it "applies block helper to static text" do
135
- template = make_template '{{#upper}}Hello, World.{{/upper}}'
136
- template.register_helper(:upper) do |this, block|
137
- block.fn(this).upcase
138
- end
139
-
140
- template.render.must_equal "HELLO, WORLD."
141
- end
142
-
143
- it "applies block helper to nested values" do
144
- template = make_template '{{#upper}}Hey {{name}}{{/upper}}!'
145
- template.register_helper(:upper) do |this, block|
146
- block.fn(this).upcase
147
- end
148
-
149
- template.render(nil, :name => 'Joe').must_equal "HEY JOE!"
150
- end
151
-
152
- it "displays properties from object using 'with' helper" do
153
- template = make_template '{{#with person}}Hello, {{ first_name }} {{ last_name }}{{/with}}'
154
- joe = Person.new "Joe", "Blow"
155
- template.render(nil, :person => joe).must_equal "Hello, Joe Blow"
156
- end
157
-
158
- end
159
-
160
- describe "using scope object to render template" do
161
- class Person
162
- attr_reader :first_name, :last_name
163
-
164
- def initialize(first_name, last_name)
165
- @first_name = first_name
166
- @last_name = last_name
167
- end
168
- end
169
-
170
- class HashPerson
171
- attr_reader :first_name, :last_name
172
-
173
- def initialize(first_name, last_name)
174
- @first_name = first_name
175
- @last_name = last_name
176
- end
177
-
178
- def to_h
179
- { first_name: @first_name, last_name: @last_name }
180
- end
181
- end
182
-
183
- joe = Person.new "John", "Doe"
184
-
185
- it "displays properties of object passed as scope" do
186
- template = make_template "Hello, {{ first_name }} {{ last_name }}."
187
- template.render(joe).must_equal "Hello, John Doe."
188
- end
189
-
190
- it "merges scope object properties with locals" do
191
- template = make_template "{{ greeting }}, {{ first_name }} {{ last_name }}."
192
- template.render(joe, :greeting => "Salut").must_equal "Salut, John Doe."
193
- end
194
-
195
- it "prefers locals over scope object properties with same name" do
196
- template = make_template "Hello, {{ first_name }} {{ last_name }}."
197
- template.render(joe, :first_name => "Jane").must_equal "Hello, Jane Doe."
198
- end
199
-
200
- it "prefers locals over scope object properties with same name when object defines to_h" do
201
- joe_hash = HashPerson.new "John", "Doe"
202
-
203
- template = make_template "Hello, {{ first_name }} {{ last_name }}."
204
- template.render(joe_hash, :first_name => "Jane").must_equal "Hello, Jane Doe."
205
- end
206
- end
207
-
208
- describe "comments" do
209
- it "does not render comments" do
210
- template = make_template "Hello world{{! what a wonderful world }}"
211
- template.render.must_equal "Hello world"
212
- end
213
-
214
- it "does not render comments, alternative syntax" do
215
- template = make_template "Hello world{{!-- what a wonderful world --}}"
216
- template.render.must_equal "Hello world"
217
- end
218
- end
219
-
220
- describe "partials" do
221
- it "looks for partial relative to the template file" do
222
- template = Tilt.new('test/fixtures/views/partial_test.hbs')
223
- template.render(nil, :author => "Stephanie Queen").must_equal "My all time favorite book is It Came From the Partial Side by Stephanie Queen."
224
- end
225
-
226
- it "can load partial from absolute path" do
227
- dir = Dir.pwd
228
- template = make_template "Have you read {{> \"#{dir}/test/fixtures/views/partial\" }}?"
229
- template.render(nil, :author => "Stephanie Queen").must_equal "Have you read It Came From the Partial Side by Stephanie Queen?"
230
- end
231
-
232
- it "also recognizes .handlebars extension" do
233
- template = Tilt.new('test/fixtures/views/partial_test2.handlebars')
234
- template.render(nil, :author => "Stephanie Queen").must_equal "My all time favorite book is It Came From the Partial Side by Stephanie Queen."
235
- end
236
-
237
- it "can load relative partial along with registered partial" do
238
- template = Tilt.new('test/fixtures/views/two_partials.hbs')
239
- template.register_partial :director, "Gary Rockhammer"
240
- template.render(nil, :author => "Stephanie Queen").must_equal "It Came From the Partial Side by Stephanie Queen may be a good book, but I'm waiting for the movie directed by Gary Rockhammer."
241
- end
242
-
243
- it "gives precedence to registered partial over relative file" do
244
- template = Tilt.new('test/fixtures/views/partial_test.hbs')
245
- template.register_partial :partial, "Revenge of the Partial"
246
- template.render.must_equal "My all time favorite book is Revenge of the Partial."
247
- end
248
-
249
- it "raises error if partial cannot be found" do
250
- template = Tilt.new('test/fixtures/views/missing_partial.hbs')
251
- # template.render
252
- proc { template.render }.must_raise V8::Error
253
- end
254
-
255
- it "cannot automatically load partial when template is created from string instead of file" do
256
- template = make_template "I wish I could load a partial like this: {{> my_partial}}."
257
- proc { template.render }.must_raise V8::Error
258
- end
259
-
260
- it "allows partial to be registered" do
261
- template = make_template "{{> greeting}}. Nice to meet you."
262
- template.register_partial :greeting, "Hey, {{name}}"
263
- template.render(nil, :name => "Joe").must_equal "Hey, Joe. Nice to meet you."
264
- end
265
-
266
- it "calls registered method if partial is missing" do
267
- template = make_template "{{> where}} I've been looking for you."
268
- template.partial_missing do |partial_name|
269
- "Where have you been, {{name}}?" if partial_name == 'where'
270
- end
271
- template.render(nil, :name => "Joe").must_equal "Where have you been, Joe? I've been looking for you."
272
- end
273
- end
274
- end
275
-
276
-