require '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 < MiniTest::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 @base.view_paths << File.expand_path("../templates", __FILE__) 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 = {}) return @base.render :inline => text, :type => :haml if options == :action_view super 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("
A word!
\nA word!
\nA word!
\nA word!
\nA word!
\nA word!
\nA word!
\nA word!
\ntabbed
\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 = form_tag 'foo' do %p bar %strong baz HAML end def test_pre assert_equal(%(Foo bar baz\n), render('= content_tag "pre", "Foo bar\n baz"', :action_view)) end # Rails >= 3.2.3 adds a newline after opening textarea tags. def self.rails_text_area_helpers_emit_a_newline? major, minor, tiny = ActionPack::VERSION::MAJOR, ActionPack::VERSION::MINOR, ActionPack::VERSION::TINY major == 4 || ((major == 3) && (minor >= 2) && (tiny >= 3)) end def text_area_content_regex @text_area_content_regex ||= if self.class.rails_text_area_helpers_emit_a_newline? /<(textarea)[^>]*>\n(.*?)<\/\1>/im else /<(textarea)[^>]*>(.*?)<\/\1>/im end end def test_text_area_tag output = render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view) match_data = output.match(text_area_content_regex) assert_equal "Foo Bar Baz Boom", match_data[2] end def test_text_area output = render('= text_area :post, :body', :action_view) match_data = output.match(text_area_content_regex) assert_equal "Foo bar baz", match_data[2] end def test_partials_should_not_cause_textareas_to_be_indented # non-indentation of textareas rendered inside partials @base.instance_variable_set('@post', Post.new("Foo", nil, PostErrors.new)) output = render(".foo\n .bar\n = render '/text_area_helper'", :action_view) match_data = output.match(text_area_content_regex) assert_equal 'Foo', match_data[2] end if rails_text_area_helpers_emit_a_newline? def test_textareas_should_prerve_leading_whitespace # leading whitespace preservation @base.instance_variable_set('@post', Post.new(" Foo", nil, PostErrors.new)) output = render(".foo\n = text_area :post, :body", :action_view) match_data = output.match(text_area_content_regex) assert_equal ' Foo', match_data[2] end def test_textareas_should_prerve_leading_whitespace_in_partials # leading whitespace in textareas rendered inside partials @base.instance_variable_set('@post', Post.new(" Foo", nil, PostErrors.new)) output = render(".foo\n .bar\n = render '/text_area_helper'", :action_view) match_data = output.match(text_area_content_regex) assert_equal ' Foo', match_data[2] end end def test_capture_haml assert_equal(<13\\n" HTML - (foo = capture_haml(13) do |a| %p= a - end) = foo.inspect HAML end def test_content_tag_block assert_equal(<
bar
bar HTML = content_tag :div do %p bar %strong bar HAML end def test_content_tag_error_wrapping def @base.protect_against_forgery?; false; end output = render(<baz
\n", render("%p{:id => 'foo&bar'} baz", :escape_html => true)) end def test_haml_tag_autoclosed_tags_are_closed assert_equal("foo
\n", render("- haml_tag :p, 'foo', :class => %w[a b]")) assert_equal("foo
\n", render("- haml_tag 'p.c.d', 'foo', :class => %w[a b]")) end def test_haml_tag_with_id_array assert_equal("foo
\n", render("- haml_tag :p, 'foo', :id => %w[a b]")) assert_equal("foo
\n", render("- haml_tag 'p#c', 'foo', :id => %w[a b]")) end def test_haml_tag_with_data_hash assert_equal("foo
\n", render("- haml_tag :p, 'foo', :data => {:foo => 'bar', :baz => true}")) end def test_haml_tag_non_autoclosed_tags_arent_closed assert_equal("\n", render("- haml_tag :p")) end def test_haml_tag_renders_text_on_a_single_line assert_equal("#{'a' * 100}
\n", render("- haml_tag :p, 'a' * 100")) end 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("\n", render("- haml_tag :p, :/")) assert_equal("kumquat
\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_with_multiline_string assert_equal(< foo bar baz HTML %p - haml_concat "foo\\nbar\\nbaz" HAML end def test_haml_tag_with_ugly assert_equal(< true))Hi!
HTML - haml_tag :p do - haml_tag :strong, "Hi!" HAML 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 = "Foo Bar\nFoo\nBar\n", render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar")) end def test_find_and_preserve_with_block_and_tags assert_equal("
Foo\nBar\nFoo\nBar\n", render("= find_and_preserve([]) do\n %pre\n Foo\n Bar\n Foo\n Bar")) end def test_preserve_with_block assert_equal("
Foo BarFoo Bar\n", 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 "some tag content
", ActsLikeTag.new.to_s end def test_capture_with_nuke_outer assert_equal "\n*