gem.gems.haml-3.1.1.test.haml.helper_test.rb Maven / Gradle / Ivy
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'
class ActionView::Base
def nested_tag
content_tag(:span) {content_tag(:div) {"something"}}
end
def wacky_form
form_tag("/foo") {"bar"}
end
end
module Haml::Helpers
def something_that_uses_haml_concat
haml_concat('foo').to_s
end
end
class HelperTest < Test::Unit::TestCase
Post = Struct.new('Post', :body, :error_field, :errors)
class PostErrors
def on(name)
return unless name == 'error_field'
["Really bad error"]
end
alias_method :full_messages, :on
def [](name)
on(name) || []
end
end
def setup
@base = ActionView::Base.new
@base.controller = ActionController::Base.new
if defined?(ActionController::Response)
# This is needed for >=3.0.0
@base.controller.response = ActionController::Response.new
end
@base.instance_variable_set('@post', Post.new("Foo bar\nbaz", nil, PostErrors.new))
end
def render(text, options = {})
if options == :action_view
@base.render :inline => text, :type => :haml
else
scope = options.delete :scope_object
Haml::Engine.new(text, options).to_html(scope ? scope : Object.new)
end
end
def test_flatten
assert_equal("FooBar", Haml::Helpers.flatten("FooBar"))
assert_equal("FooBar", Haml::Helpers.flatten("Foo\rBar"))
assert_equal("Foo
Bar", Haml::Helpers.flatten("Foo\nBar"))
assert_equal("Hello
World!
YOU ARE FLAT?
OMGZ!",
Haml::Helpers.flatten("Hello\nWorld!\nYOU ARE \rFLAT?\n\rOMGZ!"))
end
def test_list_of_should_render_correctly
assert_equal("1 \n2 \n", render("= list_of([1, 2]) do |i|\n = i"))
assert_equal("[1] \n", render("= list_of([[1]]) do |i|\n = i.inspect"))
assert_equal("\n Fee
\n A word!
\n \n\n Fi
\n A word!
\n \n\n Fo
\n A word!
\n \n\n Fum
\n A word!
\n \n",
render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
end
def test_buffer_access
assert(render("= buffer") =~ /#/)
assert_equal(render("= (buffer == _hamlout)"), "true\n")
end
def test_tabs
assert_equal("foo\n bar\nbaz\n", render("foo\n- tab_up\nbar\n- tab_down\nbaz"))
assert_equal(" tabbed
\n", render("- buffer.tabulation=5\n%p tabbed"))
end
def test_with_tabs
assert_equal(< "<%= flatten('Foo\\nBar') %>")
rescue NoMethodError, Haml::Util.av_template_class(:Error)
proper_behavior = true
end
assert(proper_behavior)
begin
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
rescue ArgumentError, NameError
proper_behavior = true
end
assert(proper_behavior)
end
def test_action_view_included
assert(Haml::Helpers.action_view?)
end
def test_form_tag
# This is usually provided by ActionController::Base.
def @base.protect_against_forgery?; false; end
assert_equal(<#{rails_form_opener}
bar
baz
HTML
#{rails_block_helper_char} form_tag 'foo' do
%p bar
%strong baz
HAML
end
def test_text_area
assert_equal(%(\n),
render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view))
assert_equal(%(\n),
render('= text_area :post, :body', :action_view))
assert_equal(%(Foo bar
baz
\n),
render('= content_tag "pre", "Foo bar\n baz"', :action_view))
end
def test_capture_haml
assert_equal(<13\\n"
HTML
- (foo = capture_haml(13) do |a|
%p= a
- end; nil)
= foo.dump
HAML
end
def test_content_tag_block
assert_equal(<bar
bar