From 38457ec3341423350f94d7a458dfad94374745e8 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sat, 29 Jan 2011 16:33:37 +1100 Subject: [PATCH] change spec config setup to use class method with_config --- spec/support/config_helper.rb | 16 ++++++++++ .../attribute_methods_spec.rb | 10 ++---- spec/validates_timeliness/conversion_spec.rb | 21 +++--------- .../extensions/date_time_select_spec.rb | 8 +---- .../orm/active_record_spec.rb | 11 ++----- spec/validates_timeliness/orm/mongoid_spec.rb | 32 ++++++++----------- spec/validates_timeliness/validator_spec.rb | 26 ++++++--------- 7 files changed, 48 insertions(+), 76 deletions(-) diff --git a/spec/support/config_helper.rb b/spec/support/config_helper.rb index 329a1f1..6b8889f 100644 --- a/spec/support/config_helper.rb +++ b/spec/support/config_helper.rb @@ -1,4 +1,6 @@ module ConfigHelper + extend ActiveSupport::Concern + # Justin French tip def with_config(preference_name, temporary_value) old_value = ValidatesTimeliness.send(preference_name) @@ -7,4 +9,18 @@ module ConfigHelper ensure ValidatesTimeliness.send(:"#{preference_name}=", old_value) end + + module ClassMethods + def with_config(preference_name, temporary_value) + original_config_value = ValidatesTimeliness.send(preference_name) + + before(:all) do + ValidatesTimeliness.send(:"#{preference_name}=", temporary_value) + end + + after(:all) do + ValidatesTimeliness.send(:"#{preference_name}=", original_config_value) + end + end + end end diff --git a/spec/validates_timeliness/attribute_methods_spec.rb b/spec/validates_timeliness/attribute_methods_spec.rb index c4e6c6e..02bdecc 100644 --- a/spec/validates_timeliness/attribute_methods_spec.rb +++ b/spec/validates_timeliness/attribute_methods_spec.rb @@ -41,6 +41,8 @@ describe ValidatesTimeliness::AttributeMethods do end context "with plugin parser" do + with_config(:use_plugin_parser, true) + class PersonWithParser include TestModel include TestModelShim @@ -52,10 +54,6 @@ describe ValidatesTimeliness::AttributeMethods do validates_datetime :birth_datetime end - before :all do - ValidatesTimeliness.use_plugin_parser = true - end - it 'should parse a string value' do Timeliness::Parser.should_receive(:parse) r = PersonWithParser.new @@ -67,10 +65,6 @@ describe ValidatesTimeliness::AttributeMethods do r.birth_datetime = '2010-01-01 12:00' r.birth_datetime.zone == Time.zone.name end - - after :all do - ValidatesTimeliness.use_plugin_parser = false - end end end diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index a113e87..88eb693 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -114,17 +114,10 @@ describe ValidatesTimeliness::Conversion do end describe "with custom dummy date" do - before do - @original_dummy_date = ValidatesTimeliness.dummy_date_for_time_type - ValidatesTimeliness.dummy_date_for_time_type = [2010, 1, 1] - end - it 'should return dummy time with custom dummy date' do - dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56) - end - - after do - ValidatesTimeliness.dummy_date_for_time_type = @original_dummy_date + with_config(:dummy_date_for_time_type, [2010, 1, 1] ) do + dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56) + end end end end @@ -209,9 +202,7 @@ describe ValidatesTimeliness::Conversion do describe "#parse" do context "use_plugin_parser setting is true" do - around do |example| - with_config(:use_plugin_parser, true, &example) - end + with_config(:use_plugin_parser, true) it 'should use timeliness' do Timeliness::Parser.should_receive(:parse) @@ -220,9 +211,7 @@ describe ValidatesTimeliness::Conversion do end context "use_plugin_parser setting is false" do - around do |example| - with_config(:use_plugin_parser, false, &example) - end + with_config(:use_plugin_parser, false) it 'should use Time.zone.parse attribute is timezone aware' do @timezone_aware = true diff --git a/spec/validates_timeliness/extensions/date_time_select_spec.rb b/spec/validates_timeliness/extensions/date_time_select_spec.rb index 179c955..d8ce31a 100644 --- a/spec/validates_timeliness/extensions/date_time_select_spec.rb +++ b/spec/validates_timeliness/extensions/date_time_select_spec.rb @@ -4,9 +4,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do include ActionView::Helpers::DateHelper attr_reader :person, :params - before :all do - ValidatesTimeliness.use_plugin_parser = true - end + with_config(:use_plugin_parser, true) before do @person = Person.new @@ -177,8 +175,4 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]') end end - - after :all do - ValidatesTimeliness.use_plugin_parser = false - end end diff --git a/spec/validates_timeliness/orm/active_record_spec.rb b/spec/validates_timeliness/orm/active_record_spec.rb index a095424..cb6a640 100644 --- a/spec/validates_timeliness/orm/active_record_spec.rb +++ b/spec/validates_timeliness/orm/active_record_spec.rb @@ -33,16 +33,14 @@ describe ValidatesTimeliness, 'ActiveRecord' do end context "with plugin parser" do + with_config(:use_plugin_parser, true) + class EmployeeWithParser < ActiveRecord::Base set_table_name 'employees' validates_date :birth_date validates_datetime :birth_datetime end - before :all do - ValidatesTimeliness.use_plugin_parser = true - end - it 'should parse a string value' do Timeliness::Parser.should_receive(:parse) r = EmployeeWithParser.new @@ -54,11 +52,6 @@ describe ValidatesTimeliness, 'ActiveRecord' do r.birth_datetime = '2010-06-01 12:00' r.birth_datetime.utc_offset.should == 10.hours end - - after :all do - Time.zone = 'Australia/Melbourne' - ValidatesTimeliness.use_plugin_parser = false - end end end diff --git a/spec/validates_timeliness/orm/mongoid_spec.rb b/spec/validates_timeliness/orm/mongoid_spec.rb index a509dba..33291cd 100644 --- a/spec/validates_timeliness/orm/mongoid_spec.rb +++ b/spec/validates_timeliness/orm/mongoid_spec.rb @@ -12,20 +12,20 @@ Mongoid.configure do |config| config.persist_in_safe_mode = false end -class Article - ::ValidatesTimeliness.use_plugin_parser = true - include Mongoid::Document - field :publish_date, :type => Date - field :publish_time, :type => Time - field :publish_datetime, :type => DateTime - validates_date :publish_date, :allow_nil => true - validates_time :publish_time, :allow_nil => true - validates_datetime :publish_datetime, :allow_nil => true - ::ValidatesTimeliness.use_plugin_parser = false -end - describe ValidatesTimeliness, 'Mongoid' do + class Article + ::ValidatesTimeliness.use_plugin_parser = true + include Mongoid::Document + field :publish_date, :type => Date + field :publish_time, :type => Time + field :publish_datetime, :type => DateTime + validates_date :publish_date, :allow_nil => true + validates_time :publish_time, :allow_nil => true + validates_datetime :publish_datetime, :allow_nil => true + ::ValidatesTimeliness.use_plugin_parser = false + end + context "validation methods" do it 'should be defined on the class' do Article.should respond_to(:validates_date) @@ -52,9 +52,7 @@ describe ValidatesTimeliness, 'Mongoid' do end context "with plugin parser" do - before :all do - ValidatesTimeliness.use_plugin_parser = true - end + with_config(:use_plugin_parser, false) it 'should parse a string value' do Timeliness::Parser.should_receive(:parse) @@ -67,10 +65,6 @@ describe ValidatesTimeliness, 'Mongoid' do r.publish_datetime = '2010-01-01 12:00' r.publish_datetime.should == Time.utc(2010,1,1,12,0) end - - after :all do - ValidatesTimeliness.use_plugin_parser = false - end end end diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index e1b5153..8a6cbab 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -117,9 +117,7 @@ describe ValidatesTimeliness::Validator do let(:person) { PersonWithFormatOption.new } - before(:all) do - ValidatesTimeliness.use_plugin_parser = true - end + with_config(:use_plugin_parser, true) it "should be valid when value matches format" do person.birth_date = '11-12-1913' @@ -132,10 +130,6 @@ describe ValidatesTimeliness::Validator do person.valid? person.errors[:birth_date].should include('is not a valid date') end - - after(:all) do - ValidatesTimeliness.use_plugin_parser = false - end end describe "restriction value errors" do @@ -146,19 +140,17 @@ describe ValidatesTimeliness::Validator do end it "should be added when ignore_restriction_errors is false" do - ValidatesTimeliness.ignore_restriction_errors = false - person.valid? - person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction") + with_config(:ignore_restriction_errors, false) do + person.valid? + person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction") + end end it "should not be added when ignore_restriction_errors is true" do - ValidatesTimeliness.ignore_restriction_errors = true - person.valid? - person.errors[:birth_date].should be_empty - end - - after :all do - ValidatesTimeliness.ignore_restriction_errors = false + with_config(:ignore_restriction_errors, true) do + person.valid? + person.errors[:birth_date].should be_empty + end end end