#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'
class EngineTest < Test::Unit::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" => "Illegal nesting: nesting within a header command is illegal.",
"a\n b" => "Illegal nesting: nesting within plain text is illegal.",
"/ a\n b" => "Illegal nesting: nesting within a tag that already has content is illegal.",
"% a" => 'Invalid tag: "% a".',
"%p a\n b" => "Illegal nesting: content can't be both given on the same line as %p and nested within it.",
"%p=" => "There's no Ruby code for = to evaluate.",
"%p~" => "There's no Ruby code for ~ to evaluate.",
"~" => "There's no Ruby code for ~ to evaluate.",
"=" => "There's no Ruby code for = to evaluate.",
"%p/\n a" => "Illegal nesting: nesting within a self-closing tag is illegal.",
"%p\n\ta" => < foo Hello \n foo\n bar\n Hello World \n Hello World\n 0 1 2 foo bar baz boom \n \nFoo
bar
baz
\n",
render('%pre= "Foo\n bar\n baz"'))
assert_equal("\n",
render("%textarea #{'a' * 100}"))
assert_equal("
foo
\n", render('%p<= "\nfoo\n"')) assert_equal(<foo
HTML %p %p<= "\\nfoo\\n" HAML end def test_both_whitespace_nukes_work_together assert_equal(<Foo BarRESULT %p %q><= "Foo\\nBar" SOURCE end # Mostly a regression test def test_both_case_indentation_work_with_deeply_nested_code result = <
foo & bar
\n", render("%p&= 'foo & bar'", :escape_html => false)) end def test_ampersand_equals_should_escape_before_preserve 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 assert_equal("\n", render("%img.atlantis{:style => 'ugly&stupid'}")) 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 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("4&3
\n", render("%p== \#{2+2}&\#{2+1}", :escape_html => true)) assert_equal("4&3
\n", render("%p== \#{2+2}&\#{2+1}", :escape_html => false)) end def test_escaped_inline_string_interpolation assert_equal("4&3
\n", render("%p&== \#{2+2}&\#{2+1}", :escape_html => true)) assert_equal("4&3
\n", render("%p&== \#{2+2}&\#{2+1}", :escape_html => false)) end def test_unescaped_inline_string_interpolation assert_equal("4&3
\n", render("%p!== \#{2+2}&\#{2+1}", :escape_html => true)) assert_equal("4&3
\n", render("%p!== \#{2+2}&\#{2+1}", :escape_html => false)) end def test_escaped_string_interpolation assert_equal("\n 4&3\n
\n", render("%p\n &== \#{2+2}&\#{2+1}", :escape_html => true)) assert_equal("\n 4&3\n
\n", render("%p\n &== \#{2+2}&\#{2+1}", :escape_html => false)) end def test_unescaped_string_interpolation assert_equal("\n 4&3\n
\n", render("%p\n !== \#{2+2}&\#{2+1}", :escape_html => true)) assert_equal("\n 4&3\n
\n", render("%p\n !== \#{2+2}&\#{2+1}", :escape_html => false)) end def test_scripts_should_respect_escape_html_option 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 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 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 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 assert_match(/test:6/, e.backtrace.first) end end def test_stop_eval 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('!!! strict').strip) assert_equal('', render('!!! frameset').strip) assert_equal('', render('!!! mobile').strip) assert_equal('', render('!!! basic').strip) assert_equal('', render('!!! transitional').strip) assert_equal('', render('!!!').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 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 => '"')) end def test_attrs_parsed_correctly 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 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(" \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(" \n", render("%p.bar.foo{:class => nil} nil")) assert_equal(" \n", render("%p.foo{{:class => 'bar'}, :class => nil} nil")) assert_equal(" \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 def test_exceptions EXCEPTION_MAP.each do |key, value| begin render(key, :filename => "(exception test for #{key.inspect})") rescue Exception => err value = [value] unless value.is_a?(Array) expected_message, line_no = value line_no ||= key.split("\n").length line_reported = err.backtrace[0].gsub(/\(.+\):/, '').to_i assert_equal(expected_message, err.message, "Line: #{key}") assert_equal(line_no, line_reported, "Line: #{key}") else assert(false, "Exception not raised for\n#{key}") end end end def test_exception_line 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 render("%p\n hi\n %a= undefined\n= 12") rescue Exception => e assert_match("(test_exception):3", e.backtrace[0]) else # Test failed... should have raised an exception assert(false) end def test_compile_error render("a\nb\n- fee)\nc") rescue Exception => e assert_match(/\(test_compile_error\):3: syntax error/i, e.message) else assert(false, '"a\nb\n- fee)\nc" doesn\'t produce an exception!') end def test_unbalanced_brackets render('== #{1 + 5} foo #{6 + 7 bar #{8 + 9}') rescue Haml::SyntaxError => e assert_equal("Unbalanced brackets.", e.message) end def test_balanced_conditional_comments assert_equal("\n", render("/[if !(IE 6)|(IE 7)] Bracket: ]")) end def test_empty_filter assert_equal(<New User
\n", render("%p[user] New User", :locals => {:user => user})) end def test_object_ref_before_attrs user = User.new 42 assert_equal("New User
\n", render("%p[user]{:style => 'width: 100px;'} New User", :locals => {:user => user})) 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" binding end assert_equal("THIS IS A STRING!
\nInstance variable
\nLocal variable
\n", render("%p= upcase\n%p= @var\n%p= var", :scope => b)) end def test_yield_should_work_with_binding assert_equal("12\nFOO\n", render("= yield\n= upcase", :scope => "foo".instance_eval{binding}) { 12 }) end def test_yield_should_work_with_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 engine("= yield\n= upcase").def_method(String, :render_haml) assert_equal("12\nFOO\n", "foo".render_haml { 12 }) end def test_def_method_locals obj = Object.new engine("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom) assert_equal("1
\n \n", obj.render(:foo => 1, :baz => 2, :boom => 3)) end def test_render_proc_locals proc = engine("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom) assert_equal("1
\n \n", proc[:foo => 1, :baz => 2, :boom => 3]) end def test_render_proc_with_binding assert_equal("FOO\n", engine("= upcase").render_proc("foo".instance_eval{binding}).call) end def test_ugly_true assert_equal("hello world
\n#{'s' * 75}
\n", render("%p #{'s' * 75}", :ugly => true)) assert_equal("#{'s' * 75}
\n", render("%p= 's' * 75", :ugly => true)) end def test_auto_preserve_unless_ugly 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
\n