require 'abstract_unit' class FormTagHelperTest < ActionView::TestCase tests ActionView::Helpers::FormTagHelper # include ActiveSupport::Configurable # DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG def setup super @controller = BasicController.new end def url_for(options) if options.is_a?(Hash) "http://www.example.com" else super end end VALID_HTML_ID = /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name def test_check_box_tag actual = check_box_tag "admin" expected = %() assert_dom_equal expected, actual end def test_check_box_tag_id_sanitized label_elem = root_elem(check_box_tag("project[2][admin]")) assert_match VALID_HTML_ID, label_elem['id'] end def test_form_tag actual = form_tag expected = %(
) assert_dom_equal expected, actual end def test_form_tag_multipart actual = form_tag({}, { 'multipart' => true }) expected = %() assert_dom_equal expected, actual end def test_form_tag_with_method_put actual = form_tag({}, { :method => :put }) expected = %(
) assert_dom_equal expected, actual end def test_form_tag_with_method_delete actual = form_tag({}, { :method => :delete }) expected = %(
) assert_dom_equal expected, actual end def test_form_tag_with_remote actual = form_tag({}, :remote => true) expected = %() assert_dom_equal expected, actual end def test_form_tag_with_block_in_erb output_buffer = form_tag("http://example.com") { concat "Hello world!" } expected = %(Hello world!
) assert_dom_equal expected, output_buffer end def test_form_tag_with_block_and_method_in_erb output_buffer = form_tag("http://example.com", :method => :put) { concat "Hello world!" } expected = %(
Hello world!
) assert_dom_equal expected, output_buffer end def test_hidden_field_tag actual = hidden_field_tag "id", 3 expected = %() assert_dom_equal expected, actual end def test_hidden_field_tag_id_sanitized input_elem = root_elem(hidden_field_tag("item[][title]")) assert_match VALID_HTML_ID, input_elem['id'] end def test_file_field_tag assert_dom_equal "", file_field_tag("picsplz") end def test_file_field_tag_with_options assert_dom_equal "", file_field_tag("picsplz", :class => "pix") end def test_password_field_tag actual = password_field_tag expected = %() assert_dom_equal expected, actual end def test_radio_button_tag actual = radio_button_tag "people", "david" expected = %() assert_dom_equal expected, actual actual = radio_button_tag("num_people", 5) expected = %() assert_dom_equal expected, actual actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f") expected = %() assert_dom_equal expected, actual actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1") expected = %() assert_dom_equal expected, actual actual = radio_button_tag("person[gender]", "m") expected = %() assert_dom_equal expected, actual actual = radio_button_tag('ctrlname', 'apache2.2') expected = %() assert_dom_equal expected, actual end def test_select_tag actual = select_tag "people", "".html_safe expected = %() assert_dom_equal expected, actual end def test_select_tag_with_multiple actual = select_tag "colors", "".html_safe, :multiple => :true expected = %() assert_dom_equal expected, actual end def test_select_tag_disabled actual = select_tag "places", "".html_safe, :disabled => :true expected = %() assert_dom_equal expected, actual end def test_select_tag_id_sanitized input_elem = root_elem(select_tag("project[1]people", "")) assert_match VALID_HTML_ID, input_elem['id'] end def test_select_tag_with_include_blank actual = select_tag "places", "".html_safe, :include_blank => true expected = %() assert_dom_equal expected, actual end def test_select_tag_with_include_blank_with_string actual = select_tag "places", "".html_safe, :include_blank => "string" expected = %() assert_dom_equal expected, actual end def test_select_tag_with_array_options assert_deprecated /array/ do select_tag "people", [""] end end def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_size_symbol actual = text_area_tag "body", "hello world", :size => "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer actual = text_area_tag "body", "hello world", :size => 20 expected = %() assert_dom_equal expected, actual end def test_text_area_tag_id_sanitized input_elem = root_elem(text_area_tag("item[][description]")) assert_match VALID_HTML_ID, input_elem['id'] end def test_text_area_tag_escape_content actual = text_area_tag "body", "hello world", :size => "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_unescaped_content actual = text_area_tag "body", "hello world", :size => "20x40", :escape => false expected = %() assert_dom_equal expected, actual end def test_text_field_tag actual = text_field_tag "title", "Hello!" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_class_string actual = text_field_tag "title", "Hello!", "class" => "admin" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_size_symbol actual = text_field_tag "title", "Hello!", :size => 75 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_size_string actual = text_field_tag "title", "Hello!", "size" => "75" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_maxlength_symbol actual = text_field_tag "title", "Hello!", :maxlength => 75 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_maxlength_string actual = text_field_tag "title", "Hello!", "maxlength" => "75" expected = %() assert_dom_equal expected, actual end def test_text_field_disabled actual = text_field_tag "title", "Hello!", :disabled => :true expected = %() assert_dom_equal expected, actual end def test_text_field_tag_with_multiple_options actual = text_field_tag "title", "Hello!", :size => 70, :maxlength => 80 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_id_sanitized input_elem = root_elem(text_field_tag("item[][title]")) assert_match VALID_HTML_ID, input_elem['id'] end def test_label_tag_without_text actual = label_tag "title" expected = %() assert_dom_equal expected, actual end def test_label_tag_with_symbol actual = label_tag :title expected = %() assert_dom_equal expected, actual end def test_label_tag_with_text actual = label_tag "title", "My Title" expected = %() assert_dom_equal expected, actual end def test_label_tag_class_string actual = label_tag "title", "My Title", "class" => "small_label" expected = %() assert_dom_equal expected, actual end def test_label_tag_id_sanitized label_elem = root_elem(label_tag("item[title]")) assert_match VALID_HTML_ID, label_elem['for'] end def test_boolean_options assert_dom_equal %(), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes") assert_dom_equal %(), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil) assert_dom_equal %(), tag(:input, :type => "checkbox", :checked => false) assert_dom_equal %(), select_tag("people", "".html_safe, :multiple => true) assert_dom_equal %(), select_tag("people[]", "".html_safe, :multiple => true) assert_dom_equal %(), select_tag("people", "".html_safe, :multiple => nil) end def test_stringify_symbol_keys actual = text_field_tag "title", "Hello!", :id => "admin" expected = %() assert_dom_equal expected, actual end def test_submit_tag assert_dom_equal( %(), submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')") ) end def test_submit_tag_with_no_onclick_options assert_dom_equal( %(), submit_tag("Save", :disable_with => "Saving...") ) end def test_submit_tag_with_confirmation assert_dom_equal( %(), submit_tag("Save", :confirm => "Are you sure?") ) end def test_submit_tag_with_confirmation_and_with_disable_with assert_dom_equal( %(), submit_tag("Save", :disable_with => "Saving...", :confirm => "Are you sure?") ) end def test_image_submit_tag_with_confirmation assert_dom_equal( %(), image_submit_tag("save.gif", :confirm => "Are you sure?") ) end def test_pass assert_equal 1, 1 end def test_field_set_tag_in_erb output_buffer = field_set_tag("Your details") { concat "Hello world!" } expected = %(
Your detailsHello world!
) assert_dom_equal expected, output_buffer output_buffer = field_set_tag { concat "Hello world!" } expected = %(
Hello world!
) assert_dom_equal expected, output_buffer output_buffer = field_set_tag('') { concat "Hello world!" } expected = %(
Hello world!
) assert_dom_equal expected, output_buffer output_buffer = field_set_tag('', :class => 'format') { concat "Hello world!" } expected = %(
Hello world!
) assert_dom_equal expected, output_buffer end def protect_against_forgery? false end private def root_elem(rendered_content) HTML::Document.new(rendered_content).root.children[0] end end