html2fortitude 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,546 @@
1
+ require 'test_helper'
2
+
3
+ class ErbTest < MiniTest::Unit::TestCase
4
+ def test_erb
5
+ assert_equal '- foo = bar', render_erb('<% foo = bar %>')
6
+ assert_equal '- foo = bar', render_erb('<% foo = bar -%>')
7
+ assert_equal '= h @item.title', render_erb('<%=h @item.title %>')
8
+ assert_equal '= h @item.title', render_erb('<%=h @item.title -%>')
9
+ end
10
+
11
+ def test_inline_erb
12
+ assert_equal("%p= foo", render_erb("<p><%= foo %></p>"))
13
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
14
+ %p= foo
15
+ FORTITUDE
16
+ <p><%= foo %>
17
+ </p>
18
+ HTML
19
+ end
20
+
21
+ def test_non_inline_erb
22
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
23
+ %p
24
+ = foo
25
+ FORTITUDE
26
+ <p>
27
+ <%= foo %>
28
+ </p>
29
+ HTML
30
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
31
+ %p
32
+ = foo
33
+ FORTITUDE
34
+ <p>
35
+ <%= foo %></p>
36
+ HTML
37
+ end
38
+
39
+ def test_erb_in_cdata
40
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
41
+ :cdata
42
+ Foo \#{bar} baz
43
+ FORTITUDE
44
+ <![CDATA[Foo <%= bar %> baz]]>
45
+ HTML
46
+ end
47
+
48
+ def test_erb_in_script
49
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
50
+ :javascript
51
+ function foo() {
52
+ return \#{foo.to_json};
53
+ }
54
+ FORTITUDE
55
+ <script type="text/javascript">
56
+ function foo() {
57
+ return <%= foo.to_json %>;
58
+ }
59
+ </script>
60
+ HTML
61
+ end
62
+
63
+ def test_erb_in_style
64
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
65
+ :css
66
+ foo {
67
+ bar: \#{"baz"};
68
+ }
69
+ FORTITUDE
70
+ <style type="text/css">
71
+ foo {
72
+ bar: <%= "baz" %>;
73
+ }
74
+ </style>
75
+ HTML
76
+ end
77
+
78
+ def test_erb_in_line
79
+ assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
80
+ assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')
81
+ end
82
+
83
+ def test_erb_multi_in_line
84
+ assert_equal('foo bar #{baz}! Bang #{bop}.',
85
+ render_erb('foo bar <%= baz %>! Bang <%= bop %>.'))
86
+ assert_equal('foo bar #{baz}#{bop}!',
87
+ render_erb('foo bar <%= baz %><%= bop %>!'))
88
+ end
89
+
90
+ def test_erb_with_html_special_chars
91
+ assert_equal '= 3 < 5 ? "OK" : "Your computer is b0rken"',
92
+ render_erb('<%= 3 < 5 ? "OK" : "Your computer is b0rken" %>')
93
+ end
94
+
95
+ def test_erb_in_class_attribute
96
+ assert_equal "%div{:class => dyna_class} I have a dynamic attribute",
97
+ render_erb('<div class="<%= dyna_class %>">I have a dynamic attribute</div>')
98
+ end
99
+
100
+ def test_erb_in_id_attribute
101
+ assert_equal "%div{:id => dyna_id} I have a dynamic attribute",
102
+ render_erb('<div id="<%= dyna_id %>">I have a dynamic attribute</div>')
103
+ end
104
+
105
+ def test_erb_in_attribute_results_in_string_interpolation
106
+ assert_equal('%div{:id => "item_#{i}"} Ruby string interpolation FTW',
107
+ render_erb('<div id="item_<%= i %>">Ruby string interpolation FTW</div>'))
108
+ end
109
+
110
+ def test_erb_in_attribute_with_trailing_content
111
+ assert_equal('%div{:class => "#{12}!"} Bang!',
112
+ render_erb('<div class="<%= 12 %>!">Bang!</div>'))
113
+ end
114
+
115
+ def test_erb_in_html_escaped_attribute
116
+ assert_equal '%div{:class => "foo"} Bang!',
117
+ render_erb('<div class="<%= "foo" %>">Bang!</div>')
118
+ end
119
+
120
+ def test_erb_in_html_escaped_attribute_with_symbol
121
+ assert_equal '%div{:class => :foo} Bang!',
122
+ render_erb('<div class="<%= :foo %>">Bang!</div>')
123
+ end
124
+
125
+ def test_empty_erb_in_attribute
126
+ assert_equal '%div{:class => ""}',
127
+ render_erb('<div class="<%= %>"></div>')
128
+ end
129
+
130
+ def test_erb_in_attribute_to_multiple_interpolations
131
+ assert_equal('%div{:class => "#{12} + #{13}"} Math is super',
132
+ render_erb('<div class="<%= 12 %> + <%= 13 %>">Math is super</div>'))
133
+ end
134
+
135
+ def test_whitespace_eating_erb_tags
136
+ assert_equal '- form_for', render_erb('<%- form_for -%>')
137
+ end
138
+
139
+ def test_interpolation_in_erb
140
+ assert_equal('= "Foo #{bar} baz"', render_erb('<%= "Foo #{bar} baz" %>'))
141
+ end
142
+
143
+ def test_interpolation_in_erb_attrs
144
+ assert_equal('%p{:foo => "#{bar} baz"}',
145
+ render_erb('<p foo="<%= "#{bar} baz" %>"></p>'))
146
+ end
147
+
148
+ def test_multiline_erb_silent_script
149
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
150
+ .blah
151
+ - foo
152
+ - bar
153
+ - baz
154
+ %p foo
155
+ FORTITUDE
156
+ <div class="blah">
157
+ <%
158
+ foo
159
+ bar
160
+ baz
161
+ %>
162
+ <p>foo</p>
163
+ </div>
164
+ ERB
165
+ end
166
+
167
+ def test_multiline_erb_loud_script
168
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
169
+ .blah
170
+ = foo + |
171
+ bar.baz.bang + |
172
+ baz |
173
+ %p foo
174
+ FORTITUDE
175
+ <div class="blah">
176
+ <%=
177
+ foo +
178
+ bar.baz.bang +
179
+ baz
180
+ %>
181
+ <p>foo</p>
182
+ </div>
183
+ ERB
184
+ end
185
+
186
+ def test_weirdly_indented_multiline_erb_loud_script
187
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
188
+ .blah
189
+ = foo + |
190
+ bar.baz.bang + |
191
+ baz |
192
+ %p foo
193
+ FORTITUDE
194
+ <div class="blah">
195
+ <%=
196
+ foo +
197
+ bar.baz.bang +
198
+ baz
199
+ %>
200
+ <p>foo</p>
201
+ </div>
202
+ ERB
203
+ end
204
+
205
+ def test_two_multiline_erb_loud_scripts
206
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
207
+ .blah
208
+ = foo + |
209
+ bar.baz.bang + |
210
+ baz |
211
+ -#
212
+ = foo.bar do |
213
+ bang |
214
+ end |
215
+ %p foo
216
+ FORTITUDE
217
+ <div class="blah">
218
+ <%=
219
+ foo +
220
+ bar.baz.bang +
221
+ baz
222
+ %>
223
+ <%= foo.bar do
224
+ bang
225
+ end %>
226
+ <p>foo</p>
227
+ </div>
228
+ ERB
229
+ end
230
+
231
+ def test_multiline_then_single_line_erb_loud_scripts
232
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
233
+ .blah
234
+ = foo + |
235
+ bar.baz.bang + |
236
+ baz |
237
+ = foo.bar
238
+ %p foo
239
+ FORTITUDE
240
+ <div class="blah">
241
+ <%=
242
+ foo +
243
+ bar.baz.bang +
244
+ baz
245
+ %>
246
+ <%= foo.bar %>
247
+ <p>foo</p>
248
+ </div>
249
+ ERB
250
+ end
251
+
252
+ def test_multiline_erb_but_really_single_line
253
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
254
+ .blah
255
+ = foo
256
+ %p foo
257
+ FORTITUDE
258
+ <div class="blah">
259
+ <%=
260
+ foo
261
+ %>
262
+ <p>foo</p>
263
+ </div>
264
+ ERB
265
+ end
266
+
267
+ ### Block Parsing
268
+
269
+ def test_block_parsing
270
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
271
+ - foo do
272
+ %p bar
273
+ FORTITUDE
274
+ <% foo do %>
275
+ <p>bar</p>
276
+ <% end %>
277
+ ERB
278
+ end
279
+
280
+ def test_block_parsing_with_args
281
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
282
+ - foo do |a, b, c|
283
+ %p bar
284
+ FORTITUDE
285
+ <% foo do |a, b, c| %>
286
+ <p>bar</p>
287
+ <% end %>
288
+ ERB
289
+ end
290
+
291
+ def test_block_parsing_with_equals
292
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
293
+ = foo do
294
+ %p bar
295
+ FORTITUDE
296
+ <%= foo do %>
297
+ <p>bar</p>
298
+ <% end %>
299
+ ERB
300
+ end
301
+
302
+ def test_block_parsing_with_modified_end
303
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
304
+ - foo do
305
+ blah
306
+ - end.bip
307
+ FORTITUDE
308
+ <% foo do %>
309
+ blah
310
+ <% end.bip %>
311
+ ERB
312
+ end
313
+
314
+ def test_block_parsing_with_modified_end_with_block
315
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
316
+ - foo do
317
+ blah
318
+ - end.bip do
319
+ brang
320
+ FORTITUDE
321
+ <% foo do %>
322
+ blah
323
+ <% end.bip do %>
324
+ brang
325
+ <% end %>
326
+ ERB
327
+ end
328
+
329
+ def test_multiline_block_opener
330
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
331
+ - foo bar
332
+ - baz bang
333
+ - biddle do
334
+ foo
335
+ FORTITUDE
336
+ <% foo bar
337
+ baz bang
338
+ biddle do %>
339
+ foo
340
+ <% end %>
341
+ ERB
342
+ end
343
+
344
+ def test_if_elsif_else_parsing
345
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
346
+ - if foo
347
+ %p bar
348
+ - elsif bar.foo("zip")
349
+ #bang baz
350
+ - else
351
+ %strong bibble
352
+ FORTITUDE
353
+ <% if foo %>
354
+ <p>bar</p>
355
+ <% elsif bar.foo("zip") %>
356
+ <div id="bang">baz</div>
357
+ <% else %>
358
+ <strong>bibble</strong>
359
+ <% end %>
360
+ ERB
361
+ end
362
+
363
+ def test_case_when_parsing
364
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
365
+ - case foo.bar
366
+ - when "bip"
367
+ %p bip
368
+ - when "bop"
369
+ %p BOP
370
+ - when bizzle.bang.boop.blip
371
+ %em BIZZLE BANG BOOP BLIP
372
+ FORTITUDE
373
+ <% case foo.bar %>
374
+ <% when "bip" %>
375
+ <p>bip</p>
376
+ <% when "bop" %>
377
+ <p>BOP</p>
378
+ <% when bizzle.bang.boop.blip %>
379
+ <em>BIZZLE BANG BOOP BLIP</em>
380
+ <% end %>
381
+ ERB
382
+
383
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
384
+ - case foo.bar
385
+ - when "bip"
386
+ %p bip
387
+ - when "bop"
388
+ %p BOP
389
+ - when bizzle.bang.boop.blip
390
+ %em BIZZLE BANG BOOP BLIP
391
+ FORTITUDE
392
+ <% case foo.bar
393
+ when "bip" %>
394
+ <p>bip</p>
395
+ <% when "bop" %>
396
+ <p>BOP</p>
397
+ <% when bizzle.bang.boop.blip %>
398
+ <em>BIZZLE BANG BOOP BLIP</em>
399
+ <% end %>
400
+ ERB
401
+ end
402
+
403
+ def test_begin_rescue_ensure
404
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
405
+ - begin
406
+ %p a
407
+ - rescue FooException => e
408
+ %p b
409
+ - ensure
410
+ %p c
411
+ FORTITUDE
412
+ <% begin %>
413
+ <p>a</p>
414
+ <% rescue FooException => e %>
415
+ <p>b</p>
416
+ <% ensure %>
417
+ <p>c</p>
418
+ <% end %>
419
+ ERB
420
+ end
421
+
422
+ # Regression
423
+
424
+ def test_tag_inside_block
425
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
426
+ %table
427
+ - foo.each do
428
+ %tr
429
+ FORTITUDE
430
+ <table>
431
+ <% foo.each do %>
432
+ <tr></tr>
433
+ <% end %>
434
+ </table>
435
+ ERB
436
+ end
437
+
438
+ def test_silent_inside_block_inside_tag
439
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
440
+ %table
441
+ - foo.each do
442
+ - fortitude_puts "foo"
443
+ FORTITUDE
444
+ <table>
445
+ <% foo.each do %>
446
+ <% fortitude_puts "foo" %>
447
+ <% end %>
448
+ </table>
449
+ ERB
450
+ end
451
+
452
+ def test_commented_erb_should_not_cause_indentation
453
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
454
+ %title
455
+ html2fortitude and multiline titles
456
+ = # stylesheet_link_tag :first
457
+ = stylesheet_link_tag 'another file'
458
+ FORTITUDE
459
+ <title>
460
+ html2fortitude and multiline titles
461
+ </title>
462
+ <%=# stylesheet_link_tag :first %>
463
+ <%#= stylesheet_link_tag :second %>
464
+ <%# stylesheet_link_tag :third %>
465
+ <%= stylesheet_link_tag 'another file' %>
466
+ ERB
467
+ end
468
+
469
+ def test_can_parse_ruby_19_hashes_as_arguments
470
+ erb = "<%= foobar 'foo', {bar: 'baz'} %>"
471
+ begin
472
+ Html2fortitude::HTML::ERB.new(erb)
473
+ rescue
474
+ flunk "should not raise an error"
475
+ end
476
+ end
477
+
478
+ def test_should_wrap_in_silent
479
+ assert_equal(<<HTML.rstrip, Html2fortitude::HTML::ERB.new(<<ERB).src)
480
+ <fortitude_silent> some_variable_or_function \n</fortitude_silent>
481
+ HTML
482
+ <% some_variable_or_function %>
483
+ ERB
484
+ end
485
+
486
+ #comment content is removed by erubis
487
+ def test_should_wrap_process_comments_as_empty_lines
488
+ assert_equal(<<HTML.rstrip, Html2fortitude::HTML::ERB.new(<<ERB).src)
489
+ <fortitude_silent>\n</fortitude_silent>
490
+ HTML
491
+ <%# some_variable_or_function %>
492
+ ERB
493
+ end
494
+
495
+ def test_conditional_structure_in_argument
496
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
497
+ %span{:class => "\#{"active" if condition}"}
498
+ FORTITUDE
499
+ <span class="<%= "active" if condition %>"></span>
500
+ HTML
501
+ end
502
+
503
+ def test_method_call_without_brackets_in_argument
504
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
505
+ %span{:class => "\#{call_me maybe}"}
506
+ FORTITUDE
507
+ <span class="<%= call_me maybe %>"></span>
508
+ HTML
509
+ end
510
+
511
+ def test_multiline_erb_comment
512
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
513
+ - # comment
514
+ %p hi
515
+ FORTITUDE
516
+ <%
517
+ # comment
518
+ -%>
519
+ <p>hi</p>
520
+ ERB
521
+ end
522
+
523
+ ##
524
+ # <%== %> is supposed to be equal to <%= raw %>
525
+ def test_erb_with_double_equals
526
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
527
+ != link_to "https://github.com/haml/html2haml/issues/44"
528
+ FORTITUDE
529
+ <%== link_to "https://github.com/haml/html2haml/issues/44" %>
530
+ ERB
531
+ end
532
+
533
+ #https://github.com/haml/html2haml/issues/43
534
+ def test_escaped_ruby_call_when_preceeded_by_text
535
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
536
+ random text
537
+ = form_tag(url: sessions_path) do
538
+ = submit_tag "cdcd"
539
+ FORTITUDE
540
+ random text
541
+ <%= form_tag(url: sessions_path) do %>
542
+ <%= submit_tag "cdcd" %>
543
+ <% end %>
544
+ ERB
545
+ end
546
+ end