\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
def test_haml_tag_error_return
assert_raises(Haml::Error) { render("= haml_tag :p") }
end
def test_haml_tag_with_multiline_string
assert_equal(<
foo
bar
baz
HTML
- haml_tag :p, "foo\\nbar\\nbaz"
HAML
end
def test_haml_concat_inside_haml_tag_escaped_with_xss
assert_equal("
\n <>&\n
\n", render(<&"
HAML
end
def test_haml_concat_with_multiline_string
assert_equal(<
foo
bar
baz
HTML
%p
- haml_concat "foo\\nbar\\nbaz"
HAML
end
def test_haml_tag
assert_equal(<
Hi!
HTML
- haml_tag :p do
- haml_tag :strong, "Hi!"
HAML
end
def test_haml_tag_if_positive
assert_equal(<
A para
HTML
- haml_tag_if true, '.conditional' do
%p A para
HAML
end
def test_haml_tag_if_positive_with_attributes
assert_equal(<
A para
HTML
- haml_tag_if true, '.conditional', {:foo => 'bar'} do
%p A para
HAML
end
def test_haml_tag_if_negative
assert_equal(<A para
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? %>'))
assert_equal("false\n", render("= render :inline => '<%= is_haml? %>'", :action_view))
end
def test_page_class
controller = Struct.new(:controller_name, :action_name).new('troller', 'tion')
scope = Struct.new(:controller).new(controller)
result = render("%div{:class => page_class} MyDiv", :scope => scope)
expected = "
MyDiv
\n"
assert_equal expected, result
end
def test_indented_capture
assert_equal(" Foo\n ", @base.render(:inline => " <% res = capture do %>\n Foo\n <% end %><%= res %>"))
end
def test_capture_deals_properly_with_collections
obj = Object.new
def obj.trc(collection, &block)
collection.each do |record|
haml_concat capture_haml(record, &block)
end
end
assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect", scope: obj))
end
def test_capture_with_string_block
assert_equal("foo\n", render("= capture { 'foo' }", :action_view))
end
def test_capture_with_non_string_value_reurns_nil
def @base.check_capture_returns_nil(&block)
contents = capture(&block)
contents << "ERROR" if contents
end
assert_equal("\n", render("= check_capture_returns_nil { 2 }", :action_view))
end
class HomemadeViewContext
include ActionView::Context
include ActionView::Helpers::FormHelper
def initialize
_prepare_context
end
def url_for(*)
"/"
end
def dom_class(*)
end
def dom_id(*)
end
def m # I have to inject the model into the view using an instance method, using locals doesn't work.
FormModel.new
end
def protect_against_forgery?
end
# def capture(*args, &block)
# capture_haml(*args, &block)
# end
end
def test_form_for_with_homemade_view_context
handler = ActionView::Template.handler_for_extension("haml")
template = ActionView::Template.new(< "/") do
%b Bold!
HAML
# see if Bold is within form tags:
assert_match(/.*Bold!<\/b>.*<\/form>/m, template.render(HomemadeViewContext.new, {}))
end
def test_find_and_preserve_with_block
assert_equal("
Foo
Bar",
render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar"))
end
def test_init_haml_helpers
context = Object.new
class << context
include Haml::Helpers
end
context.init_haml_helpers
result = context.capture_haml do
context.haml_tag :p, :attr => "val" do
context.haml_concat "Blah"
end
end
assert_equal("
\n Blah\n
\n", result)
end
def test_non_haml
assert_equal("false\n", render("= non_haml { is_haml? }"))
end
def test_content_tag_nested
assert_equal "
something
", render("= nested_tag", :action_view).strip
end
def test_error_return
assert_raises(Haml::Error, < e
assert_equal 2, e.backtrace[0].scan(/:(\d+)/).first.first.to_i
end
def test_error_return_line_in_helper
obj = Object.new
def obj.something_that_uses_haml_concat
haml_concat('foo').to_s
end
render("- something_that_uses_haml_concat", scope: obj)
assert false, "Expected Haml::Error"
rescue Haml::Error => e
assert_equal __LINE__ - 6, e.backtrace[0].scan(/:(\d+)/).first.first.to_i
end
class ActsLikeTag
# We want to be able to have people include monkeypatched ActionView helpers
# without redefining is_haml?.
# This is accomplished via Object#is_haml?, and this is a test for it.
include ActionView::Helpers::TagHelper
def to_s
content_tag :p, 'some tag content'
end
end
def test_random_class_includes_tag_helper
assert_equal "
some tag content
", ActsLikeTag.new.to_s
end
def test_capture_with_nuke_outer
assert_equal "\n*
hi there!
\n", render(< hi there!
HAML
assert_equal "\n*
hi there!
\n", render(< hi there!
HAML
end
def test_html_escape
assert_equal ""><&", Haml::Helpers.html_escape('"><&')
end
def test_html_escape_should_work_on_frozen_strings
begin
assert Haml::Helpers.html_escape('foo'.freeze)
rescue => e
flunk e.message
end
end
def test_html_escape_encoding
old_stderr, $stderr = $stderr, StringIO.new
string = "\"><&\u00e9" # if you're curious, u00e9 is "LATIN SMALL LETTER E WITH ACUTE"
assert_equal ""><&\u00e9", Haml::Helpers.html_escape(string)
assert $stderr.string == "", "html_escape shouldn't generate warnings with UTF-8 strings: #{$stderr.string}"
ensure
$stderr = old_stderr
end
def test_html_escape_non_string
assert_equal('4.58', Haml::Helpers.html_escape(4.58))
assert_equal('4.58', Haml::Helpers.html_escape_without_haml_xss(4.58))
end
def test_escape_once
assert_equal ""><&", Haml::Helpers.escape_once('"><&')
end
def test_escape_once_leaves_entity_references
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
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("\n",
render("%html{html_attrs}", :format => :xhtml))
end
def test_html_attrs_html4
assert_equal("\n",
render("%html{html_attrs}", :format => :html4))
end
def test_html_attrs_html5
assert_equal("\n",
render("%html{html_attrs}", :format => :html5))
end
def test_html_attrs_xhtml_other_lang
assert_equal("\n",
render("%html{html_attrs('es-AR')}", :format => :xhtml))
end
def test_html_attrs_html4_other_lang
assert_equal("\n",
render("%html{html_attrs('es-AR')}", :format => :html4))
end
def test_html_attrs_html5_other_lang
assert_equal("\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)
rescue => e
flunk e.message
end
end
end