From 1c70af4dbac96284b26aeded00952f0b9e6b40b0 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 23 Feb 2011 20:05:09 +1100 Subject: [PATCH] simplify data select spec --- .../extensions/date_time_select_spec.rb | 121 +++++++----------- 1 file changed, 47 insertions(+), 74 deletions(-) diff --git a/spec/validates_timeliness/extensions/date_time_select_spec.rb b/spec/validates_timeliness/extensions/date_time_select_spec.rb index d8ce31a..2ac9796 100644 --- a/spec/validates_timeliness/extensions/date_time_select_spec.rb +++ b/spec/validates_timeliness/extensions/date_time_select_spec.rb @@ -13,7 +13,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do describe "datetime_select" do it "should use param values when attribute is nil" do - params["person"] = { + @params["person"] = { "birth_datetime(1i)" => 2009, "birth_datetime(2i)" => 2, "birth_datetime(3i)" => 29, @@ -22,17 +22,12 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_datetime(6i)" => 14, } person.birth_datetime = nil - output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29') - output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') + @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14) end it "should override object values and use params if present" do - params["person"] = { + @params["person"] = { "birth_datetime(1i)" => 2009, "birth_datetime(2i)" => 2, "birth_datetime(3i)" => 29, @@ -41,100 +36,70 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_datetime(6i)" => 14, } person.birth_datetime = "2010-01-01 15:16:17" - output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29') - output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') + @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14) end it "should use attribute values from object if no params" do person.birth_datetime = "2009-01-02 12:13:14" - output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2') - output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') + @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14) end it "should use attribute values if params does not contain attribute params" do person.birth_datetime = "2009-01-02 12:13:14" - params["person"] = { } - output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2') - output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14') + @params["person"] = { } + @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14) end it "should not select values when attribute value is nil and has no param values" do person.birth_datetime = nil - output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=person_birth_datetime_1i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_datetime_2i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_datetime_3i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_datetime_4i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_datetime_5i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_datetime_6i] option[selected=selected]') + @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true) + should_not_have_datetime_selected(:birth_datetime, :year, :month, :day, :hour, :min, :sec) end end describe "date_select" do it "should use param values when attribute is nil" do - params["person"] = { + @params["person"] = { "birth_date(1i)" => 2009, "birth_date(2i)" => 2, "birth_date(3i)" => 29, } person.birth_date = nil - output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29') + @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29) end it "should override object values and use params if present" do - params["person"] = { + @params["person"] = { "birth_date(1i)" => 2009, "birth_date(2i)" => 2, "birth_date(3i)" => 29, } person.birth_date = "2009-03-01" - output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February') - output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29') + @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29) end it "should select attribute values from object if no params" do person.birth_date = "2009-01-02" - output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2') + @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2) end it "should select attribute values if params does not contain attribute params" do person.birth_date = "2009-01-02" - params["person"] = { } - output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009') - output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January') - output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2') + @params["person"] = { } + @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2) end it "should not select values when attribute value is nil and has no param values" do person.birth_date = nil - output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]') + @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + should_not_have_datetime_selected(:birth_time, :year, :month, :day) end end @@ -144,7 +109,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do end it "should use param values when attribute is nil" do - params["person"] = { + @params["person"] = { "birth_time(1i)" => 2000, "birth_time(2i)" => 1, "birth_time(3i)" => 1, @@ -153,26 +118,34 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_time(6i)" => 14, } person.birth_time = nil - output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14') + @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14) end it "should select attribute values from object if no params" do person.birth_time = "2000-01-01 12:13:14" - output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) - output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12') - output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13') - output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14') + @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14) end it "should not select values when attribute value is nil and has no param values" do person.birth_time = nil - output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) - output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]') - output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]') + @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true) + should_not_have_datetime_selected(:birth_time, :hour, :min, :sec) + end + end + + def should_have_datetime_selected(field, datetime_hash) + datetime_hash.each do |key, value| + index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key] + @output.should have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s) + end + end + + def should_not_have_datetime_selected(field, *attributes) + attributes.each do |attribute| + index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[attribute] + @output.should_not have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]") end end end