$:.unshift __dir__ require 'test_helper' class EngineTest < Haml::TestCase # A map of erroneous Haml documents to the error messages they should produce. # The error messages may be arrays; # if so, the second element should be the line number that should be reported for the error. # If this isn't provided, the tests will assume the line number should be the last line of the document. EXCEPTION_MAP = { "!!!\n a" => error(:illegal_nesting_header), "a\n b" => error(:illegal_nesting_plain), "/ a\n b" => error(:illegal_nesting_content), "% a" => error(:invalid_tag, '% a'), "%p a\n b" => error(:illegal_nesting_line, 'p'), "%p=" => error(:no_ruby_code, '='), "%p~" => error(:no_ruby_code, '~'), "~" => error(:no_ruby_code, '~'), "=" => error(:no_ruby_code, '='), "%p/\n a" => error(:illegal_nesting_self_closing), #":a\n b" => [error(:filter_not_defined, 'a'), 1], ":a= b" => error(:invalid_filter_name, 'a= b'), "." => error(:illegal_element), ".#" => error(:illegal_element), ".{} a" => error(:illegal_element), ".() a" => error(:illegal_element), ".= a" => error(:illegal_element), "%p..a" => error(:illegal_element), "%a/ b" => error(:self_closing_content), " %p foo" => error(:indenting_at_start), " %p foo" => error(:indenting_at_start), "- end" => error(:no_end), "%p{:a => 'b',\n:c => 'd'}/ e" => [error(:self_closing_content), 2], "%p{:a => 'b',\n:c => 'd'}=" => [error(:no_ruby_code, '='), 2], "%p.{:a => 'b',\n:c => 'd'} e" => [error(:illegal_element), 1], "%p{:a => 'b',\n:c => 'd',\n:e => 'f'}\n%p/ a" => [error(:self_closing_content), 4], "%p{:a => 'b',\n:c => 'd',\n:e => 'f'}\n- raise 'foo'" => ["foo", 4], "%p{:a => 'b',\n:c => raise('foo'),\n:e => 'f'}" => ["foo", 2], "%p{:a => 'b',\n:c => 'd',\n:e => raise('foo')}" => ["foo", 3], " \n\t\n %p foo" => [error(:indenting_at_start), 3], "\n\n %p foo" => [error(:indenting_at_start), 3], "%p\n foo\n foo" => [error(:inconsistent_indentation, "1 space", "2 spaces"), 3], "%p\n foo\n%p\n foo" => [error(:inconsistent_indentation, "1 space", "2 spaces"), 4], "%p\n\t\tfoo\n\tfoo" => [error(:inconsistent_indentation, "1 tab", "2 tabs"), 3], "%p\n foo\n foo" => [error(:inconsistent_indentation, "3 spaces", "2 spaces"), 3], "%p\n foo\n %p\n bar" => [error(:inconsistent_indentation, "3 spaces", "2 spaces"), 4], "%p\n :plain\n bar\n \t baz" => [error(:inconsistent_indentation, '" \t "', "2 spaces"), 4], "%p\n foo\n%p\n bar" => [error(:deeper_indenting, 2), 4], "%p\n foo\n %p\n bar" => [error(:deeper_indenting, 3), 4], "%p\n \tfoo" => [error(:cant_use_tabs_and_spaces), 2], "%p(" => error(:invalid_attribute_list, '"("'), "%p(foo=)" => error(:invalid_attribute_list, '"(foo=)"'), "%p(foo 'bar')" => error(:invalid_attribute_list, '"(foo \'bar\')"'), "%p(foo=\nbar)" => [error(:invalid_attribute_list, '"(foo="'), 1], "%p(foo 'bar'\nbaz='bang')" => [error(:invalid_attribute_list, '"(foo \'bar\'"'), 1], "%p(foo='bar'\nbaz 'bang'\nbip='bop')" => [error(:invalid_attribute_list, '"(foo=\'bar\' baz \'bang\'"'), 2], "%p{'foo' => 'bar' 'bar' => 'baz'}" => :compile, "%p{:foo => }" => :compile, "%p{=> 'bar'}" => :compile, "%p{'foo => 'bar'}" => :compile, "%p{:foo => 'bar}" => :compile, "%p{:foo => 'bar\"}" => :compile, # Regression tests "foo\n\n\n bar" => [error(:illegal_nesting_plain), 4], "%p/\n\n bar" => [error(:illegal_nesting_self_closing), 3], "%p foo\n\n bar" => [error(:illegal_nesting_line, 'p'), 3], "/ foo\n\n bar" => [error(:illegal_nesting_content), 3], "!!!\n\n bar" => [error(:illegal_nesting_header), 3], "- raise 'foo'\n\n\n\nbar" => ["foo", 1], "= 'foo'\n-raise 'foo'" => ["foo", 2], "\n\n\n- raise 'foo'" => ["foo", 4], "%p foo |\n bar |\n baz |\nbop\n- raise 'foo'" => ["foo", 5], #"foo\n:ruby\n 1\n 2\n 3\n- raise 'foo'" => ["foo", 6], #"foo\n:erb\n 1\n 2\n 3\n- raise 'foo'" => ["foo", 6], "foo\n:plain\n 1\n 2\n 3\n- raise 'foo'" => ["foo", 6], "foo\n:plain\n 1\n 2\n 3\n4\n- raise 'foo'" => ["foo", 7], "foo\n:plain\n 1\n 2\n 3\#{''}\n- raise 'foo'" => ["foo", 6], "foo\n:plain\n 1\n 2\n 3\#{''}\n4\n- raise 'foo'" => ["foo", 7], "foo\n:plain\n 1\n 2\n \#{raise 'foo'}" => ["foo", 5], "= raise 'foo'\nfoo\nbar\nbaz\nbang" => ["foo", 1], "- case 1\n\n- when 1\n - raise 'foo'" => ["foo", 4], } User = Struct.new('User', :id) class CustomHamlClass < Struct.new(:id) def haml_object_ref "my_thing" end end CpkRecord = Struct.new('CpkRecord', :id) do def to_key [*self.id] unless id.nil? end end def use_test_tracing(options) unless options[:filename] # use caller method name as fake filename. useful for debugging i = -1 caller[i+=1] =~ /`(.+?)'/ until $1 and $1.index('test_') == 0 options[:filename] = "(#{$1})" end options end def render(text, options = {}, &block) options = use_test_tracing(options) super end def engine(text, options = {}) options = use_test_tracing(options) Hamlit::HamlEngine.new(text, options) end def setup @old_default_internal = Encoding.default_internal silence_warnings{Encoding.default_internal = nil} end def teardown silence_warnings{Encoding.default_internal = @old_default_internal} end def test_empty_render assert_equal "", render("") end def test_flexible_tabulation; skip # filter assert_equal("

\n foo\n

\n\n bar\n \n baz\n \n\n", render("%p\n foo\n%q\n bar\n %a\n baz")) assert_equal("

\n foo\n

\n\n bar\n \n baz\n \n\n", render("%p\n\tfoo\n%q\n\tbar\n\t%a\n\t\tbaz")) assert_equal("

\n \t \t bar\n baz\n

\n", render("%p\n :plain\n \t \t bar\n baz")) end def test_empty_render_should_remain_empty assert_equal('', render('')) end def test_attributes_should_render_correctly assert_equal("
", render(".atlantis{:style => 'ugly'}").chomp) end def test_css_id_as_attribute_should_be_appended_with_underscore assert_equal("
", render("#my_id{:id => '1'}").chomp) assert_equal("
", render("#my_id{:id => 1}").chomp) end def test_ruby_code_should_work_inside_attributes assert_equal("

foo

", render("%p{:class => 1+2} foo").chomp) end def test_class_attr_with_array; skip # array attribute assert_equal("

foo

\n", render("%p{:class => %w[a b]} foo")) # basic assert_equal("

foo

\n", render("%p.css{:class => %w[a b]} foo")) # merge with css assert_equal("

foo

\n", render("%p.css{:class => %w[css b]} foo")) # merge uniquely assert_equal("

foo

\n", render("%p{:class => [%w[a b], %w[c d]]} foo")) # flatten assert_equal("

foo

\n", render("%p{:class => [:a, :b] } foo")) # stringify assert_equal("

foo

\n", render("%p{:class => [nil, false] } foo")) # strip falsey assert_equal("

foo

\n", render("%p{:class => :a} foo")) # single stringify assert_equal("

foo

\n", render("%p{:class => false} foo")) # single falsey assert_equal("

foo

\n", render("%p(class='html'){:class => %w[a b]} foo")) # html attrs end def test_id_attr_with_array; skip # array attribute assert_equal("

foo

\n", render("%p{:id => %w[a b]} foo")) # basic assert_equal("

foo

\n", render("%p#css{:id => %w[a b]} foo")) # merge with css assert_equal("

foo

\n", render("%p{:id => [%w[a b], %w[c d]]} foo")) # flatten assert_equal("

foo

\n", render("%p{:id => [:a, :b] } foo")) # stringify assert_equal("

foo

\n", render("%p{:id => [nil, false] } foo")) # strip falsey assert_equal("

foo

\n", render("%p{:id => :a} foo")) # single stringify assert_equal("

foo

\n", render("%p{:id => false} foo")) # single falsey assert_equal("

foo

\n", render("%p(id='html'){:id => %w[a b]} foo")) # html attrs end def test_colon_in_class_attr assert_equal("

\n", render("%p.foo:bar/")) end def test_colon_in_id_attr assert_equal("

\n", render("%p#foo:bar/")) end def test_dynamic_attributes_with_no_content assert_equal(<

HTML %p %a{:href => "http://" + "haml.info"} HAML end def test_attributes_with_to_s assert_equal(<

HTML %p#foo{:id => 1+1} %p.foo{:class => 1+1} %p{:blaz => 1+1} %p{(1+1) => 1+1} HAML end def test_nil_should_render_empty_tag assert_equal("
", render(".no_attributes{:nil => nil}").chomp) end def test_strings_should_get_stripped_inside_tags assert_equal("
This should have no spaces in front of it
", render(".stripped This should have no spaces in front of it").chomp) end def test_one_liner_should_be_one_line assert_equal("

Hello

", render('%p Hello').chomp) end def test_one_liner_with_newline_shouldnt_be_one_line; skip # dynamic indentation # script bug assert_equal("

\n foo\n bar\n

", render('%p= "foo\nbar"').chomp) end def test_multi_render engine = engine("%strong Hi there!") assert_equal("Hi there!\n", engine.to_html) assert_equal("Hi there!\n", engine.to_html) assert_equal("Hi there!\n", engine.to_html) end def test_interpolation assert_equal("

Hello World

\n", render('%p Hello #{who}', locals: {who: 'World'}, escape_html: false)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#{who}", locals: {who: 'World'}, escape_html: false)) assert_equal("

Hello World

\n", render('%p Hello #{who}', locals: {who: 'World'}, escape_html: true)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#{who}", locals: {who: 'World'}, escape_html: true)) end def test_interpolation_with_instance_var; skip # special interpolation scope = Object.new scope.instance_variable_set(:@who, 'World') assert_equal("

Hello World

\n", render('%p Hello #@who', scope: scope, escape_html: false)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#@who", scope: scope, escape_html: false)) assert_equal("

Hello World

\n", render('%p Hello #@who', scope: scope, escape_html: true)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#@who", scope: scope, escape_html: true)) end def test_interpolation_with_global; skip # special interpolation $global_var_for_testing = 'World' assert_equal("

Hello World

\n", render('%p Hello #$global_var_for_testing', escape_html: false)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#$global_var_for_testing", escape_html: false)) assert_equal("

Hello World

\n", render('%p Hello #$global_var_for_testing', escape_html: true)) assert_equal("

\n Hello World\n

\n", render("%p\n Hello \#$global_var_for_testing", escape_html: true)) ensure $global_var_for_testing = nil end def test_interpolation_in_the_middle_of_a_string assert_equal("\"title 'Title'. \"\n", render("\"title '\#{\"Title\"}'. \"")) end def test_interpolation_with_instance_var_in_the_middle_of_a_string; skip # special interpolation scope = Object.new scope.instance_variable_set(:@title, 'Title') assert_equal("\"title 'Title'. \"\n", render("\"title '\#@title'. \"", :scope => scope)) end def test_interpolation_with_global_in_the_middle_of_a_string; skip # special interpolation $global_var_for_testing = 'Title' assert_equal("\"title 'Title'. \"\n", render("\"title '\#$global_var_for_testing'. \"")) ensure $global_var_for_testing = nil end def test_interpolation_at_the_beginning_of_a_line assert_equal("

2

\n", render('%p #{1 + 1}')) assert_equal("

\n 2\n

\n", render("%p\n \#{1 + 1}")) end def test_interpolation_with_instance_var_at_the_beginning_of_a_line; skip # special interpolation scope = Object.new scope.instance_variable_set(:@foo, 2) assert_equal("

2

\n", render('%p #@foo', :scope => scope)) assert_equal("

\n 2\n

\n", render("%p\n \#@foo", :scope => scope)) end def test_interpolation_with_global_at_the_beginning_of_a_line; skip # special interpolation $global_var_for_testing = 2 assert_equal("

2

\n", render('%p #$global_var_for_testing')) assert_equal("

\n 2\n

\n", render("%p\n \#$global_var_for_testing")) ensure $global_var_for_testing = nil end def test_escaped_interpolation assert_equal("

Foo & Bar & Baz

\n", render('%p& Foo #{"&"} Bar & Baz')) end def test_nil_tag_value_should_render_as_empty assert_equal("

\n", render("%p= nil")) end def test_tag_with_failed_if_should_render_as_empty assert_equal("

\n", render("%p= 'Hello' if false")) end def test_static_attributes_with_empty_attr assert_equal("\n", render("%img{:src => '/foo.png', :alt => ''}")) end def test_dynamic_attributes_with_empty_attr assert_equal("\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}")) end def test_attribute_hash_with_newlines assert_equal("

foop

\n", render("%p{:a => 'b',\n :c => 'd'} foop")) assert_equal("

\n foop\n

\n", render("%p{:a => 'b',\n :c => 'd'}\n foop")) assert_equal("

\n", render("%p{:a => 'b',\n :c => 'd'}/")) assert_equal("

\n", render("%p{:a => 'b',\n :c => 'd',\n :e => 'f'}")) end def test_attr_hashes_not_modified hash = {:color => 'red'} assert_equal(< {:hash => hash}))
HTML %div{hash} .special{hash} %div{hash} HAML assert_equal(hash, {:color => 'red'}) end def test_ugly_semi_prerendered_tags assert_equal(< true))

foo

foo

foo bar

foo bar

foo

HTML %p{:a => 1 + 1} %p{:a => 1 + 1} foo %p{:a => 1 + 1}/ %p{:a => 1 + 1}= "foo" %p{:a => 1 + 1}= "foo\\nbar" %p{:a => 1 + 1}~ "foo\\nbar" %p{:a => 1 + 1} foo HAML end def test_end_of_file_multiline assert_equal("

0

\n

1

\n

2

\n", render("- for i in (0...3)\n %p= |\n i |")) end def test_cr_newline assert_equal("

foo

\n

bar

\n

baz

\n

boom

\n", render("%p foo\r%p bar\r\n%p baz\n\r%p boom")) end def test_textareas; skip # script bug assert_equal("\n", render('%textarea= "Foo\n bar\n baz"')) assert_equal("
Foo
  bar
   baz
\n", render('%pre= "Foo\n bar\n baz"')) assert_equal("\n", render("%textarea #{'a' * 100}")) assert_equal("

\n \n

\n", render(<Foo bar baz HTML %pre %code :preserve Foo bar baz HAML end def test_boolean_attributes assert_equal("

\n", render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :html4)) assert_equal("

\n", render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :xhtml)) assert_equal("

\n", render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :html4)) assert_equal("

\n", render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :xhtml)) end def test_nuke_inner_whitespace_in_loops; skip # whitespace assert_equal(<foobarbaz HTML %ul< - for str in %w[foo bar baz] = str HAML end def test_both_whitespace_nukes_work_together; skip # script bug assert_equal(<Foo Bar

RESULT %p %q><= "Foo\\nBar" SOURCE end def test_nil_option assert_equal("

\n", render('%p{:foo => "bar"}', :attr_wrapper => nil)) end def test_comment_with_crazy_nesting assert_equal(< foo bar HTML %html %body %img{:src => 'te'+'st'} = "foo\\nbar" HAML end def test_whitespace_nuke_with_both_newlines; skip # script bug # runtime nuke assert_equal("

foo

\n", render('%p<= "\nfoo\n"')) assert_equal(<

foo

HTML %p %p<= "\\nfoo\\n" HAML end def test_whitespace_nuke_with_tags_and_else; skip # whitespace assert_equal(< foo HTML %a %b< - if false = "foo" - else foo HAML assert_equal(< foo HTML %a %b - if false = "foo" - else foo HAML end def test_outer_whitespace_nuke_with_empty_script; skip # runtime nuke assert_equal(< foo

HTML %p foo = " " %a> HAML end def test_both_case_indentation_work_with_deeply_nested_code result = < other RESULT assert_equal(result, render(< true)) = capture_haml do foo HAML end def test_plain_equals_with_ugly assert_equal("foo\nbar\n", render(< true)) = "foo" bar HAML end def test_inline_if assert_equal(<One

Three

HTML - for name in ["One", "Two", "Three"] %p= name unless name == "Two" HAML end def test_end_with_method_call; skip # block script # silent script assert_equal(< 2|3|4 b-a-r

HTML %p = [1, 2, 3].map do |i| - i + 1 - end.join("|") = "bar".gsub(/./) do |s| - s + "-" - end.gsub(/-$/) do |s| - '' HAML end def test_silent_end_with_stuff; skip # silent script assert_equal(<hi!

HTML - if true %p hi! - end if "foo".gsub(/f/) do - "z" - end + "bar" HAML end def test_multiline_with_colon_after_filter assert_equal(< "Bar", | :b => "Baz" }[:a] | HAML assert_equal(< "Bar", | :b => "Baz" }[:a] | HAML end def test_multiline_in_filter assert_equal(< false))
bar
HTML #foo{:class => ''} bar HAML end def test_escape_attrs_always; skip # attribute escape assert_equal(< :always))
bar
HTML #foo{:class => '"<>&"'} bar HAML end def test_escape_html; skip # escape html html = < true)) &= "&" != "&" = "&" HAML assert_equal(html, render(< true)) &~ "&" !~ "&" ~ "&" HAML assert_equal(html, render(< true)) & \#{"&"} ! \#{"&"} \#{"&"} HAML assert_equal(html, render(< true)) &== \#{"&"} !== \#{"&"} == \#{"&"} HAML tag_html = <&

&

&

HTML assert_equal(tag_html, render(< true)) %p&= "&" %p!= "&" %p= "&" HAML assert_equal(tag_html, render(< true)) %p&~ "&" %p!~ "&" %p~ "&" HAML assert_equal(tag_html, render(< true)) %p& \#{"&"} %p! \#{"&"} %p \#{"&"} HAML assert_equal(tag_html, render(< true)) %p&== \#{"&"} %p!== \#{"&"} %p== \#{"&"} HAML end def test_new_attrs_with_hash assert_equal("\n", render('%a(href="#")')) end def test_silent_script_with_hyphen_case assert_equal("", render("- a = 'foo-case-bar-case'")) end def test_silent_script_with_hyphen_end assert_equal("", render("- a = 'foo-end-bar-end'")) end def test_silent_script_with_hyphen_end_and_block; skip # silent script silence_warnings do assert_equal(<foo-end

bar-end

HTML - ("foo-end-bar-end".gsub(/\\w+-end/) do |s| %p= s - end; nil) HAML end end def test_if_without_content_and_else assert_equal(<Foo\n", render('%a(href="#" rel="top") Foo')) assert_equal("Foo\n", render('%a(href="#") #{"Foo"}')) assert_equal("\n", render('%a(href="#\\"")')) end def test_case_assigned_to_var assert_equal(< true)) foo, HTML foo\#{"," if true} HAML end # HTML escaping tests def test_ampersand_equals_should_escape assert_equal("

\n foo & bar\n

\n", render("%p\n &= 'foo & bar'", :escape_html => false)) end def test_ampersand_equals_inline_should_escape; skip # script bug assert_equal("

foo & bar

\n", render("%p&= 'foo & bar'", :escape_html => false)) end def test_ampersand_equals_should_escape_before_preserve; skip # script bug assert_equal("\n", render('%textarea&= "foo\nbar"', :escape_html => false)) end def test_bang_equals_should_not_escape assert_equal("

\n foo & bar\n

\n", render("%p\n != 'foo & bar'", :escape_html => true)) end def test_bang_equals_inline_should_not_escape assert_equal("

foo & bar

\n", render("%p!= 'foo & bar'", :escape_html => true)) end def test_static_attributes_should_be_escaped; skip # attribute escape assert_equal("\n", render("%img.atlantis{:style => 'ugly&stupid'}")) assert_equal("
foo
\n", render(".atlantis{:style => 'ugly&stupid'} foo")) assert_equal("

foo

\n", render("%p.atlantis{:style => 'ugly&stupid'}= 'foo'")) assert_equal("

\n", render("%p.atlantis{:style => \"ugly\\nstupid\"}")) end def test_dynamic_attributes_should_be_escaped; skip # script bug assert_equal("\n", render("%img{:width => nil, :src => '&foo.png', :alt => String.new}")) assert_equal("

foo

\n", render("%p{:width => nil, :src => '&foo.png', :alt => String.new} foo")) assert_equal("
foo
\n", render("%div{:width => nil, :src => '&foo.png', :alt => String.new}= 'foo'")) assert_equal("\n", render("%img{:width => nil, :src => \"foo\\n.png\", :alt => String.new}")) end def test_string_double_equals_should_be_esaped; skip # escape html assert_equal("

4&<

\n", render("%p== \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p== \#{2+2}&\#{'<'}", :escape_html => false)) end def test_escaped_inline_string_double_equals assert_equal("

4&<

\n", render("%p&== \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p&== \#{2+2}&\#{'<'}", :escape_html => false)) end def test_unescaped_inline_string_double_equals assert_equal("

4&<

\n", render("%p!== \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p!== \#{2+2}&\#{'<'}", :escape_html => false)) end def test_escaped_string_double_equals assert_equal("

\n 4&<\n

\n", render("%p\n &== \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

\n 4&<\n

\n", render("%p\n &== \#{2+2}&\#{'<'}", :escape_html => false)) end def test_unescaped_string_double_equals assert_equal("

\n 4&<\n

\n", render("%p\n !== \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

\n 4&<\n

\n", render("%p\n !== \#{2+2}&\#{'<'}", :escape_html => false)) end def test_string_interpolation_should_be_esaped; skip # escape html assert_equal("

4&<

\n", render("%p \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p \#{2+2}&\#{'<'}", :escape_html => false)) end def test_escaped_inline_string_interpolation assert_equal("

4&<

\n", render("%p& \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p& \#{2+2}&\#{'<'}", :escape_html => false)) end def test_unescaped_inline_string_interpolation assert_equal("

4&<

\n", render("%p! \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

4&<

\n", render("%p! \#{2+2}&\#{'<'}", :escape_html => false)) end def test_escaped_string_interpolation assert_equal("

\n 4&<\n

\n", render("%p\n & \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

\n 4&<\n

\n", render("%p\n & \#{2+2}&\#{'<'}", :escape_html => false)) end def test_escaped_string_interpolation_with_no_space; skip # escape html assert_equal("<br>\n", render('&#{"
"}')) assert_equal("<br>\n", render('%span&#{"
"}')) end def test_unescaped_string_interpolation assert_equal("

\n 4&<\n

\n", render("%p\n ! \#{2+2}&\#{'<'}", :escape_html => true)) assert_equal("

\n 4&<\n

\n", render("%p\n ! \#{2+2}&\#{'<'}", :escape_html => false)) end def test_unescaped_string_interpolation_with_no_space; skip # without space assert_equal("
\n", render('!#{"
"}')) assert_equal("
\n", render('%span!#{"
"}')) end def test_scripts_should_respect_escape_html_option; skip # escape html assert_equal("

\n foo & bar\n

\n", render("%p\n = 'foo & bar'", :escape_html => true)) assert_equal("

\n foo & bar\n

\n", render("%p\n = 'foo & bar'", :escape_html => false)) end def test_inline_scripts_should_respect_escape_html_option; skip # escape html assert_equal("

foo & bar

\n", render("%p= 'foo & bar'", :escape_html => true)) assert_equal("

foo & bar

\n", render("%p= 'foo & bar'", :escape_html => false)) end def test_script_ending_in_comment_should_render_when_html_is_escaped; skip # script bug assert_equal("foo&bar\n", render("= 'foo&bar' #comment", :escape_html => true)) end def test_script_with_if_shouldnt_output assert_equal(<foo

HTML %p= "foo" %p= "bar" if false HAML end # Options tests def test_filename_and_line; skip # options begin render("\n\n = abc", :filename => 'test', :line => 2) rescue Exception => e assert_kind_of Haml::SyntaxError, e assert_match(/test:4/, e.backtrace.first) end begin render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2) rescue Exception => e assert_kind_of NoMethodError, e backtrace = e.backtrace backtrace.shift if rubinius? assert_match(/test:6/, backtrace.first) end end def test_stop_eval; skip # options assert_equal("", render("= 'Hello'", :suppress_eval => true)) assert_equal("", render("- haml_concat 'foo'", :suppress_eval => true)) assert_equal("
\n", render("#foo{:yes => 'no'}/", :suppress_eval => true)) assert_equal("
\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true)) assert_equal("
\n", render("%div[1]/", :suppress_eval => true)) assert_equal("", render(":ruby\n Kernel.puts 'hello'", :suppress_eval => true)) end def test_doctypes assert_equal('', render('!!!', :format => :html5).strip) assert_equal('', render('!!! 5').strip) assert_equal('', render('!!! strict', :format => :xhtml).strip) assert_equal('', render('!!! frameset', :format => :xhtml).strip) assert_equal('', render('!!! mobile', :format => :xhtml).strip) assert_equal('', render('!!! basic', :format => :xhtml).strip) assert_equal('', render('!!! transitional', :format => :xhtml).strip) assert_equal('', render('!!!', :format => :xhtml).strip) assert_equal('', render('!!! strict', :format => :html4).strip) assert_equal('', render('!!! frameset', :format => :html4).strip) assert_equal('', render('!!! transitional', :format => :html4).strip) assert_equal('', render('!!!', :format => :html4).strip) end def test_attr_wrapper; skip # options assert_equal("

\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*')) assert_equal("

\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"')) assert_equal("

\n", render("%p{ :escaped => 'quo\\'te'}", :attr_wrapper => '"')) assert_equal("

\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"')) assert_equal("\n", render("!!! XML", :attr_wrapper => '"', :format => :xhtml)) end def test_autoclose_option assert_equal("\n", render("%flaz{:foo => 'bar'}", :autoclose => ["flaz"])) assert_equal(< [/^flaz/])) HTML %flaz %flaznicate %flan HAML end def test_attrs_parsed_correctly; skip # attribute escape assert_equal("

biddly='bar => baz'>

\n", render("%p{'boom=>biddly' => 'bar => baz'}")) assert_equal("

\n", render("%p{'foo,bar' => 'baz, qux'}")) assert_equal("

\n", render("%p{ :escaped => \"quo\\nte\"}")) assert_equal("

\n", render("%p{ :escaped => \"quo\#{2 + 2}te\"}")) end def test_correct_parsing_with_brackets; skip # script bug assert_equal("

{tada} foo

\n", render("%p{:class => 'foo'} {tada} foo")) assert_equal("

deep {nested { things }}

\n", render("%p{:class => 'foo'} deep {nested { things }}")) assert_equal("

{a { d

\n", render("%p{{:class => 'foo'}, :class => 'bar'} {a { d")) assert_equal("

a}

\n", render("%p{:foo => 'bar'} a}")) foo = [] foo[0] = Struct.new('Foo', :id).new assert_equal("

New User]

\n", render("%p[foo[0]] New User]", :locals => {:foo => foo})) assert_equal("

New User]

\n", render("%p[foo[0], :prefix] New User]", :locals => {:foo => foo})) foo[0].id = 1 assert_equal("

New User]

\n", render("%p[foo[0]] New User]", :locals => {:foo => foo})) assert_equal("

New User]

\n", render("%p[foo[0], :prefix] New User]", :locals => {:foo => foo})) end def test_empty_attrs assert_equal("

empty

\n", render("%p{ :attr => '' } empty")) assert_equal("

empty

\n", render("%p{ :attr => x } empty", :locals => {:x => ''})) end def test_nil_attrs assert_equal("

nil

\n", render("%p{ :attr => nil } nil")) assert_equal("

nil

\n", render("%p{ :attr => x } nil", :locals => {:x => nil})) end def test_nil_id_with_syntactic_id assert_equal("

nil

\n", render("%p#foo{:id => nil} nil")) assert_equal("

nil

\n", render("%p#foo{{:id => 'bar'}, :id => nil} nil")) assert_equal("

nil

\n", render("%p#foo{{:id => nil}, :id => 'bar'} nil")) end def test_nil_class_with_syntactic_class assert_equal("

nil

\n", render("%p.foo{:class => nil} nil")) assert_equal("

nil

\n", render("%p.bar.foo{:class => nil} nil")) assert_equal("

nil

\n", render("%p.foo{{:class => 'bar'}, :class => nil} nil")) assert_equal("

nil

\n", render("%p.foo{{:class => nil}, :class => 'bar'} nil")) end def test_locals assert_equal("

Paragraph!

\n", render("%p= text", :locals => { :text => "Paragraph!" })) end def test_dynamic_attrs_shouldnt_register_as_literal_values assert_equal("

\n", render('%p{:a => "b#{1 + 1}c"}')) assert_equal("

\n", render("%p{:a => 'b' + (1 + 1).to_s + 'c'}")) end def test_dynamic_attrs_with_self_closed_tag assert_equal("\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n")) end EXCEPTION_MAP.each do |key, value| define_method("test_exception (#{key.inspect})") do begin silence_warnings do render(key, :filename => "(test_exception (#{key.inspect}))") end rescue Exception => err value = [value] unless value.is_a?(Array) expected_message, line_no = value line_no ||= key.split("\n").length if expected_message == :compile assert_match(/(compile error|syntax error|unterminated string|expecting)/, err.message, "Line: #{key}") else assert_equal(expected_message, err.message, "Line: #{key}") end else assert(false, "Exception not raised for\n#{key}") end end end def test_exception_line; skip # error render("a\nb\n!!!\n c\nd") rescue Haml::SyntaxError => e assert_equal("(test_exception_line):4", e.backtrace[0]) else assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce an exception') end def test_exception; skip # error render("%p\n hi\n %a= undefined\n= 12") rescue Exception => e skip backtrace = e.backtrace backtrace.shift if rubinius? assert_match("(test_exception):3", backtrace[0]) else # Test failed... should have raised an exception assert(false) end def test_compile_error; skip # error render("a\nb\n- fee)\nc") rescue Exception => e skip assert_match(/\(test_compile_error\):3:/i, e.message) assert_match(/(syntax error|expecting \$end)/i, e.message) else assert(false, '"a\nb\n- fee)\nc" doesn\'t produce an exception!') end def test_unbalanced_brackets; skip # error render('foo #{1 + 5} foo #{6 + 7 bar #{8 + 9}') rescue Hamlit::SyntaxError => e assert_equal(Hamlit::Error.message(:unbalanced_brackets), e.message) end def test_single_line_comments_are_interpolated; skip # comment assert_equal("\n", render('/ Hello #{1 + 1}')) end def test_single_line_comments_are_not_interpolated_with_suppress_eval; skip # comment assert_equal("\n", render('/ Hello #{1 + 1}', :suppress_eval => true)) end def test_single_line_comments_with_interpolation_dont_break_tabulation; skip # comment assert_equal("\nconcatted\n", render("/ Hello \#{1 + 1}\n- haml_concat 'concatted'")) end def test_balanced_conditional_comments assert_equal("\n", render("/[if !(IE 6)|(IE 7)] Bracket: ]")) end def test_downlevel_revealed_conditional_comments; skip assert_equal(" A comment \n", render("/![if !IE] A comment")) end def test_downlevel_revealed_conditional_comments_block; skip assert_equal("\n A comment\n\n", render("/![if !IE]\n A comment")) end def test_local_assigns_dont_modify_class assert_equal("bar\n", render("= foo", :locals => {:foo => 'bar'})) assert_equal(nil, defined?(foo)) end def test_object_ref_with_nil_id; skip # object reference user = User.new assert_equal("

New User

\n", render("%p[user] New User", :locals => {:user => user})) end def test_object_ref_before_attrs; skip # object reference user = User.new 42 assert_equal("

New User

\n", render("%p[user]{:style => 'width: 100px;'} New User", :locals => {:user => user})) end def test_object_ref_with_custom_haml_class; skip # object reference custom = CustomHamlClass.new 42 assert_equal("

My Thing

\n", render("%p[custom]{:style => 'width: 100px;'} My Thing", :locals => {:custom => custom})) end def test_object_ref_with_multiple_ids; skip # object reference cpk_record = CpkRecord.new([42,6,9]) assert_equal("

CPK Record

\n", render("%p[cpk_record]{:style => 'width: 100px;'} CPK Record", :locals => {:cpk_record => cpk_record})) end def test_non_literal_attributes assert_equal("

\n", render("%p{a2, a1, :a3 => 'baz'}", :locals => {:a1 => {:a1 => 'foo'}, :a2 => {:a2 => 'bar'}})) end def test_render_should_accept_a_binding_as_scope string = "This is a string!" string.instance_variable_set(:@var, "Instance variable") b = string.instance_eval do var = "Local variable" # Silence unavoidable warning; Ruby doesn't know we're going to use this # later. nil if var binding end assert_equal("

THIS IS A STRING!

\n

Instance variable

\n

Local variable

\n", render("%p= upcase\n%p= @var\n%p= var", :scope => b)) end def test_yield_should_work_with_binding; skip # options assert_equal("12\nFOO\n", render("= yield\n= upcase", :scope => "foo".instance_eval{binding}) { 12 }) end def test_yield_should_work_with_def_method; skip # def_method s = "foo" engine("= yield\n= upcase").def_method(s, :render) assert_equal("12\nFOO\n", s.render { 12 }) end def test_def_method_with_module; skip # def_method engine("= yield\n= upcase").def_method(String, :render_haml) assert_equal("12\nFOO\n", "foo".render_haml { 12 }) end def test_def_method_locals; skip # def_method obj = Object.new engine("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom) assert_equal("

1

\n
3
\n", obj.render(:foo => 1, :baz => 2, :boom => 3)) end def test_render_proc_locals; skip # render_proc proc = engine("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom) assert_equal("

1

\n
3
\n", proc[:foo => 1, :baz => 2, :boom => 3]) end def test_render_proc_with_binding; skip # render_proc assert_equal("FOO\n", engine("= upcase").render_proc("foo".instance_eval{binding}).call) end def test_haml_buffer_gets_reset_even_with_exception; skip # haml_buffer scope = Object.new render("- raise Hamlit::Error", :scope => scope) assert(false, "Expected exception") rescue Exception skip assert_nil(scope.send(:haml_buffer)) end def test_def_method_haml_buffer_gets_reset_even_with_exception; skip # def_method scope = Object.new engine("- raise Hamlit::Error").def_method(scope, :render) scope.render assert(false, "Expected exception") rescue Exception; skip assert_nil(scope.send(:haml_buffer)) end def test_render_proc_haml_buffer_gets_reset_even_with_exception; skip # render_proc scope = Object.new proc = engine("- raise Hamlit::Error").render_proc(scope) proc.call assert(false, "Expected exception") rescue Exception; skip assert_nil(scope.send(:haml_buffer)) end def test_render_proc_should_raise_haml_syntax_error_not_ruby_syntax_error assert_raises(Haml::SyntaxError) do Haml::Engine.new("%p{:foo => !}").render_proc(Object.new, :foo).call end end def test_render_should_raise_haml_syntax_error_not_ruby_syntax_error assert_raises(Haml::SyntaxError) do Haml::Engine.new("%p{:foo => !}").render end end def test_ugly_true assert_equal("
\n
\n

hello world

\n
\n
\n", render("#outer\n #inner\n %p hello world", :ugly => true)) assert_equal("

#{'s' * 75}

\n", render("%p #{'s' * 75}", :ugly => true)) assert_equal("

#{'s' * 75}

\n", render("%p= 's' * 75", :ugly => true)) end def test_remove_whitespace_true; skip # options assert_equal("

hello world

", render("#outer\n #inner\n %p hello world", :remove_whitespace => true)) assert_equal("

hello world

foo   bar\nbaz

", render(< true)) %p hello world %pre foo bar baz HAML assert_equal("
foo bar
", render('%div foo bar', :remove_whitespace => true)) end def test_auto_preserve_unless_ugly; skip # preserve assert_equal("
foo
bar
\n", render('%pre="foo\nbar"')) assert_equal("
foo\nbar
\n", render("%pre\n foo\n bar")) assert_equal("
foo\nbar
\n", render('%pre="foo\nbar"', :ugly => true)) assert_equal("
foo\nbar
\n", render("%pre\n foo\n bar", :ugly => true)) end def test_xhtml_output_option assert_equal "

\n
\n

\n", render("%p\n %br", :format => :xhtml) assert_equal "
\n", render("%a/", :format => :xhtml) end def test_arbitrary_output_option; skip # error assert_raises_message(Hamlit::Error, "Invalid output format :html1") do engine("%br", :format => :html1) end end def test_static_hashes; skip # attribute escape assert_equal("\n", render("%a{:b => 'a => b'}", :suppress_eval => true)) assert_equal("\n", render("%a{:b => 'a, b'}", :suppress_eval => true)) assert_equal("\n", render('%a{:b => "a\tb"}', :suppress_eval => true)) assert_equal("\n", render('%a{:b => "a\\#{foo}b"}', :suppress_eval => true)) assert_equal("\n", render("%a{:b => '#f00'}", :suppress_eval => true)) end def test_dynamic_hashes_with_suppress_eval; skip # options assert_equal("\n", render('%a{:b => "a #{1 + 1} b", :c => "d"}', :suppress_eval => true)) end def test_interpolates_instance_vars_in_attribute_values scope = Object.new scope.instance_variable_set :@foo, 'bar' assert_equal("\n", render('%a{:b => "a #@foo b"}', :scope => scope)) end def test_interpolates_global_vars_in_attribute_values # make sure the value isn't just interpolated in during template compilation engine = Haml::Engine.new('%a{:b => "a #$global_var_for_testing b"}') $global_var_for_testing = 'bar' assert_equal("\n", engine.to_html) ensure $global_var_for_testing = nil end def test_utf8_attrs assert_equal("\n", render("%a{:href => 'héllo'}")) assert_equal("\n", render("%a(href='héllo')")) end # HTML 4.0 def test_html_has_no_self_closing_tags assert_equal "

\n
\n

\n", render("%p\n %br", :format => :html4) assert_equal "
\n", render("%br/", :format => :html4) end def test_html_renders_empty_node_with_closing_tag assert_equal "
\n", render(".foo", :format => :html4) end def test_html_doesnt_add_slash_to_self_closing_tags assert_equal "\n", render("%a/", :format => :html4) assert_equal "\n", render("%a{:foo => 1 + 1}/", :format => :html4) assert_equal "\n", render("%meta", :format => :html4) assert_equal "\n", render("%meta{:foo => 1 + 1}", :format => :html4) end def test_html_ignores_xml_prolog_declaration assert_equal "", render('!!! XML', :format => :html4) end def test_html_has_different_doctype assert_equal %{\n}, render('!!!', :format => :html4) end # because anything before the doctype triggers quirks mode in IE def test_xml_prolog_and_doctype_dont_result_in_a_leading_whitespace_in_html refute_match(/^\s+/, render("!!! xml\n!!!", :format => :html4)) end # HTML5 def test_html5_doctype assert_equal %{\n}, render('!!!', :format => :html5) end # HTML5 custom data attributes def test_html5_data_attributes_without_hyphenation; skip # hyphenate assert_equal("
\n", render("%div{:data => {:author_id => 123, :foo => 'bar', :biz => 'baz'}}", :hyphenate_data_attrs => false)) assert_equal("
\n", render("%div{:data => {:one_plus_one => 1+1}}", :hyphenate_data_attrs => false)) assert_equal("
\n", render(%{%div{:data => {:foo => %{Here's a "quoteful" string.}}}}, :hyphenate_data_attrs => false)) #' end def test_html5_data_attributes_with_hyphens assert_equal("
\n", render("%div{:data => {:foo_bar => 'blip'}}")) assert_equal("
\n", render("%div{:data => {:foo_bar => 'blip', :baz => 'bang'}}")) end def test_html5_arbitrary_hash_valued_attributes_with assert_equal("
\n", render("%div{:aria => {:foo => 'blip'}}")) assert_equal("
\n", render("%div{:foo => {:baz => 'bang'}}")) end def test_arbitrary_attribute_hash_merging assert_equal(%Q{
\n}, render(<<-HAML)) - h1 = {:aria => {:foo => :bar}} - h2 = {:baz => :qux} %a{h1, :aria => h2} HAML end def test_html5_data_attributes_with_nested_hash assert_equal("
\n", render(<<-HAML)) - hash = {:a => {:b => 'c'}} - hash[:d] = hash %div{:data => hash} HAML end def test_html5_data_attributes_with_nested_hash_and_without_hyphenation; skip # hyphenate assert_equal("
\n", render(<<-HAML, :hyphenate_data_attrs => false)) - hash = {:a => {:b => 'c'}} - hash[:d] = hash %div{:data => hash} HAML end def test_html5_data_attributes_with_multiple_defs; skip # hyphenate # Should always use the more-explicit attribute assert_equal("
\n", render("%div{:data => {:foo => 'first'}, 'data-foo' => 'second'}")) assert_equal("
\n", render("%div{'data-foo' => 'first', :data => {:foo => 'second'}}")) end def test_html5_data_attributes_with_attr_method; skip # runtime attribute obj = Object.new def obj.data_hash {:data => {:foo => "bar", :baz => "bang"}} end def obj.data_val {:data => "dat"} end assert_equal("
\n", render("%div{data_hash, :data => {:foo => 'blip', :brat => 'wurst'}}", scope: obj)) assert_equal("
\n", render("%div{data_hash, 'data-foo' => 'blip'}", scope: obj)) assert_equal("
\n", render("%div{data_hash, :data => 'dat'}", scope: obj)) assert_equal("
\n", render("%div{data_val, :data => {:foo => 'blip', :brat => 'wurst'}}", scope: obj)) end def test_html5_data_attributes_with_identical_attribute_values assert_equal("
\n", render("%div{:data => {:x => 50, :y => 50}}")) end def test_xml_doc_using_html5_format_and_mime_type; skip # mime_type assert_equal(< :html5, :mime_type => 'text/xml' }))
XML !!! XML %root %element/ %hr HAML end def test_xml_doc_using_html4_format_and_mime_type; skip # mime_type assert_equal(< :html4, :mime_type => 'text/xml' }))
XML !!! XML %root %element/ %hr HAML end # New attributes def test_basic_new_attributes assert_equal("bar\n", render("%a() bar")) assert_equal("bar\n", render("%a(href='foo') bar")) assert_equal("baz\n", render(%q{%a(b="c" c='d' d="e") baz})) end def test_new_attribute_ids; skip # object reference assert_equal("
\n", render("#foo(id='bar')")) assert_equal("
\n", render("#foo{:id => 'bar'}(id='baz')")) assert_equal("
\n", render("#foo(id='baz'){:id => 'bar'}")) foo = User.new(42) assert_equal("
\n", render("#foo(id='baz'){:id => 'bar'}[foo]", :locals => {:foo => foo})) assert_equal("
\n", render("#foo(id='baz')[foo]{:id => 'bar'}", :locals => {:foo => foo})) assert_equal("
\n", render("#foo[foo](id='baz'){:id => 'bar'}", :locals => {:foo => foo})) assert_equal("
\n", render("#foo[foo]{:id => 'bar'}(id='baz')", :locals => {:foo => foo})) end def test_new_attribute_classes; skip # object reference assert_equal("
\n", render(".foo(class='bar')")) assert_equal("
\n", render(".foo{:class => 'bar'}(class='baz')")) assert_equal("
\n", render(".foo(class='baz'){:class => 'bar'}")) foo = User.new(42) assert_equal("
\n", render(".foo(class='baz'){:class => 'bar'}[foo]", :locals => {:foo => foo})) assert_equal("
\n", render(".foo[foo](class='baz'){:class => 'bar'}", :locals => {:foo => foo})) assert_equal("
\n", render(".foo[foo]{:class => 'bar'}(class='baz')", :locals => {:foo => foo})) end def test_dynamic_new_attributes assert_equal("bar\n", render("%a(href=foo) bar", :locals => {:foo => 12})) assert_equal("bar\n", render("%a(b=b c='13' d=d) bar", :locals => {:b => 12, :d => 14})) end def test_new_attribute_interpolation assert_equal("bar\n", render('%a(href="1#{1 + 1}") bar')) assert_equal("bar\n", render(%q{%a(href='2: #{1 + 1}, 3: #{foo}') bar}, :locals => {:foo => 3})) assert_equal(%Q{bar\n}, render('%a(href="1\#{1 + 1}") bar')) end def test_truthy_new_attributes; skip # xhtml assert_equal("bar\n", render("%a(href) bar", :format => :xhtml)) assert_equal("bar\n", render("%a(href bar='baz') bar", :format => :html5)) assert_equal("bar\n", render("%a(href=true) bar")) assert_equal("bar\n", render("%a(href=false) bar")) end def test_new_attribute_parsing; skip # attribute escape assert_equal("bar\n", render("%a(a2=b2) bar", :locals => {:b2 => 'b2'})) assert_equal(%Q{bar\n}, render(%q{%a(a="#{'foo"bar'}") bar})) #' assert_equal(%Q{bar\n}, render(%q{%a(a="#{"foo'bar"}") bar})) #' assert_equal(%Q{bar\n}, render(%q{%a(a='foo"bar') bar})) assert_equal(%Q{bar\n}, render(%q{%a(a="foo'bar") bar})) assert_equal("bar\n", render("%a(a:b='foo') bar")) assert_equal("bar\n", render("%a(a = 'foo' b = 'bar') bar")) assert_equal("bar\n", render("%a(a = foo b = bar) bar", :locals => {:foo => 'foo', :bar => 'bar'})) assert_equal("(b='bar')\n", render("%a(a='foo')(b='bar')")) assert_equal("baz\n", render("%a(a='foo)bar') baz")) assert_equal("baz\n", render("%a( a = 'foo' ) baz")) end def test_new_attribute_escaping; skip # attribute escape assert_equal(%Q{bar\n}, render(%q{%a(a="foo \" bar") bar})) assert_equal(%Q{bar\n}, render(%q{%a(a="foo \\\\\" bar") bar})) assert_equal(%Q{bar\n}, render(%q{%a(a='foo \' bar') bar})) assert_equal(%Q{bar\n}, render(%q{%a(a='foo \\\\\' bar') bar})) assert_equal(%Q{bar\n}, render(%q{%a(a="foo \\\\ bar") bar})) assert_equal(%Q{bar\n}, render(%q{%a(a="foo \#{1 + 1} bar") bar})) end def test_multiline_new_attribute assert_equal("bar\n", render("%a(a='b'\n c='d') bar")) assert_equal("bar\n", render("%a(a='b' b='c'\n c='d' d=e\n e='f' f='j') bar", :locals => {:e => 'e'})) end def test_new_and_old_attributes assert_equal("bar\n", render("%a(a='b'){:c => 'd'} bar")) assert_equal("bar\n", render("%a{:c => 'd'}(a='b') bar")) assert_equal("bar\n", render("%a(c='d'){:a => 'b'} bar")) assert_equal("bar\n", render("%a{:a => 'b'}(c='d') bar")) # Old-style always takes precedence over new-style, # because theoretically old-style could have arbitrary end-of-method-call syntax. assert_equal("bar\n", render("%a{:a => 'b'}(a='d') bar")) assert_equal("bar\n", render("%a(a='d'){:a => 'b'} bar")) assert_equal("bar\n", render("%a{:a => 'b',\n:b => 'c'}(c='d'\nd='e') bar")) locals = {:b => 'b', :d => 'd'} assert_equal("

\n", render("%p{:a => b}(c=d)", :locals => locals)) assert_equal("

\n", render("%p(a=b){:c => d}", :locals => locals)) end # Ruby Multiline def test_silent_ruby_multiline assert_equal(<foo

HTML - foo = ["bar", "baz", "bang"] = foo.join(", ") %p foo HAML end def test_loud_ruby_multiline assert_equal(<foo

bar

HTML = ["bar", "baz", "bang"].join(", ") %p foo %p bar HAML end def test_ruby_multiline_with_punctuated_methods_is_continuation assert_equal(<foo

bar

HTML = ["bar", " ".strip!, "".empty?, "bang"].join(", ") %p foo %p bar HAML end def test_ruby_character_literals_are_not_continuation html = ",\n,\n

foo

\n" assert_equal(html, render(<foo

bar

HTML &= ["bar<", "baz", "bang"].join(", ") %p foo %p bar HAML end def test_unescaped_loud_ruby_multiline assert_equal(< true)) bar<, baz, bang

foo

bar

HTML != ["bar<", "baz", "bang"].join(", ") %p foo %p bar HAML end def test_flattened_loud_ruby_multiline assert_equal(<bar baz bang

foo

bar

HTML ~ "
" + ["bar",
             "baz",
             "bang"].join("\\n") + "
" %p foo %p bar HAML end def test_loud_ruby_multiline_with_block; skip # block script assert_equal(<foo

bar

HTML = ["bar", "baz", "bang"].map do |str| - str.gsub("ba", "fa") %p foo %p bar HAML end def test_silent_ruby_multiline_with_block; skip # block script assert_equal(<foo

bar

HTML - ["bar", "baz", "bang"].map do |str| = str.gsub("ba", "fa") %p foo %p bar HAML end def test_ruby_multiline_in_tag assert_equal(<foo, bar, baz

foo

bar

HTML %p= ["foo", "bar", "baz"].join(", ") %p foo %p bar HAML end def test_escaped_ruby_multiline_in_tag; skip # script bug assert_equal(<foo<, bar, baz

foo

bar

HTML %p&= ["foo<", "bar", "baz"].join(", ") %p foo %p bar HAML end def test_unescaped_ruby_multiline_in_tag assert_equal(< true))

foo<, bar, baz

foo

bar

HTML %p!= ["foo<", "bar", "baz"].join(", ") %p foo %p bar HAML end def test_ruby_multiline_with_normal_multiline assert_equal(<foo

bar

HTML = "foo" + | "bar" + | ["bar", | "baz", "bang"].join(", ") %p foo %p bar HAML end def test_ruby_multiline_after_filter assert_equal(<foo

bar

HTML :plain foo bar = ["bar", "baz", "bang"].join(", ") %p foo %p bar HAML end # Encodings def test_utf_8_bom; skip # encoding assert_equal <

baz

HTML \xEF\xBB\xBF.foo %p baz HAML end def test_default_encoding assert_equal(Encoding.find("utf-8"), render(< "ascii-8bit"))

bâr

föö

HTML %p bâr %p föö HAML end def test_convert_template_render_proc assert_converts_template_properly {|e| e.render_proc.call} end def test_convert_template_render assert_converts_template_properly {|e| e.render} end def test_convert_template_def_method assert_converts_template_properly do |e| o = Object.new e.def_method(o, :render) o.render end end def test_encoding_error; skip # encoding render("foo\nbar\nb\xFEaz".force_encoding("utf-8")) assert(false, "Expected exception") rescue Hamlit::Error => e; skip assert_equal(3, e.line) assert_match(/Invalid .* character/, e.message) end def test_ascii_incompatible_encoding_error; skip # encoding template = "foo\nbar\nb_z".encode("utf-16le") template[9] = "\xFE".force_encoding("utf-16le") render(template) assert(false, "Expected exception") rescue Hamlit::Error => e assert_equal(3, e.line) assert_match(/Invalid .* character/, e.message) end def test_same_coding_comment_as_encoding assert_renders_encoded(<bâr

föö

HTML -# coding: utf-8 %p bâr %p föö HAML end def test_coding_comments; skip # encoding assert_valid_encoding_comment("-# coding: ibm866") assert_valid_encoding_comment("-# CodINg: IbM866") assert_valid_encoding_comment("-#coding:ibm866") assert_valid_encoding_comment("-# CodINg= ibm866") assert_valid_encoding_comment("-# foo BAR FAOJcoding: ibm866") assert_valid_encoding_comment("-# coding: ibm866 ASFJ (&(&#!$") assert_valid_encoding_comment("-# -*- coding: ibm866") assert_valid_encoding_comment("-# coding: ibm866 -*- coding: blah") assert_valid_encoding_comment("-# -*- coding: ibm866 -*-") assert_valid_encoding_comment("-# -*- encoding: ibm866 -*-") assert_valid_encoding_comment('-# -*- coding: "ibm866" -*-') assert_valid_encoding_comment("-#-*-coding:ibm866-*-") assert_valid_encoding_comment("-#-*-coding:ibm866-*-") assert_valid_encoding_comment("-# -*- foo: bar; coding: ibm866; baz: bang -*-") assert_valid_encoding_comment("-# foo bar coding: baz -*- coding: ibm866 -*-") assert_valid_encoding_comment("-# -*- coding: ibm866 -*- foo bar coding: baz") end def test_different_coding_than_system; skip # encoding assert_renders_encoded(<тАЬ

HTML %p тАЬ HAML end def test_block_spacing begin assert render(<<-HAML) - foo = ["bar", "baz", "kni"] - foo.each do | item | = item HAML rescue ::SyntaxError flunk("Should not have raised syntax error") end end def test_tracing; skip # options result = render('%p{:class => "hello"}', :trace => true, :filename => 'foo').strip assert_equal "

", result end private def assert_valid_encoding_comment(comment) assert_renders_encoded(<ЖЛЫ

тАЬ

HTML #{comment} %p ЖЛЫ %p тАЬ HAML end def assert_converts_template_properly engine = Haml::Engine.new(< "macRoman") %p bâr %p föö HAML assert_encoded_equal(<bâr

föö

HTML end def assert_renders_encoded(html, haml) result = render(haml) assert_encoded_equal html, result end def assert_encoded_equal(expected, actual) assert_equal expected.encoding, actual.encoding assert_equal expected, actual end end