vendor/plugins/haml/test/haml/engine_test.rb in radiant-0.6.7 vs vendor/plugins/haml/test/haml/engine_test.rb in radiant-0.6.8
- old
+ new
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
-require File.dirname(__FILE__) + '/test_helper'
+require File.dirname(__FILE__) + '/../test_helper'
class EngineTest < Test::Unit::TestCase
# A map of erroneous Sass 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.
@@ -30,15 +30,27 @@
".#" => "Illegal element: classes and ids must have values.",
".{} a" => "Illegal element: classes and ids must have values.",
".= a" => "Illegal element: classes and ids must have values.",
"%p..a" => "Illegal element: classes and ids must have values.",
"%a/ b" => "Self-closing tags can't have content.",
+ " %p foo" => "Indenting at the beginning of the document is illegal.",
+ " %p foo" => "Indenting at the beginning of the document is illegal.",
+ " \n\t\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3],
# Regression tests
"- raise 'foo'\n\n\n\nbar" => ["foo", 1],
"= 'foo'\n-raise 'foo'" => ["foo", 2],
"\n\n\n- raise 'foo'" => ["foo", 4],
+ "foo\n\n\n bar" => ["Illegal nesting: nesting within plain text is illegal.", 4],
+ "%p/\n\n bar" => ["Illegal nesting: nesting within a self-closing tag is illegal.", 3],
+ "%p foo\n\n bar" => ["Illegal nesting: content can't be both given on the same line as %p and nested within it.", 3],
+ "/ foo\n\n bar" => ["Illegal nesting: nesting within a tag that already has content is illegal.", 3],
+ "!!!\n\n bar" => ["Illegal nesting: nesting within a header command is illegal.", 3],
+ "foo\n\n\n\tbar" => [<<END.strip, 4],
+A tab character was used for indentation. Haml must be indented using two spaces.
+Are you sure you have soft tabs enabled in your editor?
+END
}
User = Struct.new('User', :id)
def render(text, options = {}, &block)
@@ -50,20 +62,20 @@
def test_empty_render_should_remain_empty
assert_equal('', render(''))
end
def test_attributes_should_render_correctly
- assert_equal("<div class='atlantis' style='ugly'>\n</div>", render(".atlantis{:style => 'ugly'}").chomp)
+ assert_equal("<div class='atlantis' style='ugly'></div>", render(".atlantis{:style => 'ugly'}").chomp)
end
def test_ruby_code_should_work_inside_attributes
author = 'hcatlin'
assert_equal("<p class='3'>foo</p>", render("%p{:class => 1+2} foo").chomp)
end
def test_nil_should_render_empty_tag
- assert_equal("<div class='no_attributes'>\n</div>",
+ assert_equal("<div class='no_attributes'></div>",
render(".no_attributes{:nil => nil}").chomp)
end
def test_strings_should_get_stripped_inside_tags
assert_equal("<div class='stripped'>This should have no spaces in front of it</div>",
@@ -126,24 +138,42 @@
assert_equal("<pre>Foo
 bar
 baz</pre>\n",
render('%pre= "Foo\n bar\n baz"'))
assert_equal("<textarea>#{'a' * 100}</textarea>\n",
render("%textarea #{'a' * 100}"))
+
+ assert_equal("<p>\n <textarea>Foo\n Bar\n Baz</textarea>\n</p>\n", render(<<SOURCE))
+%p
+ %textarea
+ Foo
+ Bar
+ Baz
+SOURCE
end
def test_boolean_attributes
- assert_equal("<p bar baz='true' foo='bar'>\n</p>\n",
+ assert_equal("<p bar baz='true' foo='bar'></p>\n",
render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :html4))
- assert_equal("<p bar='bar' baz='true' foo='bar'>\n</p>\n",
+ assert_equal("<p bar='bar' baz='true' foo='bar'></p>\n",
render("%p{:foo => 'bar', :bar => true, :baz => 'true'}", :format => :xhtml))
- assert_equal("<p baz='false' foo='bar'>\n</p>\n",
+ assert_equal("<p baz='false' foo='bar'></p>\n",
render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :html4))
- assert_equal("<p baz='false' foo='bar'>\n</p>\n",
+ assert_equal("<p baz='false' foo='bar'></p>\n",
render("%p{:foo => 'bar', :bar => false, :baz => 'false'}", :format => :xhtml))
end
+ def test_both_whitespace_nukes_work_together
+ assert_equal(<<RESULT, render(<<SOURCE))
+<p><q>Foo
+ Bar</q></p>
+RESULT
+%p
+ %q><= "Foo\\nBar"
+SOURCE
+ end
+
# HTML escaping tests
def test_ampersand_equals_should_escape
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n &= 'foo & bar'", :escape_html => false))
end
@@ -239,33 +269,26 @@
assert_equal("", render("= 'Hello'", :suppress_eval => true))
assert_equal("", render("- puts 'foo'", :suppress_eval => true))
assert_equal("<div id='foo' yes='no' />\n", render("#foo{:yes => 'no'}/", :suppress_eval => true))
assert_equal("<div id='foo' />\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true))
assert_equal("<div />\n", render("%div[1]/", :suppress_eval => true))
-
- begin
- assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true))
- rescue Haml::Error => err
- caught = true
- assert_equal('Filter "ruby" is not defined.', err.message)
- end
- assert(caught, "Rendering a ruby filter without evaluating didn't throw an error!")
+ assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true))
end
def test_attr_wrapper
- assert_equal("<p strange=*attrs*>\n</p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
- assert_equal("<p escaped='quo\"te'>\n</p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
- assert_equal("<p escaped=\"quo'te\">\n</p>\n", render("%p{ :escaped => 'quo\\'te'}", :attr_wrapper => '"'))
- assert_equal("<p escaped=\"q'uo"te\">\n</p>\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"'))
+ assert_equal("<p strange=*attrs*></p>\n", render("%p{ :strange => 'attrs'}", :attr_wrapper => '*'))
+ assert_equal("<p escaped='quo\"te'></p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
+ assert_equal("<p escaped=\"quo'te\"></p>\n", render("%p{ :escaped => 'quo\\'te'}", :attr_wrapper => '"'))
+ assert_equal("<p escaped=\"q'uo"te\"></p>\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"'))
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n", render("!!! XML", :attr_wrapper => '"'))
end
def test_attrs_parsed_correctly
- assert_equal("<p boom=>biddly='bar => baz'>\n</p>\n", render("%p{'boom=>biddly' => 'bar => baz'}"))
- assert_equal("<p foo,bar='baz, qux'>\n</p>\n", render("%p{'foo,bar' => 'baz, qux'}"))
- assert_equal("<p escaped='quo\nte'>\n</p>\n", render("%p{ :escaped => \"quo\\nte\"}"))
- assert_equal("<p escaped='quo4te'>\n</p>\n", render("%p{ :escaped => \"quo\#{2 + 2}te\"}"))
+ assert_equal("<p boom=>biddly='bar => baz'></p>\n", render("%p{'boom=>biddly' => 'bar => baz'}"))
+ assert_equal("<p foo,bar='baz, qux'></p>\n", render("%p{'foo,bar' => 'baz, qux'}"))
+ assert_equal("<p escaped='quo\nte'></p>\n", render("%p{ :escaped => \"quo\\nte\"}"))
+ assert_equal("<p escaped='quo4te'></p>\n", render("%p{ :escaped => \"quo\#{2 + 2}te\"}"))
end
def test_correct_parsing_with_brackets
assert_equal("<p class='foo'>{tada} foo</p>\n", render("%p{:class => 'foo'} {tada} foo"))
assert_equal("<p class='foo'>deep {nested { things }}</p>\n", render("%p{:class => 'foo'} deep {nested { things }}"))
@@ -311,25 +334,13 @@
def test_locals
assert_equal("<p>Paragraph!</p>\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("<p>Paragraph!</p>\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("<p a='b2c'>\n</p>\n", render('%p{:a => "b#{1 + 1}c"}'))
- assert_equal("<p a='b2c'>\n</p>\n", render("%p{:a => 'b' + (1 + 1).to_s + 'c'}"))
+ assert_equal("<p a='b2c'></p>\n", render('%p{:a => "b#{1 + 1}c"}'))
+ assert_equal("<p a='b2c'></p>\n", render("%p{:a => 'b' + (1 + 1).to_s + 'c'}"))
end
def test_dynamic_attrs_with_self_closed_tag
assert_equal("<a b='2' />\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n"))
end
@@ -540,10 +551,17 @@
assert_equal("<p>#{'s' * 75}</p>\n",
render("%p= 's' * 75", :ugly => true))
end
+ def test_auto_preserve_unless_ugly
+ assert_equal("<pre>foo
bar</pre>\n", render('%pre="foo\nbar"'))
+ assert_equal("<pre>foo\nbar</pre>\n", render("%pre\n foo\n bar"))
+ assert_equal("<pre>foo\nbar</pre>\n", render('%pre="foo\nbar"', :ugly => true))
+ assert_equal("<pre>foo\nbar</pre>\n", render("%pre\n foo\n bar", :ugly => true))
+ end
+
def test_xhtml_output_option
assert_equal "<p>\n <br />\n</p>\n", render("%p\n %br", :format => :xhtml)
assert_equal "<a />\n", render("%a/", :format => :xhtml)
end
@@ -557,14 +575,14 @@
assert_equal "<p>\n <br>\n</p>\n", render("%p\n %br", :format => :html4)
assert_equal "<br>\n", render("%br/", :format => :html4)
end
def test_html_renders_empty_node_with_closing_tag
- assert_equal %{<div class='foo'>\n</div>\n}, render(".foo", :format => :html4)
+ assert_equal "<div class='foo'></div>\n", render(".foo", :format => :html4)
end
def test_html_ignores_explicit_self_closing_declaration
- assert_equal "<a>\n</a>\n", render("%a/", :format => :html4)
+ assert_equal "<a></a>\n", render("%a/", :format => :html4)
end
def test_html_ignores_xml_prolog_declaration
assert_equal "", render('!!! XML', :format => :html4)
end