mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
Merge branch '2.0.0'
This commit is contained in:
@@ -39,17 +39,17 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||
end
|
||||
|
||||
it "should call parser on write for datetime attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
||||
end
|
||||
|
||||
it "should call parser on write for date attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_date = "2000-01-01"
|
||||
end
|
||||
|
||||
it "should call parser on write for time attribute" do
|
||||
@person.class.should_receive(:parse_date_time).once
|
||||
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||
@person.birth_time = "12:00"
|
||||
end
|
||||
|
||||
|
||||
@@ -6,46 +6,6 @@ describe ValidatesTimeliness::Formats do
|
||||
before do
|
||||
@formats = ValidatesTimeliness::Formats
|
||||
end
|
||||
|
||||
describe "expression generator" do
|
||||
it "should generate regexp for time" do
|
||||
generate_regexp_str('hh:nn:ss').should == '/(\d{2}):(\d{2}):(\d{2})/'
|
||||
end
|
||||
|
||||
it "should generate regexp for time with meridian" do
|
||||
generate_regexp_str('hh:nn:ss ampm').should == '/(\d{2}):(\d{2}):(\d{2}) ((?:[aApP])\.?[mM]\.?)/'
|
||||
end
|
||||
|
||||
it "should generate regexp for time with meridian and optional space between" do
|
||||
generate_regexp_str('hh:nn:ss_ampm').should == '/(\d{2}):(\d{2}):(\d{2})\s?((?:[aApP])\.?[mM]\.?)/'
|
||||
end
|
||||
|
||||
it "should generate regexp for time with single or double digits" do
|
||||
generate_regexp_str('h:n:s').should == '/(\d{1,2}):(\d{1,2}):(\d{1,2})/'
|
||||
end
|
||||
|
||||
it "should generate regexp for date" do
|
||||
generate_regexp_str('yyyy-mm-dd').should == '/(\d{4})-(\d{2})-(\d{2})/'
|
||||
end
|
||||
|
||||
it "should generate regexp for date with slashes" do
|
||||
generate_regexp_str('dd/mm/yyyy').should == '/(\d{2})\/(\d{2})\/(\d{4})/'
|
||||
end
|
||||
|
||||
it "should generate regexp for date with dots" do
|
||||
generate_regexp_str('dd.mm.yyyy').should == '/(\d{2})\.(\d{2})\.(\d{4})/'
|
||||
end
|
||||
|
||||
it "should generate regexp for Ruby time string" do
|
||||
expected = '/(\w{3,9}) (\w{3,9}) (\d{2}):(\d{2}):(\d{2}) (?:[+-]\d{2}:?\d{2}) (\d{4})/'
|
||||
generate_regexp_str('ddd mmm hh:nn:ss zo yyyy').should == expected
|
||||
end
|
||||
|
||||
it "should generate regexp for iso8601 datetime" do
|
||||
expected = '/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:Z|(?:[+-]\d{2}:?\d{2}))/'
|
||||
generate_regexp_str('yyyy-mm-ddThh:nn:ss(?:Z|zo)').should == expected
|
||||
end
|
||||
end
|
||||
|
||||
describe "format proc generator" do
|
||||
it "should generate proc which outputs date array with values in correct order" do
|
||||
@@ -71,6 +31,10 @@ describe ValidatesTimeliness::Formats do
|
||||
it "should generate proc which outputs time array with microseconds" do
|
||||
generate_proc('hh:nn:ss.u').call('01', '02', '03', '99').should == [0,0,0,1,2,3,990000]
|
||||
end
|
||||
|
||||
it "should generate proc which outputs datetime array with zone offset" do
|
||||
generate_proc('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
|
||||
end
|
||||
end
|
||||
|
||||
describe "validation regexps" do
|
||||
@@ -139,38 +103,43 @@ describe ValidatesTimeliness::Formats do
|
||||
describe "extracting values" do
|
||||
|
||||
it "should return time array from date string" do
|
||||
time_array = formats.parse('12:13:14', :time, true)
|
||||
time_array = formats.parse('12:13:14', :time, :strict => true)
|
||||
time_array.should == [0,0,0,12,13,14,0]
|
||||
end
|
||||
|
||||
it "should return date array from time string" do
|
||||
time_array = formats.parse('2000-02-01', :date, true)
|
||||
time_array = formats.parse('2000-02-01', :date, :strict => true)
|
||||
time_array.should == [2000,2,1,0,0,0,0]
|
||||
end
|
||||
|
||||
it "should return datetime array from string value" do
|
||||
time_array = formats.parse('2000-02-01 12:13:14', :datetime, true)
|
||||
time_array = formats.parse('2000-02-01 12:13:14', :datetime, :strict => true)
|
||||
time_array.should == [2000,2,1,12,13,14,0]
|
||||
end
|
||||
|
||||
it "should parse date string when type is datetime" do
|
||||
time_array = formats.parse('2000-02-01', :datetime, false)
|
||||
time_array = formats.parse('2000-02-01', :datetime, :strict => false)
|
||||
time_array.should == [2000,2,1,0,0,0,0]
|
||||
end
|
||||
|
||||
it "should ignore time when extracting date and strict is false" do
|
||||
time_array = formats.parse('2000-02-01 12:12', :date, false)
|
||||
time_array = formats.parse('2000-02-01 12:13', :date, :strict => false)
|
||||
time_array.should == [2000,2,1,0,0,0,0]
|
||||
end
|
||||
|
||||
it "should ignore time when extracting date from format with trailing year and strict is false" do
|
||||
time_array = formats.parse('01-02-2000 12:12', :date, false)
|
||||
time_array = formats.parse('01-02-2000 12:13', :date, :strict => false)
|
||||
time_array.should == [2000,2,1,0,0,0,0]
|
||||
end
|
||||
|
||||
it "should ignore date when extracting time and strict is false" do
|
||||
time_array = formats.parse('2000-02-01 12:12', :time, false)
|
||||
time_array.should == [0,0,0,12,12,0,0]
|
||||
time_array = formats.parse('2000-02-01 12:13', :time, :strict => false)
|
||||
time_array.should == [0,0,0,12,13,0,0]
|
||||
end
|
||||
|
||||
it "should return zone offset when :include_offset options is true" do
|
||||
time_array = formats.parse('2000-02-01T12:13:14-10:30', :datetime, :include_offset => true)
|
||||
time_array.should == [2000,2,1,12,13,14,0,-37800]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
describe ValidatesTimeliness::ValidationMethods do
|
||||
describe ValidatesTimeliness::Parser do
|
||||
attr_accessor :person
|
||||
|
||||
describe "parse_date_time" do
|
||||
describe "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)
|
||||
parse("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
|
||||
parse("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
|
||||
parse("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)
|
||||
parse(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 = parse("2000-01-01 12:13:14", :datetime)
|
||||
Time.zone.utc_offset.should == 10.hours
|
||||
end
|
||||
end
|
||||
|
||||
it "should return nil for invalid date string" do
|
||||
parse_method("2000-02-30", :date).should be_nil
|
||||
parse("2000-02-30", :date).should be_nil
|
||||
end
|
||||
|
||||
def parse_method(*args)
|
||||
ActiveRecord::Base.parse_date_time(*args)
|
||||
def parse(*args)
|
||||
ValidatesTimeliness::Parser.parse(*args)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,14 +43,14 @@ describe ValidatesTimeliness::ValidationMethods do
|
||||
|
||||
it "should create time using current timezone" do
|
||||
Time.zone = 'Melbourne'
|
||||
time = ActiveRecord::Base.send(:make_time, [2000,1,1,12,0,0])
|
||||
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||
time.zone.should == "EST"
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
it "should create time using default timezone" do
|
||||
time = ActiveRecord::Base.send(:make_time, [2000,1,1,12,0,0])
|
||||
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||
time.zone.should == "UTC"
|
||||
end
|
||||
|
||||
@@ -30,6 +30,7 @@ require 'active_record'
|
||||
require 'active_record/version'
|
||||
require 'action_controller'
|
||||
require 'action_view'
|
||||
require 'action_mailer'
|
||||
|
||||
require 'spec/rails'
|
||||
require 'time_travel/time_travel'
|
||||
@@ -38,13 +39,15 @@ ActiveRecord::Base.default_timezone = :utc
|
||||
RAILS_VER = Rails::VERSION::STRING
|
||||
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
||||
|
||||
require 'validates_timeliness'
|
||||
|
||||
if RAILS_VER >= '2.1'
|
||||
Time.zone_default = ActiveSupport::TimeZone['UTC']
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
end
|
||||
|
||||
require 'validates_timeliness'
|
||||
|
||||
ValidatesTimeliness.enable_datetime_select_extension!
|
||||
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
it "should return array of Time objects when restriction is array of strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
evaluate_option_value([time1, time2], :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value([time1, time2], :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||
end
|
||||
|
||||
it "should return array of Time objects when restriction is Range of Time objects" do
|
||||
@@ -76,7 +76,7 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
it "should return array of Time objects when restriction is Range of time strings" do
|
||||
time1, time2 = "2000-01-02", "2000-01-01"
|
||||
evaluate_option_value(time1..time2, :datetime).should == [Person.parse_date_time(time2, :datetime), Person.parse_date_time(time1, :datetime)]
|
||||
evaluate_option_value(time1..time2, :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||
end
|
||||
def evaluate_option_value(restriction, type)
|
||||
configure_validator(:type => type)
|
||||
@@ -554,12 +554,18 @@ describe ValidatesTimeliness::Validator do
|
||||
describe "custom formats" do
|
||||
|
||||
before :all do
|
||||
@@formats = ValidatesTimeliness::Validator.error_value_formats
|
||||
ValidatesTimeliness::Validator.error_value_formats = {
|
||||
custom = {
|
||||
:time => '%H:%M %p',
|
||||
:date => '%d-%m-%Y',
|
||||
:datetime => '%d-%m-%Y %H:%M %p'
|
||||
}
|
||||
|
||||
if defined?(I18n)
|
||||
I18n.backend.store_translations 'en', :validates_timeliness => { :error_value_formats => custom }
|
||||
else
|
||||
@@formats = ValidatesTimeliness::Validator.error_value_formats
|
||||
ValidatesTimeliness::Validator.error_value_formats = custom
|
||||
end
|
||||
end
|
||||
|
||||
it "should format datetime value of restriction" do
|
||||
@@ -581,12 +587,20 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
|
||||
after :all do
|
||||
ValidatesTimeliness::Validator.error_value_formats = @@formats
|
||||
if defined?(I18n)
|
||||
I18n.reload!
|
||||
else
|
||||
ValidatesTimeliness::Validator.error_value_formats = @@formats
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def parse(*args)
|
||||
ValidatesTimeliness::Parser.parse(*args)
|
||||
end
|
||||
|
||||
def configure_validator(options={})
|
||||
@validator = ValidatesTimeliness::Validator.new(options)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user