From 175b5c8d3684a0e35c66b6fdaf7c372508e90ef5 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 22 Jul 2008 10:12:53 +1000 Subject: [PATCH] moved type cast conversion specs from attribute_methods to validations --- spec/attribute_methods_spec.rb | 28 +------------------------ spec/validations_spec.rb | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 59504f1..8d42965 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -7,33 +7,7 @@ describe ValidatesTimeliness::AttributeMethods do before do @person = Person.new end - - describe "strict_time_type_cast" do - it "should return time object for valid time string" do - strict_time_type_cast("2000-01-01 12:13:14").should be_kind_of(Time) - end - - it "should return nil for time string with invalid date part" do - strict_time_type_cast("2000-02-30 12:13:14").should be_nil - end - - it "should return nil for time string with invalid time part" do - strict_time_type_cast("2000-02-01 25:13:14").should be_nil - end - - it "should return Time object when passed a Time object" do - strict_time_type_cast(Time.now).should be_kind_of(Time) - end - - if RAILS_VER >= '2.1' - it "should convert time string into current timezone" do - Time.zone = 'Melbourne' - time = strict_time_type_cast("2000-01-01 12:13:14") - Time.zone.utc_offset.should == 10.hours - end - end - end - + it "should return string value for attribute_before_type_cast when written as string" do time_string = "2000-06-01 01:02:03" @person.birth_date_and_time = time_string diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index e3ed71c..55a78d8 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -10,6 +10,44 @@ describe ValidatesTimeliness::Validations do Time.now = nil end + describe "timeliness_date_time_parse" do + it "should return time object for valid time string" do + parse_method("2000-01-01 12:13:14", :datetime).should be_kind_of(Time) + end + + it "should return nil for time string with invalid date part" do + parse_method("2000-02-30 12:13:14", :datetime).should be_nil + end + + it "should return nil for time string with invalid time part" do + parse_method("2000-02-01 25:13:14", :datetime).should be_nil + end + + it "should return Time object when passed a Time object" do + parse_method(Time.now, :datetime).should be_kind_of(Time) + end + + if RAILS_VER >= '2.1' + it "should convert time string into current timezone" do + Time.zone = 'Melbourne' + time = parse_method("2000-01-01 12:13:14", :datetime) + Time.zone.utc_offset.should == 10.hours + end + end + + it "should return Date object valid date string" do + parse_method("2000-02-01", :date).should be_kind_of(Date) + end + + it "should return nil for invalid date string" do + parse_method("2000-02-30", :date).should be_nil + end + + def parse_method(*args) + ActiveRecord::Base.timeliness_date_time_parse(*args) + end + end + describe "with no restrictions" do before :all do class BasicValidation < Person