Skip to content

Commit 6882922

Browse files
Josep Mª Bachcodeswamp
Josep Mª Bach
authored andcommitted
Fixed some bugs and refactored both code and specs
1 parent a2fc5b6 commit 6882922

File tree

6 files changed

+46
-45
lines changed

6 files changed

+46
-45
lines changed

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--colour
2+
--format documentation

Rakefile

+12-15
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,27 @@ begin
1111
gem.homepage = "http://github.com/codegram/date_validator"
1212
gem.authors = ["Oriol Gual", "Josep Mª Bach", "Josep Jaume Rey"]
1313

14-
gem.add_dependency 'activemodel', '>= 3.0.0.beta3'
14+
gem.add_dependency 'activemodel', '>= 3.0.0.beta4'
1515

16-
gem.add_development_dependency "rspec"
17-
18-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16+
gem.add_development_dependency "rspec", '>= 2.0.0.beta.12'
17+
gem.add_development_dependency "activesupport", '>= 3.0.0.beta4'
1918
end
2019
Jeweler::GemcutterTasks.new
2120
rescue LoadError
2221
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
2322
end
2423

25-
require 'spec/rake/spectask'
26-
Spec::Rake::SpecTask.new(:spec) do |spec|
27-
spec.libs << 'lib' << 'spec'
28-
spec.spec_files = FileList['spec/**/*_spec.rb']
29-
end
3024

31-
Spec::Rake::SpecTask.new(:rcov) do |spec|
32-
spec.libs << 'lib' << 'spec'
33-
spec.pattern = 'spec/**/*_spec.rb'
34-
spec.rcov = true
35-
end
25+
# Rake RSpec2 task stuff
26+
gem 'rspec', '>= 2.0.0.beta.12'
27+
gem 'rspec-expectations'
3628

37-
task :spec => :check_dependencies
29+
require 'rspec/core/rake_task'
30+
31+
desc "Run the specs under spec"
32+
RSpec::Core::RakeTask.new do |t|
33+
34+
end
3835

3936
task :default => :spec
4037

lib/date_validator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def initialize(options)
1111
def check_validity!
1212
keys = CHECKS.keys
1313
options.slice(*keys).each do |option, value|
14-
next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol)
15-
raise ArgumentError, ":#{option} must be a time, a date, a symbol or a proc"
14+
next if is_time?(value) || value.is_a?(Proc) || value.is_a?(Symbol) || (defined?(ActiveSupport::TimeWithZone) and value.is_a? ActiveSupport::TimeWithZone)
15+
raise ArgumentError, ":#{option} must be a time, a date, a time_with_zone, a symbol or a proc"
1616
end
1717
end
1818

spec/date_validator_spec.rb

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
22

3+
require 'active_support/time' # For testing Date and TimeWithZone objects
4+
35
describe "DateValidator" do
46

57
before(:each) do
@@ -8,23 +10,22 @@
810

911
it "should check validity of the arguments" do
1012
[3, "foo", 1..6].each do |wrong_argument|
11-
begin
13+
expect {
1214
TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
13-
fail "should not accept a #{wrong_argument.class} as an option (only Time, Date, Symbol or Proc)"
14-
rescue=>e
15-
fail e unless e.is_a?(ArgumentError)
16-
end
15+
}.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc")
1716
end
1817
end
1918

2019
[:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
2120
[:valid,:invalid].each do |should_be|
2221

22+
now = Time.now.to_datetime
23+
2324
model_date = case check
24-
when :after then should_be == :valid ? Time.now + 21000 : Time.now - 1
25-
when :before then should_be == :valid ? Time.now - 21000 : Time.now + 1
26-
when :after_or_equal_to then should_be == :valid ? Time.now + 21000 : Time.now - 21000
27-
when :before_or_equal_to then should_be == :valid ? Time.now - 21000 : Time.now + 21000
25+
when :after then should_be == :valid ? now + 21000 : now - 1
26+
when :before then should_be == :valid ? now - 21000 : now + 1
27+
when :after_or_equal_to then should_be == :valid ? now : now - 21000
28+
when :before_or_equal_to then should_be == :valid ? now : now + 21000
2829
end
2930

3031
it "should ensure that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
@@ -37,34 +38,42 @@
3738
end
3839

3940
extra_types = [:proc, :symbol]
40-
# extra_types.push(:date) if defined?(Date) and defined?(DateTime)
41+
extra_types.push(:date) if defined?(Date) and defined?(DateTime)
42+
extra_types.push(:time_with_zone) if defined?(ActiveSupport::TimeWithZone)
4143

4244
extra_types.each do |type|
4345
it "should accept a #{type} as an argument to a check" do
4446
case type
4547
when :proc then
46-
TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
48+
expect {
49+
TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
50+
}.to_not raise_error
4751
when :symbol then
48-
begin
52+
expect {
4953
TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
5054
TestRecord.validates :expiration_date, :date => {:after => :min_date}
51-
rescue=>e
52-
fail "is not accepting a #{type} as an argument to a check"
53-
end
54-
# when :date then
55-
# TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
55+
}.to_not raise_error
56+
when :date then
57+
expect {
58+
TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
59+
}.to_not raise_error
60+
when :time_with_zone then
61+
expect {
62+
Time.zone = "Hawaii"
63+
TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)}
64+
}.to_not raise_error
5665
end
5766
end
5867
end
5968

6069
it "should gracefully handle an unexpected result from a proc argument evaluation" do
6170
TestRecord.validates :expiration_date, :date => {:after => Proc.new{ nil }}
62-
TestRecord.new(Time.now).valid?.should be_false
71+
TestRecord.new(Time.now).should_not be_valid
6372
end
6473

6574
it "should gracefully handle an unexpected result from a symbol argument evaluation" do
6675
TestRecord.send(:define_method, :min_date, lambda { nil })
6776
TestRecord.validates :expiration_date, :date => {:after => :min_date}
68-
TestRecord.new(Time.now).valid?.should be_false
77+
TestRecord.new(Time.now).should_not be_valid
6978
end
7079
end

spec/spec.opts

-2
This file was deleted.

spec/spec_helper.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
require 'active_model'
66

77
require 'lib/date_validator'
8-
require 'spec'
9-
require 'spec/autorun'
10-
8+
require 'rspec'
9+
require 'rspec/autorun'
1110

1211
class TestRecord
1312
include ActiveModel::Validations
@@ -17,7 +16,3 @@ def initialize(expiration_date)
1716
@expiration_date = expiration_date
1817
end
1918
end
20-
21-
Spec::Runner.configure do |config|
22-
23-
end

0 commit comments

Comments
 (0)