test/helper_test.rb in haml-4.0.7 vs test/helper_test.rb in haml-4.1.0.alpha.2
- old
+ new
@@ -12,20 +12,10 @@
module Haml::Helpers
def something_that_uses_haml_concat
haml_concat('foo').to_s
end
-
- def render_something_with_haml_concat
- haml_concat "<p>"
- end
-
- def render_something_with_haml_tag_and_concat
- haml_tag 'p' do
- haml_concat '<foo>'
- end
- end
end
class HelperTest < MiniTest::Unit::TestCase
Post = Struct.new('Post', :body, :error_field, :errors)
class PostErrors
@@ -56,19 +46,10 @@
def render(text, options = {})
return @base.render :inline => text, :type => :haml if options == :action_view
super
end
- def test_rendering_with_escapes
- output = render(<<-HAML, :action_view)
-- render_something_with_haml_concat
-- render_something_with_haml_tag_and_concat
-- render_something_with_haml_concat
-HAML
- assert_equal("<p>\n<p>\n <foo>\n</p>\n<p>\n", output)
- end
-
def test_flatten
assert_equal("FooBar", Haml::Helpers.flatten("FooBar"))
assert_equal("FooBar", Haml::Helpers.flatten("Foo\rBar"))
@@ -138,20 +119,20 @@
end
def test_form_tag
# This is usually provided by ActionController::Base.
def @base.protect_against_forgery?; false; end
-
- rendered = render(<<HAML, :action_view)
+ assert_equal(<<HTML, render(<<HAML, :action_view))
+<form accept-charset="UTF-8" action="foo" method="post">#{rails_form_opener}
+ <p>bar</p>
+ <strong>baz</strong>
+</form>
+HTML
= form_tag 'foo' do
%p bar
%strong baz
HAML
- fragment = Nokogiri::HTML.fragment(rendered)
- assert_equal 'foo', fragment.css('form').first.attributes['action'].to_s
- assert_equal 'bar', fragment.css('form p').first.text.strip
- assert_equal 'baz', fragment.css('form strong').first.text.strip
end
def test_pre
assert_equal(%(<pre>Foo bar
 baz</pre>\n),
render('= content_tag "pre", "Foo bar\n baz"', :action_view))
@@ -242,14 +223,15 @@
refute_nil fragment.css('form div.field_with_errors label[for=post_error_field]').first
end
def test_form_tag_in_helper_with_string_block
def @base.protect_against_forgery?; false; end
- rendered = render('= wacky_form', :action_view)
- fragment = Nokogiri::HTML.fragment(rendered)
- assert_equal 'bar', fragment.text.strip
- assert_equal '/foo', fragment.css('form').first.attributes['action'].to_s
+ assert_equal(<<HTML, render(<<HAML, :action_view))
+<form accept-charset="UTF-8" action="/foo" method="post">#{rails_form_opener}bar</form>
+HTML
+= wacky_form
+HAML
end
def test_haml_tag_name_attribute_with_id
assert_equal("<p id='some_id'></p>\n", render("- haml_tag 'p#some_id'"))
end
@@ -304,14 +286,18 @@
def test_haml_tag_attribute_html_escaping
assert_equal("<p id='foo&bar'>baz</p>\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true))
end
- def test_haml_tag_autoclosed_tags_are_closed
- assert_equal("<br class='foo' />\n", render("- haml_tag :br, :class => 'foo'"))
+ def test_haml_tag_autoclosed_tags_are_closed_xhtml
+ assert_equal("<br class='foo' />\n", render("- haml_tag :br, :class => 'foo'", :format => :xhtml))
end
+ def test_haml_tag_autoclosed_tags_are_closed_html
+ assert_equal("<br class='foo'>\n", render("- haml_tag :br, :class => 'foo'", :format => :html5))
+ end
+
def test_haml_tag_with_class_array
assert_equal("<p class='a b'>foo</p>\n", render("- haml_tag :p, 'foo', :class => %w[a b]"))
assert_equal("<p class='a b c d'>foo</p>\n", render("- haml_tag 'p.c.d', 'foo', :class => %w[a b]"))
end
@@ -336,11 +322,12 @@
def test_haml_tag_raises_error_for_multiple_content
assert_raises(Haml::Error) { render("- haml_tag :p, 'foo' do\n bar") }
end
def test_haml_tag_flags
- assert_equal("<p />\n", render("- haml_tag :p, :/"))
+ assert_equal("<p />\n", render("- haml_tag :p, :/", :format => :xhtml))
+ assert_equal("<p>\n", render("- haml_tag :p, :/", :format => :html5))
assert_equal("<p>kumquat</p>\n", render("- haml_tag :p, :< do\n kumquat"))
assert_raises(Haml::Error) { render("- haml_tag :p, 'foo', :/") }
assert_raises(Haml::Error) { render("- haml_tag :p, :/ do\n foo") }
end
@@ -383,10 +370,45 @@
- haml_tag :p do
- haml_tag :strong, "Hi!"
HAML
end
+ def test_haml_tag_if_positive
+ assert_equal(<<HTML, render(<<HAML))
+<div class='conditional'>
+ <p>A para</p>
+</div>
+HTML
+- haml_tag_if true, '.conditional' do
+ %p A para
+HAML
+ end
+
+ def test_haml_tag_if_positive_with_attributes
+ assert_equal(<<HTML, render(<<HAML))
+<div class='conditional' foo='bar'>
+ <p>A para</p>
+</div>
+HTML
+- haml_tag_if true, '.conditional', {:foo => 'bar'} do
+ %p A para
+HAML
+ end
+
+ def test_haml_tag_if_negative
+ assert_equal(<<HTML, render(<<HAML))
+<p>A para</p>
+HTML
+- haml_tag_if false, '.conditional' do
+ %p A para
+HAML
+ end
+
+ def test_haml_tag_if_error_return
+ assert_raises(Haml::Error) { render("= haml_tag_if false, '.conditional' do\n %p Hello") }
+ end
+
def test_is_haml
assert(!ActionView::Base.new.is_haml?)
assert_equal("true\n", render("= is_haml?"))
assert_equal("true\n", render("= is_haml?", :action_view))
assert_equal("false", @base.render(:inline => '<%= is_haml? %>'))
@@ -557,19 +579,49 @@
assert_equal ""><& ", Haml::Helpers.escape_once('"><& ')
end
def test_escape_once_leaves_numeric_references
assert_equal ""><&  ", Haml::Helpers.escape_once('"><&  ') #decimal
- #assert_equal ""><&  ", Haml::Helpers.escape_once('"><&  ') #hexadecimal
+ assert_equal ""><&  ", Haml::Helpers.escape_once('"><&  ') #hexadecimal
end
def test_escape_once_encoding
old_stderr, $stderr = $stderr, StringIO.new
string = "\"><&\u00e9 "
assert_equal ""><&\u00e9 ", Haml::Helpers.escape_once(string)
assert $stderr.string == "", "html_escape shouldn't generate warnings with UTF-8 strings: #{$stderr.string}"
ensure
$stderr = old_stderr
+ end
+
+ def test_html_attrs_xhtml
+ assert_equal("<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'></html>\n",
+ render("%html{html_attrs}", :format => :xhtml))
+ end
+
+ def test_html_attrs_html4
+ assert_equal("<html lang='en-US'></html>\n",
+ render("%html{html_attrs}", :format => :html4))
+ end
+
+ def test_html_attrs_html5
+ assert_equal("<html lang='en-US'></html>\n",
+ render("%html{html_attrs}", :format => :html5))
+ end
+
+ def test_html_attrs_xhtml_other_lang
+ assert_equal("<html lang='es-AR' xml:lang='es-AR' xmlns='http://www.w3.org/1999/xhtml'></html>\n",
+ render("%html{html_attrs('es-AR')}", :format => :xhtml))
+ end
+
+ def test_html_attrs_html4_other_lang
+ assert_equal("<html lang='es-AR'></html>\n",
+ render("%html{html_attrs('es-AR')}", :format => :html4))
+ end
+
+ def test_html_attrs_html5_other_lang
+ assert_equal("<html lang='es-AR'></html>\n",
+ render("%html{html_attrs('es-AR')}", :format => :html5))
end
def test_escape_once_should_work_on_frozen_strings
begin
Haml::Helpers.escape_once('foo'.freeze)