From f570602c09256d739ad34ba1e1a726b42ac7f819 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 26 Sep 2010 19:24:30 -0300 Subject: [PATCH] More concat cleanup on tests --- test/action_view_extensions/builder_test.rb | 69 +++++++++++---------- test/components/error_test.rb | 4 +- test/components/hint_test.rb | 4 +- test/components/label_test.rb | 4 +- test/components/wrapper_test.rb | 8 +-- test/error_notification_test.rb | 4 +- test/form_builder_test.rb | 32 +++++----- test/inputs_test.rb | 4 +- test/support/misc_helpers.rb | 4 ++ 9 files changed, 71 insertions(+), 62 deletions(-) diff --git a/test/action_view_extensions/builder_test.rb b/test/action_view_extensions/builder_test.rb index 6056eb70..80f9e2d6 100644 --- a/test/action_view_extensions/builder_test.rb +++ b/test/action_view_extensions/builder_test.rb @@ -1,29 +1,34 @@ require 'test_helper' class BuilderTest < ActionView::TestCase + + def with_concat_form_for(object, &block) + concat form_for(object, &block) + end + # COLLECTION RADIO test 'collection radio accepts a collection and generate inputs from value method' do - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, [true, false], :to_s, :to_s - end) + end assert_select 'form input[type=radio][value=true]#user_active_true' assert_select 'form input[type=radio][value=false]#user_active_false' end test 'collection radio accepts a collection and generate inputs from label method' do - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, [true, false], :to_s, :to_s - end) + end assert_select 'form label.collection_radio[for=user_active_true]', 'true' assert_select 'form label.collection_radio[for=user_active_false]', 'false' end test 'collection radio accepts checked item' do - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, [[1, true], [0, false]], :last, :first, :checked => true - end) + end assert_select 'form input[type=radio][value=true][checked=checked]' assert_no_select 'form input[type=radio][value=false][checked=checked]' @@ -31,9 +36,9 @@ class BuilderTest < ActionView::TestCase test 'collection radio accepts multiple disabled items' do collection = [[1, true], [0, false], [2, 'other']] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, collection, :last, :first, :disabled => [true, false] - end) + end assert_select 'form input[type=radio][value=true][disabled=disabled]' assert_select 'form input[type=radio][value=false][disabled=disabled]' @@ -42,18 +47,18 @@ class BuilderTest < ActionView::TestCase test 'collection radio accepts single disable item' do collection = [[1, true], [0, false]] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, collection, :last, :first, :disabled => true - end) + end assert_select 'form input[type=radio][value=true][disabled=disabled]' assert_no_select 'form input[type=radio][value=false][disabled=disabled]' end test 'collection radio accepts html options as input' do - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_radio :active, [[1, true], [0, false]], :last, :first, {}, :class => 'radio' - end) + end assert_select 'form input[type=radio][value=true].radio#user_active_true' assert_select 'form input[type=radio][value=false].radio#user_active_false' @@ -62,9 +67,9 @@ class BuilderTest < ActionView::TestCase # COLLECTION CHECK BOX test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :id, :name - end) + end assert_select "form input[type=hidden][name='user[tag_ids][]'][value=]" assert_select 'form input#user_tag_ids_1[type=checkbox][value=1]' @@ -73,9 +78,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts a collection and generate a serie of checkboxes with labels for label method' do collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :id, :name - end) + end assert_select 'form label.collection_check_boxes[for=user_tag_ids_1]', 'Tag 1' assert_select 'form label.collection_check_boxes[for=user_tag_ids_2]', 'Tag 2' @@ -83,9 +88,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts selected values as :checked option' do collection = (1..3).map{|i| [i, "Tag #{i}"] } - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, :checked => [1, 3] - end) + end assert_select 'form input[type=checkbox][value=1][checked=checked]' assert_select 'form input[type=checkbox][value=3][checked=checked]' @@ -94,9 +99,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts a single checked value' do collection = (1..3).map{|i| [i, "Tag #{i}"] } - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, :checked => 3 - end) + end assert_select 'form input[type=checkbox][value=3][checked=checked]' assert_no_select 'form input[type=checkbox][value=1][checked=checked]' @@ -105,9 +110,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts multiple disabled items' do collection = (1..3).map{|i| [i, "Tag #{i}"] } - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => [1, 3] - end) + end assert_select 'form input[type=checkbox][value=1][disabled=disabled]' assert_select 'form input[type=checkbox][value=3][disabled=disabled]' @@ -116,9 +121,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts single disable item' do collection = (1..3).map{|i| [i, "Tag #{i}"] } - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => 1 - end) + end assert_select 'form input[type=checkbox][value=1][disabled=disabled]' assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]' @@ -127,9 +132,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts a proc to disabled items' do collection = (1..3).map{|i| [i, "Tag #{i}"] } - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, :disabled => proc { |i| i.first == 1 } - end) + end assert_select 'form input[type=checkbox][value=1][disabled=disabled]' assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]' @@ -138,9 +143,9 @@ class BuilderTest < ActionView::TestCase test 'collection check box accepts html options' do collection = [[1, 'Tag 1'], [2, 'Tag 2']] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.collection_check_boxes :tag_ids, collection, :first, :last, {}, :class => 'check' - end) + end assert_select 'form input.check[type=checkbox][value=1]' assert_select 'form input.check[type=checkbox][value=2]' @@ -148,11 +153,11 @@ class BuilderTest < ActionView::TestCase test 'collection check box with fields for' do collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')] - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.fields_for(:post) do |p| p.collection_check_boxes :tag_ids, collection, :id, :name end - end) + end assert_select 'form input#user_post_tag_ids_1[type=checkbox][value=1]' assert_select 'form input#user_post_tag_ids_2[type=checkbox][value=2]' @@ -163,10 +168,10 @@ class BuilderTest < ActionView::TestCase # SIMPLE FIELDS test 'simple fields for is available and yields an instance of FormBuilder' do - concat(form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.simple_fields_for(:posts) do |posts_form| assert posts_form.instance_of?(SimpleForm::FormBuilder) end - end) + end end end diff --git a/test/components/error_test.rb b/test/components/error_test.rb index 28c71327..e737eafe 100644 --- a/test/components/error_test.rb +++ b/test/components/error_test.rb @@ -3,14 +3,14 @@ require 'test_helper' class ErrorTest < ActionView::TestCase def with_error_for(object, attribute_name, type, options={}, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.attribute_name = attribute_name f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) f.input_type = type f.options = options SimpleForm::Inputs::Base.new(f).error.to_s - end) + end end test 'error should not generate content for attribute without errors' do diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index 045be480..b74aebf4 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -3,14 +3,14 @@ require 'test_helper' class HintTest < ActionView::TestCase def with_hint_for(object, attribute_name, type, options={}, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.attribute_name = attribute_name f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) f.input_type = type f.options = options SimpleForm::Inputs::Base.new(f).hint.to_s - end) + end end test 'hint should not be generated by default' do diff --git a/test/components/label_test.rb b/test/components/label_test.rb index e7dd5b3b..65404071 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -8,14 +8,14 @@ class LabelTest < ActionView::TestCase end def with_label_for(object, attribute_name, type, options={}) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.attribute_name = attribute_name f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) f.input_type = type f.options = options SimpleForm::Inputs::Base.new(f).label - end) + end end test 'label should generate a default humanized description' do diff --git a/test/components/wrapper_test.rb b/test/components/wrapper_test.rb index b0abb951..7b7abe0c 100644 --- a/test/components/wrapper_test.rb +++ b/test/components/wrapper_test.rb @@ -2,15 +2,15 @@ require 'test_helper' class WrapperTest < ActionView::TestCase def with_error_for(object, attribute_name, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.input attribute_name - end) + end end def with_form_for(object, *args, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.input(*args, &block) - end) + end end test 'wrapper should not have error class for attribute without errors' do diff --git a/test/error_notification_test.rb b/test/error_notification_test.rb index 2d8c035c..37d60eb9 100644 --- a/test/error_notification_test.rb +++ b/test/error_notification_test.rb @@ -3,9 +3,9 @@ require 'test_helper' class ErrorNotificationTest < ActionView::TestCase def with_error_notification_for(object, options={}, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.error_notification(options) - end) + end end test 'error notification is not generated when the object has no error' do diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 69ee8d70..d8968608 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -4,39 +4,39 @@ require 'test_helper' class FormBuilderTest < ActionView::TestCase def with_form_for(object, *args, &block) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.input(*args, &block) - end) + end end def with_button_for(object, *args) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.button(*args) - end) + end end def with_error_for(object, *args) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.error(*args) - end) + end end def with_hint_for(object, *args) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.hint(*args) - end) + end end def with_label_for(object, *args) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.label(*args) - end) + end end def with_association_for(object, *args) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.association(*args) - end) + end end # All @@ -299,16 +299,16 @@ class FormBuilderTest < ActionView::TestCase end test 'builder allows wrapper tag to be given on demand' do - concat(simple_form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.input :name, :wrapper_tag => :b - end) + end assert_select 'form b.required.string' end test 'builder allows wrapper class to be given on demand' do - concat(simple_form_for(@user) do |f| + with_concat_form_for(@user) do |f| f.input :name, :wrapper_class => :wrapper - end) + end assert_select 'form div.wrapper.required.string' end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 8decfc58..8d3a84b7 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -8,9 +8,9 @@ class InputTest < ActionView::TestCase end def with_input_for(object, attribute_name, type, options={}) - concat(simple_form_for(object) do |f| + with_concat_form_for(object) do |f| f.input(attribute_name, options.merge(:as => type)) - end) + end end # ALL diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index 3147bdb5..c87c1527 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -24,4 +24,8 @@ module MiscHelpers object.send :"#{key}=", value end end + + def with_concat_form_for(object, &block) + concat simple_form_for(object, &block) + end end