#!/usr/bin/env ruby require File.dirname(__FILE__) + '/test_helper' class EngineTest < Test::Unit::TestCase def render(text, options = {}, &block) scope = options.delete(:scope) || Object.new locals = options.delete(:locals) || {} Haml::Engine.new(text, options).to_html(scope, locals, &block) end def test_empty_render_should_remain_empty assert_equal('', render('')) end def test_attributes_should_render_correctly assert_equal("
\n
", render(".atlantis{:style => 'ugly'}").chomp) end def test_ruby_code_should_work_inside_attributes author = 'hcatlin' assert_equal("

foo

", render("%p{:class => 1+2} foo").chomp) end def test_nil_should_render_empty_tag assert_equal("
\n
", 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_long_liner_should_not_print_on_one_line assert_equal("
\n #{'x' * 51}\n
", render("%div #{'x' * 51}").chomp) end def test_non_prerendered_one_liner assert_equal("

One line

\n", render("%p{:class => c} One line", :locals => {:c => 'awesome'})) end def test_non_prerendered_script_one_liner assert_equal("

One line

\n", render("%p{:class => c}= 'One line'", :locals => {:c => 'awesome'})) end def test_non_prerendered_long_script_one_liner assert_equal("

\n #{'x' * 60}\n

\n", render("%p{:class => c}= 'x' * 60", :locals => {:c => 'awesome'})) end def test_multi_render engine = Haml::Engine.new("%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_double_equals assert_equal("

Hello World

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

\n Hello World\n

\n", render("%p\n == Hello \#{who}", :locals => {:who => 'World'})) end def test_double_equals_in_the_middle_of_a_string assert_equal("\"title 'Title'. \"\n", render("== \"title '\#{\"Title\"}'. \"")) 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_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 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}")) end def test_boolean_attributes assert_equal("

\n

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

\n

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

\n

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

\n

\n", render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :xhtml)) 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 assert_equal("

foo & bar

\n", render("%p&= 'foo & bar'", :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'}", :escape_html => true)) assert_equal("
foo
\n", render(".atlantis{:style => 'ugly&stupid'} foo", :escape_html => true)) assert_equal("

foo

\n", render("%p.atlantis{:style => 'ugly&stupid'}= 'foo'", :escape_html => true)) end def test_dynamic_attributes_should_be_escaped assert_equal("\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}", :escape_html => true)) assert_equal("

foo

\n", render("%p{:width => nil, :src => '/foo.png', :alt => String.new} foo", :escape_html => true)) assert_equal("
foo
\n", render("%div{:width => nil, :src => '/foo.png', :alt => String.new}= 'foo'", :escape_html => true)) end def test_string_interpolation_should_be_esaped 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 # Options tests def test_stop_eval assert_equal("", render("= 'Hello'", :suppress_eval => true)) assert_equal("", render("- puts '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)) begin assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true)) rescue Haml::HamlError => err caught = true assert_equal('"ruby" filter is not defined!', err.message) end assert(caught, "Rendering a ruby filter without evaluating didn't throw an error!") end def test_attr_wrapper assert_equal("

\n

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

\n

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

\n

\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

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

\n

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

\n

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

\n

\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("

{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})) 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_deprecated_locals_option Kernel.module_eval do def warn_with_stub(msg); end alias_method :warn_without_stub, :warn alias_method :warn, :warn_with_stub end assert_equal("

Paragraph!

\n", Haml::Engine.new("%p= text", :locals => { :text => "Paragraph!" }).render) Kernel.module_eval { alias_method :warn, :warn_without_stub } end def test_dynamic_attrs_shouldnt_register_as_literal_values assert_equal("

\n

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

\n

\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_rec_merge hash1 = {1=>2, 3=>{5=>7, 8=>9}} hash2 = {4=>5, 3=>{5=>2, 16=>12}} hash3 = {1=>2, 4=>5, 3=>{5=>2, 8=>9, 16=>12}} hash1.rec_merge!(hash2) assert_equal(hash3, hash1) end def test_syntax_errors errs = [ "!!!\n a", "a\n b", "a\n:foo\nb", "/ a\n b", "% a", "%p a\n b", "a\n%p=\nb", "%p=\n a", "a\n%p~\nb", "a\n~\nb", "a\n~\n b", "%p~\n b", "%p/\n a", "%p\n \t%a b", "%a\n b\nc", "%a\n b\nc", ":notafilter\n This isn't\n a filter!", ".{} a", "\#{} a", ".= 'foo'", "%a/ b", "%p..class", "%p..#." ] errs.each do |err| begin render(err) rescue Exception => e assert(e.is_a?(Haml::Error), "#{err.dump} doesn't produce Haml::SyntaxError") else assert(false, "#{err.dump} doesn't produce an exception") end end end def test_syntax_error render("a\nb\n!!!\n c\nd") rescue Haml::SyntaxError => e assert_equal(e.message, "Illegal Nesting: Nesting within a header command is illegal.") assert_equal("(haml):3", e.backtrace[0]) rescue Exception => e assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce a Haml::SyntaxError') 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("(haml):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(/^compile error\n\(haml\):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_no_bluecloth Kernel.module_eval do def gem_original_require_with_bluecloth(file) raise LoadError if file == 'bluecloth' gem_original_require_without_bluecloth(file) end alias_method :gem_original_require_without_bluecloth, :gem_original_require alias_method :gem_original_require, :gem_original_require_with_bluecloth end begin assert_equal("

Foo

\t

- a\n- b

\n", Haml::Engine.new(":markdown\n Foo\n ===\n - a\n - b").to_html) rescue Haml::HamlError => e if e.message == "Can't run Markdown filter; required 'bluecloth' or 'redcloth', but none were found" puts "\nCouldn't require 'bluecloth' or 'redcloth'; skipping a test." else raise e end end Kernel.module_eval do alias_method :gem_original_require, :gem_original_require_without_bluecloth end end def test_no_redcloth Kernel.module_eval do def gem_original_require_with_redcloth(file) raise LoadError if file == 'redcloth' gem_original_require_without_redcloth(file) end alias_method :gem_original_require_without_redcloth, :gem_original_require alias_method :gem_original_require, :gem_original_require_with_redcloth end begin Haml::Engine.new(":redcloth\n _foo_").to_html rescue Haml::HamlError else assert(false, "No exception raised!") end Kernel.module_eval do alias_method :gem_original_require, :gem_original_require_without_redcloth end end def test_no_redcloth_or_bluecloth Kernel.module_eval do def gem_original_require_with_redcloth_and_bluecloth(file) raise LoadError if file == 'redcloth' || file == 'bluecloth' gem_original_require_without_redcloth_and_bluecloth(file) end alias_method :gem_original_require_without_redcloth_and_bluecloth, :gem_original_require alias_method :gem_original_require, :gem_original_require_with_redcloth_and_bluecloth end begin Haml::Engine.new(":markdown\n _foo_").to_html rescue Haml::HamlError else assert(false, "No exception raised!") end Kernel.module_eval do alias_method :gem_original_require, :gem_original_require_without_redcloth_and_bluecloth end 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 user = Struct.new('User', :id).new assert_equal("

New User

\n", render("%p[user] 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!

\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 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" Haml::Engine.new("= yield\n= upcase").def_method(s, :render) assert_equal("12\nFOO\n", s.render { 12 }) end def test_def_method_with_module Haml::Engine.new("= 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 Haml::Engine.new("%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 proc = Haml::Engine.new("%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 assert_equal("FOO\n", Haml::Engine.new("= upcase").render_proc("foo".instance_eval{binding}).call) 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_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 assert_raise(Haml::Error, "Invalid output format :html1") { Haml::Engine.new("%br", :format => :html1) } 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
\n}, render(".foo", :format => :html4) end def test_html_ignores_explicit_self_closing_declaration assert_equal "
\n\n", render("%a/", :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 assert_no_match /^\s+/, render("!!! xml\n!!!", :format => :html4) end # HTML5 def test_html5_doctype assert_equal %{\n}, render('!!!', :format => :html5) end end