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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
$: << './test'
require 'test_helper'
class ErbTest < Minitest::Test
def test_erb
assert_equal '- foo = bar', render_erb('<% foo = bar %>')
assert_equal '- foo = bar', render_erb('<% foo = bar -%>')
assert_equal '= h @item.title', render_erb('<%=h @item.title %>')
assert_equal '= h @item.title', render_erb('<%=h @item.title -%>')
end
def test_inline_erb
assert_equal("%p= foo", render_erb("<p><%= foo %></p>"))
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
%p
= foo
HAML
<p><%= foo %>
</p>
HTML
end
def test_non_inline_erb
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
%p
= foo
HAML
<p>
<%= foo %>
</p>
HTML
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
%p
= foo
HAML
<p>
<%= foo %></p>
HTML
end
def test_erb_in_cdata
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:cdata
Foo \#{bar} baz
HAML
<![CDATA[Foo <%= bar %> baz]]>
HTML
end
def test_erb_in_script
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:javascript
function foo() {
return \#{foo.to_json};
}
HAML
<script type="text/javascript">
function foo() {
return <%= foo.to_json %>;
}
</script>
HTML
end
def test_erb_in_style
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:css
foo {
bar: \#{"baz"};
}
HAML
<style type="text/css">
foo {
bar: <%= "baz" %>;
}
</style>
HTML
end
def test_erb_in_line
assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')
end
def test_erb_multi_in_line
assert_equal('foo bar #{baz}! Bang #{bop}.',
render_erb('foo bar <%= baz %>! Bang <%= bop %>.'))
assert_equal('foo bar #{baz}#{bop}!',
render_erb('foo bar <%= baz %><%= bop %>!'))
end
def test_erb_with_html_special_chars
assert_equal '= 3 < 5 ? "OK" : "Your computer is b0rken"',
render_erb('<%= 3 < 5 ? "OK" : "Your computer is b0rken" %>')
end
def test_erb_in_class_attribute
assert_equal "%div{:class => dyna_class} I have a dynamic attribute",
render_erb('<div class="<%= dyna_class %>">I have a dynamic attribute</div>')
end
def test_erb_in_id_attribute
assert_equal "%div{:id => dyna_id} I have a dynamic attribute",
render_erb('<div id="<%= dyna_id %>">I have a dynamic attribute</div>')
end
def test_erb_in_attribute_results_in_string_interpolation
assert_equal('%div{:id => "item_#{i}"} Ruby string interpolation FTW',
render_erb('<div id="item_<%= i %>">Ruby string interpolation FTW</div>'))
end
def test_erb_in_attribute_with_trailing_content
assert_equal('%div{:class => "#{12}!"} Bang!',
render_erb('<div class="<%= 12 %>!">Bang!</div>'))
end
def test_erb_in_html_escaped_attribute
assert_equal '%div{:class => "foo"} Bang!',
render_erb('<div class="<%= "foo" %>">Bang!</div>')
end
def test_erb_in_html_escaped_attribute_with_symbol
assert_equal '%div{:class => :foo} Bang!',
render_erb('<div class="<%= :foo %>">Bang!</div>')
end
def test_empty_erb_in_attribute
assert_equal '%div{:class => ""}',
render_erb('<div class="<%= %>"></div>')
end
def test_erb_in_attribute_to_multiple_interpolations
assert_equal('%div{:class => "#{12} + #{13}"} Math is super',
render_erb('<div class="<%= 12 %> + <%= 13 %>">Math is super</div>'))
end
def test_whitespace_eating_erb_tags
assert_equal '- form_for', render_erb('<%- form_for -%>')
end
def test_interpolation_in_erb
assert_equal('= "Foo #{bar} baz"', render_erb('<%= "Foo #{bar} baz" %>'))
end
def test_interpolation_in_erb_attrs
assert_equal('%p{:foo => "#{bar} baz"}',
render_erb('<p foo="<%= "#{bar} baz" %>"></p>'))
end
def test_multiline_erb_silent_script
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
- foo
- bar
- baz
%p foo
HAML
<div class="blah">
<%
foo
bar
baz
%>
<p>foo</p>
</div>
ERB
end
def test_multiline_erb_loud_script
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
= foo + |
bar.baz.bang + |
baz |
%p foo
HAML
<div class="blah">
<%=
foo +
bar.baz.bang +
baz
%>
<p>foo</p>
</div>
ERB
end
def test_weirdly_indented_multiline_erb_loud_script
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
= foo + |
bar.baz.bang + |
baz |
%p foo
HAML
<div class="blah">
<%=
foo +
bar.baz.bang +
baz
%>
<p>foo</p>
</div>
ERB
end
def test_two_multiline_erb_loud_scripts
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
= foo + |
bar.baz.bang + |
baz |
= foo.bar do |
bang |
end |
%p foo
HAML
<div class="blah">
<%=
foo +
bar.baz.bang +
baz
%>
<%= foo.bar do
bang
end %>
<p>foo</p>
</div>
ERB
end
def test_multiline_then_single_line_erb_loud_scripts
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
= foo + |
bar.baz.bang + |
baz |
= foo.bar
%p foo
HAML
<div class="blah">
<%=
foo +
bar.baz.bang +
baz
%>
<%= foo.bar %>
<p>foo</p>
</div>
ERB
end
def test_multiline_erb_but_really_single_line
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
.blah
= foo
%p foo
HAML
<div class="blah">
<%=
foo
%>
<p>foo</p>
</div>
ERB
end
### Block Parsing
def test_block_parsing
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- foo do
%p bar
HAML
<% foo do %>
<p>bar</p>
<% end %>
ERB
end
def test_block_parsing_with_args
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- foo do |a, b, c|
%p bar
HAML
<% foo do |a, b, c| %>
<p>bar</p>
<% end %>
ERB
end
def test_block_parsing_with_equals
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
= foo do
%p bar
HAML
<%= foo do %>
<p>bar</p>
<% end %>
ERB
end
def test_block_parsing_with_modified_end
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- foo do
blah
- end.bip
HAML
<% foo do %>
blah
<% end.bip %>
ERB
end
def test_block_parsing_with_modified_end_with_block
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- foo do
blah
- end.bip do
brang
HAML
<% foo do %>
blah
<% end.bip do %>
brang
<% end %>
ERB
end
def test_multiline_block_opener
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- foo bar
- baz bang
- biddle do
foo
HAML
<% foo bar
baz bang
biddle do %>
foo
<% end %>
ERB
end
def test_if_elsif_else_parsing
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- if foo
%p bar
- elsif bar.foo("zip")
#bang baz
- else
%strong bibble
HAML
<% if foo %>
<p>bar</p>
<% elsif bar.foo("zip") %>
<div id="bang">baz</div>
<% else %>
<strong>bibble</strong>
<% end %>
ERB
end
def test_case_when_parsing
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- case foo.bar
- when "bip"
%p bip
- when "bop"
%p BOP
- when bizzle.bang.boop.blip
%em BIZZLE BANG BOOP BLIP
HAML
<% case foo.bar %>
<% when "bip" %>
<p>bip</p>
<% when "bop" %>
<p>BOP</p>
<% when bizzle.bang.boop.blip %>
<em>BIZZLE BANG BOOP BLIP</em>
<% end %>
ERB
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- case foo.bar
- when "bip"
%p bip
- when "bop"
%p BOP
- when bizzle.bang.boop.blip
%em BIZZLE BANG BOOP BLIP
HAML
<% case foo.bar
when "bip" %>
<p>bip</p>
<% when "bop" %>
<p>BOP</p>
<% when bizzle.bang.boop.blip %>
<em>BIZZLE BANG BOOP BLIP</em>
<% end %>
ERB
end
def test_begin_rescue_ensure
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- begin
%p a
- rescue FooException => e
%p b
- ensure
%p c
HAML
<% begin %>
<p>a</p>
<% rescue FooException => e %>
<p>b</p>
<% ensure %>
<p>c</p>
<% end %>
ERB
end
# Regression
def test_tag_inside_block
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
%table
- foo.each do
%tr
HAML
<table>
<% foo.each do %>
<tr></tr>
<% end %>
</table>
ERB
end
def test_silent_inside_block_inside_tag
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
%table
- foo.each do
- haml_puts "foo"
HAML
<table>
<% foo.each do %>
<% haml_puts "foo" %>
<% end %>
</table>
ERB
end
def test_commented_erb_should_not_cause_indentation
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
%title
html2haml and multiline titles
= # stylesheet_link_tag :first
= stylesheet_link_tag 'another file'
HAML
<title>
html2haml and multiline titles
</title>
<%=# stylesheet_link_tag :first %>
<%#= stylesheet_link_tag :second %>
<%# stylesheet_link_tag :third %>
<%= stylesheet_link_tag 'another file' %>
ERB
end
def test_can_parse_ruby_19_hashes_as_arguments
erb = "<%= foobar 'foo', {bar: 'baz'} %>"
begin
Html2haml::HTML::ERB.new(erb)
rescue
flunk "should not raise an error"
end
end
def test_should_wrap_in_silent
assert_equal(<<HTML.rstrip, Html2haml::HTML::ERB.new(<<ERB).src)
<haml_silent> some_variable_or_function \n</haml_silent>
HTML
<% some_variable_or_function %>
ERB
end
#comment content is removed by erubis
def test_should_wrap_process_comments_as_empty_lines
assert_equal(<<HTML.rstrip, Html2haml::HTML::ERB.new(<<ERB).src)
<haml_silent>\n</haml_silent>
HTML
<%# some_variable_or_function %>
ERB
end
def test_conditional_structure_in_argument
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
%span{:class => "\#{"active" if condition}"}
HAML
<span class="<%= "active" if condition %>"></span>
HTML
end
def test_method_call_without_brackets_in_argument
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
%span{:class => "\#{call_me maybe}"}
HAML
<span class="<%= call_me maybe %>"></span>
HTML
end
def test_multiline_erb_comment
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
- # comment
%p hi
HAML
<%
# comment
-%>
<p>hi</p>
ERB
end
##
# <%== %> is supposed to be equal to <%= raw %>
def test_erb_with_double_equals
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
!= link_to "https://github.com/haml/html2haml/issues/44"
HAML
<%== link_to "https://github.com/haml/html2haml/issues/44" %>
ERB
end
#https://github.com/haml/html2haml/issues/43
def test_escaped_ruby_call_when_preceded_by_text
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
random text
= form_tag(url: sessions_path) do
= submit_tag "cdcd"
HAML
random text
<%= form_tag(url: sessions_path) do %>
<%= submit_tag "cdcd" %>
<% end %>
ERB
end
end
|